diff --git a/common/redir/tproxy_linux.go b/common/redir/tproxy_linux.go index 795e9ffe..5458ee3c 100644 --- a/common/redir/tproxy_linux.go +++ b/common/redir/tproxy_linux.go @@ -16,28 +16,20 @@ import ( ) func TProxy(fd uintptr, isIPv6 bool) error { - err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1) - if err != nil { - return err + err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1) + if err == nil { + err = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1) } - if isIPv6 { + if err == nil && isIPv6 { err = syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_TRANSPARENT, 1) } - return err -} - -func TProxyUDP(fd uintptr, isIPv6 bool) error { - err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1) - if err != nil { - return err + if err == nil { + err = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1) } - if isIPv6 { + if err == nil && isIPv6 { err = syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1) - if err != nil { - return err - } } - return nil + return err } func GetOriginalDestinationFromOOB(oob []byte) (netip.AddrPort, error) { diff --git a/common/redir/tproxy_other.go b/common/redir/tproxy_other.go index a2f767a1..12e575d8 100644 --- a/common/redir/tproxy_other.go +++ b/common/redir/tproxy_other.go @@ -12,10 +12,6 @@ func TProxy(fd uintptr, isIPv6 bool) error { return os.ErrInvalid } -func TProxyUDP(fd uintptr, isIPv6 bool) error { - return os.ErrInvalid -} - func GetOriginalDestinationFromOOB(oob []byte) (netip.AddrPort, error) { return netip.AddrPort{}, os.ErrInvalid } diff --git a/inbound/tproxy.go b/inbound/tproxy.go index 016247ce..c28cbca4 100644 --- a/inbound/tproxy.go +++ b/inbound/tproxy.go @@ -68,7 +68,7 @@ func (t *TProxy) Start() error { if err != nil { return err } - err = redir.TProxyUDP(udpFd, M.SocksaddrFromNet(t.udpConn.LocalAddr()).Addr.Is6()) + err = redir.TProxy(udpFd, M.SocksaddrFromNet(t.udpConn.LocalAddr()).Addr.Is6()) if err != nil { return E.Cause(err, "configure tproxy UDP listener") }