2016-06-11 23:35:40 +00:00
|
|
|
// +build linux
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
package tcp
|
2016-06-11 23:35:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
|
2017-08-29 12:32:54 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
2016-06-11 23:35:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SO_ORIGINAL_DST = 80
|
|
|
|
|
2017-08-29 12:32:54 +00:00
|
|
|
func GetOriginalDestination(conn internet.Connection) (net.Destination, error) {
|
2017-08-25 21:02:54 +00:00
|
|
|
sysrawconn, f := conn.(syscall.Conn)
|
|
|
|
if !f {
|
2017-08-29 12:32:54 +00:00
|
|
|
return net.Destination{}, newError("unable to get syscall.Conn")
|
2017-08-25 21:02:54 +00:00
|
|
|
}
|
|
|
|
rawConn, err := sysrawconn.SyscallConn()
|
2016-06-11 23:35:40 +00:00
|
|
|
if err != nil {
|
2017-08-29 12:32:54 +00:00
|
|
|
return net.Destination{}, newError("failed to get sys fd").Base(err)
|
2016-06-11 23:35:40 +00:00
|
|
|
}
|
2017-08-29 12:32:54 +00:00
|
|
|
var dest net.Destination
|
2017-08-25 21:17:18 +00:00
|
|
|
err = rawConn.Control(func(fd uintptr) {
|
2017-08-25 21:02:54 +00:00
|
|
|
addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
|
|
|
|
if err != nil {
|
2017-12-19 20:28:12 +00:00
|
|
|
newError("failed to call getsockopt").Base(err).WriteToLog()
|
2017-08-25 21:46:07 +00:00
|
|
|
return
|
2017-08-25 21:02:54 +00:00
|
|
|
}
|
2017-08-29 12:32:54 +00:00
|
|
|
ip := net.IPAddress(addr.Multiaddr[4:8])
|
2017-08-25 21:02:54 +00:00
|
|
|
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
|
2017-08-29 12:32:54 +00:00
|
|
|
dest = net.TCPDestination(ip, net.Port(port))
|
2017-08-25 21:02:54 +00:00
|
|
|
})
|
2016-06-11 23:35:40 +00:00
|
|
|
if err != nil {
|
2017-08-29 12:32:54 +00:00
|
|
|
return net.Destination{}, newError("failed to control connection").Base(err)
|
2017-08-25 21:46:07 +00:00
|
|
|
}
|
|
|
|
if !dest.IsValid() {
|
2017-08-29 12:32:54 +00:00
|
|
|
return net.Destination{}, newError("failed to call getsockopt")
|
2016-06-11 23:35:40 +00:00
|
|
|
}
|
2017-08-25 21:02:54 +00:00
|
|
|
return dest, nil
|
2016-06-11 23:35:40 +00:00
|
|
|
}
|