feat: extend TCP keepalive support to all TCP-based listeners and dialers

Add configurable TCP keepalive (keepalive, keepalive.idle,
keepalive.interval, keepalive.count) to all TCP-based transport types:
tls, mtls, mtcp, ws/wss, mws/mwss, http2, ssh, sshd listeners and
tls, mtls, mtcp, ws/wss, mws/mwss, ssh, sshd dialers.

For types that already use "keepalive" for an app-level protocol
(grpc listener/dialer, ws/mws dialers, ssh/sshd dialers), the TCP
keepalive parameters are exposed under the tcp.keepalive.* prefix to
avoid ambiguity.

Also extract shared helpers into internal/net/keepalive.go:
- WrapKeepaliveListener: applies KeepAliveConfig on every accepted TCPConn
- ApplyKeepalive: applies KeepAliveConfig to a single TCPConn

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Andrew Beresford
2026-05-08 14:23:51 +01:00
parent cf70a0cbe1
commit ac99425002
36 changed files with 383 additions and 32 deletions
+10
View File
@@ -9,6 +9,7 @@ import (
"github.com/go-gost/core/dialer"
md "github.com/go-gost/core/metadata"
xctx "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/internal/net/proxyproto"
ws_util "github.com/go-gost/x/internal/util/ws"
"github.com/go-gost/x/registry"
@@ -64,6 +65,15 @@ func (d *wsDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOpt
d.options.Logger.Error(err)
}
if d.md.tcpKeepalive {
xnet.ApplyKeepalive(conn, net.KeepAliveConfig{
Enable: true,
Idle: d.md.tcpKeepaliveIdle,
Interval: d.md.tcpKeepaliveInterval,
Count: d.md.tcpKeepaliveCount,
})
}
conn = proxyproto.WrapClientConn(
d.options.ProxyProtocol,
xctx.SrcAddrFromContext(ctx),
+10
View File
@@ -25,6 +25,11 @@ type metadata struct {
header http.Header
keepaliveInterval time.Duration
tcpKeepalive bool
tcpKeepaliveIdle time.Duration
tcpKeepaliveInterval time.Duration
tcpKeepaliveCount int
}
func (d *wsDialer) parseMetadata(md mdata.Metadata) (err error) {
@@ -56,5 +61,10 @@ func (d *wsDialer) parseMetadata(md mdata.Metadata) (err error) {
}
}
d.md.tcpKeepalive = mdutil.GetBool(md, "tcp.keepalive")
d.md.tcpKeepaliveIdle = mdutil.GetDuration(md, "tcp.keepalive.idle")
d.md.tcpKeepaliveInterval = mdutil.GetDuration(md, "tcp.keepalive.interval")
d.md.tcpKeepaliveCount = mdutil.GetInt(md, "tcp.keepalive.count")
return
}