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.
This commit is contained in:
ginuerzh
2026-06-05 21:16:16 +08:00
parent 4d4690b535
commit 5e3280d166
2 changed files with 13 additions and 2 deletions
+2 -2
View File
@@ -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)
+11
View File
@@ -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(),