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-02-01 20:35:40 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2017-01-13 23:27:45 +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-01-13 23:27:45 +00:00
|
|
|
func GetOriginalDestination(conn internet.Connection) net.Destination {
|
2016-06-29 22:08:20 +00:00
|
|
|
tcpConn, ok := conn.(internet.SysFd)
|
|
|
|
if !ok {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to get sys fd"))
|
2017-01-13 23:27:45 +00:00
|
|
|
return net.Destination{}
|
2016-06-29 22:08:20 +00:00
|
|
|
}
|
2016-06-14 20:54:08 +00:00
|
|
|
fd, err := tcpConn.SysFd()
|
2016-06-11 23:35:40 +00:00
|
|
|
if err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to get original destination").Base(err))
|
2017-01-13 23:27:45 +00:00
|
|
|
return net.Destination{}
|
2016-06-11 23:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := syscall.GetsockoptIPv6Mreq(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST)
|
|
|
|
if err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to call getsockopt").Base(err))
|
2017-01-13 23:27:45 +00:00
|
|
|
return net.Destination{}
|
2016-06-11 23:35:40 +00:00
|
|
|
}
|
2017-01-13 23:27:45 +00:00
|
|
|
ip := net.IPAddress(addr.Multiaddr[4:8])
|
2016-06-11 23:35:40 +00:00
|
|
|
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
|
2017-01-13 23:27:45 +00:00
|
|
|
return net.TCPDestination(ip, net.Port(port))
|
2016-06-11 23:35:40 +00:00
|
|
|
}
|