Skip to content

feat: add switch http(s) button to error page #12942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add switch http(s) button to error page
  • Loading branch information
f0ssel committed Apr 26, 2024
commit e545ad19b7e4b6952417ab5ccbc627834683e73f
53 changes: 47 additions & 6 deletions coderd/tailnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"bufio"
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/netip"
"net/url"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
Expand All @@ -23,6 +26,8 @@ import (
"cdr.dev/slog"
"github.com/coder/coder/v2/coderd/tracing"
"github.com/coder/coder/v2/coderd/workspaceapps"
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
"github.com/coder/coder/v2/site"
"github.com/coder/coder/v2/tailnet"
Expand Down Expand Up @@ -351,13 +356,49 @@ func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID u
tgt.Host = net.JoinHostPort(tailnet.IPFromUUID(agentID).String(), port)

proxy := httputil.NewSingleHostReverseProxy(&tgt)
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, theErr error) {
var (
switchProtoScheme codersdk.WorkspaceAgentPortShareProtocol
switchProtoLink = ""
)
au, err := appurl.ParseSubdomainAppURL(tgt.String())
if err != nil {
site.RenderStaticErrorPage(w, r, site.ErrorPageData{
Status: http.StatusBadGateway,
Title: "Bad Gateway",
Description: "Failed to proxy request to application: " + err.Error(),
RetryEnabled: true,
DashboardURL: dashboardURL.String(),
})
return
}
if strings.HasSuffix(au.AppSlugOrPort, "s") {
p := strings.TrimSuffix(au.AppSlugOrPort, "s")
_, err = strconv.ParseInt(p, 10, 64)
if err == nil {
au.AppSlugOrPort = p
switchProtoLink = au.String()
switchProtoScheme = codersdk.WorkspaceAgentPortShareProtocolHTTP
}
} else {
au.AppSlugOrPort += "s"
switchProtoLink = au.String()
switchProtoScheme = codersdk.WorkspaceAgentPortShareProtocolHTTPS
}

desc := "Failed to proxy request to application: " + theErr.Error()
if strings.Contains(theErr.Error(), "tls:") {
desc = fmt.Sprintf("This error seems to be due to a protocol mistake, please try switching to %s. \n%s", switchProtoScheme, theErr.Error())
}

site.RenderStaticErrorPage(w, r, site.ErrorPageData{
Status: http.StatusBadGateway,
Title: "Bad Gateway",
Description: "Failed to proxy request to application: " + err.Error(),
RetryEnabled: true,
DashboardURL: dashboardURL.String(),
Status: http.StatusBadGateway,
Title: "Bad Gateway",
Description: desc,
RetryEnabled: true,
DashboardURL: dashboardURL.String(),
SwitchProtocolLink: switchProtoLink,
SwitchProtocolTarget: switchProtoScheme,
})
}
proxy.Director = s.director(agentID, proxy.Director)
Expand Down
14 changes: 8 additions & 6 deletions site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,12 +786,14 @@ func extractBin(dest string, r io.Reader) (numExtracted int, err error) {
type ErrorPageData struct {
Status int
// HideStatus will remove the status code from the page.
HideStatus bool
Title string
Description string
RetryEnabled bool
DashboardURL string
Warnings []string
HideStatus bool
Title string
Description string
RetryEnabled bool
DashboardURL string
Warnings []string
SwitchProtocolLink string
SwitchProtocolTarget codersdk.WorkspaceAgentPortShareProtocol

RenderDescriptionMarkdown bool
}
Expand Down
9 changes: 9 additions & 0 deletions site/static/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ <h1>
{{- if not .Error.HideStatus }}{{ .Error.Status }} - {{end}}{{
.Error.Title }}
</h1>
{{- if .Error.SwitchProtocolLink }}
<p>It looks like the error was due to a protocol mismatch. Do you want to try using {{ .Error.SwitchProtocolTarget }}?</p>
<div class="button-group">
<a href="{{ .Error.SwitchProtocolLink }}">Use {{ .Error.SwitchProtocolTarget }}</a>
</div>
{{ end }}
{{- if .Error.RenderDescriptionMarkdown }} {{ .ErrorDescriptionHTML }} {{
else }}
<p>{{ .Error.Description }}</p>
Expand Down Expand Up @@ -198,6 +204,9 @@ <h3>Warnings</h3>
{{- if .Error.RetryEnabled }}
<button onclick="window.location.reload()">Retry</button>
{{ end }}
{{- if .Error.SwitchProtocolLink }}
<a href="{{ .Error.SwitchProtocolLink }}">Switch to {{ .Error.SwitchProtocolTarget }}</a>
{{ end }}
<a href="{{ .Error.DashboardURL }}">Back to site</a>
</div>
</div>
Expand Down