From 93ca624bb504b4b3359666d8f7f9443ecca2fc25 Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Wed, 8 Jul 2026 22:44:25 +0800 Subject: [PATCH] Revert "more outbounds" This reverts commit f30526a60fb94970c5afa27a854a3b098bcead0f. --- proxy/freedom/freedom.go | 15 +++++++++++++-- proxy/http/client.go | 15 +++++++++++++-- proxy/hysteria/client.go | 15 +++++++++++++-- proxy/shadowsocks/client.go | 15 +++++++++++++-- proxy/shadowsocks_2022/outbound.go | 2 +- proxy/socks/client.go | 15 +++++++++++++-- proxy/trojan/client.go | 15 +++++++++++++-- proxy/vless/outbound/outbound.go | 15 +++++++++++++-- proxy/vmess/outbound/outbound.go | 15 +++++++++++++-- proxy/wireguard/client.go | 15 +++++++++++++-- 10 files changed, 118 insertions(+), 19 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 4b94af5a0..9cdce38a3 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -388,13 +388,20 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte defer conn.Close() errors.LogInfo(ctx, "connection opened to ", destination, ", local endpoint ", conn.LocalAddr(), ", remote endpoint ", conn.RemoteAddr()) + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } plcy := h.policy() ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, plcy.Timeouts.ConnectionIdle) requestDone := func() error { defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly) @@ -455,6 +462,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte return nil } + if newCtx != nil { + ctx = newCtx + } + if err := task.Run(ctx, requestDone, task.OnSuccess(responseDone, task.Close(output))); err != nil { return errors.New("connection ends").Base(err) } diff --git a/proxy/http/client.go b/proxy/http/client.go index 540286e17..0e50edba3 100644 --- a/proxy/http/client.go +++ b/proxy/http/client.go @@ -127,12 +127,19 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter p = c.policyManager.ForLevel(user.Level) } + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, p.Timeouts.ConnectionIdle) requestFunc := func() error { defer timer.SetTimeout(p.Timeouts.DownlinkOnly) @@ -144,6 +151,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter return buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer)) } + if newCtx != nil { + ctx = newCtx + } + responseDonePost := task.OnSuccess(responseFunc, task.Close(link.Writer)) if err := task.Run(ctx, requestFunc, responseDonePost); err != nil { return errors.New("connection ends").Base(err) diff --git a/proxy/hysteria/client.go b/proxy/hysteria/client.go index 8d5634bc1..5b602c342 100644 --- a/proxy/hysteria/client.go +++ b/proxy/hysteria/client.go @@ -67,13 +67,24 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter defer conn.Close() errors.LogInfo(ctx, "tunneling request to ", target, " via ", target.Network, ":", c.server.Destination.NetAddr()) + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } sessionPolicy := c.policyManager.ForLevel(0) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, sessionPolicy.Timeouts.ConnectionIdle) + + if newCtx != nil { + ctx = newCtx + } if target.Network == net.Network_TCP { requestDone := func() error { diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index 075b56824..29cb04566 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -95,13 +95,24 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter } request.User = user + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } sessionPolicy := c.policyManager.ForLevel(user.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, sessionPolicy.Timeouts.ConnectionIdle) + + if newCtx != nil { + ctx = newCtx + } if request.Command == protocol.RequestCommandTCP { requestDone := func() error { diff --git a/proxy/shadowsocks_2022/outbound.go b/proxy/shadowsocks_2022/outbound.go index dd4efce55..f1195950d 100644 --- a/proxy/shadowsocks_2022/outbound.go +++ b/proxy/shadowsocks_2022/outbound.go @@ -84,7 +84,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int } if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + ctx, _ = context.WithCancel(context.Background()) } if network == net.Network_TCP { diff --git a/proxy/socks/client.go b/proxy/socks/client.go index e74776508..eac3f35b8 100644 --- a/proxy/socks/client.go +++ b/proxy/socks/client.go @@ -117,12 +117,19 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter errors.LogInfoInner(ctx, err, "failed to clear deadline after handshake") } + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, p.Timeouts.ConnectionIdle) var requestFunc func() error var responseFunc func() error @@ -155,6 +162,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter } } + if newCtx != nil { + ctx = newCtx + } + responseDonePost := task.OnSuccess(responseFunc, task.Close(link.Writer)) if err := task.Run(ctx, requestFunc, responseDonePost); err != nil { return errors.New("connection ends").Base(err) diff --git a/proxy/trojan/client.go b/proxy/trojan/client.go index 0cd450ad5..4af8c019d 100644 --- a/proxy/trojan/client.go +++ b/proxy/trojan/client.go @@ -81,13 +81,20 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter return errors.New("user account is not valid") } + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } sessionPolicy := c.policyManager.ForLevel(user.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, sessionPolicy.Timeouts.ConnectionIdle) postRequest := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) @@ -143,6 +150,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)) } + if newCtx != nil { + ctx = newCtx + } + responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer)) if err := task.Run(ctx, postRequest, responseDoneAndCloseWriter); err != nil { return errors.New("connection ends").Base(err) diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index 3103b2eb6..ac087c637 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -294,13 +294,20 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte ob.CanSpliceCopy = 3 } + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } sessionPolicy := h.policyManager.ForLevel(request.User.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, sessionPolicy.Timeouts.ConnectionIdle) clientReader := link.Reader // .(*pipe.Reader) clientWriter := link.Writer // .(*pipe.Writer) @@ -406,6 +413,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte return nil } + if newCtx != nil { + ctx = newCtx + } + if err := task.Run(ctx, postRequest, task.OnSuccess(getResponse, task.Close(clientWriter))); err != nil { return errors.New("connection ends").Base(err).AtInfo() } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 65ec65ac6..903e73ce3 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -131,15 +131,22 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte behaviorSeed := crc64.Checksum(hashkdf.Sum(nil), crc64.MakeTable(crc64.ISO)) + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } session := encoding.NewClientSession(ctx, int64(behaviorSeed)) sessionPolicy := h.policyManager.ForLevel(request.User.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, sessionPolicy.Timeouts.ConnectionIdle) if request.Command == protocol.RequestCommandUDP && h.cone && request.Port != 53 && request.Port != 443 { request.Command = protocol.RequestCommandMux @@ -205,6 +212,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte return buf.Copy(bodyReader, output, buf.UpdateActivity(timer)) } + if newCtx != nil { + ctx = newCtx + } + responseDonePost := task.OnSuccess(responseDone, task.Close(output)) if err := task.Run(ctx, requestDone, responseDonePost); err != nil { return errors.New("connection ends").Base(err) diff --git a/proxy/wireguard/client.go b/proxy/wireguard/client.go index efcb4a9df..4491886ab 100644 --- a/proxy/wireguard/client.go +++ b/proxy/wireguard/client.go @@ -165,13 +165,24 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte return errors.New("invalid target ", ob.Target) } + var newCtx context.Context + var newCancel context.CancelFunc if session.TimeoutOnlyFromContext(ctx) { - ctx = context.WithoutCancel(ctx) + newCtx, newCancel = context.WithCancel(context.Background()) } sessionPolicy := h.policyManager.ForLevel(0) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, func() { + cancel() + if newCancel != nil { + newCancel() + } + }, sessionPolicy.Timeouts.ConnectionIdle) + + if newCtx != nil { + ctx = newCtx + } var reader buf.Reader var writer buf.Writer