diff --git a/transport/internet/sockopt_darwin.go b/transport/internet/sockopt_darwin.go index ccac238d9..e6281923d 100644 --- a/transport/internet/sockopt_darwin.go +++ b/transport/internet/sockopt_darwin.go @@ -50,3 +50,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) func bindAddr(fd uintptr, address []byte, port uint32) error { return nil } + +func setReuseAddr(fd uintptr) error { + return nil +} + +func setReusePort(fd uintptr) error { + return nil +} diff --git a/transport/internet/sockopt_freebsd.go b/transport/internet/sockopt_freebsd.go index 46634cb68..48c5eda69 100644 --- a/transport/internet/sockopt_freebsd.go +++ b/transport/internet/sockopt_freebsd.go @@ -188,15 +188,8 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) } func bindAddr(fd uintptr, ip []byte, port uint32) error { - if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil { - return newError("failed to set resuse_addr").Base(err).AtWarning() - } - - if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePortLB, 1); err != nil { - if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePort, 1); err != nil { - return newError("failed to set resuse_port").Base(err).AtWarning() - } - } + setReuseAddr(fd) + setReusePort(fd) var sockaddr syscall.Sockaddr @@ -219,3 +212,19 @@ func bindAddr(fd uintptr, ip []byte, port uint32) error { return syscall.Bind(int(fd), sockaddr) } + +func setReuseAddr(fd uintptr) error { + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil { + return newError("failed to set SO_REUSEADDR").Base(err).AtWarning() + } + return nil +} + +func setReusePort(fd uintptr) error { + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePortLB, 1); err != nil { + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePort, 1); err != nil { + return newError("failed to set SO_REUSEPORT").Base(err).AtWarning() + } + } + return nil +} diff --git a/transport/internet/sockopt_linux.go b/transport/internet/sockopt_linux.go index e6718affd..9c1c8f310 100644 --- a/transport/internet/sockopt_linux.go +++ b/transport/internet/sockopt_linux.go @@ -15,13 +15,8 @@ const ( ) func bindAddr(fd uintptr, ip []byte, port uint32) error { - if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil { - return newError("failed to set SO_REUSEADDR").Base(err).AtWarning() - } - - if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { - return newError("failed to set SO_REUSEPORT").Base(err).AtWarning() - } + setReuseAddr(fd) + setReusePort(fd) var sockaddr syscall.Sockaddr @@ -109,3 +104,17 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return nil } + +func setReuseAddr(fd uintptr) error { + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil { + return newError("failed to set SO_REUSEADDR").Base(err).AtWarning() + } + return nil +} + +func setReusePort(fd uintptr) error { + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { + return newError("failed to set SO_REUSEPORT").Base(err).AtWarning() + } + return nil +} diff --git a/transport/internet/sockopt_other.go b/transport/internet/sockopt_other.go index 962c981d8..ac61296bf 100644 --- a/transport/internet/sockopt_other.go +++ b/transport/internet/sockopt_other.go @@ -13,3 +13,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) func bindAddr(fd uintptr, ip []byte, port uint32) error { return nil } + +func setReuseAddr(fd uintptr) error { + return nil +} + +func setReusePort(fd uintptr) error { + return nil +} diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index f47aa07a7..cb6292bca 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -46,3 +46,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) func bindAddr(fd uintptr, ip []byte, port uint32) error { return nil } + +func setReuseAddr(fd uintptr) error { + return nil +} + +func setReusePort(fd uintptr) error { + return nil +} diff --git a/transport/internet/system_listener.go b/transport/internet/system_listener.go index 1d26f7924..b17cbbba5 100644 --- a/transport/internet/system_listener.go +++ b/transport/internet/system_listener.go @@ -4,7 +4,6 @@ import ( "context" "syscall" - "golang.org/x/sys/unix" "v2ray.com/core/common/net" "v2ray.com/core/common/session" ) @@ -28,9 +27,7 @@ func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []co } } - if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { - newError("failed to set SO_REUSEPORT").Base(err).AtWarning() - } + setReusePort(fd) for _, controller := range controllers { if err := controller(network, address, fd); err != nil {