mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-05 06:33:02 +00:00
340234166b
* Add TCP keep alive idle setting * Add TCP keep alive idle setting: auto generated * Add TCP keep alive support in Linux * Add TCP keep alive support in MacOS, FreeBSD * Add TCP keep alive support in Windows * fix bug introduced in adding tcp keep alive adjustment * embed macOS const to avoid platform inconsistency * embed macOS const to avoid platform inconsistency(again) * add TCP Keep Alive support in config * use sys/unix instead of syscall Suggestion from: https://github.com/v2fly/v2ray-core/pull/1395#issuecomment-974761647 * use sys/unix instead of syscall Suggestion from: https://github.com/v2fly/v2ray-core/pull/1395#issuecomment-974761647 * Separate TcpKeepAliveIdle and TcpKeepAliveInterval check logic * Disable tcp keepAlive when TcpKeepAliveIdle < 0 and TcpKeepAliveInterval <= 0 Co-authored-by: xqzr <34030394+xqzr@users.noreply.github.com> Co-authored-by: ValdikSS <iam@valdikss.org.ru> Co-authored-by: Shelikhoo <xiaokangwang@outlook.com> Co-authored-by: xqzr <34030394+xqzr@users.noreply.github.com>
98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
package internet
|
|
|
|
import (
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
const (
|
|
// TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
|
|
TCP_FASTOPEN_SERVER = 0x01
|
|
// TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
|
|
TCP_FASTOPEN_CLIENT = 0x02 // nolint: revive,stylecheck
|
|
// syscall.TCP_KEEPINTVL is missing on some darwin architectures.
|
|
sysTCP_KEEPINTVL = 0x101 // nolint: revive,stylecheck
|
|
)
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
|
if isTCPSocket(network) {
|
|
tfo := config.ParseTFOValue()
|
|
if tfo > 0 {
|
|
tfo = TCP_FASTOPEN_CLIENT
|
|
}
|
|
if tfo >= 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
|
}
|
|
}
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
|
|
return newError("failed to set TCP_KEEPIDLE", err)
|
|
}
|
|
}
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
}
|
|
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
|
if isTCPSocket(network) {
|
|
tfo := config.ParseTFOValue()
|
|
if tfo > 0 {
|
|
tfo = TCP_FASTOPEN_SERVER
|
|
}
|
|
if tfo >= 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
|
}
|
|
}
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
|
|
return newError("failed to set TCP_KEEPIDLE", err)
|
|
}
|
|
}
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
}
|
|
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|