2016-06-11 19:35:40 -04:00
|
|
|
// +build linux
|
2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
2016-06-11 19:35:40 -04:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
package tcp
|
2016-06-11 19:35:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
2016-06-11 19:35:40 -04:00
|
|
|
)
|
|
|
|
|
2020-12-03 03:14:34 -05:00
|
|
|
const SO_ORIGINAL_DST = 80 // nolint: golint,stylecheck
|
2016-06-11 19:35:40 -04:00
|
|
|
|
2017-08-29 08:32:54 -04:00
|
|
|
func GetOriginalDestination(conn internet.Connection) (net.Destination, error) {
|
2017-08-25 17:02:54 -04:00
|
|
|
sysrawconn, f := conn.(syscall.Conn)
|
|
|
|
if !f {
|
2017-08-29 08:32:54 -04:00
|
|
|
return net.Destination{}, newError("unable to get syscall.Conn")
|
2017-08-25 17:02:54 -04:00
|
|
|
}
|
|
|
|
rawConn, err := sysrawconn.SyscallConn()
|
2016-06-11 19:35:40 -04:00
|
|
|
if err != nil {
|
2017-08-29 08:32:54 -04:00
|
|
|
return net.Destination{}, newError("failed to get sys fd").Base(err)
|
2016-06-11 19:35:40 -04:00
|
|
|
}
|
2017-08-29 08:32:54 -04:00
|
|
|
var dest net.Destination
|
2017-08-25 17:17:18 -04:00
|
|
|
err = rawConn.Control(func(fd uintptr) {
|
2021-03-25 04:42:58 -04:00
|
|
|
var remoteIP net.IP
|
|
|
|
switch addr := conn.RemoteAddr().(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
remoteIP = addr.IP
|
|
|
|
case *net.UDPAddr:
|
|
|
|
remoteIP = addr.IP
|
|
|
|
default:
|
|
|
|
newError("failed to call getsockopt").WriteToLog()
|
2017-08-25 17:46:07 -04:00
|
|
|
return
|
2017-08-25 17:02:54 -04:00
|
|
|
}
|
2021-03-25 04:42:58 -04:00
|
|
|
if remoteIP.To4() != nil {
|
|
|
|
// ipv4
|
|
|
|
addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to call getsockopt").Base(err).WriteToLog()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ip := net.IPAddress(addr.Multiaddr[4:8])
|
|
|
|
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
|
|
|
|
dest = net.TCPDestination(ip, net.Port(port))
|
|
|
|
} else {
|
|
|
|
// ipv6
|
|
|
|
addr, err := syscall.GetsockoptIPv6MTUInfo(int(fd), syscall.IPPROTO_IPV6, SO_ORIGINAL_DST)
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to call getsockopt").Base(err).WriteToLog()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ip := net.IPAddress(addr.Addr.Addr[:])
|
|
|
|
port := net.PortFromBytes([]byte{byte(addr.Addr.Port), byte(addr.Addr.Port >> 8)})
|
|
|
|
dest = net.TCPDestination(ip, port)
|
|
|
|
}
|
2017-08-25 17:02:54 -04:00
|
|
|
})
|
2016-06-11 19:35:40 -04:00
|
|
|
if err != nil {
|
2017-08-29 08:32:54 -04:00
|
|
|
return net.Destination{}, newError("failed to control connection").Base(err)
|
2017-08-25 17:46:07 -04:00
|
|
|
}
|
|
|
|
if !dest.IsValid() {
|
2017-08-29 08:32:54 -04:00
|
|
|
return net.Destination{}, newError("failed to call getsockopt")
|
2016-06-11 19:35:40 -04:00
|
|
|
}
|
2017-08-25 17:02:54 -04:00
|
|
|
return dest, nil
|
2016-06-11 19:35:40 -04:00
|
|
|
}
|