Skip to content

Commit

Permalink
chore: update tcp keepAlive setting for go1.23
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Aug 14, 2024
1 parent acaacd8 commit 24c6e7d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
12 changes: 12 additions & 0 deletions common/net/tcp_keepalive_go122.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !go1.23

package net

import "net"

func TCPKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAlive(true)
_ = tcp.SetKeepAlivePeriod(KeepAliveInterval)
}
}
15 changes: 15 additions & 0 deletions common/net/tcp_keepalive_go123.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build go1.23

package net

import "net"

func TCPKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAliveConfig(net.KeepAliveConfig{
Enable: true,
Idle: KeepAliveIdle,
Interval: KeepAliveInterval,
})
}
}
12 changes: 4 additions & 8 deletions common/net/tcpip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"time"
)

var KeepAliveInterval = 15 * time.Second
var (
KeepAliveIdle = 0 * time.Second
KeepAliveInterval = 0 * time.Second
)

func SplitNetworkType(s string) (string, string, error) {
var (
Expand Down Expand Up @@ -47,10 +50,3 @@ func SplitHostPort(s string) (host, port string, hasPort bool, err error) {
host, port, err = net.SplitHostPort(temp)
return
}

func TCPKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAlive(true)
_ = tcp.SetKeepAlivePeriod(KeepAliveInterval)
}
}
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ type RawConfig struct {
FindProcessMode P.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"`
GlobalClientFingerprint string `yaml:"global-client-fingerprint"`
GlobalUA string `yaml:"global-ua"`
KeepAliveIdle int `yaml:"keep-alive-idle"`
KeepAliveInterval int `yaml:"keep-alive-interval"`

Sniffer RawSniffer `yaml:"sniffer" json:"sniffer"`
Expand Down Expand Up @@ -649,6 +650,10 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
C.ASNUrl = cfg.GeoXUrl.ASN
C.GeodataMode = cfg.GeodataMode
C.UA = cfg.GlobalUA

if cfg.KeepAliveIdle != 0 {
N.KeepAliveIdle = time.Duration(cfg.KeepAliveIdle) * time.Second
}
if cfg.KeepAliveInterval != 0 {
N.KeepAliveInterval = time.Duration(cfg.KeepAliveInterval) * time.Second
}
Expand Down
3 changes: 2 additions & 1 deletion docs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ external-doh-server: /dns-query
global-client-fingerprint: chrome

# TCP keep alive interval
keep-alive-interval: 15
# keep-alive-idle: 7200
# keep-alive-interval: 75

# routing-mark:6666 # 配置 fwmark 仅用于 Linux
experimental:
Expand Down

0 comments on commit 24c6e7d

Please sign in to comment.