diff --git a/server/proxy/tcp.go b/server/proxy/tcp.go index 0cda27a7..b7c72674 100755 --- a/server/proxy/tcp.go +++ b/server/proxy/tcp.go @@ -13,7 +13,6 @@ import ( "net/http" "path/filepath" "strconv" - "syscall" ) type TunnelModeServer struct { @@ -115,35 +114,3 @@ func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error { } return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy) } - -func HandleTrans(c *conn.Conn, s *TunnelModeServer) error { - if addr, err := getAddress(c.Conn); err != nil { - return err - } else { - return s.DealClient(c, s.task.Client, addr, nil, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy) - } -} - -const SO_ORIGINAL_DST = 80 - -func getAddress(conn net.Conn) (string, error) { - sysrawconn, f := conn.(syscall.Conn) - if !f { - return "", nil - } - rawConn, err := sysrawconn.SyscallConn() - if err != nil { - return "", nil - } - var ip string - var port uint16 - err = rawConn.Control(func(fd uintptr) { - addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST) - if err != nil { - return - } - ip = net.IP(addr.Multiaddr[4:8]).String() - port = uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3]) - }) - return ip + ":" + strconv.Itoa(int(port)), nil -} diff --git a/server/proxy/transport_linux_darwin.go b/server/proxy/transport_linux_darwin.go new file mode 100644 index 00000000..364a7b88 --- /dev/null +++ b/server/proxy/transport_linux_darwin.go @@ -0,0 +1,43 @@ +// +build !windows + +package proxy + +import ( + "github.com/cnlh/nps/lib/common" + "github.com/cnlh/nps/lib/conn" + "net" + "strconv" + "syscall" +) + +func HandleTrans(c *conn.Conn, s *TunnelModeServer) error { + if addr, err := getAddress(c.Conn); err != nil { + return err + } else { + return s.DealClient(c, s.task.Client, addr, nil, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy) + } +} + +const SO_ORIGINAL_DST = 80 + +func getAddress(conn net.Conn) (string, error) { + sysrawconn, f := conn.(syscall.Conn) + if !f { + return "", nil + } + rawConn, err := sysrawconn.SyscallConn() + if err != nil { + return "", nil + } + var ip string + var port uint16 + err = rawConn.Control(func(fd uintptr) { + addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST) + if err != nil { + return + } + ip = net.IP(addr.Multiaddr[4:8]).String() + port = uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3]) + }) + return ip + ":" + strconv.Itoa(int(port)), nil +} diff --git a/server/proxy/transport_windows.go b/server/proxy/transport_windows.go new file mode 100644 index 00000000..cf4c22da --- /dev/null +++ b/server/proxy/transport_windows.go @@ -0,0 +1,11 @@ +// +build windows + +package proxy + +import ( + "github.com/cnlh/nps/lib/conn" +) + +func HandleTrans(c *conn.Conn, s *TunnelModeServer) error { + return nil +}