Dialer: Set TimeoutOnly for `gctx` and `hctx`

https://github.com/XTLS/Xray-core/issues/2232#issuecomment-1694570914

Thank @cty123 for testing

Fixes https://github.com/XTLS/Xray-core/issues/2232

BTW: Use `uConn.HandshakeContext(ctx)` in REALITY
This commit is contained in:
RPRX 2023-08-27 05:55:58 +00:00 committed by GitHub
parent b24a4028f1
commit d92002ad12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 12 deletions

View File

@ -98,16 +98,13 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
MinConnectTimeout: 5 * time.Second, MinConnectTimeout: 5 * time.Second,
}), }),
grpc.WithContextDialer(func(gctx context.Context, s string) (gonet.Conn, error) { grpc.WithContextDialer(func(gctx context.Context, s string) (gonet.Conn, error) {
gctx = session.ContextWithID(gctx, session.IDFromContext(ctx))
gctx = session.ContextWithOutbound(gctx, session.OutboundFromContext(ctx))
rawHost, rawPort, err := net.SplitHostPort(s)
select { select {
case <-gctx.Done(): case <-gctx.Done():
return nil, gctx.Err() return nil, gctx.Err()
default: default:
} }
rawHost, rawPort, err := net.SplitHostPort(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -119,9 +116,14 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
return nil, err return nil, err
} }
address := net.ParseAddress(rawHost) address := net.ParseAddress(rawHost)
gctx = session.ContextWithID(gctx, session.IDFromContext(ctx))
gctx = session.ContextWithOutbound(gctx, session.OutboundFromContext(ctx))
gctx = session.ContextWithTimeoutOnly(gctx, true)
c, err := internet.DialSystem(gctx, net.TCPDestination(address, port), sockopt) c, err := internet.DialSystem(gctx, net.TCPDestination(address, port), sockopt)
if err == nil && realityConfig != nil { if err == nil && realityConfig != nil {
return reality.UClient(c, realityConfig, ctx, dest) return reality.UClient(c, realityConfig, gctx, dest)
} }
return c, err return c, err
}), }),

View File

@ -53,7 +53,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
} }
transport := &http2.Transport{ transport := &http2.Transport{
DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) { DialTLSContext: func(hctx context.Context, string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
rawHost, rawPort, err := net.SplitHostPort(addr) rawHost, rawPort, err := net.SplitHostPort(addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -67,18 +67,18 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
} }
address := net.ParseAddress(rawHost) address := net.ParseAddress(rawHost)
dctx := context.Background() hctx = session.ContextWithID(hctx, session.IDFromContext(ctx))
dctx = session.ContextWithID(dctx, session.IDFromContext(ctx)) hctx = session.ContextWithOutbound(hctx, session.OutboundFromContext(ctx))
dctx = session.ContextWithOutbound(dctx, session.OutboundFromContext(ctx)) hctx = session.ContextWithTimeoutOnly(hctx, true)
pconn, err := internet.DialSystem(dctx, net.TCPDestination(address, port), sockopt) pconn, err := internet.DialSystem(hctx, net.TCPDestination(address, port), sockopt)
if err != nil { if err != nil {
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog() newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
return nil, err return nil, err
} }
if realityConfigs != nil { if realityConfigs != nil {
return reality.UClient(pconn, realityConfigs, ctx, dest) return reality.UClient(pconn, realityConfigs, hctx, dest)
} }
var cn tls.Interface var cn tls.Interface

View File

@ -156,7 +156,7 @@ func UClient(c net.Conn, config *Config, ctx context.Context, dest net.Destinati
aead.Seal(hello.SessionId[:0], hello.Random[20:], hello.SessionId[:16], hello.Raw) aead.Seal(hello.SessionId[:0], hello.Random[20:], hello.SessionId[:16], hello.Raw)
copy(hello.Raw[39:], hello.SessionId) copy(hello.Raw[39:], hello.SessionId)
} }
if err := uConn.Handshake(); err != nil { if err := uConn.HandshakeContext(ctx); err != nil {
return nil, err return nil, err
} }
if config.Show { if config.Show {