forked from andybalholm/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransparent_linux.go
49 lines (39 loc) · 973 Bytes
/
transparent_linux.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
45
46
47
48
49
// +build !go1.9
package main
import (
"errors"
"net"
"reflect"
"syscall"
"unsafe"
)
type sockaddr struct {
family uint16
data [14]byte
}
const SO_ORIGINAL_DST = 80
// realServerAddress returns an intercepted connection's original destination.
func realServerAddress(conn net.Conn) (net.Addr, error) {
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
return nil, errors.New("not a TCPConn")
}
TCPConn := reflect.ValueOf(tcpConn).Elem()
netFD := TCPConn.FieldByName("fd").Elem()
fd := netFD.FieldByName("sysfd").Int()
var addr sockaddr
size := uint32(unsafe.Sizeof(addr))
err := getsockopt(int(fd), syscall.SOL_IP, SO_ORIGINAL_DST, unsafe.Pointer(&addr), &size)
if err != nil {
return nil, err
}
var ip net.IP
switch addr.family {
case syscall.AF_INET:
ip = addr.data[2:6]
default:
return nil, errors.New("unrecognized address family")
}
port := int(addr.data[0])<<8 + int(addr.data[1])
return &net.TCPAddr{IP: ip, Port: port}, nil
}