Skip to content

Commit 6669d6d

Browse files
committed
the site now renders, cleanup backend
1 parent 4ff2b20 commit 6669d6d

File tree

4 files changed

+80
-23
lines changed

4 files changed

+80
-23
lines changed

coderd/tailnet.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"bufio"
55
"context"
66
"crypto/tls"
7+
"fmt"
78
"net"
89
"net/http"
910
"net/http/httputil"
1011
"net/netip"
1112
"net/url"
12-
"strconv"
1313
"strings"
1414
"sync"
1515
"sync/atomic"
@@ -26,6 +26,7 @@ import (
2626
"github.com/coder/coder/v2/coderd/tracing"
2727
"github.com/coder/coder/v2/coderd/workspaceapps"
2828
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
29+
"github.com/coder/coder/v2/codersdk"
2930
"github.com/coder/coder/v2/codersdk/workspacesdk"
3031
"github.com/coder/coder/v2/site"
3132
"github.com/coder/coder/v2/tailnet"
@@ -355,34 +356,32 @@ func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID u
355356

356357
proxy := httputil.NewSingleHostReverseProxy(&tgt)
357358
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, theErr error) {
358-
desc := "Failed to proxy request to application: \n\n" + theErr.Error()
359-
descAddition := ""
359+
desc := "Failed to proxy request to application: " + theErr.Error()
360+
additional := ""
361+
360362
if strings.Contains(theErr.Error(), "tls:") {
361363
// If the error is due to an HTTP/HTTPS mismatch, we can provide a
362364
// more helpful error message with redirect buttons.
363-
if strings.HasSuffix(app.AppSlugOrPort, "s") {
364-
_, err := strconv.ParseInt(app.AppSlugOrPort, 10, 64)
365-
if err == nil {
366-
app.AppSlugOrPort = strings.TrimSuffix(app.AppSlugOrPort, "s")
367-
descAddition += "This error seems to be due to an HTTPS mismatch, please try switching to HTTP."
368-
}
369-
} else {
370-
_, err := strconv.ParseInt(app.AppSlugOrPort, 10, 64)
371-
if err == nil {
372-
app.AppSlugOrPort += "s"
373-
descAddition += "This error seems to be due to an HTTP mismatch, please try switching to HTTPS."
365+
if app.IsPort() {
366+
if app.Protocol() == codersdk.WorkspaceAgentPortShareProtocolHTTPS {
367+
app.ChangePortProtocol(codersdk.WorkspaceAgentPortShareProtocolHTTP)
368+
} else {
369+
app.ChangePortProtocol(codersdk.WorkspaceAgentPortShareProtocolHTTPS)
374370
}
371+
372+
additional += fmt.Sprintf("This error seems to be due to an app protocol mismatch, please try switching to %s.", app.Protocol())
375373
}
376374
}
377-
desc += descAddition
378375

379376
site.RenderStaticErrorPage(w, r, site.ErrorPageData{
380-
Status: http.StatusBadGateway,
381-
Title: "Bad Gateway Dood",
382-
Description: desc,
383-
RetryEnabled: true,
384-
DashboardURL: dashboardURL.String(),
385-
SwitchProtocolLink: app.String(),
377+
Status: http.StatusBadGateway,
378+
Title: "Bad Gateway Dood",
379+
Description: desc,
380+
RetryEnabled: true,
381+
DashboardURL: dashboardURL.String(),
382+
SwitchProtocolLink: app.String(),
383+
SwitchProtocolTarget: string(app.Protocol()),
384+
AdditionalInfo: additional,
386385
})
387386
}
388387
proxy.Director = s.director(agentID, proxy.Director)

coderd/workspaceapps/appurl/appurl.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"net"
66
"net/url"
77
"regexp"
8+
"strconv"
89
"strings"
910

11+
"github.com/coder/coder/v2/codersdk"
1012
"golang.org/x/xerrors"
1113
)
1214

@@ -83,6 +85,57 @@ func (a ApplicationURL) Path() string {
8385
return fmt.Sprintf("/@%s/%s.%s/apps/%s", a.Username, a.WorkspaceName, a.AgentName, a.AppSlugOrPort)
8486
}
8587

88+
func (a ApplicationURL) IsPort() bool {
89+
// check if https port
90+
if strings.HasSuffix(a.AppSlugOrPort, "s") {
91+
trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
92+
_, err := strconv.ParseInt(trimmed, 10, 64)
93+
if err != nil {
94+
return false
95+
}
96+
97+
return true
98+
}
99+
100+
// check if port at all
101+
_, err := strconv.ParseInt(a.AppSlugOrPort, 10, 64)
102+
if err != nil {
103+
return false
104+
}
105+
106+
return true
107+
108+
}
109+
110+
func (a ApplicationURL) Protocol() codersdk.WorkspaceAgentPortShareProtocol {
111+
if strings.HasSuffix(a.AppSlugOrPort, "s") {
112+
trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
113+
_, err := strconv.ParseInt(trimmed, 10, 64)
114+
if err == nil {
115+
return codersdk.WorkspaceAgentPortShareProtocolHTTPS
116+
}
117+
}
118+
119+
return codersdk.WorkspaceAgentPortShareProtocolHTTP
120+
}
121+
122+
func (a ApplicationURL) ChangePortProtocol(target codersdk.WorkspaceAgentPortShareProtocol) error {
123+
if target == codersdk.WorkspaceAgentPortShareProtocolHTTP {
124+
if strings.HasSuffix(a.AppSlugOrPort, "s") {
125+
trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
126+
_, err := strconv.ParseInt(trimmed, 10, 64)
127+
if err != nil {
128+
return xerrors.Errorf("invalid port: %s", a.AppSlugOrPort)
129+
}
130+
a.AppSlugOrPort = trimmed
131+
}
132+
}
133+
134+
a.AppSlugOrPort = fmt.Sprintf("%s%s", a.AppSlugOrPort, "s")
135+
136+
return nil
137+
}
138+
86139
// ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If
87140
// the subdomain is not a valid application URL hostname, returns a non-nil
88141
// error. If the hostname is not a subdomain of the given base hostname, returns

site/site.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ type ErrorPageData struct {
794794
Warnings []string
795795
SwitchProtocolLink string
796796
SwitchProtocolTarget codersdk.WorkspaceAgentPortShareProtocol
797+
AdditionalInfo string
797798

798799
RenderDescriptionMarkdown bool
799800
}

site/static/error.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ <h1>
170170
{{- if .Error.RenderDescriptionMarkdown }} {{ .ErrorDescriptionHTML }} {{
171171
else }}
172172
<p>{{ .Error.Description }}</p>
173-
{{ end }} {{- if .Error.Warnings }}
173+
{{ end }}
174+
{{- if .Error.AdditionalInfo }}
175+
<br><p>{{ .Error.AdditionalInfo }}</p>
176+
{{ end }}
177+
{{- if .Error.Warnings }}
174178
<div class="warning">
175179
<div class="warning-title">
176180
<svg
@@ -199,7 +203,7 @@ <h3>Warnings</h3>
199203
<button onclick="window.location.reload()">Retry</button>
200204
{{ end }}
201205
{{- if .Error.SwitchProtocolLink }}
202-
<a href="{{ .Error.SwitchProtocolLink }}">Switch to HTTPX</a>
206+
<a href="{{ .Error.SwitchProtocolLink }}">Switch to {{ .ErrorSwitchProtocolTarget }}</a>
203207
{{ end }}
204208
<a href="{{ .Error.DashboardURL }}">Back to site</a>
205209
</div>

0 commit comments

Comments
 (0)