diff --git a/redir_iptables.go b/redir_iptables.go index 14b15e4..ccf641c 100644 --- a/redir_iptables.go +++ b/redir_iptables.go @@ -16,8 +16,6 @@ const ( ) func getOriginDst(c net.Conn) (net.Addr, error) { - var sockaddr syscall.RawSockaddrAny - var len = unsafe.Sizeof(sockaddr) cc, ok := c.(*net.TCPConn) if !ok { @@ -31,13 +29,11 @@ func getOriginDst(c net.Conn) (net.Addr, error) { defer f.Close() - // ipv4 - level := syscall.SOL_IP - remoteIP := c.RemoteAddr().(*net.TCPAddr).IP if remoteIP.To4() == nil { // ipv6 - level = syscall.SOL_IPV6 + // not supported, just return local socket address + return c.LocalAddr(), nil } // get original ip destination, in C like this @@ -47,31 +43,21 @@ func getOriginDst(c net.Conn) (net.Addr, error) { // int len = sizeof(addr); // getsocketopt(fd, SOL_IP, SO_ORIGINAL_DST, &addr, &len); // - _, _, errno := syscall.Syscall6(sysGetSockOpt, f.Fd(), - uintptr(level), uintptr(soOriginalDst), - uintptr(unsafe.Pointer(&sockaddr)), - uintptr(unsafe.Pointer(&len)), 0) + //_, _, errno := syscall.Syscall6(sysGetSockOpt, f.Fd(), + // uintptr(level), uintptr(soOriginalDst), + // uintptr(unsafe.Pointer(&sockaddr)), + // uintptr(unsafe.Pointer(&len)), 0) + maddr, err := syscall.GetsockoptIPv6Mreq(int(f.Fd()), syscall.SOL_IP, soOriginalDst) - if errno != 0 { - return nil, fmt.Errorf("syscall error %d", errno) + if err != nil { + return nil, err } var port uint16 var ip net.IP - - switch sockaddr.Addr.Family { - case syscall.AF_INET: - a := (*syscall.RawSockaddrInet4)(unsafe.Pointer(&sockaddr)) - ip = net.IP(a.Addr[0:]) - port = ntohs(a.Port) - case syscall.AF_INET6: - a := (*syscall.RawSockaddrInet6)(unsafe.Pointer(&sockaddr)) - ip = net.IP(a.Addr[0:]) - port = ntohs(a.Port) - default: - return nil, fmt.Errorf("unknown socket family: %d", - sockaddr.Addr.Family) - } + rawaddr := (*syscall.RawSockaddrInet4)(unsafe.Pointer(&maddr.Multiaddr)) + ip = net.IP(rawaddr.Addr[0:]) + port = ntohs(rawaddr.Port) addr := &net.TCPAddr{IP: ip, Port: int(port)} return addr, nil diff --git a/redir_linux_386.go b/redir_linux_386.go deleted file mode 100644 index 8b5a394..0000000 --- a/redir_linux_386.go +++ /dev/null @@ -1,5 +0,0 @@ -package obfssh - -// getsockopt syscall number -// refer to $GOROOT/src/syscall/syscall_linux_386.go -const sysGetSockOpt = 15 diff --git a/redir_vars.go b/redir_vars.go deleted file mode 100644 index dc7463f..0000000 --- a/redir_vars.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build linux, !386 -// +build linux - -package obfssh - -import ( - "syscall" -) - -// getsockopt syscall number -const sysGetSockOpt = syscall.SYS_GETSOCKOPT