mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-07-23 15:53:28 +03:00
Fix h2c transport
This commit is contained in:
@@ -17,6 +17,9 @@ import (
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
sHttp "github.com/sagernet/sing/protocol/http"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/net/http2/h2c"
|
||||
)
|
||||
|
||||
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
||||
@@ -25,6 +28,8 @@ type Server struct {
|
||||
handler N.TCPConnectionHandler
|
||||
errorHandler E.Handler
|
||||
httpServer *http.Server
|
||||
h2Server *http2.Server
|
||||
h2cHandler http.Handler
|
||||
path string
|
||||
}
|
||||
|
||||
@@ -37,6 +42,7 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig *
|
||||
handler: handler,
|
||||
errorHandler: errorHandler,
|
||||
path: fmt.Sprintf("/%s/Tun", url.QueryEscape(options.ServiceName)),
|
||||
h2Server: new(http2.Server),
|
||||
}
|
||||
if tlsConfig != nil {
|
||||
if !common.Contains(tlsConfig.NextProtos, "h2") {
|
||||
@@ -47,10 +53,15 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig *
|
||||
Handler: server,
|
||||
TLSConfig: tlsConfig,
|
||||
}
|
||||
server.h2cHandler = h2c.NewHandler(server, server.h2Server)
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.Method == "PRI" && len(request.Header) == 0 && request.URL.Path == "*" && request.Proto == "HTTP/2.0" {
|
||||
s.h2cHandler.ServeHTTP(writer, request)
|
||||
return
|
||||
}
|
||||
if request.URL.Path != s.path {
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
s.badRequest(request, E.New("bad path: ", request.URL.Path))
|
||||
@@ -80,6 +91,10 @@ func (s *Server) badRequest(request *http.Request, err error) {
|
||||
}
|
||||
|
||||
func (s *Server) Serve(listener net.Listener) error {
|
||||
err := http2.ConfigureServer(s.httpServer, s.h2Server)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.httpServer.TLSConfig == nil {
|
||||
return s.httpServer.Serve(listener)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user