From 5ac9b57f2c568aaf41a406c83a2695b1ba8dd1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 25 Nov 2026 20:07:38 +0800 Subject: [PATCH] Fix HTTP inbounds aborting connections after error responses --- go.mod | 2 +- go.sum | 4 ++-- protocol/http/inbound.go | 5 ++++- protocol/mixed/inbound.go | 21 ++++++++++++++++----- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 96aaa2a7..cd252668 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/sagernet/netlink v0.0.0-20261116064630-4ea47d5d44b1 github.com/sagernet/nftables v0.3.0-mod.4.0.20261114074246-0b310c3e010b github.com/sagernet/quic-go v0.61.0-sing-box-mod.15 - github.com/sagernet/sing v0.9.7-0.20261124055526-8badbba80825 + github.com/sagernet/sing v0.9.7-0.20261125120655-610c8699131b github.com/sagernet/sing-cloudflared v0.1.4-0.20261108120326-83391fb7b139 github.com/sagernet/sing-mux v0.3.10-0.20261125130604-6c5b46f55eeb github.com/sagernet/sing-openconnect v0.1.1-0.20261119020037-0dad74dcc5e1 diff --git a/go.sum b/go.sum index 3f0e5669..d83531d0 100644 --- a/go.sum +++ b/go.sum @@ -318,8 +318,8 @@ github.com/sagernet/nftables v0.3.0-mod.4.0.20261114074246-0b310c3e010b h1:dgnXJ github.com/sagernet/nftables v0.3.0-mod.4.0.20261114074246-0b310c3e010b/go.mod h1:8kslHG4VvYNihcco+i6uxIX7qbT8A56T0y5q7U44ZaQ= github.com/sagernet/quic-go v0.61.0-sing-box-mod.15 h1:bBw8CvMTBPw5kwJiF8dqZRUgJaJFnDkQxRxXv78mlEM= github.com/sagernet/quic-go v0.61.0-sing-box-mod.15/go.mod h1:hmLC8GJPp+BrpBgrgJnacvI4fZGIhasuy1ROJJ8GR4E= -github.com/sagernet/sing v0.9.7-0.20261124055526-8badbba80825 h1:eASbKRvCGh3xTgl5GoZD/5zFgUP/bhhdfwqlJDpLIxs= -github.com/sagernet/sing v0.9.7-0.20261124055526-8badbba80825/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo= +github.com/sagernet/sing v0.9.7-0.20261125120655-610c8699131b h1:MRuIq4f+ode18cs5UqZLXL+4Di3WxjA/l8ehLFHf76M= +github.com/sagernet/sing v0.9.7-0.20261125120655-610c8699131b/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo= github.com/sagernet/sing-cloudflared v0.1.4-0.20261108120326-83391fb7b139 h1:jVPI9UHsgvNivQLcd4thzFnxxchuwwfiLyp5qhs42n4= github.com/sagernet/sing-cloudflared v0.1.4-0.20261108120326-83391fb7b139/go.mod h1:XEqEDYRCAYLaoPjZ1ifVWJg5iWAJHL2gOAXe/PM28Cg= github.com/sagernet/sing-mux v0.3.10-0.20261125130604-6c5b46f55eeb h1:iK0uoxfzEEDhez9zXj/Pvu4zEqAwNFzc6nAZzdaaC1Q= diff --git a/protocol/http/inbound.go b/protocol/http/inbound.go index 6e0c3ebd..898d5e1a 100644 --- a/protocol/http/inbound.go +++ b/protocol/http/inbound.go @@ -97,7 +97,10 @@ func (h *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata ada } err := http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, adapter.NewUpstreamHandler(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose) if err != nil { - N.CloseOnHandshakeFailure(conn, onClose, err) + conn.Close() + if onClose != nil { + onClose(err) + } h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source)) } } diff --git a/protocol/mixed/inbound.go b/protocol/mixed/inbound.go index 22c25f30..4810107e 100644 --- a/protocol/mixed/inbound.go +++ b/protocol/mixed/inbound.go @@ -100,7 +100,6 @@ func (h *Inbound) Start(stage adapter.StartStage, scope *adapter.Scope) error { func (h *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) { err := h.newConnection(ctx, conn, metadata, onClose) - N.CloseOnHandshakeFailure(conn, onClose, err) if err != nil { if E.IsClosedOrCanceled(err) { h.logger.DebugContext(ctx, "connection closed: ", err) @@ -114,21 +113,33 @@ func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata ada if h.tlsConfig != nil { tlsConn, err := tls.ServerHandshake(ctx, conn, h.tlsConfig) if err != nil { - return E.Cause(err, "TLS handshake") + err = E.Cause(err, "TLS handshake") + N.CloseOnHandshakeFailure(conn, onClose, err) + return err } conn = tlsConn } reader := std_bufio.NewReader(conn) headerBytes, err := reader.Peek(1) if err != nil { - return E.Cause(err, "peek first byte") + err = E.Cause(err, "peek first byte") + N.CloseOnHandshakeFailure(conn, onClose, err) + return err } switch headerBytes[0] { case socks4.Version, socks5.Version: - return socks.HandleConnectionEx(ctx, conn, reader, h.authenticator, adapter.NewUpstreamHandler(metadata, h.newUserConnection, h.streamUserPacketConnection), h.listener, h.udpTimeout, metadata.Source, onClose) + err = socks.HandleConnectionEx(ctx, conn, reader, h.authenticator, adapter.NewUpstreamHandler(metadata, h.newUserConnection, h.streamUserPacketConnection), h.listener, h.udpTimeout, metadata.Source, onClose) + N.CloseOnHandshakeFailure(conn, onClose, err) default: - return http.HandleConnectionEx(ctx, conn, reader, h.authenticator, adapter.NewUpstreamHandler(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose) + err = http.HandleConnectionEx(ctx, conn, reader, h.authenticator, adapter.NewUpstreamHandler(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose) + if err != nil { + conn.Close() + } + if onClose != nil { + onClose(err) + } } + return err } func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {