1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00

Add TCP keep alive support in Linux

This commit is contained in:
ValdikSS 2021-11-13 21:31:49 +00:00 committed by Xiaokang Wang
parent 6e7e864f2f
commit 499a0e19cb
3 changed files with 23 additions and 3 deletions

View File

@ -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 { 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) 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 { 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) 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)
}
} }
} }

View File

@ -63,10 +63,14 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
dest: destAddr, dest: destAddr,
}, nil }, nil
} }
goStdKeepAlive := time.Duration(0)
if sockopt.TcpKeepAliveIdle != 0 {
goStdKeepAlive = time.Duration(-1)
}
dialer := &net.Dialer{ dialer := &net.Dialer{
Timeout: time.Second * 16, Timeout: time.Second * 16,
LocalAddr: resolveSrcAddr(dest.Network, src), LocalAddr: resolveSrcAddr(dest.Network, src),
KeepAlive: goStdKeepAlive,
} }
if sockopt != nil || len(d.controllers) > 0 { if sockopt != nil || len(d.controllers) > 0 {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"runtime" "runtime"
"syscall" "syscall"
"time"
"github.com/pires/go-proxyproto" "github.com/pires/go-proxyproto"
@ -49,6 +50,9 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
network = addr.Network() network = addr.Network()
address = addr.String() address = addr.String()
lc.Control = getControlFunc(ctx, sockopt, dl.controllers) lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
if sockopt.TcpKeepAliveIdle != 0 {
lc.KeepAlive = time.Duration(-1)
}
case *net.UnixAddr: case *net.UnixAddr:
lc.Control = nil lc.Control = nil
network = addr.Network() network = addr.Network()