diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index ec75e2042..998abbd26 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -61,6 +61,8 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in return errors.New("Dokodemo: Unable to get destination.") } ctx, cancel := context.WithCancel(ctx) + defer cancel() + timeout := time.Second * time.Duration(d.config.Timeout) if timeout == 0 { timeout = time.Minute * 2 diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 1d01ac079..da0341187 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -109,6 +109,8 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial conn.SetReusable(false) ctx, cancel := context.WithCancel(ctx) + defer cancel() + timeout := time.Second * time.Duration(v.timeout) if timeout == 0 { timeout = time.Minute * 5 @@ -136,7 +138,6 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil { log.Info("Freedom: Connection ending with ", err) - cancel() input.CloseError() output.CloseError() return err diff --git a/proxy/http/server.go b/proxy/http/server.go index 038909597..f74341ade 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -122,6 +122,8 @@ func (s *Server) handleConnect(ctx context.Context, request *http.Request, reade } ctx, cancel := context.WithCancel(ctx) + defer cancel() + timeout := time.Second * time.Duration(s.config.Timeout) if timeout == 0 { timeout = time.Minute * 2 diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index 55530e37a..089963bdd 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -91,6 +91,8 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale } ctx, cancel := context.WithCancel(ctx) + defer cancel() + timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2) if request.Command == protocol.RequestCommandTCP { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index c969e7bcc..1af0bc2c0 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -196,6 +196,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil { log.Info("Shadowsocks|Server: Connection ends with ", err) + cancel() ray.InboundInput().CloseError() ray.InboundOutput().CloseError() return err diff --git a/proxy/socks/client.go b/proxy/socks/client.go index bd90c15f4..f3fde0125 100644 --- a/proxy/socks/client.go +++ b/proxy/socks/client.go @@ -82,6 +82,8 @@ func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy. } ctx, cancel := context.WithCancel(ctx) + defer cancel() + timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2) var requestFunc func() error diff --git a/proxy/socks/server.go b/proxy/socks/server.go index a4ab03d8a..df5534442 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -111,6 +111,8 @@ func (*Server) handleUDP() error { func (v *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher dispatcher.Interface) error { ctx, cancel := context.WithCancel(ctx) + defer cancel() + timeout := time.Second * time.Duration(v.config.Timeout) if timeout == 0 { timeout = time.Minute * 2 diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 27e87a2b8..6fbb0ad31 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -194,6 +194,8 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i ctx = protocol.ContextWithUser(ctx, request.User) ctx, cancel := context.WithCancel(ctx) + defer cancel() + timer := signal.CancelAfterInactivity(ctx, cancel, userSettings.PayloadTimeout) ray, err := dispatcher.Dispatch(ctx, request.Destination()) if err != nil { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index c90fd535c..81c30a822 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -106,6 +106,8 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial session := encoding.NewClientSession(protocol.DefaultIDHash) ctx, cancel := context.WithCancel(ctx) + defer cancel() + timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2) requestDone := signal.ExecuteAsync(func() error {