Feature: REDIRECT support IPv6 (#2473)

This commit is contained in:
embeddedlove 2022-12-22 19:25:30 +08:00 committed by GitHub
parent 4a57917783
commit fbca37c42b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 1 deletions

View File

@ -32,7 +32,11 @@ func parserPacket(conn net.Conn) (socks5.Addr, error) {
var addr netip.AddrPort
rc.Control(func(fd uintptr) {
addr, err = getorigdst(fd)
if ip4 := c.LocalAddr().(*net.TCPAddr).IP.To4(); ip4 != nil {
addr, err = getorigdst(fd)
} else {
addr, err = getorigdst6(fd)
}
})
return socks5.AddrFromStdAddrPort(addr), err
@ -48,3 +52,13 @@ func getorigdst(fd uintptr) (netip.AddrPort, error) {
port := binary.BigEndian.Uint16((*(*[2]byte)(unsafe.Pointer(&addr.Port)))[:])
return netip.AddrPortFrom(netip.AddrFrom4(addr.Addr), port), nil
}
func getorigdst6(fd uintptr) (netip.AddrPort, error) {
addr := unix.RawSockaddrInet6{}
size := uint32(unsafe.Sizeof(addr))
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IPV6, IP6T_SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&addr)), uintptr(unsafe.Pointer(&size)), 0); err != nil {
return netip.AddrPort{}, err
}
port := binary.BigEndian.Uint16((*(*[2]byte)(unsafe.Pointer(&addr.Port)))[:])
return netip.AddrPortFrom(netip.AddrFrom16(addr.Addr), port), nil
}