From faa58f27fc3a664a4849dc4b76c899a050bc2ce0 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 12 May 2026 09:51:51 +0800 Subject: [PATCH] chore: add some comments for using the new net/http API --- adapter/outbound/masque.go | 6 +++++- component/tls/reality.go | 4 ++++ listener/sing_vless/server.go | 12 ++++++++++++ listener/sing_vmess/server.go | 6 ++++++ listener/trojan/server.go | 6 ++++++ transport/gun/gun.go | 4 ++++ transport/trusttunnel/client.go | 4 ++++ transport/trusttunnel/service.go | 5 +++++ transport/vmess/h2.go | 4 ++++ transport/xhttp/client.go | 4 ++++ 10 files changed, 54 insertions(+), 1 deletion(-) diff --git a/adapter/outbound/masque.go b/adapter/outbound/masque.go index d00deaf8d..1eae923bf 100644 --- a/adapter/outbound/masque.go +++ b/adapter/outbound/masque.go @@ -163,7 +163,11 @@ func NewMasque(option MasqueOption) (*Masque, error) { if option.Network == "h2" { tlsConfig.NextProtos = []string{"h2"} - // use h2c mode to disallow the net/http fallback to http1.1 when server returns a not h2 ALPN + // use h2c mode to disallow the net/http fallback to http1.1 when the server returns a not h2 ALPN + // + // Note that this usage is only applicable to our own net/http fork. + // The standard library also needs to mask the tls.Conn type for the conn returned by DialTLSContext + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols := new(http.Protocols) protocols.SetUnencryptedHTTP2(true) outbound.h2Transport = &http.Transport{ diff --git a/component/tls/reality.go b/component/tls/reality.go index 4fdaf6961..ff6a0a3d7 100644 --- a/component/tls/reality.go +++ b/component/tls/reality.go @@ -134,6 +134,10 @@ func GetRealityConn(ctx context.Context, conn net.Conn, fingerprint UClientHello func realityClientFallback(uConn net.Conn, serverName string, fingerprint utls.ClientHelloID) { defer uConn.Close() // use h2c mode to disallow the net/http fallback to http1.1 + // + // Note that this usage is only applicable to our own net/http fork. + // The standard library also needs to mask the tls.Conn type for the conn returned by DialTLSContext + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols := new(http.Protocols) protocols.SetUnencryptedHTTP2(true) client := http.Client{ diff --git a/listener/sing_vless/server.go b/listener/sing_vless/server.go index 774b6e53d..f5d46c2dd 100644 --- a/listener/sing_vless/server.go +++ b/listener/sing_vless/server.go @@ -151,6 +151,12 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition) }) httpServer.Protocols.SetHTTP2(true) // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + // + // Enable HTTP/2 support unconditionally on the server. + // + // Note that this usage is limited to our own net/http fork + // The standard library also needs to mask the tls.Conn type for the conn returned by the Listener. + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 httpServer.Protocols.SetUnencryptedHTTP2(true) tlsConfig.NextProtos = append([]string{"h2"}, tlsConfig.NextProtos...) // h2 must before http/1.1 } @@ -197,6 +203,12 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition) httpServer.Protocols.SetHTTP1(true) httpServer.Protocols.SetHTTP2(true) // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + // + // Enable HTTP/2 support unconditionally on the server. + // + // Note that this usage is limited to our own net/http fork + // The standard library also needs to mask the tls.Conn type for the conn returned by the Listener. + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 httpServer.Protocols.SetUnencryptedHTTP2(true) if !slices.Contains(tlsConfig.NextProtos, "http/1.1") { tlsConfig.NextProtos = append([]string{"http/1.1"}, tlsConfig.NextProtos...) diff --git a/listener/sing_vmess/server.go b/listener/sing_vmess/server.go index 8b62e2c9a..b8e09a238 100644 --- a/listener/sing_vmess/server.go +++ b/listener/sing_vmess/server.go @@ -149,6 +149,12 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) }) httpServer.Protocols.SetHTTP2(true) // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + // + // Enable HTTP/2 support unconditionally on the server. + // + // Note that this usage is limited to our own net/http fork + // The standard library also needs to mask the tls.Conn type for the conn returned by the Listener. + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 httpServer.Protocols.SetUnencryptedHTTP2(true) tlsConfig.NextProtos = append([]string{"h2"}, tlsConfig.NextProtos...) // h2 must before http/1.1 } diff --git a/listener/trojan/server.go b/listener/trojan/server.go index f33e7c613..85ce2d09c 100644 --- a/listener/trojan/server.go +++ b/listener/trojan/server.go @@ -146,6 +146,12 @@ func New(config LC.TrojanServer, tunnel C.Tunnel, additions ...inbound.Addition) }) httpServer.Protocols.SetHTTP2(true) // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + // + // Enable HTTP/2 support unconditionally on the server. + // + // Note that this usage is limited to our own net/http fork + // The standard library also needs to mask the tls.Conn type for the conn returned by the Listener. + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 httpServer.Protocols.SetUnencryptedHTTP2(true) tlsConfig.NextProtos = append([]string{"h2"}, tlsConfig.NextProtos...) // h2 must before http/1.1 } diff --git a/transport/gun/gun.go b/transport/gun/gun.go index dce42e1aa..65d54ef7f 100644 --- a/transport/gun/gun.go +++ b/transport/gun/gun.go @@ -290,6 +290,10 @@ func NewTransport(dialFn DialFn, tlsConfig *vmess.TLSConfig, gunCfg *Config) *Tr } // use h2c mode to disallow the net/http fallback to http1.1 + // + // Note that this usage is only applicable to our own net/http fork. + // The standard library also needs to mask the tls.Conn type for the conn returned by DialTLSContext, + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols := new(http.Protocols) protocols.SetUnencryptedHTTP2(true) transport := &http.Transport{ diff --git a/transport/trusttunnel/client.go b/transport/trusttunnel/client.go index a760d164f..7a98a1b20 100644 --- a/transport/trusttunnel/client.go +++ b/transport/trusttunnel/client.go @@ -87,6 +87,10 @@ func NewClient(ctx context.Context, options ClientOptions) (client *Client, err func (c *Client) h2RoundTripper(tlsConfig *vmess.TLSConfig) { // use h2c mode to disallow the net/http fallback to http1.1 + // + // Note that this usage is only applicable to our own net/http fork. + // The standard library also needs to mask the tls.Conn type for the conn returned by DialTLSContext, + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols := new(http.Protocols) protocols.SetUnencryptedHTTP2(true) c.roundTripper = &http.Transport{ diff --git a/transport/trusttunnel/service.go b/transport/trusttunnel/service.go index 53a5597f0..9aa87ee3c 100644 --- a/transport/trusttunnel/service.go +++ b/transport/trusttunnel/service.go @@ -74,6 +74,11 @@ func (s *Service) Start(tcpListener net.Listener, udpConn net.PacketConn, tlsCon protocols := new(http.Protocols) protocols.SetHTTP1(true) protocols.SetHTTP2(true) + // Enable HTTP/2 support unconditionally on the server. + // + // Note that this usage is limited to our own net/http fork + // The standard library also needs to mask the tls.Conn type for the conn returned by the Listener. + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols.SetUnencryptedHTTP2(true) s.httpServer = &http.Server{ Handler: s, diff --git a/transport/vmess/h2.go b/transport/vmess/h2.go index 3ad336368..a656b6f11 100644 --- a/transport/vmess/h2.go +++ b/transport/vmess/h2.go @@ -101,6 +101,10 @@ func StreamH2Conn(ctx context.Context, conn net.Conn, cfg *H2Config) (_ net.Conn } // use h2c mode to disallow the net/http fallback to http1.1 + // + // Note that this usage is only applicable to our own net/http fork. + // The standard library also needs to mask the tls.Conn type for the conn returned by DialTLSContext, + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols := new(http.Protocols) protocols.SetUnencryptedHTTP2(true) transport := &http.Transport{ diff --git a/transport/xhttp/client.go b/transport/xhttp/client.go index dd58f80de..93c1de9b9 100644 --- a/transport/xhttp/client.go +++ b/transport/xhttp/client.go @@ -203,6 +203,10 @@ func NewTransport(dialRaw DialRawFunc, wrapTLS WrapTLSFunc, dialQUIC DialQUICFun keepAlivePeriod = 0 } // use h2c mode to disallow the net/http fallback to http1.1 + // + // Note that this usage is only applicable to our own net/http fork. + // The standard library also needs to mask the tls.Conn type for the conn returned by DialTLSContext, + // see: https://github.com/golang/go/issues/79293#issuecomment-4426393534 protocols := new(http.Protocols) protocols.SetUnencryptedHTTP2(true) return &http.Transport{