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.
This commit is contained in:
世界
2026-11-14 07:52:37 +00:00
parent 25d6a21f75
commit 08eaa2878c
+3 -11
View File
@@ -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,