unencrypted.go 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2024 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "crypto/tls"
  7. "errors"
  8. "net"
  9. )
  10. const nextProtoUnencryptedHTTP2 = "unencrypted_http2"
  11. // unencryptedNetConnFromTLSConn retrieves a net.Conn wrapped in a *tls.Conn.
  12. //
  13. // TLSNextProto functions accept a *tls.Conn.
  14. //
  15. // When passing an unencrypted HTTP/2 connection to a TLSNextProto function,
  16. // we pass a *tls.Conn with an underlying net.Conn containing the unencrypted connection.
  17. // To be extra careful about mistakes (accidentally dropping TLS encryption in a place
  18. // where we want it), the tls.Conn contains a net.Conn with an UnencryptedNetConn method
  19. // that returns the actual connection we want to use.
  20. func unencryptedNetConnFromTLSConn(tc *tls.Conn) (net.Conn, error) {
  21. conner, ok := tc.NetConn().(interface {
  22. UnencryptedNetConn() net.Conn
  23. })
  24. if !ok {
  25. return nil, errors.New("http2: TLS conn unexpectedly found in unencrypted handoff")
  26. }
  27. return conner.UnencryptedNetConn(), nil
  28. }