From ad522ebb357941dad72a547ec0a4b567ddd44d0d Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 3 Apr 2023 17:43:19 +0800 Subject: [PATCH] Chore: minor renames --- tunnel/tcp.go | 28 ++++++++++++++-------------- tunnel/udp.go | 10 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/tunnel/tcp.go b/tunnel/tcp.go index 693c58b..4502804 100644 --- a/tunnel/tcp.go +++ b/tunnel/tcp.go @@ -22,10 +22,10 @@ func SetTCPWaitTimeout(t time.Duration) { _tcpWaitTimeout = t } -func handleTCPConn(localConn adapter.TCPConn) { - defer localConn.Close() +func handleTCPConn(originConn adapter.TCPConn) { + defer originConn.Close() - id := localConn.ID() + id := originConn.ID() metadata := &M.Metadata{ Network: M.TCP, SrcIP: net.IP(id.RemoteAddress), @@ -34,24 +34,24 @@ func handleTCPConn(localConn adapter.TCPConn) { DstPort: id.LocalPort, } - targetConn, err := proxy.Dial(metadata) + remoteConn, err := proxy.Dial(metadata) if err != nil { log.Warnf("[TCP] dial %s: %v", metadata.DestinationAddress(), err) return } - metadata.MidIP, metadata.MidPort = parseAddr(targetConn.LocalAddr()) + metadata.MidIP, metadata.MidPort = parseAddr(remoteConn.LocalAddr()) - targetConn = statistic.DefaultTCPTracker(targetConn, metadata) - defer targetConn.Close() + remoteConn = statistic.DefaultTCPTracker(remoteConn, metadata) + defer remoteConn.Close() log.Infof("[TCP] %s <-> %s", metadata.SourceAddress(), metadata.DestinationAddress()) - if err = relay(localConn, targetConn); err != nil { + if err = pipe(originConn, remoteConn); err != nil { log.Debugf("[TCP] %s <-> %s: %v", metadata.SourceAddress(), metadata.DestinationAddress(), err) } } -// relay copies between left and right bidirectionally. -func relay(left, right net.Conn) error { +// pipe copies copy data to & from provided net.Conn(s) bidirectionally. +func pipe(origin, remote net.Conn) error { wg := sync.WaitGroup{} wg.Add(2) @@ -59,18 +59,18 @@ func relay(left, right net.Conn) error { go func() { defer wg.Done() - if err := copyBuffer(right, left); err != nil { + if err := copyBuffer(remote, origin); err != nil { leftErr = errors.Join(leftErr, err) } - right.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) + remote.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) }() go func() { defer wg.Done() - if err := copyBuffer(left, right); err != nil { + if err := copyBuffer(origin, remote); err != nil { rightErr = errors.Join(rightErr, err) } - left.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) + origin.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) }() wg.Wait() diff --git a/tunnel/udp.go b/tunnel/udp.go index 5a3a17d..eef2142 100644 --- a/tunnel/udp.go +++ b/tunnel/udp.go @@ -54,12 +54,12 @@ func handleUDPConn(uc adapter.UDPConn) { pc = newSymmetricNATPacketConn(pc, metadata) log.Infof("[UDP] %s <-> %s", metadata.SourceAddress(), metadata.DestinationAddress()) - if err = relayPacket(uc, pc, remote); err != nil { + if err = pipePacket(uc, pc, remote); err != nil { log.Debugf("[UDP] %s <-> %s: %v", metadata.SourceAddress(), metadata.DestinationAddress(), err) } } -func relayPacket(left net.PacketConn, right net.PacketConn, to net.Addr) error { +func pipePacket(origin net.PacketConn, remote net.PacketConn, to net.Addr) error { wg := sync.WaitGroup{} wg.Add(2) @@ -67,14 +67,14 @@ func relayPacket(left net.PacketConn, right net.PacketConn, to net.Addr) error { go func() { defer wg.Done() - if err := copyPacketBuffer(right, left, to, _udpSessionTimeout); err != nil { + if err := copyPacketData(remote, origin, to, _udpSessionTimeout); err != nil { leftErr = errors.Join(leftErr, err) } }() go func() { defer wg.Done() - if err := copyPacketBuffer(left, right, nil, _udpSessionTimeout); err != nil { + if err := copyPacketData(origin, remote, nil, _udpSessionTimeout); err != nil { rightErr = errors.Join(rightErr, err) } }() @@ -83,7 +83,7 @@ func relayPacket(left net.PacketConn, right net.PacketConn, to net.Addr) error { return errors.Join(leftErr, rightErr) } -func copyPacketBuffer(dst net.PacketConn, src net.PacketConn, to net.Addr, timeout time.Duration) error { +func copyPacketData(dst net.PacketConn, src net.PacketConn, to net.Addr, timeout time.Duration) error { buf := pool.Get(pool.MaxSegmentSize) defer pool.Put(buf)