forked from ehang-io/nps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransport.go
44 lines (38 loc) · 961 Bytes
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// +build !windows
package proxy
import (
"net"
"strconv"
"syscall"
"ehang.io/nps/lib/common"
"ehang.io/nps/lib/conn"
)
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
}