mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-07-27 04:14:14 +00:00
Inbounds & Outbounds: TCP KeepAlive better default value (#4931)
From https://github.com/XTLS/Xray-core/pull/4927
This commit is contained in:
parent
87d8b97d9a
commit
eb433d9462
3 changed files with 46 additions and 28 deletions
|
@ -64,26 +64,6 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
|
|||
}
|
||||
}
|
||||
|
||||
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
||||
if config.TcpKeepAliveInterval > 0 {
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
||||
return errors.New("failed to set TCP_KEEPINTVL", err)
|
||||
}
|
||||
}
|
||||
if config.TcpKeepAliveIdle > 0 {
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
|
||||
return errors.New("failed to set TCP_KEEPIDLE", err)
|
||||
}
|
||||
}
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
||||
return errors.New("failed to set SO_KEEPALIVE", err)
|
||||
}
|
||||
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
||||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
||||
return errors.New("failed to unset SO_KEEPALIVE", err)
|
||||
}
|
||||
}
|
||||
|
||||
if config.TcpCongestion != "" {
|
||||
if err := syscall.SetsockoptString(int(fd), syscall.SOL_TCP, syscall.TCP_CONGESTION, config.TcpCongestion); err != nil {
|
||||
return errors.New("failed to set TCP_CONGESTION", err)
|
||||
|
|
|
@ -3,6 +3,7 @@ package internet
|
|||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
gonet "net"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
@ -87,14 +88,34 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
|
|||
Dest: destAddr,
|
||||
}, nil
|
||||
}
|
||||
goStdKeepAlive := time.Duration(0)
|
||||
if sockopt != nil && (sockopt.TcpKeepAliveInterval != 0 || sockopt.TcpKeepAliveIdle != 0) {
|
||||
goStdKeepAlive = time.Duration(-1)
|
||||
// Chrome defaults
|
||||
keepAliveConfig := gonet.KeepAliveConfig{
|
||||
Enable: true,
|
||||
Idle: 45 * time.Second,
|
||||
Interval: 45 * time.Second,
|
||||
Count: -1,
|
||||
}
|
||||
keepAlive := time.Duration(0)
|
||||
if sockopt != nil {
|
||||
if sockopt.TcpKeepAliveIdle*sockopt.TcpKeepAliveInterval < 0 {
|
||||
return nil, errors.New("invalid TcpKeepAliveIdle or TcpKeepAliveInterval value: ", sockopt.TcpKeepAliveIdle, " ", sockopt.TcpKeepAliveInterval)
|
||||
}
|
||||
if sockopt.TcpKeepAliveIdle < 0 || sockopt.TcpKeepAliveInterval < 0 {
|
||||
keepAlive = -1
|
||||
keepAliveConfig.Enable = false
|
||||
}
|
||||
if sockopt.TcpKeepAliveIdle > 0 {
|
||||
keepAliveConfig.Idle = time.Duration(sockopt.TcpKeepAliveIdle) * time.Second
|
||||
}
|
||||
if sockopt.TcpKeepAliveInterval > 0 {
|
||||
keepAliveConfig.Interval = time.Duration(sockopt.TcpKeepAliveInterval) * time.Second
|
||||
}
|
||||
}
|
||||
dialer := &net.Dialer{
|
||||
Timeout: time.Second * 16,
|
||||
LocalAddr: resolveSrcAddr(dest.Network, src),
|
||||
KeepAlive: goStdKeepAlive,
|
||||
KeepAlive: keepAlive,
|
||||
KeepAliveConfig: keepAliveConfig,
|
||||
}
|
||||
|
||||
if sockopt != nil || len(d.controllers) > 0 {
|
||||
|
|
|
@ -2,6 +2,7 @@ package internet
|
|||
|
||||
import (
|
||||
"context"
|
||||
gonet "net"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
@ -88,9 +89,25 @@ 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)
|
||||
// default disable keepalive
|
||||
lc.KeepAlive = -1
|
||||
if sockopt != nil {
|
||||
if sockopt.TcpKeepAliveInterval != 0 || sockopt.TcpKeepAliveIdle != 0 {
|
||||
lc.KeepAlive = time.Duration(-1)
|
||||
if sockopt.TcpKeepAliveIdle*sockopt.TcpKeepAliveInterval < 0 {
|
||||
return nil, errors.New("invalid TcpKeepAliveIdle or TcpKeepAliveInterval value: ", sockopt.TcpKeepAliveIdle, " ", sockopt.TcpKeepAliveInterval)
|
||||
}
|
||||
lc.KeepAliveConfig = gonet.KeepAliveConfig{
|
||||
Enable: false,
|
||||
Idle: -1,
|
||||
Interval: -1,
|
||||
Count: -1,
|
||||
}
|
||||
if sockopt.TcpKeepAliveIdle > 0 {
|
||||
lc.KeepAliveConfig.Enable = true
|
||||
lc.KeepAliveConfig.Idle = time.Duration(sockopt.TcpKeepAliveIdle) * time.Second
|
||||
}
|
||||
if sockopt.TcpKeepAliveInterval > 0 {
|
||||
lc.KeepAliveConfig.Enable = true
|
||||
lc.KeepAliveConfig.Interval = time.Duration(sockopt.TcpKeepAliveInterval) * time.Second
|
||||
}
|
||||
if sockopt.TcpMptcp {
|
||||
lc.SetMultipathTCP(true)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue