Skip to content

fix: force websockets when http2 is negotiated #16

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 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 11 additions & 12 deletions derp/derphttp/derphttp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ func (c *Client) connect(ctx context.Context, caller string) (client *derp.Clien
req.Header.Set("Upgrade", "DERP")
req.Header.Set("Connection", "Upgrade")

regionID := 0
if reg != nil {
regionID = reg.RegionID
}
if tlsState != nil && tlsState.NegotiatedProtocol == "h2" {
reason := "The server wanted us to use HTTP/2, but DERP requires Upgrade which needs HTTP/1.1"
c.forceWebsockets(regionID, reason)
return nil, 0, fmt.Errorf("DERP server did not switch protocols: %s", reason)
}

if !serverPub.IsZero() && serverProtoVersion != 0 {
// parseMetaCert found the server's public key (no TLS
// middlebox was in the way), so skip the HTTP upgrade
Expand All @@ -492,17 +502,6 @@ func (c *Client) connect(ctx context.Context, caller string) (client *derp.Clien
// No need to flush the HTTP request. the derp.Client's initial
// client auth frame will flush it.
} else {
regionID := 0
if reg != nil {
regionID = reg.RegionID
}

if tlsState != nil && tlsState.NegotiatedProtocol == "h2" {
reason := fmt.Sprintf("The server wanted us to use HTTP/2, but DERP requires Upgrade which needs HTTP/1.1")
c.forceWebsockets(regionID, reason)
return nil, 0, fmt.Errorf("DERP server did not switch protocols: %s", reason)
}

if err := req.Write(brw); err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -650,7 +649,7 @@ func (c *Client) tlsConfig(node *tailcfg.DERPNode) *tls.Config {
tlsdial.SetConfigExpectedCert(tlsConf, node.CertName)
}
}
tlsConf.NextProtos = []string{"h2", "http/1.1"}
tlsConf.NextProtos = []string{"http/1.1", "h2"}
return tlsConf
}

Expand Down
10 changes: 8 additions & 2 deletions derp/derphttp/derphttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,13 @@ func TestHTTP2WebSocketFallback(t *testing.T) {
s.Accept(context.Background(), wc, brw, r.RemoteAddr)
}))
httpsrv.TLS = &tls.Config{
NextProtos: []string{"h2", "http/1.1"},
NextProtos: []string{"h2"},
GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
// Add this to ensure fast start works!
cert := httpsrv.TLS.Certificates[0]
cert.Certificate = append(cert.Certificate, s.MetaCert())
return &cert, nil
},
}
httpsrv.StartTLS()

Expand Down Expand Up @@ -264,7 +270,7 @@ func TestHTTP2WebSocketFallback(t *testing.T) {
t.Fatal("client didn't error on initial connect")
}
reason := <-reasonCh
if !strings.Contains(reason, "wanted us to use HTTP/2") {
if !strings.Contains(reason, "DERP requires Upgrade which needs") {
t.Fatalf("reason doesn't contain message: %s", reason)
}
if err := c.Connect(context.Background()); err != nil {
Expand Down