From 34cc7f176e9988af1d918251cbe41d74373040c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 12 Apr 2023 15:14:55 +0800 Subject: [PATCH] Fix parsing query in http path --- outbound/http.go | 11 ++++++++--- transport/v2ray/transport.go | 2 +- transport/v2rayhttp/client.go | 11 ++++++----- transport/v2raywebsocket/client.go | 11 ++++------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/outbound/http.go b/outbound/http.go index 1711e98a..334fcc35 100644 --- a/outbound/http.go +++ b/outbound/http.go @@ -14,14 +14,14 @@ import ( "github.com/sagernet/sing/common" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" - "github.com/sagernet/sing/protocol/http" + sHTTP "github.com/sagernet/sing/protocol/http" ) var _ adapter.Outbound = (*HTTP)(nil) type HTTP struct { myOutboundAdapter - client *http.Client + client *sHTTP.Client } func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) (*HTTP, error) { @@ -37,7 +37,12 @@ func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, option logger: logger, tag: tag, }, - http.NewClient(detour, options.ServerOptions.Build(), options.Username, options.Password, nil), + sHTTP.NewClient(sHTTP.Options{ + Dialer: detour, + Server: options.ServerOptions.Build(), + Username: options.Username, + Password: options.Password, + }), }, nil } diff --git a/transport/v2ray/transport.go b/transport/v2ray/transport.go index 649999a8..9e46204a 100644 --- a/transport/v2ray/transport.go +++ b/transport/v2ray/transport.go @@ -46,7 +46,7 @@ func NewClientTransport(ctx context.Context, dialer N.Dialer, serverAddr M.Socks } switch options.Type { case C.V2RayTransportTypeHTTP: - return v2rayhttp.NewClient(ctx, dialer, serverAddr, options.HTTPOptions, tlsConfig), nil + return v2rayhttp.NewClient(ctx, dialer, serverAddr, options.HTTPOptions, tlsConfig) case C.V2RayTransportTypeGRPC: if tlsConfig == nil { return nil, C.ErrTLSRequired diff --git a/transport/v2rayhttp/client.go b/transport/v2rayhttp/client.go index 2131160f..c982493d 100644 --- a/transport/v2rayhttp/client.go +++ b/transport/v2rayhttp/client.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "net/url" - "strings" "time" "github.com/sagernet/sing-box/adapter" @@ -16,6 +15,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" 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" ) @@ -34,7 +34,7 @@ type Client struct { headers http.Header } -func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayHTTPOptions, tlsConfig tls.Config) adapter.V2RayClientTransport { +func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayHTTPOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) { var transport http.RoundTripper if tlsConfig == nil { transport = &http.Transport{ @@ -77,14 +77,15 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt } uri.Host = serverAddr.String() uri.Path = options.Path - if !strings.HasPrefix(uri.Path, "/") { - uri.Path = "/" + uri.Path + err := sHTTP.URLSetPath(&uri, options.Path) + if err != nil { + return nil, E.New("failed to set path: " + err.Error()) } for key, valueList := range options.Headers { client.headers[key] = valueList } client.url = &uri - return client + return client, nil } func (c *Client) DialContext(ctx context.Context) (net.Conn, error) { diff --git a/transport/v2raywebsocket/client.go b/transport/v2raywebsocket/client.go index be766b76..18fca276 100644 --- a/transport/v2raywebsocket/client.go +++ b/transport/v2raywebsocket/client.go @@ -5,7 +5,6 @@ import ( "net" "net/http" "net/url" - "strings" "time" "github.com/sagernet/sing-box/adapter" @@ -14,6 +13,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" + sHTTP "github.com/sagernet/sing/protocol/http" "github.com/sagernet/websocket" ) @@ -57,12 +57,9 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt } uri.Host = serverAddr.String() uri.Path = options.Path - if !strings.HasPrefix(uri.Path, "/") { - uri.Path = "/" + uri.Path - } - if strings.HasSuffix(uri.Path, "?") { - uri.ForceQuery = true - uri.Path = strings.TrimSuffix(uri.Path, "?") + err := sHTTP.URLSetPath(&uri, options.Path) + if err != nil { + return nil } headers := make(http.Header) for key, value := range options.Headers {