From 3fb93bd79ef5d59470fb7116e000f4419b5ec03d Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Fri, 8 May 2026 15:40:20 +0800 Subject: [PATCH] chore: using new net/http api in gun and xhttp listener --- listener/sing_vless/server.go | 14 +++++++++++++- listener/sing_vmess/server.go | 10 +++++++++- listener/trojan/server.go | 10 +++++++++- transport/gun/server.go | 10 +--------- transport/xhttp/server.go | 9 ++------- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/listener/sing_vless/server.go b/listener/sing_vless/server.go index 4e41f6bee..774b6e53d 100644 --- a/listener/sing_vless/server.go +++ b/listener/sing_vless/server.go @@ -5,6 +5,7 @@ import ( "errors" "net" "strings" + "time" "github.com/metacubex/mihomo/adapter/inbound" "github.com/metacubex/mihomo/component/ca" @@ -78,9 +79,12 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition) }() } + httpServer := http.Server{ + IdleTimeout: 30 * time.Second, + Protocols: new(http.Protocols), + } tlsConfig := &tls.Config{Time: ntp.Now} var realityBuilder *reality.Builder - var httpServer http.Server if config.Certificate != "" && config.PrivateKey != "" { certLoader, err := ca.NewTLSKeyPairLoader(config.Certificate, config.PrivateKey) @@ -134,6 +138,7 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition) sl.HandleConn(conn, tunnel, additions...) }) httpServer.Handler = httpMux + httpServer.Protocols.SetHTTP1(true) tlsConfig.NextProtos = append(tlsConfig.NextProtos, "http/1.1") } if config.GrpcServiceName != "" { @@ -144,6 +149,9 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition) }, HttpHandler: httpServer.Handler, }) + httpServer.Protocols.SetHTTP2(true) + // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + httpServer.Protocols.SetUnencryptedHTTP2(true) tlsConfig.NextProtos = append([]string{"h2"}, tlsConfig.NextProtos...) // h2 must before http/1.1 } if config.XHTTPConfig.Mode != "" { @@ -186,6 +194,10 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition) if err != nil { return nil, err } + 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) + 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 5604700f6..8b62e2c9a 100644 --- a/listener/sing_vmess/server.go +++ b/listener/sing_vmess/server.go @@ -5,6 +5,7 @@ import ( "errors" "net" "strings" + "time" "github.com/metacubex/mihomo/adapter/inbound" "github.com/metacubex/mihomo/component/ca" @@ -76,9 +77,12 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) sl = &Listener{false, config, nil, service} + httpServer := http.Server{ + IdleTimeout: 30 * time.Second, + Protocols: new(http.Protocols), + } tlsConfig := &tls.Config{Time: ntp.Now} var realityBuilder *reality.Builder - var httpServer http.Server if config.Certificate != "" && config.PrivateKey != "" { certLoader, err := ca.NewTLSKeyPairLoader(config.Certificate, config.PrivateKey) @@ -132,6 +136,7 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) sl.HandleConn(conn, tunnel, additions...) }) httpServer.Handler = httpMux + httpServer.Protocols.SetHTTP1(true) tlsConfig.NextProtos = append(tlsConfig.NextProtos, "http/1.1") } if config.GrpcServiceName != "" { @@ -142,6 +147,9 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) }, HttpHandler: httpServer.Handler, }) + httpServer.Protocols.SetHTTP2(true) + // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + 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 c402a6f5e..f33e7c613 100644 --- a/listener/trojan/server.go +++ b/listener/trojan/server.go @@ -5,6 +5,7 @@ import ( "io" "net" "strings" + "time" "github.com/metacubex/mihomo/adapter/inbound" N "github.com/metacubex/mihomo/common/net" @@ -73,9 +74,12 @@ func New(config LC.TrojanServer, tunnel C.Tunnel, additions ...inbound.Addition) } sl = &Listener{false, config, nil, keys, pickCipher, h} + httpServer := http.Server{ + IdleTimeout: 30 * time.Second, + Protocols: new(http.Protocols), + } tlsConfig := &tls.Config{Time: ntp.Now} var realityBuilder *reality.Builder - var httpServer http.Server if config.Certificate != "" && config.PrivateKey != "" { certLoader, err := ca.NewTLSKeyPairLoader(config.Certificate, config.PrivateKey) @@ -129,6 +133,7 @@ func New(config LC.TrojanServer, tunnel C.Tunnel, additions ...inbound.Addition) sl.HandleConn(conn, tunnel, additions...) }) httpServer.Handler = httpMux + httpServer.Protocols.SetHTTP1(true) tlsConfig.NextProtos = append(tlsConfig.NextProtos, "http/1.1") } if config.GrpcServiceName != "" { @@ -139,6 +144,9 @@ func New(config LC.TrojanServer, tunnel C.Tunnel, additions ...inbound.Addition) }, HttpHandler: httpServer.Handler, }) + httpServer.Protocols.SetHTTP2(true) + // SetUnencryptedHTTP2 to ensure we can work in plain http2 and some tls conn is not *tls.Conn (like *reality.Conn) + httpServer.Protocols.SetUnencryptedHTTP2(true) tlsConfig.NextProtos = append([]string{"h2"}, tlsConfig.NextProtos...) // h2 must before http/1.1 } diff --git a/transport/gun/server.go b/transport/gun/server.go index 4c5280f7d..8c88a306f 100644 --- a/transport/gun/server.go +++ b/transport/gun/server.go @@ -6,18 +6,14 @@ import ( "net" "strings" "sync" - "time" "github.com/metacubex/mihomo/common/buf" "github.com/metacubex/mihomo/common/httputils" N "github.com/metacubex/mihomo/common/net" "github.com/metacubex/http" - "github.com/metacubex/http/h2c" ) -const idleTimeout = 30 * time.Second - type ServerOption struct { ServiceName string ConnHandler func(conn net.Conn) @@ -31,9 +27,7 @@ func NewServerHandler(options ServerOption) http.Handler { if httpHandler == nil { httpHandler = http.NewServeMux() } - // using h2c.NewHandler to ensure we can work in plain http2 - // and some tls conn is not *tls.Conn (like *reality.Conn) - return h2c.NewHandler(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { if request.URL.Path == path && request.Method == http.MethodPost && strings.HasPrefix(request.Header.Get("Content-Type"), "application/grpc") { @@ -63,8 +57,6 @@ func NewServerHandler(options ServerOption) http.Handler { } httpHandler.ServeHTTP(writer, request) - }), &http.Http2Server{ - IdleTimeout: idleTimeout, }) } diff --git a/transport/xhttp/server.go b/transport/xhttp/server.go index 6351d1cf0..fcdb437eb 100644 --- a/transport/xhttp/server.go +++ b/transport/xhttp/server.go @@ -15,7 +15,6 @@ import ( N "github.com/metacubex/mihomo/common/net" "github.com/metacubex/http" - "github.com/metacubex/http/h2c" ) type ServerOption struct { @@ -127,9 +126,7 @@ func NewServerHandler(opt ServerOption) (http.Handler, error) { if err != nil { return nil, err } - // using h2c.NewHandler to ensure we can work in plain http2 - // and some tls conn is not *tls.Conn (like *reality.Conn) - return h2c.NewHandler(&requestHandler{ + return &requestHandler{ config: opt.Config, connHandler: opt.ConnHandler, httpHandler: opt.HttpHandler, @@ -138,9 +135,7 @@ func NewServerHandler(opt ServerOption) (http.Handler, error) { scStreamUpServerSecs: scStreamUpServerSecs, scMaxBufferedPosts: scMaxBufferedPosts, sessions: map[string]*httpSession{}, - }, &http.Http2Server{ - IdleTimeout: 30 * time.Second, - }), nil + }, nil } func (h *requestHandler) upsertSession(sessionID string) *httpSession {