Skip to content

Commit

Permalink
Refactor: Support TCP/IPv6 in REDIRECT mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX authored Dec 9, 2020
1 parent b3f3c5b commit f1eb5e3
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions transport/internet/tcp/sockopt_linux.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down

4 comments on commit f1eb5e3

@badO1a5A90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -1,10 +1,10 @@
虽然这是最后的成果.
但是中间经历的过程,尝试,学习的知识点和累积的经验....
前后一天一夜,不容易呀.

我tm直接哭了.jpg

@RPRX
Copy link
Member Author

@RPRX RPRX commented on f1eb5e3 Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,尝试了 N 个版本,这是最终浓缩的精华

@huzy0909
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢辛苦付出!!

@RPRX
Copy link
Member Author

@RPRX RPRX commented on f1eb5e3 Dec 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keyu5525

这个修改是作用在本地的,与协议走的 IPv4/IPv6 无关

Please sign in to comment.