Skip to content

Make TCP keep-alive timeout options configurable#505

Open
twelsh-aw wants to merge 5 commits intoxjasonlyu:mainfrom
twelsh-aw:tcp-keepalive-opts
Open

Make TCP keep-alive timeout options configurable#505
twelsh-aw wants to merge 5 commits intoxjasonlyu:mainfrom
twelsh-aw:tcp-keepalive-opts

Conversation

@twelsh-aw
Copy link

@twelsh-aw twelsh-aw commented Nov 22, 2025

First off, thanks for this entire codebase! I spent a lot of time trying to setup something similar and then stumbled on this which is exactly what I was after.

I have a use case where tun2socks sits behind a load balancer which currently does sticky flow state tracking to route flows always to the same instance behind it (based on source/dest ips, ports, proto). For reasons, this load balancer must have short timeouts around inactive TCP flows; once timeout is hit, it will stop being sticky and start being able to route traffic to different instances registered to it.

I want to be able to control tun2socks TCP keepalive settings to align with this LB timeout, which requires the ability to configure values that are currently hardcoded.

I tried to set things up similar to how we do other network stack options, and just mimic that pattern for socket options. Open to different ways to open this up!

Thank you once again!

@twelsh-aw twelsh-aw marked this pull request as ready for review November 22, 2025 17:13
Copy link
Owner

@xjasonlyu xjasonlyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your pull request!

I think it's nice to make some stack/tcp options configurable. Just got some comments.


// tcpKeepAlivesEnabled is the value used to enable or disable keepalives on
// the socket.
tcpKeepAlivesEnabled = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tcpKeepAlivesEnabled = true
tcpKeepAliveEnabled = true

without -s?

core/stack.go Outdated

tcpSockOpts := append(
option.DefaultTCPSocketOptions(),
cfg.TCPSocketOptions...,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned about the potential for duplicate calls; the current way to call this option chain might not be ideal/efficient because they might be called for the same option with different values for every TCP endpoint.

I'm not sure what the best way to do it is, but I'd prefer to avoid calling an option multiple times.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya good callout. this is doing duplicate options every TCP conn. I can refactor a bit so we always register the Options, preferring user supplied values if present but using defaults otherwise, but never both

engine/engine.go Outdated

if k.TCPKeepaliveIdleTime > 0 {
tcpSocketOpts = append(tcpSocketOpts, option.WithTCPKeepaliveIdleTime(k.TCPKeepaliveIdleTime))
log.Infof("[TCP] keepalive idle time: %v", k.TCPKeepaliveIdleTime)
Copy link
Owner

@xjasonlyu xjasonlyu Nov 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, logging these options seems good to me. My minor suggestion here is to use [STACK] instead of [TCP] since these are part of netstack settings.

@github-actions github-actions bot added the Stale label Jan 27, 2026
@github-actions github-actions bot closed this Feb 3, 2026
@xjasonlyu xjasonlyu added enhancement New feature or request and removed Stale labels Feb 16, 2026
@xjasonlyu xjasonlyu reopened this Feb 16, 2026
@xjasonlyu xjasonlyu added the pending Waiting for further review label Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pending Waiting for further review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants