From f1eb5e3d081a37bcfcb49adf223a37122e13ebab Mon Sep 17 00:00:00 2001 From: RPRX <63339210+rprx@users.noreply.github.com> Date: Wed, 9 Dec 2020 12:16:38 +0000 Subject: [PATCH] Refactor: Support TCP/IPv6 in REDIRECT mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 十分感谢 @badO1a5A90 @LGA1150 协助测试 https://github.com/XTLS/Xray-core/issues/48#issuecomment-741509789 https://github.com/v2ray/v2ray-core/issues/1309#issuecomment-447432696 --- transport/internet/tcp/sockopt_linux.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/transport/internet/tcp/sockopt_linux.go b/transport/internet/tcp/sockopt_linux.go index d954a5cb..ebe53123 100644 --- a/transport/internet/tcp/sockopt_linux.go +++ b/transport/internet/tcp/sockopt_linux.go @@ -1,10 +1,10 @@ // +build linux -// +build !confonly package tcp import ( "syscall" + "unsafe" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/transport/internet" @@ -23,14 +23,21 @@ func GetOriginalDestination(conn internet.Connection) (net.Destination, error) { } var dest net.Destination err = rawConn.Control(func(fd uintptr) { - addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST) + level := syscall.IPPROTO_IP + if conn.RemoteAddr().String()[0] == '[' { + level = syscall.IPPROTO_IPV6 + } + addr, err := syscall.GetsockoptIPv6MTUInfo(int(fd), level, 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)) + 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)) }) if err != nil { return net.Destination{}, newError("failed to control connection").Base(err)