remove echo & redirect

This commit is contained in:
Jason
2019-08-13 15:27:17 +08:00
parent 7abb5fbe34
commit 02a800d93e
6 changed files with 0 additions and 245 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}