From 6a70ae6408def0c5ba838567cdc175a07f3e3d47 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:55:48 -0400 Subject: [PATCH] HTTP transport: Fix an issue when HTTP client start fail with 403 (#3910) Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com> --- transport/internet/http/dialer.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/transport/internet/http/dialer.go b/transport/internet/http/dialer.go index 31ded010..d0a86e8b 100644 --- a/transport/internet/http/dialer.go +++ b/transport/internet/http/dialer.go @@ -134,21 +134,21 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in return nil, err } address := net.ParseAddress(rawHost) - + hctx = c.ContextWithID(hctx, c.IDFromContext(ctx)) hctx = session.ContextWithOutbounds(hctx, session.OutboundsFromContext(ctx)) hctx = session.ContextWithTimeoutOnly(hctx, true) - + pconn, err := internet.DialSystem(hctx, net.TCPDestination(address, port), sockopt) if err != nil { - errors.LogErrorInner(ctx, err, "failed to dial to " + addr) + errors.LogErrorInner(ctx, err, "failed to dial to "+addr) return nil, err } - + if realityConfigs != nil { return reality.UClient(pconn, realityConfigs, hctx, dest) } - + var cn tls.Interface if fingerprint := tls.GetFingerprint(tlsConfigs.Fingerprint); fingerprint != nil { cn = tls.UClient(pconn, tlsConfig, fingerprint).(*tls.UConn) @@ -156,12 +156,12 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in cn = tls.Client(pconn, tlsConfig).(*tls.Conn) } if err := cn.HandshakeContext(ctx); err != nil { - errors.LogErrorInner(ctx, err, "failed to dial to " + addr) + errors.LogErrorInner(ctx, err, "failed to dial to "+addr) return nil, err } if !tlsConfig.InsecureSkipVerify { if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil { - errors.LogErrorInner(ctx, err, "failed to dial to " + addr) + errors.LogErrorInner(ctx, err, "failed to dial to "+addr) return nil, err } } @@ -224,7 +224,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me Host: dest.NetAddr(), Path: httpSettings.getNormalizedPath(), }, - Header: httpHeaders, + Header: httpHeaders, } // Disable any compression method from server. request.Header.Set("Accept-Encoding", "identity") @@ -232,8 +232,12 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me wrc := &WaitReadCloser{Wait: make(chan struct{})} go func() { response, err := client.Do(request) - if err != nil { - errors.LogWarningInner(ctx, err, "failed to dial to ", dest) + if err != nil || response.StatusCode != 200 { + if err != nil { + errors.LogWarningInner(ctx, err, "failed to dial to ", dest) + } else { + errors.LogWarning(ctx, "unexpected status ", response.StatusCode) + } wrc.Close() { // Abandon `client` if `client.Do(request)` failed @@ -246,11 +250,6 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me } return } - if response.StatusCode != 200 { - errors.LogWarning(ctx, "unexpected status", response.StatusCode) - wrc.Close() - return - } wrc.Set(response.Body) }()