Browse Source

Refactor(tunnel): using parseAddr

pull/76/head
xjasonlyu 3 years ago
parent
commit
ccc35c3c1b
  1. 6
      tunnel/tcp.go
  2. 7
      tunnel/tunnel.go
  3. 6
      tunnel/udp.go
  4. 20
      tunnel/util.go

6
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)

7
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

6
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)

20
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)
}
Loading…
Cancel
Save