2021-09-20 13:00:55 +00:00
|
|
|
//go:build linux
|
2020-11-25 11:01:53 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2024-06-29 18:32:57 +00:00
|
|
|
"context"
|
2020-11-25 11:01:53 +00:00
|
|
|
"syscall"
|
2020-12-09 12:16:38 +00:00
|
|
|
"unsafe"
|
2020-11-25 11:01:53 +00:00
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-12-15 00:28:47 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SO_ORIGINAL_DST = 80
|
|
|
|
|
2021-09-20 12:11:21 +00:00
|
|
|
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
|
2020-11-25 11:01:53 +00:00
|
|
|
sysrawconn, f := conn.(syscall.Conn)
|
|
|
|
if !f {
|
2024-06-29 18:32:57 +00:00
|
|
|
return net.Destination{}, errors.New("unable to get syscall.Conn")
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
rawConn, err := sysrawconn.SyscallConn()
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return net.Destination{}, errors.New("failed to get sys fd").Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
var dest net.Destination
|
|
|
|
err = rawConn.Control(func(fd uintptr) {
|
2020-12-09 12:16:38 +00:00
|
|
|
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 {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfoInner(context.Background(), err, "failed to call getsockopt")
|
2020-11-25 11:01:53 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-09 12:16:38 +00:00
|
|
|
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 {
|
2024-06-29 18:32:57 +00:00
|
|
|
return net.Destination{}, errors.New("failed to control connection").Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
if !dest.IsValid() {
|
2024-06-29 18:32:57 +00:00
|
|
|
return net.Destination{}, errors.New("failed to call getsockopt")
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
return dest, nil
|
|
|
|
}
|