mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 16:41:29 +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>
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package internet
|
|
|
|
import (
|
|
"syscall"
|
|
)
|
|
|
|
const (
|
|
TCP_FASTOPEN = 15
|
|
)
|
|
|
|
func setTFO(fd syscall.Handle, tfo int) error {
|
|
if tfo > 0 {
|
|
tfo = 1
|
|
}
|
|
if tfo >= 0 {
|
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, tfo); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
|
if isTCPSocket(network) {
|
|
if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil {
|
|
return err
|
|
}
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
}
|
|
} else if config.TcpKeepAliveIdle < 0 {
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.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) {
|
|
if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil {
|
|
return err
|
|
}
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
}
|
|
} else if config.TcpKeepAliveIdle < 0 {
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
|
return nil
|
|
}
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
return nil
|
|
}
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
return nil
|
|
}
|