-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2: support unencrypted HTTP/2 handoff from net/http
Allow net/http to pass unencrypted net.Conns to Server/Transport. We don't have an existing way to pass a conn other than a *tls.Conn into this package, so (ab)use TLSNextProto to pass unencrypted connections: The http2 package adds an "unencrypted_http2" entry to the TLSNextProto maps. The net/http package calls this function with a *tls.Conn wrapping a net.Conn with an UnencryptedNetConn method returning the underlying, unencrypted net.Conn. For golang/go#67816 Change-Id: I31f9c1ba31a17c82c8ed651382bd94193acf09b9 Reviewed-on: https://go-review.googlesource.com/c/net/+/625175 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
- Loading branch information
Showing
4 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package http2 | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"net" | ||
) | ||
|
||
const nextProtoUnencryptedHTTP2 = "unencrypted_http2" | ||
|
||
// unencryptedNetConnFromTLSConn retrieves a net.Conn wrapped in a *tls.Conn. | ||
// | ||
// TLSNextProto functions accept a *tls.Conn. | ||
// | ||
// When passing an unencrypted HTTP/2 connection to a TLSNextProto function, | ||
// we pass a *tls.Conn with an underlying net.Conn containing the unencrypted connection. | ||
// To be extra careful about mistakes (accidentally dropping TLS encryption in a place | ||
// where we want it), the tls.Conn contains a net.Conn with an UnencryptedNetConn method | ||
// that returns the actual connection we want to use. | ||
func unencryptedNetConnFromTLSConn(tc *tls.Conn) (net.Conn, error) { | ||
conner, ok := tc.NetConn().(interface { | ||
UnencryptedNetConn() net.Conn | ||
}) | ||
if !ok { | ||
return nil, errors.New("http2: TLS conn unexpectedly found in unencrypted handoff") | ||
} | ||
return conner.UnencryptedNetConn(), nil | ||
} |