mirror of https://github.com/v2ray/v2ray-core
Implement `setReuseAddr` and `setReusePort` in sockopt_{os}.go
parent
bb932d8a1e
commit
39cbe4ab13
|
@ -50,3 +50,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
||||||
func bindAddr(fd uintptr, address []byte, port uint32) error {
|
func bindAddr(fd uintptr, address []byte, port uint32) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setReuseAddr(fd uintptr) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setReusePort(fd uintptr) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -188,15 +188,8 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
||||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
|
setReuseAddr(fd)
|
||||||
return newError("failed to set resuse_addr").Base(err).AtWarning()
|
setReusePort(fd)
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var sockaddr syscall.Sockaddr
|
var sockaddr syscall.Sockaddr
|
||||||
|
|
||||||
|
@ -219,3 +212,19 @@ func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
||||||
|
|
||||||
return syscall.Bind(int(fd), sockaddr)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -15,13 +15,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
||||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
|
setReuseAddr(fd)
|
||||||
return newError("failed to set SO_REUSEADDR").Base(err).AtWarning()
|
setReusePort(fd)
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
var sockaddr syscall.Sockaddr
|
var sockaddr syscall.Sockaddr
|
||||||
|
|
||||||
|
@ -109,3 +104,17 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -13,3 +13,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
||||||
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setReuseAddr(fd uintptr) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setReusePort(fd uintptr) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -46,3 +46,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
||||||
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setReuseAddr(fd uintptr) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setReusePort(fd uintptr) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
"v2ray.com/core/common/session"
|
"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 {
|
setReusePort(fd)
|
||||||
newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, controller := range controllers {
|
for _, controller := range controllers {
|
||||||
if err := controller(network, address, fd); err != nil {
|
if err := controller(network, address, fd); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue