chore: linux ipv6 REDIRECT (#311)

This commit is contained in:
embeddedlove 2022-12-22 08:45:11 +08:00 committed by GitHub
parent 22414ce399
commit 63922f86a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,6 +29,9 @@ func parserPacket(conn net.Conn) (socks5.Addr, error) {
rc.Control(func(fd uintptr) { rc.Control(func(fd uintptr) {
addr, err = getorigdst(fd) addr, err = getorigdst(fd)
if err != nil {
addr, err = getorigdst6(fd)
}
}) })
return addr, err 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] addr[1+net.IPv4len], addr[1+net.IPv4len+1] = port[0], port[1]
return addr, nil 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
}