From 7fb1f6535481ca544dd6d167b93c9181af6a18dd Mon Sep 17 00:00:00 2001 From: Jim Han <50871214+JimhHan@users.noreply.github.com> Date: Mon, 1 Mar 2021 10:43:27 +0800 Subject: [PATCH] Fix https://github.com/XTLS/Xray-core/issues/289 (#300) --- transport/internet/http/dialer.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/transport/internet/http/dialer.go b/transport/internet/http/dialer.go index 2255b9f2..e7d6242a 100644 --- a/transport/internet/http/dialer.go +++ b/transport/internet/http/dialer.go @@ -11,6 +11,7 @@ import ( "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net/cnc" + "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/pipe" @@ -22,7 +23,7 @@ var ( globalDialerAccess sync.Mutex ) -func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) (*http.Client, error) { +func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config, sockopt *internet.SocketConfig) (*http.Client, error) { globalDialerAccess.Lock() defer globalDialerAccess.Unlock() @@ -49,17 +50,24 @@ func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.C } address := net.ParseAddress(rawHost) - pconn, err := internet.DialSystem(ctx, net.TCPDestination(address, port), nil) + dctx := context.Background() + dctx = session.ContextWithID(dctx, session.IDFromContext(ctx)) + dctx = session.ContextWithOutbound(dctx, session.OutboundFromContext(ctx)) + + pconn, err := internet.DialSystem(dctx, net.TCPDestination(address, port), sockopt) if err != nil { + newError("failed to dial to " + addr).Base(err).AtError().WriteToLog() return nil, err } cn := gotls.Client(pconn, tlsConfig) if err := cn.Handshake(); err != nil { + newError("failed to dial to " + addr).Base(err).AtError().WriteToLog() return nil, err } if !tlsConfig.InsecureSkipVerify { if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil { + newError("failed to dial to " + addr).Base(err).AtError().WriteToLog() return nil, err } } @@ -90,7 +98,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me if tlsConfig == nil { return nil, newError("TLS must be enabled for http transport.").AtWarning() } - client, err := getHTTPClient(ctx, dest, tlsConfig) + client, err := getHTTPClient(ctx, dest, tlsConfig, streamSettings.SocketSettings) if err != nil { return nil, err }