Skip to content

Commit d92002a

Browse files
authored
Dialer: Set TimeoutOnly for gctx and hctx
#2232 (comment) Thank @cty123 for testing Fixes #2232 BTW: Use `uConn.HandshakeContext(ctx)` in REALITY
1 parent b24a402 commit d92002a

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

transport/internet/grpc/dial.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,13 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
9898
MinConnectTimeout: 5 * time.Second,
9999
}),
100100
grpc.WithContextDialer(func(gctx context.Context, s string) (gonet.Conn, error) {
101-
gctx = session.ContextWithID(gctx, session.IDFromContext(ctx))
102-
gctx = session.ContextWithOutbound(gctx, session.OutboundFromContext(ctx))
103-
104-
rawHost, rawPort, err := net.SplitHostPort(s)
105101
select {
106102
case <-gctx.Done():
107103
return nil, gctx.Err()
108104
default:
109105
}
110106

107+
rawHost, rawPort, err := net.SplitHostPort(s)
111108
if err != nil {
112109
return nil, err
113110
}
@@ -119,9 +116,14 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
119116
return nil, err
120117
}
121118
address := net.ParseAddress(rawHost)
119+
120+
gctx = session.ContextWithID(gctx, session.IDFromContext(ctx))
121+
gctx = session.ContextWithOutbound(gctx, session.OutboundFromContext(ctx))
122+
gctx = session.ContextWithTimeoutOnly(gctx, true)
123+
122124
c, err := internet.DialSystem(gctx, net.TCPDestination(address, port), sockopt)
123125
if err == nil && realityConfig != nil {
124-
return reality.UClient(c, realityConfig, ctx, dest)
126+
return reality.UClient(c, realityConfig, gctx, dest)
125127
}
126128
return c, err
127129
}),

transport/internet/http/dialer.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
5353
}
5454

5555
transport := &http2.Transport{
56-
DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
56+
DialTLSContext: func(hctx context.Context, string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
5757
rawHost, rawPort, err := net.SplitHostPort(addr)
5858
if err != nil {
5959
return nil, err
@@ -67,18 +67,18 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
6767
}
6868
address := net.ParseAddress(rawHost)
6969

70-
dctx := context.Background()
71-
dctx = session.ContextWithID(dctx, session.IDFromContext(ctx))
72-
dctx = session.ContextWithOutbound(dctx, session.OutboundFromContext(ctx))
70+
hctx = session.ContextWithID(hctx, session.IDFromContext(ctx))
71+
hctx = session.ContextWithOutbound(hctx, session.OutboundFromContext(ctx))
72+
hctx = session.ContextWithTimeoutOnly(hctx, true)
7373

74-
pconn, err := internet.DialSystem(dctx, net.TCPDestination(address, port), sockopt)
74+
pconn, err := internet.DialSystem(hctx, net.TCPDestination(address, port), sockopt)
7575
if err != nil {
7676
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
7777
return nil, err
7878
}
7979

8080
if realityConfigs != nil {
81-
return reality.UClient(pconn, realityConfigs, ctx, dest)
81+
return reality.UClient(pconn, realityConfigs, hctx, dest)
8282
}
8383

8484
var cn tls.Interface

transport/internet/reality/reality.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func UClient(c net.Conn, config *Config, ctx context.Context, dest net.Destinati
156156
aead.Seal(hello.SessionId[:0], hello.Random[20:], hello.SessionId[:16], hello.Raw)
157157
copy(hello.Raw[39:], hello.SessionId)
158158
}
159-
if err := uConn.Handshake(); err != nil {
159+
if err := uConn.HandshakeContext(ctx); err != nil {
160160
return nil, err
161161
}
162162
if config.Show {

0 commit comments

Comments
 (0)