Skip to content

Commit 93ca624

Browse files
committed
Revert "more outbounds"
This reverts commit f30526a.
1 parent dc7ae49 commit 93ca624

10 files changed

Lines changed: 118 additions & 19 deletions

File tree

proxy/freedom/freedom.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,20 @@ 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+
var newCtx context.Context
392+
var newCancel context.CancelFunc
391393
if session.TimeoutOnlyFromContext(ctx) {
392-
ctx = context.WithoutCancel(ctx)
394+
newCtx, newCancel = context.WithCancel(context.Background())
393395
}
394396

395397
plcy := h.policy()
396398
ctx, cancel := context.WithCancel(ctx)
397-
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
399+
timer := signal.CancelAfterInactivity(ctx, func() {
400+
cancel()
401+
if newCancel != nil {
402+
newCancel()
403+
}
404+
}, plcy.Timeouts.ConnectionIdle)
398405

399406
requestDone := func() error {
400407
defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
@@ -455,6 +462,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
455462
return nil
456463
}
457464

465+
if newCtx != nil {
466+
ctx = newCtx
467+
}
468+
458469
if err := task.Run(ctx, requestDone, task.OnSuccess(responseDone, task.Close(output))); err != nil {
459470
return errors.New("connection ends").Base(err)
460471
}

proxy/http/client.go

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

130+
var newCtx context.Context
131+
var newCancel context.CancelFunc
130132
if session.TimeoutOnlyFromContext(ctx) {
131-
ctx = context.WithoutCancel(ctx)
133+
newCtx, newCancel = context.WithCancel(context.Background())
132134
}
133135

134136
ctx, cancel := context.WithCancel(ctx)
135-
timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle)
137+
timer := signal.CancelAfterInactivity(ctx, func() {
138+
cancel()
139+
if newCancel != nil {
140+
newCancel()
141+
}
142+
}, p.Timeouts.ConnectionIdle)
136143

137144
requestFunc := func() error {
138145
defer timer.SetTimeout(p.Timeouts.DownlinkOnly)
@@ -144,6 +151,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
144151
return buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer))
145152
}
146153

154+
if newCtx != nil {
155+
ctx = newCtx
156+
}
157+
147158
responseDonePost := task.OnSuccess(responseFunc, task.Close(link.Writer))
148159
if err := task.Run(ctx, requestFunc, responseDonePost); err != nil {
149160
return errors.New("connection ends").Base(err)

proxy/hysteria/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,24 @@ 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+
var newCtx context.Context
71+
var newCancel context.CancelFunc
7072
if session.TimeoutOnlyFromContext(ctx) {
71-
ctx = context.WithoutCancel(ctx)
73+
newCtx, newCancel = context.WithCancel(context.Background())
7274
}
7375

7476
sessionPolicy := c.policyManager.ForLevel(0)
7577
ctx, cancel := context.WithCancel(ctx)
76-
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
78+
timer := signal.CancelAfterInactivity(ctx, func() {
79+
cancel()
80+
if newCancel != nil {
81+
newCancel()
82+
}
83+
}, sessionPolicy.Timeouts.ConnectionIdle)
84+
85+
if newCtx != nil {
86+
ctx = newCtx
87+
}
7788

7889
if target.Network == net.Network_TCP {
7990
requestDone := func() error {

proxy/shadowsocks/client.go

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

98+
var newCtx context.Context
99+
var newCancel context.CancelFunc
98100
if session.TimeoutOnlyFromContext(ctx) {
99-
ctx = context.WithoutCancel(ctx)
101+
newCtx, newCancel = context.WithCancel(context.Background())
100102
}
101103

102104
sessionPolicy := c.policyManager.ForLevel(user.Level)
103105
ctx, cancel := context.WithCancel(ctx)
104-
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
106+
timer := signal.CancelAfterInactivity(ctx, func() {
107+
cancel()
108+
if newCancel != nil {
109+
newCancel()
110+
}
111+
}, sessionPolicy.Timeouts.ConnectionIdle)
112+
113+
if newCtx != nil {
114+
ctx = newCtx
115+
}
105116

106117
if request.Command == protocol.RequestCommandTCP {
107118
requestDone := func() error {

proxy/shadowsocks_2022/outbound.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
8484
}
8585

8686
if session.TimeoutOnlyFromContext(ctx) {
87-
ctx = context.WithoutCancel(ctx)
87+
ctx, _ = context.WithCancel(context.Background())
8888
}
8989

9090
if network == net.Network_TCP {

proxy/socks/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,19 @@ 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+
var newCtx context.Context
121+
var newCancel context.CancelFunc
120122
if session.TimeoutOnlyFromContext(ctx) {
121-
ctx = context.WithoutCancel(ctx)
123+
newCtx, newCancel = context.WithCancel(context.Background())
122124
}
123125

124126
ctx, cancel := context.WithCancel(ctx)
125-
timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle)
127+
timer := signal.CancelAfterInactivity(ctx, func() {
128+
cancel()
129+
if newCancel != nil {
130+
newCancel()
131+
}
132+
}, p.Timeouts.ConnectionIdle)
126133

127134
var requestFunc func() error
128135
var responseFunc func() error
@@ -155,6 +162,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
155162
}
156163
}
157164

165+
if newCtx != nil {
166+
ctx = newCtx
167+
}
168+
158169
responseDonePost := task.OnSuccess(responseFunc, task.Close(link.Writer))
159170
if err := task.Run(ctx, requestFunc, responseDonePost); err != nil {
160171
return errors.New("connection ends").Base(err)

proxy/trojan/client.go

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

84+
var newCtx context.Context
85+
var newCancel context.CancelFunc
8486
if session.TimeoutOnlyFromContext(ctx) {
85-
ctx = context.WithoutCancel(ctx)
87+
newCtx, newCancel = context.WithCancel(context.Background())
8688
}
8789

8890
sessionPolicy := c.policyManager.ForLevel(user.Level)
8991
ctx, cancel := context.WithCancel(ctx)
90-
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
92+
timer := signal.CancelAfterInactivity(ctx, func() {
93+
cancel()
94+
if newCancel != nil {
95+
newCancel()
96+
}
97+
}, sessionPolicy.Timeouts.ConnectionIdle)
9198

9299
postRequest := func() error {
93100
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
@@ -143,6 +150,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
143150
return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer))
144151
}
145152

153+
if newCtx != nil {
154+
ctx = newCtx
155+
}
156+
146157
responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer))
147158
if err := task.Run(ctx, postRequest, responseDoneAndCloseWriter); err != nil {
148159
return errors.New("connection ends").Base(err)

proxy/vless/outbound/outbound.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,20 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
294294
ob.CanSpliceCopy = 3
295295
}
296296

297+
var newCtx context.Context
298+
var newCancel context.CancelFunc
297299
if session.TimeoutOnlyFromContext(ctx) {
298-
ctx = context.WithoutCancel(ctx)
300+
newCtx, newCancel = context.WithCancel(context.Background())
299301
}
300302

301303
sessionPolicy := h.policyManager.ForLevel(request.User.Level)
302304
ctx, cancel := context.WithCancel(ctx)
303-
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
305+
timer := signal.CancelAfterInactivity(ctx, func() {
306+
cancel()
307+
if newCancel != nil {
308+
newCancel()
309+
}
310+
}, sessionPolicy.Timeouts.ConnectionIdle)
304311

305312
clientReader := link.Reader // .(*pipe.Reader)
306313
clientWriter := link.Writer // .(*pipe.Writer)
@@ -406,6 +413,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
406413
return nil
407414
}
408415

416+
if newCtx != nil {
417+
ctx = newCtx
418+
}
419+
409420
if err := task.Run(ctx, postRequest, task.OnSuccess(getResponse, task.Close(clientWriter))); err != nil {
410421
return errors.New("connection ends").Base(err).AtInfo()
411422
}

proxy/vmess/outbound/outbound.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,22 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
131131

132132
behaviorSeed := crc64.Checksum(hashkdf.Sum(nil), crc64.MakeTable(crc64.ISO))
133133

134+
var newCtx context.Context
135+
var newCancel context.CancelFunc
134136
if session.TimeoutOnlyFromContext(ctx) {
135-
ctx = context.WithoutCancel(ctx)
137+
newCtx, newCancel = context.WithCancel(context.Background())
136138
}
137139

138140
session := encoding.NewClientSession(ctx, int64(behaviorSeed))
139141
sessionPolicy := h.policyManager.ForLevel(request.User.Level)
140142

141143
ctx, cancel := context.WithCancel(ctx)
142-
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
144+
timer := signal.CancelAfterInactivity(ctx, func() {
145+
cancel()
146+
if newCancel != nil {
147+
newCancel()
148+
}
149+
}, sessionPolicy.Timeouts.ConnectionIdle)
143150

144151
if request.Command == protocol.RequestCommandUDP && h.cone && request.Port != 53 && request.Port != 443 {
145152
request.Command = protocol.RequestCommandMux
@@ -205,6 +212,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
205212
return buf.Copy(bodyReader, output, buf.UpdateActivity(timer))
206213
}
207214

215+
if newCtx != nil {
216+
ctx = newCtx
217+
}
218+
208219
responseDonePost := task.OnSuccess(responseDone, task.Close(output))
209220
if err := task.Run(ctx, requestDone, responseDonePost); err != nil {
210221
return errors.New("connection ends").Base(err)

proxy/wireguard/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,24 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
165165
return errors.New("invalid target ", ob.Target)
166166
}
167167

168+
var newCtx context.Context
169+
var newCancel context.CancelFunc
168170
if session.TimeoutOnlyFromContext(ctx) {
169-
ctx = context.WithoutCancel(ctx)
171+
newCtx, newCancel = context.WithCancel(context.Background())
170172
}
171173

172174
sessionPolicy := h.policyManager.ForLevel(0)
173175
ctx, cancel := context.WithCancel(ctx)
174-
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
176+
timer := signal.CancelAfterInactivity(ctx, func() {
177+
cancel()
178+
if newCancel != nil {
179+
newCancel()
180+
}
181+
}, sessionPolicy.Timeouts.ConnectionIdle)
182+
183+
if newCtx != nil {
184+
ctx = newCtx
185+
}
175186

176187
var reader buf.Reader
177188
var writer buf.Writer

0 commit comments

Comments
 (0)