Files
x/dialer/mws/metadata.go
T
Andrew Beresford ac99425002 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>
2026-05-08 14:32:53 +01:00

82 lines
2.4 KiB
Go

package mws
import (
"net/http"
"time"
mdata "github.com/go-gost/core/metadata"
mdutil "github.com/go-gost/x/metadata/util"
"github.com/go-gost/x/internal/util/mux"
)
const (
defaultPath = "/ws"
defaultKeepalivePeriod = 15 * time.Second
)
type metadata struct {
host string
path string
handshakeTimeout time.Duration
readHeaderTimeout time.Duration
readBufferSize int
writeBufferSize int
enableCompression bool
header http.Header
keepaliveInterval time.Duration
muxCfg *mux.Config
tcpKeepalive bool
tcpKeepaliveIdle time.Duration
tcpKeepaliveInterval time.Duration
tcpKeepaliveCount int
}
func (d *mwsDialer) parseMetadata(md mdata.Metadata) (err error) {
d.md.host = mdutil.GetString(md, "ws.host", "host")
d.md.path = mdutil.GetString(md, "ws.path", "path")
if d.md.path == "" {
d.md.path = defaultPath
}
d.md.muxCfg = &mux.Config{
Version: mdutil.GetInt(md, "mux.version"),
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
}
d.md.handshakeTimeout = mdutil.GetDuration(md, "ws.handshakeTimeout", "handshakeTimeout")
d.md.readHeaderTimeout = mdutil.GetDuration(md, "ws.readHeaderTimeout", "readHeaderTimeout")
d.md.readBufferSize = mdutil.GetInt(md, "ws.readBufferSize", "readBufferSize")
d.md.writeBufferSize = mdutil.GetInt(md, "ws.writeBufferSize", "writeBufferSize")
d.md.enableCompression = mdutil.GetBool(md, "ws.enableCompression", "enableCompression")
if m := mdutil.GetStringMapString(md, "ws.header", "header"); len(m) > 0 {
h := http.Header{}
for k, v := range m {
h.Add(k, v)
}
d.md.header = h
}
if mdutil.GetBool(md, "ws.keepalive", "keepalive") {
d.md.keepaliveInterval = mdutil.GetDuration(md, "ttl", "keepalive.interval")
if d.md.keepaliveInterval <= 0 {
d.md.keepaliveInterval = defaultKeepalivePeriod
}
}
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
}