From 61a9d268152caac8e9185df240ce77fd61ffb7dd Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 3 Apr 2023 19:13:03 +0800 Subject: [PATCH] Minor: UDP unidirectionalPacketStream --- tunnel/udp.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tunnel/udp.go b/tunnel/udp.go index 7b87223..22ba4e1 100644 --- a/tunnel/udp.go +++ b/tunnel/udp.go @@ -60,23 +60,19 @@ func pipePacket(origin, remote net.PacketConn, to net.Addr) { wg := sync.WaitGroup{} wg.Add(2) - go func() { - defer wg.Done() - if err := copyPacketData(remote, origin, to, _udpSessionTimeout); err != nil { - log.Debugf("[UDP] copy data for origin->remote: %v", err) - } - }() - - go func() { - defer wg.Done() - if err := copyPacketData(origin, remote, nil, _udpSessionTimeout); err != nil { - log.Debugf("[UDP] copy data for remote->origin: %v", err) - } - }() + go unidirectionalPacketStream(remote, origin, to, "origin->remote", &wg) + go unidirectionalPacketStream(origin, remote, nil, "remote->origin", &wg) wg.Wait() } +func unidirectionalPacketStream(dst, src net.PacketConn, to net.Addr, dir string, wg *sync.WaitGroup) { + defer wg.Done() + if err := copyPacketData(dst, src, to, _udpSessionTimeout); err != nil { + log.Debugf("[UDP] copy data for %s: %v", dir, err) + } +} + func copyPacketData(dst, src net.PacketConn, to net.Addr, timeout time.Duration) error { buf := pool.Get(pool.MaxSegmentSize) defer pool.Put(buf)