Skip to content

Commit 01f6419

Browse files
authored
Improved Proxy Agent Recovery and Tunnel Management
## TLDR Fix agent recovery, tunnel restoration, and listener management in proxy ## Description The changes improve the proxy's ability to handle agent disconnections and reconnections, properly restoring tunnels and listeners while preventing resource leaks and conflicts. (Fixing the "File exists" error when interfaces already existed) ## Major Fixes: ### Duplicate Connection Prevention * Validates that existing sessions are truly functional before rejecting duplicates * Tests session functionality by attempting to open a test stream * Closes duplicate connections gracefully with proper error messages ### Complete Tunnel Restoration * Critical Fix: Always restores tunnels if an interface was previously configured, not just if Running flag was set * Properly cleans up stale TUN interfaces and routes before recreation * Destroys old interface file descriptors to prevent resource conflicts * Recreates interface with fresh file descriptor before tunnel start * Adds proper delays for interface cleanup and recreation ### Proper Listener Recovery * Critical Fix: Completely recreates listeners on the agent side instead of just resetting multiplexer * Collects listener information before stopping old listeners * Clears old listener array to prevent stale references * Adds delay for agent-side port binding cleanup (500ms) * Creates new listeners via AddListener() which properly binds on agent * Each restored listener gets its own relay goroutine ### Enhanced Tunnel Lifecycle Management * Added route cleanup on user-initiated tunnel stop * Added stale route cleanup when agent connection drops * Destroys interfaces after removing routes to release file descriptors * Improved logging for tunnel state transitions * Better handling of "file exists" errors for routes ### Improved StartTunnel Function * Creates interfaces even without prior config if needed * Better error handling with wrapped errors * Handles duplicate route addition gracefully * Validates interface existence before operations ### Listener Error Handling * Changed listener relay errors from Error to Warn level * Prevents cascade failures in double-pivot scenarios * More appropriate logging for expected disconnections
1 parent 8eacc8a commit 01f6419

File tree

1 file changed

+259
-75
lines changed

1 file changed

+259
-75
lines changed

0 commit comments

Comments
 (0)