From a48fad0034e56468e1cbdfca081a9cf5fc5db607 Mon Sep 17 00:00:00 2001 From: rPDmYQ <195319688+rPDmYQ@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:06:04 +0800 Subject: [PATCH 1/2] Socks inbound: Handle immediately closing connection gracefully --- proxy/socks/server.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 34c42ae994d0..a12e676780dd 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -2,6 +2,7 @@ package socks import ( "context" + goerrors "errors" "io" "time" @@ -78,7 +79,14 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con switch network { case net.Network_TCP: firstbyte := make([]byte, 1) - conn.Read(firstbyte) + n, err := conn.Read(firstbyte) + if n == 0 && goerrors.Is(err, io.EOF) { + errors.LogDebug(ctx, "Connection closed immediately, likely health check connection") + return nil + } + if n != 1 { + return errors.New("failed to read from connection").Base(err) + } if firstbyte[0] != 5 && firstbyte[0] != 4 { // Check if it is Socks5/4/4a errors.LogDebug(ctx, "Not Socks request, try to parse as HTTP request") return s.httpServer.ProcessWithFirstbyte(ctx, network, conn, dispatcher, firstbyte...) From 121e58bf0ff2c8798bcc1942cd98bf1909938b8a Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Fri, 17 Jan 2025 13:17:17 +0000 Subject: [PATCH 2/2] Update server.go --- proxy/socks/server.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/proxy/socks/server.go b/proxy/socks/server.go index a12e676780dd..472b23a066e0 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -79,12 +79,11 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con switch network { case net.Network_TCP: firstbyte := make([]byte, 1) - n, err := conn.Read(firstbyte) - if n == 0 && goerrors.Is(err, io.EOF) { - errors.LogDebug(ctx, "Connection closed immediately, likely health check connection") - return nil - } - if n != 1 { + if n, err := conn.Read(firstbyte); n == 0 { + if goerrors.Is(err, io.EOF) { + errors.LogInfo(ctx, "Connection closed immediately, likely health check connection") + return nil + } return errors.New("failed to read from connection").Base(err) } if firstbyte[0] != 5 && firstbyte[0] != 4 { // Check if it is Socks5/4/4a