4
4
"bufio"
5
5
"context"
6
6
"crypto/tls"
7
- "fmt"
8
7
"net"
9
8
"net/http"
10
9
"net/http/httputil"
@@ -27,7 +26,6 @@ import (
27
26
"github.com/coder/coder/v2/coderd/tracing"
28
27
"github.com/coder/coder/v2/coderd/workspaceapps"
29
28
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
30
- "github.com/coder/coder/v2/codersdk"
31
29
"github.com/coder/coder/v2/codersdk/workspacesdk"
32
30
"github.com/coder/coder/v2/site"
33
31
"github.com/coder/coder/v2/tailnet"
@@ -346,7 +344,7 @@ type ServerTailnet struct {
346
344
totalConns * prometheus.CounterVec
347
345
}
348
346
349
- func (s * ServerTailnet ) ReverseProxy (targetURL , dashboardURL * url.URL , agentID uuid.UUID ) * httputil.ReverseProxy {
347
+ func (s * ServerTailnet ) ReverseProxy (targetURL , dashboardURL * url.URL , agentID uuid.UUID , app appurl. ApplicationURL ) * httputil.ReverseProxy {
350
348
// Rewrite the targetURL's Host to point to the agent's IP. This is
351
349
// necessary because due to TCP connection caching, each agent needs to be
352
350
// addressed invidivually. Otherwise, all connections get dialed as
@@ -357,48 +355,34 @@ func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID u
357
355
358
356
proxy := httputil .NewSingleHostReverseProxy (& tgt )
359
357
proxy .ErrorHandler = func (w http.ResponseWriter , r * http.Request , theErr error ) {
360
- var (
361
- switchProtoScheme codersdk.WorkspaceAgentPortShareProtocol
362
- switchProtoLink = ""
363
- )
364
- au , err := appurl .ParseSubdomainAppURL (tgt .String ())
365
- if err != nil {
366
- site .RenderStaticErrorPage (w , r , site.ErrorPageData {
367
- Status : http .StatusBadGateway ,
368
- Title : "Bad Gateway" ,
369
- Description : "Failed to proxy request to application: " + err .Error (),
370
- RetryEnabled : true ,
371
- DashboardURL : dashboardURL .String (),
372
- })
373
- return
374
- }
375
- if strings .HasSuffix (au .AppSlugOrPort , "s" ) {
376
- p := strings .TrimSuffix (au .AppSlugOrPort , "s" )
377
- _ , err = strconv .ParseInt (p , 10 , 64 )
378
- if err == nil {
379
- au .AppSlugOrPort = p
380
- switchProtoLink = au .String ()
381
- switchProtoScheme = codersdk .WorkspaceAgentPortShareProtocolHTTP
382
- }
383
- } else {
384
- au .AppSlugOrPort += "s"
385
- switchProtoLink = au .String ()
386
- switchProtoScheme = codersdk .WorkspaceAgentPortShareProtocolHTTPS
387
- }
388
-
389
- desc := "Failed to proxy request to application: " + theErr .Error ()
358
+ desc := "Failed to proxy request to application: \n \n " + theErr .Error ()
359
+ descAddition := ""
390
360
if strings .Contains (theErr .Error (), "tls:" ) {
391
- desc = fmt .Sprintf ("This error seems to be due to a protocol mismatch, please try switching to %s. \n %s" , switchProtoScheme , theErr .Error ())
361
+ // If the error is due to an HTTP/HTTPS mismatch, we can provide a
362
+ // 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."
374
+ }
375
+ }
392
376
}
377
+ desc += descAddition
393
378
394
379
site .RenderStaticErrorPage (w , r , site.ErrorPageData {
395
- Status : http .StatusBadGateway ,
396
- Title : "Bad Gateway" ,
397
- Description : desc ,
398
- RetryEnabled : true ,
399
- DashboardURL : dashboardURL .String (),
400
- SwitchProtocolLink : switchProtoLink ,
401
- SwitchProtocolTarget : switchProtoScheme ,
380
+ Status : http .StatusBadGateway ,
381
+ Title : "Bad Gateway Dood" ,
382
+ Description : desc ,
383
+ RetryEnabled : true ,
384
+ DashboardURL : dashboardURL .String (),
385
+ SwitchProtocolLink : app .String (),
402
386
})
403
387
}
404
388
proxy .Director = s .director (agentID , proxy .Director )
0 commit comments