mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
Add TCP keep alive support in Linux
This commit is contained in:
parent
6e7e864f2f
commit
499a0e19cb
@ -59,10 +59,16 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
||||
}
|
||||
}
|
||||
|
||||
if config.TcpKeepAliveInterval != 0 {
|
||||
if config.TcpKeepAliveInterval > 0 {
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
||||
return newError("failed to set TCP_KEEPINTVL", err)
|
||||
}
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
||||
return newError("failed to set TCP_KEEPIDLE", err)
|
||||
}
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
||||
return newError("failed to set SO_KEEPALIVE", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,10 +99,16 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
|
||||
}
|
||||
}
|
||||
|
||||
if config.TcpKeepAliveInterval != 0 {
|
||||
if config.TcpKeepAliveInterval > 0 {
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
||||
return newError("failed to set TCP_KEEPINTVL", err)
|
||||
}
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
||||
return newError("failed to set TCP_KEEPIDLE", err)
|
||||
}
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
||||
return newError("failed to set SO_KEEPALIVE", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,10 +63,14 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
|
||||
dest: destAddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
goStdKeepAlive := time.Duration(0)
|
||||
if sockopt.TcpKeepAliveIdle != 0 {
|
||||
goStdKeepAlive = time.Duration(-1)
|
||||
}
|
||||
dialer := &net.Dialer{
|
||||
Timeout: time.Second * 16,
|
||||
LocalAddr: resolveSrcAddr(dest.Network, src),
|
||||
KeepAlive: goStdKeepAlive,
|
||||
}
|
||||
|
||||
if sockopt != nil || len(d.controllers) > 0 {
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/pires/go-proxyproto"
|
||||
|
||||
@ -49,6 +50,9 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
|
||||
network = addr.Network()
|
||||
address = addr.String()
|
||||
lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
|
||||
if sockopt.TcpKeepAliveIdle != 0 {
|
||||
lc.KeepAlive = time.Duration(-1)
|
||||
}
|
||||
case *net.UnixAddr:
|
||||
lc.Control = nil
|
||||
network = addr.Network()
|
||||
|
Loading…
Reference in New Issue
Block a user