chore: using new net/http api in gun and xhttp listener

This commit is contained in:
wwqgtxx committed 2026-05-08 15:40:20 +08:00
1 parent b8b443942e
commit 3fb93bd79e
5 files changed
+34 -19

No files matched your search

+13 -1
View File
@@ -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...)
}
+9 -1
View File
@@ -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
}
+9 -1
View File
@@ -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
}
+1 -9
View File
@@ -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,
})
}
+2 -7
View File
@@ -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 {