From e20e2d57c9d9add0fb133e73fc1c1d03336ba4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 11 Apr 2023 11:38:53 +0800 Subject: [PATCH] Fix set HTTP TLS ALPN --- common/tls/std_server.go | 2 +- transport/v2raygrpc/server.go | 5 ++++- transport/v2raygrpclite/server.go | 4 ++-- transport/v2rayhttp/server.go | 5 +++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/common/tls/std_server.go b/common/tls/std_server.go index 11c78f48..2c875855 100644 --- a/common/tls/std_server.go +++ b/common/tls/std_server.go @@ -180,7 +180,7 @@ func NewSTDServer(ctx context.Context, router adapter.Router, logger log.Logger, tlsConfig.ServerName = options.ServerName } if len(options.ALPN) > 0 { - tlsConfig.NextProtos = append(tlsConfig.NextProtos, options.ALPN...) + tlsConfig.NextProtos = append(options.ALPN, tlsConfig.NextProtos...) } if options.MinVersion != "" { minVersion, err := ParseTLSVersion(options.MinVersion) diff --git a/transport/v2raygrpc/server.go b/transport/v2raygrpc/server.go index aadc23d2..7073eb08 100644 --- a/transport/v2raygrpc/server.go +++ b/transport/v2raygrpc/server.go @@ -15,6 +15,7 @@ import ( M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" + "golang.org/x/net/http2" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" gM "google.golang.org/grpc/metadata" @@ -32,7 +33,9 @@ type Server struct { func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler N.TCPConnectionHandler) (*Server, error) { var serverOptions []grpc.ServerOption if tlsConfig != nil { - tlsConfig.SetNextProtos([]string{"h2"}) + if !common.Contains(tlsConfig.NextProtos(), http2.NextProtoTLS) { + tlsConfig.SetNextProtos(append([]string{"h2"}, tlsConfig.NextProtos()...)) + } serverOptions = append(serverOptions, grpc.Creds(NewTLSTransportCredentials(tlsConfig))) } if options.IdleTimeout > 0 { diff --git a/transport/v2raygrpclite/server.go b/transport/v2raygrpclite/server.go index c3c62232..2d6687fe 100644 --- a/transport/v2raygrpclite/server.go +++ b/transport/v2raygrpclite/server.go @@ -101,8 +101,8 @@ func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter func (s *Server) Serve(listener net.Listener) error { if s.tlsConfig != nil { - if len(s.tlsConfig.NextProtos()) == 0 { - s.tlsConfig.SetNextProtos([]string{http2.NextProtoTLS}) + if !common.Contains(s.tlsConfig.NextProtos(), http2.NextProtoTLS) { + s.tlsConfig.SetNextProtos(append([]string{"h2"}, s.tlsConfig.NextProtos()...)) } listener = aTLS.NewListener(listener, s.tlsConfig) } diff --git a/transport/v2rayhttp/server.go b/transport/v2rayhttp/server.go index 95588f43..304836ec 100644 --- a/transport/v2rayhttp/server.go +++ b/transport/v2rayhttp/server.go @@ -161,6 +161,11 @@ func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter func (s *Server) Serve(listener net.Listener) error { if s.tlsConfig != nil { + if len(s.tlsConfig.NextProtos()) == 0 { + s.tlsConfig.SetNextProtos([]string{http2.NextProtoTLS, "http/1.1"}) + } else if !common.Contains(s.tlsConfig.NextProtos(), http2.NextProtoTLS) { + s.tlsConfig.SetNextProtos(append([]string{"h2"}, s.tlsConfig.NextProtos()...)) + } listener = aTLS.NewListener(listener, s.tlsConfig) } return s.httpServer.Serve(listener)