Skip to content

Commit 723ef2b

Browse files
committed
refactor (remove TimeoutOnly)
1 parent b53f8b5 commit 723ef2b

14 files changed

Lines changed: 14 additions & 71 deletions

File tree

common/mux/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
227227
errors.LogInfoInner(ctx, err, "XUDP hit ", meta.GlobalID)
228228
}
229229
if mb != nil {
230-
ctx = session.ContextWithTimeoutOnly(ctx, true)
230+
ctx = context.WithoutCancel(ctx)
231231
// Actually, it won't return an error in Xray-core's implementations.
232232
link, err := w.dispatcher.Dispatch(ctx, meta.Target)
233233
if err != nil {

common/session/context.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ import (
1111
)
1212

1313
const (
14-
inboundSessionKey ctx.SessionKey = 1
15-
outboundSessionKey ctx.SessionKey = 2
16-
contentSessionKey ctx.SessionKey = 3
17-
isReverseMuxKey ctx.SessionKey = 4 // is reverse mux
18-
sockoptSessionKey ctx.SessionKey = 5 // used by dokodemo to only receive sockopt.Mark
19-
trackedConnectionErrorKey ctx.SessionKey = 6 // used by observer to get outbound error
20-
dispatcherKey ctx.SessionKey = 7 // used by ss2022 inbounds to get dispatcher
21-
timeoutOnlyKey ctx.SessionKey = 8 // mux context's child contexts to only cancel when its own traffic times out
22-
allowedNetworkKey ctx.SessionKey = 9 // muxcool server control incoming request tcp/udp
23-
fullHandlerKey ctx.SessionKey = 10 // outbound gets full handler
24-
mitmAlpn11Key ctx.SessionKey = 11 // used by TLS dialer
25-
mitmServerNameKey ctx.SessionKey = 12 // used by TLS dialer
26-
27-
streamSettingsKey ctx.SessionKey = 13
14+
inboundSessionKey ctx.SessionKey = iota
15+
outboundSessionKey
16+
contentSessionKey
17+
isReverseMuxKey // is reverse mux
18+
sockoptSessionKey // used by dokodemo to only receive sockopt.Mark
19+
trackedConnectionErrorKey // used by observer to get outbound error
20+
dispatcherKey // used by ss2022 inbounds to get dispatcher
21+
allowedNetworkKey // muxcool server control incoming request tcp/udp
22+
fullHandlerKey // outbound gets full handler
23+
mitmAlpn11Key // used by TLS dialer
24+
mitmServerNameKey // used by TLS dialer
25+
streamSettingsKey
2826
)
2927

3028
func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
@@ -137,17 +135,6 @@ func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
137135
return nil
138136
}
139137

140-
func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
141-
return context.WithValue(ctx, timeoutOnlyKey, only)
142-
}
143-
144-
func TimeoutOnlyFromContext(ctx context.Context) bool {
145-
if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
146-
return val
147-
}
148-
return false
149-
}
150-
151138
func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
152139
return context.WithValue(ctx, allowedNetworkKey, network)
153140
}

proxy/dns/dns.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
214214
}
215215
}
216216

217-
if session.TimeoutOnlyFromContext(ctx) {
218-
ctx = context.WithoutCancel(ctx)
219-
}
220-
221217
ctx, cancel := context.WithCancel(ctx)
222218
terminate := func() {
223219
cancel()

proxy/freedom/freedom.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,6 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
388388
defer conn.Close()
389389
errors.LogInfo(ctx, "connection opened to ", destination, ", local endpoint ", conn.LocalAddr(), ", remote endpoint ", conn.RemoteAddr())
390390

391-
if session.TimeoutOnlyFromContext(ctx) {
392-
ctx = context.WithoutCancel(ctx)
393-
}
394-
395391
plcy := h.policy()
396392
ctx, cancel := context.WithCancel(ctx)
397393
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)

proxy/http/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
127127
p = c.policyManager.ForLevel(user.Level)
128128
}
129129

130-
if session.TimeoutOnlyFromContext(ctx) {
131-
ctx = context.WithoutCancel(ctx)
132-
}
133-
134130
ctx, cancel := context.WithCancel(ctx)
135131
timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle)
136132

proxy/hysteria/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
6767
defer conn.Close()
6868
errors.LogInfo(ctx, "tunneling request to ", target, " via ", target.Network, ":", c.server.Destination.NetAddr())
6969

70-
if session.TimeoutOnlyFromContext(ctx) {
71-
ctx = context.WithoutCancel(ctx)
72-
}
73-
7470
sessionPolicy := c.policyManager.ForLevel(0)
7571
ctx, cancel := context.WithCancel(ctx)
7672
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)

proxy/shadowsocks/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
9595
}
9696
request.User = user
9797

98-
if session.TimeoutOnlyFromContext(ctx) {
99-
ctx = context.WithoutCancel(ctx)
100-
}
101-
10298
sessionPolicy := c.policyManager.ForLevel(user.Level)
10399
ctx, cancel := context.WithCancel(ctx)
104100
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)

proxy/shadowsocks_2022/outbound.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
8383
return errors.New("failed to connect to server").Base(err)
8484
}
8585

86-
if session.TimeoutOnlyFromContext(ctx) {
87-
ctx = context.WithoutCancel(ctx)
88-
}
89-
9086
if network == net.Network_TCP {
9187
serverConn := o.method.DialEarlyConn(connection, singbridge.ToSocksaddr(destination))
9288
var handshake bool

proxy/socks/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
117117
errors.LogInfoInner(ctx, err, "failed to clear deadline after handshake")
118118
}
119119

120-
if session.TimeoutOnlyFromContext(ctx) {
121-
ctx = context.WithoutCancel(ctx)
122-
}
123-
124120
ctx, cancel := context.WithCancel(ctx)
125121
timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle)
126122

proxy/trojan/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
8181
return errors.New("user account is not valid")
8282
}
8383

84-
if session.TimeoutOnlyFromContext(ctx) {
85-
ctx = context.WithoutCancel(ctx)
86-
}
87-
8884
sessionPolicy := c.policyManager.ForLevel(user.Level)
8985
ctx, cancel := context.WithCancel(ctx)
9086
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)

0 commit comments

Comments
 (0)