transocks/original_dst_linux.go

79 lines
2.0 KiB
Go
Raw Normal View History

2024-11-01 22:43:08 +07:00
//go:build linux
2016-03-04 07:54:59 +06:00
// +build linux
package transocks
import (
"net"
"os"
"unsafe"
2024-11-01 22:43:08 +07:00
syscall "golang.org/x/sys/unix"
2016-03-04 07:54:59 +06:00
)
func getsockopt(s int, level int, optname int, optval unsafe.Pointer, optlen *uint32) (err error) {
_, _, e := syscall.Syscall6(
syscall.SYS_GETSOCKOPT, uintptr(s), uintptr(level), uintptr(optname),
uintptr(optval), uintptr(unsafe.Pointer(optlen)), 0)
if e != 0 {
return e
}
return
}
// GetOriginalDST retrieves the original destination address from
// NATed connection. Currently, only Linux iptables using DNAT/REDIRECT
// is supported. For other operating systems, this will just return
// conn.LocalAddr().
//
// Note that this function only works when nf_conntrack_ipv4 and/or
// nf_conntrack_ipv6 is loaded in the kernel.
func GetOriginalDST(conn *net.TCPConn) (*net.TCPAddr, error) {
f, err := conn.File()
if err != nil {
return nil, err
}
2024-11-01 22:43:08 +07:00
defer func() { _ = f.Close() }()
2016-03-04 07:54:59 +06:00
fd := int(f.Fd())
// revert to non-blocking mode.
// see http://stackoverflow.com/a/28968431/1493661
if err = syscall.SetNonblock(fd, true); err != nil {
return nil, os.NewSyscallError("setnonblock", err)
}
v6 := conn.LocalAddr().(*net.TCPAddr).IP.To4() == nil
if v6 {
var addr syscall.RawSockaddrInet6
2024-11-01 22:43:08 +07:00
len := uint32(unsafe.Sizeof(addr))
2016-03-04 07:54:59 +06:00
err = getsockopt(fd, syscall.IPPROTO_IPV6, IP6T_SO_ORIGINAL_DST,
unsafe.Pointer(&addr), &len)
if err != nil {
return nil, os.NewSyscallError("getsockopt", err)
}
ip := make([]byte, 16)
2024-11-01 22:43:08 +07:00
copy(ip, addr.Addr[:])
2016-03-04 07:54:59 +06:00
pb := *(*[2]byte)(unsafe.Pointer(&addr.Port))
return &net.TCPAddr{
IP: ip,
Port: int(pb[0])*256 + int(pb[1]),
}, nil
}
// IPv4
var addr syscall.RawSockaddrInet4
2024-11-01 22:43:08 +07:00
len := uint32(unsafe.Sizeof(addr))
2016-03-04 07:54:59 +06:00
err = getsockopt(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST,
unsafe.Pointer(&addr), &len)
if err != nil {
return nil, os.NewSyscallError("getsockopt", err)
}
ip := make([]byte, 4)
2024-11-01 22:43:08 +07:00
copy(ip, addr.Addr[:])
2016-03-04 07:54:59 +06:00
pb := *(*[2]byte)(unsafe.Pointer(&addr.Port))
return &net.TCPAddr{
IP: ip,
Port: int(pb[0])*256 + int(pb[1]),
}, nil
}