diff --git a/tunnel/tcp.go b/tunnel/tcp.go index a1f7aff..e0c03b1 100755 --- a/tunnel/tcp.go +++ b/tunnel/tcp.go @@ -3,7 +3,6 @@ package tunnel import ( "io" "net" - "strconv" "sync" "time" @@ -45,10 +44,7 @@ func handleTCP(localConn core.TCPConn) { metadata.MidIP = dialerAddr.IP metadata.MidPort = uint16(dialerAddr.Port) } else { /* fallback */ - ip, p, _ := net.SplitHostPort(targetConn.LocalAddr().String()) - port, _ := strconv.ParseUint(p, 10, 16) - metadata.MidIP = net.ParseIP(ip) - metadata.MidPort = uint16(port) + metadata.MidIP, metadata.MidPort = parseAddr(targetConn.LocalAddr().String()) } targetConn = newTCPTracker(targetConn, metadata) diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index cb3e389..1402066 100755 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -39,13 +39,6 @@ func AddPacket(packet core.UDPPacket) { } } -func max(a, b int) int { - if a > b { - return a - } - return b -} - func process() { for i := 0; i < _numUDPWorkers; i++ { queue := _udpQueue diff --git a/tunnel/udp.go b/tunnel/udp.go index f9bac4d..03b3e76 100755 --- a/tunnel/udp.go +++ b/tunnel/udp.go @@ -4,7 +4,6 @@ import ( "errors" "net" "os" - "strconv" "time" "github.com/xjasonlyu/tun2socks/common/pool" @@ -88,10 +87,7 @@ func handleUDP(packet core.UDPPacket) { metadata.MidIP = dialerAddr.IP metadata.MidPort = uint16(dialerAddr.Port) } else { /* fallback */ - ip, p, _ := net.SplitHostPort(pc.LocalAddr().String()) - port, _ := strconv.ParseUint(p, 10, 16) - metadata.MidIP = net.ParseIP(ip) - metadata.MidPort = uint16(port) + metadata.MidIP, metadata.MidPort = parseAddr(pc.LocalAddr().String()) } pc = newUDPTracker(pc, metadata) diff --git a/tunnel/util.go b/tunnel/util.go new file mode 100644 index 0000000..c7b7f7c --- /dev/null +++ b/tunnel/util.go @@ -0,0 +1,20 @@ +package tunnel + +import ( + "net" + "strconv" +) + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +// parseAddr parses address to IP and port. +func parseAddr(addr string) (net.IP, uint16) { + host, portStr, _ := net.SplitHostPort(addr) + portInt, _ := strconv.ParseUint(portStr, 10, 16) + return net.ParseIP(host), uint16(portInt) +}