From 08eaa2878c3ec3307c783e6380757abdd911a2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 14 Nov 2026 07:52:37 +0000 Subject: [PATCH] Send reality fallback request before returning When authentication failed, the reality client returned the error and sent the fallback request from a goroutine on the same connection. The TLS dialer closes the connection when the handshake returns an error, so the request always failed with "use of closed network connection" and the client closed the connection right after the target's handshake. Send the request within the handshake context before returning the error, so the caller owns the connection again once it gets the error. --- common/tls/reality_client.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/common/tls/reality_client.go b/common/tls/reality_client.go index 80edad8c..0f8e1dc4 100644 --- a/common/tls/reality_client.go +++ b/common/tls/reality_client.go @@ -27,14 +27,12 @@ import ( "time" "unsafe" - "github.com/sagernet/sing-box/adapter" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing/common/debug" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/logger" M "github.com/sagernet/sing/common/metadata" - "github.com/sagernet/sing/common/ntp" aTLS "github.com/sagernet/sing/common/tls" utls "github.com/metacubex/utls" @@ -45,7 +43,6 @@ import ( var _ ConfigCompat = (*RealityClientConfig)(nil) type RealityClientConfig struct { - ctx context.Context uClient *UTLSClientConfig publicKey []byte shortID [8]byte @@ -84,7 +81,7 @@ func newRealityClient(ctx context.Context, logger logger.ContextLogger, serverAd return nil, E.New("invalid short_id") } - var config Config = &RealityClientConfig{ctx, uClient.(*UTLSClientConfig), publicKey, shortID} + var config Config = &RealityClientConfig{uClient.(*UTLSClientConfig), publicKey, shortID} if options.KernelRx || options.KernelTx { if !C.IsLinux { return nil, E.New("kTLS is only supported on Linux") @@ -209,7 +206,7 @@ func (e *RealityClientConfig) ClientHandshake(ctx context.Context, conn net.Conn } if !verifier.verified { - go realityClientFallback(e.ctx, uConn, e.uClient.ServerName(), e.uClient.id) + realityClientFallback(ctx, uConn, e.uClient.ServerName(), e.uClient.id) return nil, E.New("reality verification failed") } @@ -223,10 +220,6 @@ func realityClientFallback(ctx context.Context, uConn net.Conn, serverName strin DialTLSContext: func(ctx context.Context, network, addr string, config *tls.Config) (net.Conn, error) { return uConn, nil }, - TLSClientConfig: &tls.Config{ - Time: ntp.TimeFuncFromContext(ctx), - RootCAs: adapter.RootPoolFromContext(ctx), - }, }, } serverAddress := M.ParseSocksaddrHostPort(serverName, 0) @@ -234,7 +227,7 @@ func realityClientFallback(ctx context.Context, uConn net.Conn, serverName strin if serverAddress.IsIPv6() { host = "[" + host + "]" } - request, err := http.NewRequest("GET", "https://"+host, nil) + request, err := http.NewRequestWithContext(ctx, "GET", "https://"+host, nil) if err != nil { return } @@ -250,7 +243,6 @@ func realityClientFallback(ctx context.Context, uConn net.Conn, serverName strin func (e *RealityClientConfig) Clone() Config { return &RealityClientConfig{ - e.ctx, e.uClient.Clone().(*UTLSClientConfig), e.publicKey, e.shortID,