diff --git a/cmd/tun2socks/main_echo.go b/cmd/tun2socks/main_echo.go deleted file mode 100644 index 946e5d5..0000000 --- a/cmd/tun2socks/main_echo.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build echo - -package main - -import ( - "github.com/xjasonlyu/tun2socks/core" - "github.com/xjasonlyu/tun2socks/proxy/echo" -) - -func init() { - registerHandlerCreator(func() { - core.RegisterTCPConnHandler(echo.NewTCPHandler()) - core.RegisterUDPConnHandler(echo.NewUDPHandler()) - }) -} diff --git a/cmd/tun2socks/main_redirect.go b/cmd/tun2socks/main_redirect.go deleted file mode 100644 index 7ce30c4..0000000 --- a/cmd/tun2socks/main_redirect.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build redirect - -package main - -import ( - "github.com/xjasonlyu/tun2socks/core" - "github.com/xjasonlyu/tun2socks/proxy/redirect" -) - -func init() { - registerHandlerCreator(func() { - core.RegisterTCPConnHandler(redirect.NewTCPHandler(*args.ProxyServer)) - core.RegisterUDPConnHandler(redirect.NewUDPHandler(*args.ProxyServer, *args.UdpTimeout)) - }) -} diff --git a/proxy/echo/tcp.go b/proxy/echo/tcp.go deleted file mode 100644 index 675d8c9..0000000 --- a/proxy/echo/tcp.go +++ /dev/null @@ -1,26 +0,0 @@ -package echo - -import ( - "io" - "net" - - "github.com/xjasonlyu/tun2socks/core" -) - -// An echo proxy, do nothing but echo back data to the sender, the handler was -// created for testing purposes, it may causes issues when more than one clients -// are connecting the handler simultaneously. -type tcpHandler struct{} - -func NewTCPHandler() core.TCPConnHandler { - return &tcpHandler{} -} - -func (h *tcpHandler) echoBack(localConn net.Conn) { - io.Copy(localConn, localConn) -} - -func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error { - go h.echoBack(localConn) - return nil -} diff --git a/proxy/echo/udp.go b/proxy/echo/udp.go deleted file mode 100644 index a7238ff..0000000 --- a/proxy/echo/udp.go +++ /dev/null @@ -1,31 +0,0 @@ -package echo - -import ( - "net" - - "github.com/xjasonlyu/tun2socks/common/log" - "github.com/xjasonlyu/tun2socks/core" -) - -// An echo server, do nothing but echo back data to the sender. -type udpHandler struct{} - -func NewUDPHandler() core.UDPConnHandler { - return &udpHandler{} -} - -func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { - return nil -} - -func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { - // Dispatch to another goroutine, otherwise will result in deadlock. - payload := append([]byte(nil), data...) - go func(b []byte) { - _, err := conn.WriteFrom(b, addr) - if err != nil { - log.Warnf("failed to echo back data: %v", err) - } - }(payload) - return nil -} diff --git a/proxy/redirect/tcp.go b/proxy/redirect/tcp.go deleted file mode 100644 index 37da54d..0000000 --- a/proxy/redirect/tcp.go +++ /dev/null @@ -1,52 +0,0 @@ -package redirect - -import ( - "net" - - "github.com/xjasonlyu/tun2socks/common/log" - "github.com/xjasonlyu/tun2socks/core" - . "github.com/xjasonlyu/tun2socks/proxy/utils" -) - -// To do a benchmark using iperf3 locally, you may follow these steps: -// -// 1. Setup and configure the TUN device and start tun2socks with the -// redirect handler using the following command: -// tun2socks -proxyType redirect -proxyServer 127.0.0.1:1234 -// Tun2socks will redirect all traffic to 127.0.0.1:1234. -// -// 2. Route traffic targeting 1.2.3.4 to the TUN interface (240.0.0.1): -// route add 1.2.3.4/32 240.0.0.1 -// -// 3. Run iperf3 server locally and listening on 1234 port: -// iperf3 -s -p 1234 -// -// 4. Run iperf3 client locally and connect to 1.2.3.4:1234: -// iperf3 -c 1.2.3.4 -p 1234 -// -// It works this way: -// iperf3 client -> 1.2.3.4:1234 -> routing table -> TUN (240.0.0.1) -> tun2socks -> tun2socks redirect anything to 127.0.0.1:1234 -> iperf3 server -// -type tcpHandler struct { - target string -} - -func NewTCPHandler(target string) core.TCPConnHandler { - return &tcpHandler{target: target} -} - -func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error { - remoteConn, err := net.Dial("tcp", h.target) - if err != nil { - return err - } - - // set keepalive - TCPKeepAlive(localConn) - TCPKeepAlive(remoteConn) - - go TCPRelay(localConn, remoteConn) - - log.Infof("new proxy connection for target: %s:%s", target.Network(), target.String()) - return nil -} diff --git a/proxy/redirect/udp.go b/proxy/redirect/udp.go deleted file mode 100644 index 0c2a57d..0000000 --- a/proxy/redirect/udp.go +++ /dev/null @@ -1,106 +0,0 @@ -package redirect - -import ( - "errors" - "fmt" - "net" - "sync" - "time" - - "github.com/xjasonlyu/tun2socks/common/log" - "github.com/xjasonlyu/tun2socks/common/pool" - "github.com/xjasonlyu/tun2socks/core" -) - -type udpHandler struct { - target string - timeout time.Duration - - remoteAddrMap sync.Map - remoteUDPConnMap sync.Map -} - -func NewUDPHandler(target string, timeout time.Duration) core.UDPConnHandler { - return &udpHandler{ - target: target, - timeout: timeout, - } -} - -func (h *udpHandler) fetchUDPInput(conn core.UDPConn, pc *net.UDPConn) { - buf := pool.BufPool.Get().([]byte) - - defer func() { - h.Close(conn) - pool.BufPool.Put(buf[:cap(buf)]) - }() - - for { - pc.SetDeadline(time.Now().Add(h.timeout)) - n, addr, err := pc.ReadFromUDP(buf) - if err != nil { - if err, ok := err.(net.Error); !ok && !err.Timeout() { - log.Warnf("failed to read UDP data from remote: %v", err) - } - return - } - - if _, err := conn.WriteFrom(buf[:n], addr); err != nil { - log.Warnf("failed to write UDP data to TUN") - return - } - } -} - -func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { - bindAddr := &net.UDPAddr{IP: nil, Port: 0} - pc, err := net.ListenUDP("udp", bindAddr) - if err != nil { - log.Errorf("failed to bind udp address") - return err - } - targetAddr, _ := net.ResolveUDPAddr("udp", h.target) - - h.remoteAddrMap.Store(conn, targetAddr) - h.remoteUDPConnMap.Store(conn, pc) - - go h.fetchUDPInput(conn, pc) - - log.Infof("new proxy connection for target: %s:%s", target.Network(), target.String()) - return nil -} - -func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { - var pc *net.UDPConn - var targetAddr *net.UDPAddr - - if value, ok := h.remoteAddrMap.Load(conn); ok { - targetAddr = value.(*net.UDPAddr) - } - - if value, ok := h.remoteUDPConnMap.Load(conn); ok { - pc = value.(*net.UDPConn) - } - - if pc != nil && targetAddr != nil { - _, err := pc.WriteToUDP(data, targetAddr) - if err != nil { - log.Warnf("failed to write UDP payload to SOCKS5 server: %v", err) - return errors.New("failed to write UDP data") - } - return nil - } else { - return errors.New(fmt.Sprintf("proxy connection %v->%v does not exists", conn.LocalAddr(), addr)) - } -} - -func (h *udpHandler) Close(conn core.UDPConn) { - conn.Close() - - if pc, ok := h.remoteUDPConnMap.Load(conn); ok { - pc.(*net.UDPConn).Close() - h.remoteUDPConnMap.Delete(conn) - } - - h.remoteAddrMap.Delete(conn) -}