From fcd55808759ccaf89422c51673cff206e3dd4d01 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 22 Dec 2022 08:45:11 +0800 Subject: [PATCH] chore: linux ipv6 REDIRECT (#311) --- listener/redir/tcp_linux.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/listener/redir/tcp_linux.go b/listener/redir/tcp_linux.go index c4a47d8e..949ca717 100644 --- a/listener/redir/tcp_linux.go +++ b/listener/redir/tcp_linux.go @@ -29,6 +29,9 @@ func parserPacket(conn net.Conn) (socks5.Addr, error) { rc.Control(func(fd uintptr) { addr, err = getorigdst(fd) + if err != nil { + addr, err = getorigdst6(fd) + } }) return addr, err @@ -49,3 +52,17 @@ func getorigdst(fd uintptr) (socks5.Addr, error) { addr[1+net.IPv4len], addr[1+net.IPv4len+1] = port[0], port[1] return addr, nil } + +func getorigdst6(fd uintptr) (socks5.Addr, error) { + raw := syscall.RawSockaddrInet6{} + siz := uint32(unsafe.Sizeof(raw)) + if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IPV6, IP6T_SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil { + return nil, err + } + addr := make([]byte, 1+net.IPv6len+2) + addr[0] = socks5.AtypIPv6 + copy(addr[1:1+net.IPv6len], raw.Addr[:]) + port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // big-endian + addr[1+net.IPv6len], addr[1+net.IPv6len+1] = port[0], port[1] + return addr, nil +}