Skip to content

Commit

Permalink
Transport
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Apr 21, 2019
1 parent f6c596f commit 2912246
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
33 changes: 0 additions & 33 deletions server/proxy/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http"
"path/filepath"
"strconv"
"syscall"
)

type TunnelModeServer struct {
Expand Down Expand Up @@ -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
}
43 changes: 43 additions & 0 deletions server/proxy/transport_linux_darwin.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions server/proxy/transport_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2912246

Please sign in to comment.