From f30526a60fb94970c5afa27a854a3b098bcead0f Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Sat, 4 Jul 2026 01:20:27 +0800 Subject: [PATCH] more outbounds --- 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, 19 insertions(+), 118 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 9cdce38a3..4b94af5a0 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -388,20 +388,13 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } plcy := h.policy() ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, plcy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle) requestDone := func() error { defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly) @@ -462,10 +455,6 @@ 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 0e50edba3..540286e17 100644 --- a/proxy/http/client.go +++ b/proxy/http/client.go @@ -127,19 +127,12 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, p.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle) requestFunc := func() error { defer timer.SetTimeout(p.Timeouts.DownlinkOnly) @@ -151,10 +144,6 @@ 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 5b602c342..8d5634bc1 100644 --- a/proxy/hysteria/client.go +++ b/proxy/hysteria/client.go @@ -67,24 +67,13 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } sessionPolicy := c.policyManager.ForLevel(0) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, sessionPolicy.Timeouts.ConnectionIdle) - - if newCtx != nil { - ctx = newCtx - } + timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) if target.Network == net.Network_TCP { requestDone := func() error { diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index 29cb04566..075b56824 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -95,24 +95,13 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } sessionPolicy := c.policyManager.ForLevel(user.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, sessionPolicy.Timeouts.ConnectionIdle) - - if newCtx != nil { - ctx = newCtx - } + timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) if request.Command == protocol.RequestCommandTCP { requestDone := func() error { diff --git a/proxy/shadowsocks_2022/outbound.go b/proxy/shadowsocks_2022/outbound.go index f1195950d..dd4efce55 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.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } if network == net.Network_TCP { diff --git a/proxy/socks/client.go b/proxy/socks/client.go index eac3f35b8..e74776508 100644 --- a/proxy/socks/client.go +++ b/proxy/socks/client.go @@ -117,19 +117,12 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, p.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle) var requestFunc func() error var responseFunc func() error @@ -162,10 +155,6 @@ 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 4af8c019d..0cd450ad5 100644 --- a/proxy/trojan/client.go +++ b/proxy/trojan/client.go @@ -81,20 +81,13 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } sessionPolicy := c.policyManager.ForLevel(user.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) postRequest := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) @@ -150,10 +143,6 @@ 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 ac087c637..3103b2eb6 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -294,20 +294,13 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } sessionPolicy := h.policyManager.ForLevel(request.User.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) clientReader := link.Reader // .(*pipe.Reader) clientWriter := link.Writer // .(*pipe.Writer) @@ -413,10 +406,6 @@ 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 903e73ce3..65ec65ac6 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -131,22 +131,15 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } session := encoding.NewClientSession(ctx, int64(behaviorSeed)) sessionPolicy := h.policyManager.ForLevel(request.User.Level) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, sessionPolicy.Timeouts.ConnectionIdle) + timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) if request.Command == protocol.RequestCommandUDP && h.cone && request.Port != 53 && request.Port != 443 { request.Command = protocol.RequestCommandMux @@ -212,10 +205,6 @@ 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 4491886ab..efcb4a9df 100644 --- a/proxy/wireguard/client.go +++ b/proxy/wireguard/client.go @@ -165,24 +165,13 @@ 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) { - newCtx, newCancel = context.WithCancel(context.Background()) + ctx = context.WithoutCancel(ctx) } sessionPolicy := h.policyManager.ForLevel(0) ctx, cancel := context.WithCancel(ctx) - timer := signal.CancelAfterInactivity(ctx, func() { - cancel() - if newCancel != nil { - newCancel() - } - }, sessionPolicy.Timeouts.ConnectionIdle) - - if newCtx != nil { - ctx = newCtx - } + timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) var reader buf.Reader var writer buf.Writer