From bb932d8a1e1f9dccc929d107c21c5ea9734c3989 Mon Sep 17 00:00:00 2001 From: Vigilans Date: Fri, 31 Jul 2020 00:01:36 +0800 Subject: [PATCH 1/2] Move setting of SO_REUSEPORT out of applyInboundSocketOptions --- transport/internet/sockopt_linux.go | 8 ++------ transport/internet/system_listener.go | 13 +++++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/transport/internet/sockopt_linux.go b/transport/internet/sockopt_linux.go index 068879e8..e6718aff 100644 --- a/transport/internet/sockopt_linux.go +++ b/transport/internet/sockopt_linux.go @@ -16,11 +16,11 @@ 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 resuse_addr").Base(err).AtWarning() + 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 resuse_port").Base(err).AtWarning() + return newError("failed to set SO_REUSEPORT").Base(err).AtWarning() } var sockaddr syscall.Sockaddr @@ -107,9 +107,5 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) } } - 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/system_listener.go b/transport/internet/system_listener.go index 4c85c9ae..1d26f792 100644 --- a/transport/internet/system_listener.go +++ b/transport/internet/system_listener.go @@ -4,6 +4,7 @@ import ( "context" "syscall" + "golang.org/x/sys/unix" "v2ray.com/core/common/net" "v2ray.com/core/common/session" ) @@ -27,6 +28,10 @@ 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() + } + for _, controller := range controllers { if err := controller(network, address, fd); err != nil { newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx)) @@ -39,9 +44,7 @@ func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []co func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) { var lc net.ListenConfig - if sockopt != nil || len(dl.controllers) > 0 { - lc.Control = getControlFunc(ctx, sockopt, dl.controllers) - } + lc.Control = getControlFunc(ctx, sockopt, dl.controllers) return lc.Listen(ctx, addr.Network(), addr.String()) } @@ -49,9 +52,7 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S func (dl *DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) { var lc net.ListenConfig - if sockopt != nil || len(dl.controllers) > 0 { - lc.Control = getControlFunc(ctx, sockopt, dl.controllers) - } + lc.Control = getControlFunc(ctx, sockopt, dl.controllers) return lc.ListenPacket(ctx, addr.Network(), addr.String()) } From 39cbe4ab13103d333f78c415b0d0f35904b2d11e Mon Sep 17 00:00:00 2001 From: Vigilans Date: Fri, 31 Jul 2020 01:20:12 +0800 Subject: [PATCH 2/2] Implement `setReuseAddr` and `setReusePort` in sockopt_{os}.go --- transport/internet/sockopt_darwin.go | 8 ++++++++ transport/internet/sockopt_freebsd.go | 27 ++++++++++++++++++--------- transport/internet/sockopt_linux.go | 23 ++++++++++++++++------- transport/internet/sockopt_other.go | 8 ++++++++ transport/internet/sockopt_windows.go | 8 ++++++++ transport/internet/system_listener.go | 5 +---- 6 files changed, 59 insertions(+), 20 deletions(-) diff --git a/transport/internet/sockopt_darwin.go b/transport/internet/sockopt_darwin.go index ccac238d..e6281923 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 46634cb6..48c5eda6 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 e6718aff..9c1c8f31 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 962c981d..ac61296b 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 f47aa07a..cb6292bc 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 1d26f792..b17cbbba 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 {