Xray-core/transport/internet/tcp/sockopt_linux.go

51 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-09-20 13:00:55 +00:00
//go:build linux
2020-11-25 11:01:53 +00:00
// +build linux
package tcp
import (
"syscall"
"unsafe"
2020-11-25 11:01:53 +00:00
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet/stat"
2020-11-25 11:01:53 +00:00
)
const SO_ORIGINAL_DST = 80
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
2020-11-25 11:01:53 +00:00
sysrawconn, f := conn.(syscall.Conn)
if !f {
return net.Destination{}, newError("unable to get syscall.Conn")
}
rawConn, err := sysrawconn.SyscallConn()
if err != nil {
return net.Destination{}, newError("failed to get sys fd").Base(err)
}
var dest net.Destination
err = rawConn.Control(func(fd uintptr) {
level := syscall.IPPROTO_IP
if conn.RemoteAddr().String()[0] == '[' {
level = syscall.IPPROTO_IPV6
}
addr, err := syscall.GetsockoptIPv6MTUInfo(int(fd), level, SO_ORIGINAL_DST)
2020-11-25 11:01:53 +00:00
if err != nil {
newError("failed to call getsockopt").Base(err).WriteToLog()
return
}
ip := (*[4]byte)(unsafe.Pointer(&addr.Addr.Flowinfo))[:4]
if level == syscall.IPPROTO_IPV6 {
ip = addr.Addr.Addr[:]
}
port := (*[2]byte)(unsafe.Pointer(&addr.Addr.Port))[:2]
dest = net.TCPDestination(net.IPAddress(ip), net.PortFromBytes(port))
2020-11-25 11:01:53 +00:00
})
if err != nil {
return net.Destination{}, newError("failed to control connection").Base(err)
}
if !dest.IsValid() {
return net.Destination{}, newError("failed to call getsockopt")
}
return dest, nil
}