From 5e3280d166d936986819c8a09babf64e1378ffc9 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Fri, 5 Jun 2026 21:16:16 +0800 Subject: [PATCH] fix(handler/socks5): accumulate UDP traffic metrics into recorder (issue #45) The SOCKS5 handler recorder only tracked TCP traffic metrics. UDP associations (CmdUdp) overwrote TCP byte counts with UDP counts, and UDP tunnels (CmdUDPTun) recorded zero bytes. Changes: - udp.go: change UDP stats assignment (=) to accumulation (+=) so the TCP control connection bytes tracked by Handle() are preserved. - udp_tun.go: add xstats.Stats{} wrapper around the PacketConn with deferred accumulation into HandlerRecorderObject, mirroring the handleUDP pattern. --- handler/socks/v5/udp.go | 4 ++-- handler/socks/v5/udp_tun.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/handler/socks/v5/udp.go b/handler/socks/v5/udp.go index fd4084a5..2f1b2d6a 100644 --- a/handler/socks/v5/udp.go +++ b/handler/socks/v5/udp.go @@ -97,8 +97,8 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, network st cc = stats_wrapper.WrapPacketConn(cc, &pStats) defer func() { - ro.InputBytes = pStats.Get(stats.KindInputBytes) - ro.OutputBytes = pStats.Get(stats.KindOutputBytes) + ro.InputBytes += pStats.Get(stats.KindInputBytes) + ro.OutputBytes += pStats.Get(stats.KindOutputBytes) }() clientID := ctxvalue.ClientIDFromContext(ctx) diff --git a/handler/socks/v5/udp_tun.go b/handler/socks/v5/udp_tun.go index 03b6398e..1666daf6 100644 --- a/handler/socks/v5/udp_tun.go +++ b/handler/socks/v5/udp_tun.go @@ -17,6 +17,7 @@ import ( "github.com/go-gost/x/internal/net/udp" "github.com/go-gost/x/internal/util/socks" traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper" + xstats "github.com/go-gost/x/observer/stats" stats_wrapper "github.com/go-gost/x/observer/stats/wrapper" xrecorder "github.com/go-gost/x/recorder" ) @@ -108,6 +109,16 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, network } defer pc.Close() + { + pStats := xstats.Stats{} + pc = stats_wrapper.WrapPacketConn(pc, &pStats) + + defer func() { + ro.InputBytes += pStats.Get(stats.KindInputBytes) + ro.OutputBytes += pStats.Get(stats.KindOutputBytes) + }() + } + log = log.WithFields(map[string]any{ "src": pc.LocalAddr().String(), "bind": pc.LocalAddr().String(),