Fix HTTP inbounds aborting connections after error responses

This commit is contained in:
世界
2026-11-25 21:25:12 +08:00
parent 46acf61419
commit 5ac9b57f2c
4 changed files with 23 additions and 9 deletions
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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=
+4 -1
View File
@@ -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))
}
}
+16 -5
View File
@@ -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) {