5.3 KiB
5.3 KiB
NetworkProxy Connection Termination and SmartProxy Connection Handling
Overview
The connection management between NetworkProxy and SmartProxy involves complex coordination to handle TLS termination, connection forwarding, and proper cleanup. This document outlines how these systems work together.
SmartProxy Connection Management
Connection Tracking (ConnectionManager)
-
Connection Lifecycle:
- New connections are registered in
ConnectionManager.createConnection()
- Each connection gets a unique ID and tracking record
- Connection records track both incoming (client) and outgoing (target) sockets
- Connections are removed from tracking upon cleanup
- New connections are registered in
-
Connection Cleanup Flow:
initiateCleanupOnce() -> cleanupConnection() -> cleanupSocket()
initiateCleanupOnce()
: Prevents duplicate cleanup operationscleanupConnection()
: Main cleanup logic, removes connections from trackingcleanupSocket()
: Handles socket termination (graceful end, then forced destroy)
-
Cleanup Triggers:
- Socket errors (ECONNRESET, ETIMEDOUT, etc.)
- Socket close events
- Inactivity timeouts
- Connection lifetime limits
- Manual cleanup (e.g., NFTables-handled connections)
NetworkProxy Integration
NetworkProxyBridge
The NetworkProxyBridge
class manages the connection between SmartProxy and NetworkProxy:
-
Connection Forwarding:
forwardToNetworkProxy( connectionId: string, socket: net.Socket, record: IConnectionRecord, initialChunk: Buffer, networkProxyPort: number, cleanupCallback: (reason: string) => void )
- Creates a new socket connection to NetworkProxy
- Pipes data between client and NetworkProxy sockets
- Sets up cleanup handlers for both sockets
-
Cleanup Coordination:
- When either socket ends or errors, both are cleaned up
- Cleanup callback notifies SmartProxy's ConnectionManager
- Proper unpipe operations prevent memory leaks
NetworkProxy Connection Tracking
Connection Tracking in NetworkProxy
-
Raw TCP Connection Tracking:
setupConnectionTracking(): void { this.httpsServer.on('connection', (connection: net.Socket) => { // Track connections in socketMap this.socketMap.add(connection); // Setup cleanup handlers connection.on('close', cleanupConnection); connection.on('error', cleanupConnection); connection.on('end', cleanupConnection); }); }
-
SmartProxy Connection Detection:
- Connections from localhost (127.0.0.1) are identified as SmartProxy connections
- Special counter tracks
portProxyConnections
- Connection counts are updated when connections close
-
Metrics and Monitoring:
- Active connections tracked in
connectedClients
- TLS handshake completions tracked in
tlsTerminatedConnections
- Connection pool status monitored periodically
- Active connections tracked in
Connection Termination Flow
Typical TLS Termination Flow:
- Client connects to SmartProxy
- SmartProxy creates connection record and tracks socket
- SmartProxy determines route requires TLS termination
- NetworkProxyBridge forwards connection to NetworkProxy
- NetworkProxy performs TLS termination
- Data flows through piped sockets
- When connection ends:
- NetworkProxy cleans up its socket tracking
- NetworkProxyBridge handles cleanup coordination
- SmartProxy's ConnectionManager removes connection record
- All resources are properly released
Cleanup Coordination Points:
-
SmartProxy Cleanup:
- ConnectionManager tracks all cleanup reasons
- Socket handlers removed to prevent memory leaks
- Timeout timers cleared
- Connection records removed from maps
- Security manager notified of connection removal
-
NetworkProxy Cleanup:
- Sockets removed from tracking map
- Connection counters updated
- Metrics updated for monitoring
- Connection pool resources freed
-
Bridge Cleanup:
- Unpipe operations prevent data loss
- Both sockets properly destroyed
- Cleanup callback ensures SmartProxy is notified
Important Considerations
-
Memory Management:
- All event listeners must be removed during cleanup
- Proper unpipe operations prevent memory leaks
- Connection records cleared from all tracking maps
-
Error Handling:
- Multiple cleanup mechanisms prevent orphaned connections
- Graceful shutdown attempted before forced destruction
- Timeout mechanisms ensure cleanup even in edge cases
-
State Consistency:
- Connection closed flags prevent duplicate cleanup
- Termination reasons tracked for debugging
- Activity timestamps updated for accurate timeout handling
-
Performance:
- Connection pools minimize TCP handshake overhead
- Efficient socket tracking using Maps
- Periodic cleanup prevents resource accumulation
Best Practices
- Always use
initiateCleanupOnce()
to prevent duplicate cleanup operations - Track termination reasons for debugging and monitoring
- Ensure all event listeners are removed during cleanup
- Use proper unpipe operations when breaking socket connections
- Monitor connection counts and cleanup statistics
- Implement proper timeout handling for all connection types
- Keep socket tracking maps synchronized with actual socket state