Skip to content

Commit bbbd3c2

Browse files
committed
pr comments
1 parent e291e8c commit bbbd3c2

File tree

5 files changed

+93
-62
lines changed

5 files changed

+93
-62
lines changed

coderd/tailnet.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"context"
66
"crypto/tls"
7+
"errors"
78
"fmt"
89
"net"
910
"net/http"
@@ -362,23 +363,27 @@ func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID u
362363
switchTarget = ""
363364
)
364365

365-
if strings.Contains(theErr.Error(), "tls:") {
366+
var tlsError tls.RecordHeaderError
367+
if (errors.As(theErr, &tlsError) && tlsError.Msg == "first record does not look like a TLS handshake") ||
368+
errors.Is(theErr, http.ErrSchemeMismatch) {
366369
// If the error is due to an HTTP/HTTPS mismatch, we can provide a
367370
// more helpful error message with redirect buttons.
368371
switchURL := url.URL{
369372
Scheme: dashboardURL.Scheme,
370373
}
371-
if app.IsPort() {
372-
if app.PortProtocol() == "https" {
373-
app.ChangePortProtocol("http")
374-
} else {
375-
app.ChangePortProtocol("https")
374+
_, protocol, isPort := app.PortInfo()
375+
if isPort {
376+
if protocol == "https" {
377+
app = app.ChangePortProtocol("http")
378+
}
379+
if protocol == "http" {
380+
app = app.ChangePortProtocol("https")
376381
}
377382

378383
switchURL.Host = fmt.Sprintf("%s%s", app.String(), strings.TrimPrefix(wildcardHostname, "*"))
379384
switchLink = switchURL.String()
380-
switchTarget = app.PortProtocol()
381-
additional += fmt.Sprintf("This error seems to be due to an app protocol mismatch, try switching to %s.", strings.ToUpper(app.PortProtocol()))
385+
switchTarget = protocol
386+
additional += fmt.Sprintf("This error seems to be due to an app protocol mismatch, try switching to %s.", strings.ToUpper(protocol))
382387
}
383388
}
384389

@@ -388,9 +393,9 @@ func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID u
388393
Description: desc,
389394
RetryEnabled: true,
390395
DashboardURL: dashboardURL.String(),
391-
SwitchProtocolLink: switchLink,
392-
SwitchProtocolTarget: strings.ToUpper(switchTarget),
393396
AdditionalInfo: additional,
397+
AdditionalButtonLink: switchLink,
398+
AdditionalButtonText: fmt.Sprintf("Switch to %s", strings.ToUpper(switchTarget)),
394399
})
395400
}
396401
proxy.Director = s.director(agentID, proxy.Director)

coderd/workspaceapps/appurl/appurl.go

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -84,62 +84,87 @@ func (a ApplicationURL) Path() string {
8484
return fmt.Sprintf("/@%s/%s.%s/apps/%s", a.Username, a.WorkspaceName, a.AgentName, a.AppSlugOrPort)
8585
}
8686

87-
func (a ApplicationURL) IsPort() bool {
88-
// check if https port
89-
if strings.HasSuffix(a.AppSlugOrPort, "s") {
90-
trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
91-
_, err := strconv.ParseInt(trimmed, 10, 64)
92-
//nolint:gosimple
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-
//nolint:gosimple
103-
if err != nil {
104-
return false
105-
}
106-
107-
return true
108-
}
87+
// func (a ApplicationURL) IsPort() bool {
88+
// // check if https port
89+
// if strings.HasSuffix(a.AppSlugOrPort, "s") {
90+
// trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
91+
// _, err := strconv.ParseInt(trimmed, 10, 64)
92+
// //nolint:gosimple
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+
// //nolint:gosimple
103+
// if err != nil {
104+
// return false
105+
// }
106+
107+
// return true
108+
// }
109+
110+
// func (a ApplicationURL) PortProtocol() string {
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 "https"
116+
// }
117+
// }
118+
119+
// return "http"
120+
// }
121+
122+
func (a ApplicationURL) PortInfo() (uint, string, bool) {
123+
var (
124+
port uint64
125+
protocol string
126+
isPort bool
127+
err error
128+
)
109129

110-
func (a ApplicationURL) PortProtocol() string {
111130
if strings.HasSuffix(a.AppSlugOrPort, "s") {
112131
trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
113-
_, err := strconv.ParseInt(trimmed, 10, 64)
132+
port, err = strconv.ParseUint(trimmed, 10, 16)
133+
if err == nil {
134+
protocol = "https"
135+
isPort = true
136+
}
137+
} else {
138+
port, err = strconv.ParseUint(a.AppSlugOrPort, 10, 16)
114139
if err == nil {
115-
return "https"
140+
protocol = "http"
141+
isPort = true
116142
}
117143
}
118144

119-
return "http"
145+
return uint(port), protocol, isPort
120146
}
121147

122-
func (a *ApplicationURL) ChangePortProtocol(target string) {
123-
if target == "http" {
124-
if a.PortProtocol() == "http" {
125-
return
126-
}
127-
trimmed := strings.TrimSuffix(a.AppSlugOrPort, "s")
128-
_, err := strconv.ParseInt(trimmed, 10, 64)
129-
if err == nil {
130-
a.AppSlugOrPort = trimmed
131-
}
148+
func (a *ApplicationURL) ChangePortProtocol(target string) ApplicationURL {
149+
newAppURL := *a
150+
port, protocol, isPort := a.PortInfo()
151+
if !isPort {
152+
return newAppURL
153+
}
154+
155+
if target == protocol {
156+
return newAppURL
132157
}
133158

134159
if target == "https" {
135-
if a.PortProtocol() == "https" {
136-
return
137-
}
138-
_, err := strconv.ParseInt(a.AppSlugOrPort, 10, 64)
139-
if err == nil {
140-
a.AppSlugOrPort = fmt.Sprintf("%s%s", a.AppSlugOrPort, "s")
141-
}
160+
newAppURL.AppSlugOrPort = fmt.Sprintf("%ds", port)
142161
}
162+
163+
if target == "http" {
164+
newAppURL.AppSlugOrPort = fmt.Sprintf("%d", port)
165+
}
166+
167+
return newAppURL
143168
}
144169

145170
// ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If

coderd/workspaceapps/proxy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,9 @@ func (s *Server) proxyWorkspaceApp(rw http.ResponseWriter, r *http.Request, appT
545545

546546
r.URL.Path = path
547547
appURL.RawQuery = ""
548-
if app.IsPort() {
549-
appURL.Scheme = app.PortProtocol()
548+
_, protocol, isPort := app.PortInfo()
549+
if isPort {
550+
appURL.Scheme = protocol
550551
}
551552

552553
proxy := s.AgentProvider.ReverseProxy(appURL, s.DashboardURL, appToken.AgentID, app, s.Hostname)

site/site.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,9 @@ type ErrorPageData struct {
792792
RetryEnabled bool
793793
DashboardURL string
794794
Warnings []string
795-
SwitchProtocolLink string
796-
SwitchProtocolTarget string
797795
AdditionalInfo string
796+
AdditionalButtonLink string
797+
AdditionalButtonText string
798798

799799
RenderDescriptionMarkdown bool
800800
}

site/static/error.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ <h3>Warnings</h3>
198198
</div>
199199
{{ end }}
200200
<div class="button-group">
201-
{{- if .Error.RetryEnabled }}
202-
<button onclick="window.location.reload()">Retry</button>
203-
{{ end }} {{- if .Error.SwitchProtocolLink }}
204-
<a href="{{ .Error.SwitchProtocolLink }}"
205-
>Switch to {{ .Error.SwitchProtocolTarget }}</a
201+
{{- if and .Error.AdditionalButtonText .Error.AdditionButtonLink }}
202+
<a href="{{ .Error.AdditionButtonLink }}"
203+
>Switch to {{ .Error.AdditionalButtonText }}</a
206204
>
205+
{{ end }} {{- if .Error.RetryEnabled }}
206+
<button onclick="window.location.reload()">Retry</button>
207207
{{ end }}
208208
<a href="{{ .Error.DashboardURL }}">Back to site</a>
209209
</div>

0 commit comments

Comments
 (0)