Compare commits

...

22 Commits

Author SHA1 Message Date
98f1e0df4c 3.31.1
Some checks failed
Default (tags) / security (push) Successful in 37s
Default (tags) / test (push) Failing after 1m3s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-11 03:48:10 +00:00
d6022c8f8a fix(PortProxy): Improve TLS handshake buffering and enhance debug logging for SNI forwarding in PortProxy 2025-03-11 03:48:10 +00:00
0ea0f02428 fix(PortProxy): Improve connection reliability for initial and resumed TLS sessions
Added enhanced connection handling to fix issues with both initial connections and TLS session resumption:

1. Improved debugging for connection setup with detailed logging
2. Added explicit timeout for backend connections to prevent hanging connections
3. Enhanced error recovery for connection failures with faster client notification
4. Added detailed session tracking to maintain domain context across TLS sessions
5. Fixed handling of TLS renegotiation with improved activity timestamp updates

This should address the issue where initial connections may fail but subsequent retries succeed,
as well as ensuring proper certificate selection for resumed TLS sessions.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 03:33:03 +00:00
e452f55203 3.31.0
Some checks failed
Default (tags) / security (push) Successful in 35s
Default (tags) / test (push) Failing after 1m4s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-11 03:16:04 +00:00
55f25f1976 feat(PortProxy): Improve TLS handshake SNI extraction and add session resumption tracking in PortProxy 2025-03-11 03:16:04 +00:00
98b7f3ed7f 3.30.8
Some checks failed
Default (tags) / security (push) Failing after 11m56s
Default (tags) / test (push) Has been cancelled
Default (tags) / release (push) Has been cancelled
Default (tags) / metadata (push) Has been cancelled
2025-03-11 02:50:01 +00:00
cb83caeafd fix(core): No changes in this commit. 2025-03-11 02:50:01 +00:00
7850a80452 fix(PortProxy): Fix TypeScript errors by using correct variable names
Fixed TypeScript errors caused by using 'connectionRecord' instead of 'record' in TLS renegotiation handlers.
The variable name mistake occurred when moving and restructuring the TLS handshake detection code.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 02:47:57 +00:00
ef8f583a90 fix(PortProxy): Move TLS renegotiation detection before socket piping
Fundamentally restructured TLS renegotiation handling to ensure handshake packets are properly detected. The previous implementation attached event handlers after pipe() was established, which might have caused handshake packets to bypass detection. Key changes:

1. Moved renegotiation detection before pipe() to ensure all TLS handshake packets are detected
2. Added explicit lockedDomain setting for all SNI connections
3. Simplified the NetworkProxy TLS handshake detection
4. Removed redundant data handlers that could interfere with each other

These changes should make renegotiation detection more reliable regardless of how Node.js internal pipe() implementation handles data events.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 02:45:51 +00:00
2bdd6f8c1f fix(PortProxy): Update activity timestamp during TLS renegotiation to prevent connection timeouts
Ensures that TLS renegotiation packets properly update the connection's activity timestamp even when no SNI is present or when there are errors processing the renegotiation. This prevents connections from being closed due to inactivity during legitimate TLS renegotiation.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 02:40:08 +00:00
99d28eafd1 3.30.7
Some checks failed
Default (tags) / security (push) Successful in 29s
Default (tags) / test (push) Failing after 1m1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-11 02:25:59 +00:00
788b444fcc fix(PortProxy): Improve TLS renegotiation SNI handling by first checking if the new SNI is allowed under the existing domain config. If not, attempt to find an alternative domain config and update the locked domain accordingly; otherwise, terminate the connection on SNI mismatch. 2025-03-11 02:25:58 +00:00
4225abe3c4 3.30.6
Some checks failed
Default (tags) / security (push) Successful in 36s
Default (tags) / test (push) Failing after 1m0s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-11 02:18:56 +00:00
74fdb58f84 fix(PortProxy): Improve TLS renegotiation handling in PortProxy by validating the new SNI against allowed domain configurations. If the new SNI is permitted based on existing IP rules, update the locked domain to allow connection reuse; otherwise, terminate the connection to prevent misrouting. 2025-03-11 02:18:56 +00:00
bffdaffe39 3.30.5
Some checks failed
Default (tags) / security (push) Successful in 20s
Default (tags) / test (push) Failing after 1m1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-10 22:36:28 +00:00
67a4228518 fix(internal): No uncommitted changes detected; project files and tests remain unchanged. 2025-03-10 22:36:28 +00:00
681209f2e1 3.30.4
Some checks failed
Default (tags) / security (push) Successful in 36s
Default (tags) / test (push) Failing after 1m1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-10 22:35:34 +00:00
c415a6c361 fix(PortProxy): Fix TLS renegotiation handling and adjust TLS keep-alive timeouts in PortProxy implementation 2025-03-10 22:35:34 +00:00
009e3c4f0e 3.30.3
Some checks failed
Default (tags) / security (push) Failing after 14m48s
Default (tags) / test (push) Has been cancelled
Default (tags) / release (push) Has been cancelled
Default (tags) / metadata (push) Has been cancelled
2025-03-10 22:07:12 +00:00
f9c42975dc fix(classes.portproxy.ts): Simplify timeout management in PortProxy and fix chained proxy certificate refresh issues 2025-03-10 22:07:12 +00:00
feef949afe 3.30.2
Some checks failed
Default (tags) / security (push) Successful in 34s
Default (tags) / test (push) Failing after 1m10s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-03-10 14:15:03 +00:00
8d3b07b1e6 fix(classes.portproxy.ts): Adjust TLS keep-alive timeout to refresh certificate context. 2025-03-10 14:15:03 +00:00
4 changed files with 670 additions and 107 deletions

View File

@ -1,5 +1,78 @@
# Changelog # Changelog
## 2025-03-11 - 3.31.1 - fix(PortProxy)
Improve TLS handshake buffering and enhance debug logging for SNI forwarding in PortProxy
- Explicitly copy the initial TLS handshake data to prevent mutation before buffering
- Log buffered TLS handshake data with SNI information for better diagnostics
- Add detailed error logs on TLS connection failures, including server and domain config status
- Output additional debug messages during ClientHello forwarding to verify proper TLS handshake processing
## 2025-03-11 - 3.31.0 - feat(PortProxy)
Improve TLS handshake SNI extraction and add session resumption tracking in PortProxy
- Added ITlsSessionInfo interface and a global tlsSessionCache to track TLS session IDs for session resumption
- Implemented a cleanup timer for the TLS session cache with startSessionCleanupTimer and stopSessionCleanupTimer
- Enhanced extractSNIInfo to return detailed SNI information including session IDs, ticket details, and resumption status
- Updated renegotiation handlers to use extractSNIInfo for proper SNI extraction during TLS rehandshake
## 2025-03-11 - 3.30.8 - fix(core)
No changes in this commit.
## 2025-03-11 - 3.30.7 - fix(PortProxy)
Improve TLS renegotiation SNI handling by first checking if the new SNI is allowed under the existing domain config. If not, attempt to find an alternative domain config and update the locked domain accordingly; otherwise, terminate the connection on SNI mismatch.
- Added a preliminary check against the original domain config to allow re-handshakes if the new SNI matches allowed patterns.
- If the original config does not allow, search for an alternative domain config and validate IP rules.
- Update the locked domain when allowed, ensuring connection reuse with valid certificate context.
- Terminate the connection if no suitable domain config is found or IP restrictions are violated.
## 2025-03-11 - 3.30.6 - fix(PortProxy)
Improve TLS renegotiation handling in PortProxy by validating the new SNI against allowed domain configurations. If the new SNI is permitted based on existing IP rules, update the locked domain to allow connection reuse; otherwise, terminate the connection to prevent misrouting.
- Added logic to check if a new SNI during renegotiation is allowed by comparing IP rules from the matching domain configuration.
- Updated detailed logging to indicate when a valid SNI change is accepted and when it results in a mismatch termination.
## 2025-03-10 - 3.30.5 - fix(internal)
No uncommitted changes detected; project files and tests remain unchanged.
## 2025-03-10 - 3.30.4 - fix(PortProxy)
Fix TLS renegotiation handling and adjust TLS keep-alive timeouts in PortProxy implementation
- Allow TLS renegotiation data without an explicit SNI extraction to pass through, ensuring valid renegotiations are not dropped (critical for Chrome).
- Update TLS keep-alive timeout from an aggressive 30 minutes to a more generous 4 hours to reduce unnecessary reconnections.
- Increase inactivity thresholds for TLS connections from 20 minutes to 2 hours with an additional verification interval extended from 5 to 15 minutes.
- Adjust long-lived TLS connection timeout from 45 minutes to 8 hours for improved certificate context refresh in chained proxy scenarios.
## 2025-03-10 - 3.30.3 - fix(classes.portproxy.ts)
Simplify timeout management in PortProxy and fix chained proxy certificate refresh issues
- Reduced TLS keep-alive timeout from 8 hours to 30 minutes to ensure frequent certificate refresh
- Added aggressive TLS state refresh after 20 minutes of inactivity and secondary verification checks
- Lowered long-lived TLS connection lifetime from 12 hours to 45 minutes to prevent stale certificates
- Removed configurable timeout settings from the public API in favor of hardcoded sensible defaults
- Simplified internal timeout management to reduce code complexity and improve certificate handling in chained proxies
## 2025-03-10 - 3.31.0 - fix(classes.portproxy.ts)
Simplified timeout management and fixed certificate issues in chained proxy scenarios
- Dramatically reduced TLS keep-alive timeout from 8 hours to 30 minutes to ensure fresh certificates
- Added aggressive certificate refresh after 20 minutes of inactivity (down from 4 hours)
- Added secondary verification checks for TLS refresh operations
- Reduced long-lived TLS connection lifetime from 12 hours to 45 minutes
- Removed configurable timeouts completely from the public API in favor of hardcoded sensible defaults
- Simplified interface by removing no-longer-configurable settings while maintaining internal compatibility
- Reduced overall code complexity by eliminating complex timeout management
- Fixed chained proxy certificate issues by ensuring more frequent certificate refreshes in all deployment scenarios
## 2025-03-10 - 3.30.2 - fix(classes.portproxy.ts)
Adjust TLS keep-alive timeout to refresh certificate context.
- Modified TLS keep-alive timeout for connections to 8 hours to refresh certificate context.
- Updated timeout log messages for clarity on TLS certificate refresh.
## 2025-03-10 - 3.30.1 - fix(PortProxy) ## 2025-03-10 - 3.30.1 - fix(PortProxy)
Improve TLS keep-alive management and fix whitespace formatting Improve TLS keep-alive management and fix whitespace formatting

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartproxy", "name": "@push.rocks/smartproxy",
"version": "3.30.1", "version": "3.31.1",
"private": false, "private": false,
"description": "A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.", "description": "A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartproxy', name: '@push.rocks/smartproxy',
version: '3.30.1', version: '3.31.1',
description: 'A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.' description: 'A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, and dynamic routing with authentication options.'
} }

View File

@ -16,7 +16,12 @@ export interface IDomainConfig {
networkProxyIndex?: number; // Optional index to specify which NetworkProxy to use (defaults to 0) networkProxyIndex?: number; // Optional index to specify which NetworkProxy to use (defaults to 0)
} }
/** Port proxy settings including global allowed port ranges */ /**
* Port proxy settings including global allowed port ranges
*
* NOTE: In version 3.31.0+, timeout settings have been simplified and hardcoded with sensible defaults
* to ensure TLS certificate safety in all deployment scenarios, especially chained proxies.
*/
export interface IPortProxySettings extends plugins.tls.TlsOptions { export interface IPortProxySettings extends plugins.tls.TlsOptions {
fromPort: number; fromPort: number;
toPort: number; toPort: number;
@ -27,14 +32,10 @@ export interface IPortProxySettings extends plugins.tls.TlsOptions {
defaultBlockedIPs?: string[]; defaultBlockedIPs?: string[];
preserveSourceIP?: boolean; preserveSourceIP?: boolean;
// Timeout settings // Simplified timeout settings
initialDataTimeout?: number; // Timeout for initial data/SNI (ms), default: 60000 (60s)
socketTimeout?: number; // Socket inactivity timeout (ms), default: 3600000 (1h)
inactivityCheckInterval?: number; // How often to check for inactive connections (ms), default: 60000 (60s)
maxConnectionLifetime?: number; // Default max connection lifetime (ms), default: 86400000 (24h)
inactivityTimeout?: number; // Inactivity timeout (ms), default: 14400000 (4h)
gracefulShutdownTimeout?: number; // (ms) maximum time to wait for connections to close during shutdown gracefulShutdownTimeout?: number; // (ms) maximum time to wait for connections to close during shutdown
// Ranged port settings
globalPortRanges: Array<{ from: number; to: number }>; // Global allowed port ranges globalPortRanges: Array<{ from: number; to: number }>; // Global allowed port ranges
forwardAllGlobalRanges?: boolean; // When true, forwards all connections on global port ranges to the global targetIP forwardAllGlobalRanges?: boolean; // When true, forwards all connections on global port ranges to the global targetIP
@ -44,9 +45,7 @@ export interface IPortProxySettings extends plugins.tls.TlsOptions {
keepAliveInitialDelay?: number; // Initial delay before sending keepalive probes (ms) keepAliveInitialDelay?: number; // Initial delay before sending keepalive probes (ms)
maxPendingDataSize?: number; // Maximum bytes to buffer during connection setup maxPendingDataSize?: number; // Maximum bytes to buffer during connection setup
// Enhanced features // Logging settings
disableInactivityCheck?: boolean; // Disable inactivity checking entirely
enableKeepAliveProbes?: boolean; // Enable TCP keep-alive probes
enableDetailedLogging?: boolean; // Enable detailed connection logging enableDetailedLogging?: boolean; // Enable detailed connection logging
enableTlsDebugLogging?: boolean; // Enable TLS handshake debug logging enableTlsDebugLogging?: boolean; // Enable TLS handshake debug logging
enableRandomizedTimeouts?: boolean; // Randomize timeouts slightly to prevent thundering herd enableRandomizedTimeouts?: boolean; // Randomize timeouts slightly to prevent thundering herd
@ -55,12 +54,7 @@ export interface IPortProxySettings extends plugins.tls.TlsOptions {
maxConnectionsPerIP?: number; // Maximum simultaneous connections from a single IP maxConnectionsPerIP?: number; // Maximum simultaneous connections from a single IP
connectionRateLimitPerMinute?: number; // Max new connections per minute from a single IP connectionRateLimitPerMinute?: number; // Max new connections per minute from a single IP
// Enhanced keep-alive settings // NetworkProxy integration
keepAliveTreatment?: 'standard' | 'extended' | 'immortal'; // How to treat keep-alive connections
keepAliveInactivityMultiplier?: number; // Multiplier for inactivity timeout for keep-alive connections
extendedKeepAliveLifetime?: number; // Extended lifetime for keep-alive connections (ms)
// New property for NetworkProxy integration
networkProxies?: NetworkProxy[]; // Array of NetworkProxy instances to use for TLS termination networkProxies?: NetworkProxy[]; // Array of NetworkProxy instances to use for TLS termination
} }
@ -106,14 +100,83 @@ interface IConnectionRecord {
lastSleepDetection?: number; // Timestamp of the last sleep detection lastSleepDetection?: number; // Timestamp of the last sleep detection
} }
/**
* Structure to track TLS session information for proper resumption handling
*/
interface ITlsSessionInfo {
domain: string; // The SNI domain associated with this session
sessionId?: Buffer; // The TLS session ID (if available)
ticketId?: string; // Session ticket identifier for newer TLS versions
ticketTimestamp: number; // When this session was recorded
}
// Global cache of TLS session IDs to SNI domains
// This ensures resumed sessions maintain their SNI binding
const tlsSessionCache = new Map<string, ITlsSessionInfo>();
// Reference to session cleanup timer so we can clear it
let tlsSessionCleanupTimer: NodeJS.Timeout | null = null;
// Start the cleanup timer for session cache
function startSessionCleanupTimer() {
// Avoid creating multiple timers
if (tlsSessionCleanupTimer) {
clearInterval(tlsSessionCleanupTimer);
}
// Create new cleanup timer
tlsSessionCleanupTimer = setInterval(() => {
const now = Date.now();
const expiryTime = 24 * 60 * 60 * 1000; // 24 hours
for (const [sessionId, info] of tlsSessionCache.entries()) {
if (now - info.ticketTimestamp > expiryTime) {
tlsSessionCache.delete(sessionId);
}
}
}, 60 * 60 * 1000); // Clean up once per hour
// Make sure the interval doesn't keep the process alive
if (tlsSessionCleanupTimer.unref) {
tlsSessionCleanupTimer.unref();
}
}
// Start the timer initially
startSessionCleanupTimer();
// Function to stop the cleanup timer (used during shutdown)
function stopSessionCleanupTimer() {
if (tlsSessionCleanupTimer) {
clearInterval(tlsSessionCleanupTimer);
tlsSessionCleanupTimer = null;
}
}
/**
* Return type for the extractSNIInfo function
*/
interface ISNIExtractResult {
serverName?: string; // The extracted SNI hostname
sessionId?: Buffer; // The TLS session ID if present
sessionIdKey?: string; // The hex string representation of session ID
sessionTicketId?: string; // Session ticket identifier for TLS 1.3+ resumption
hasSessionTicket?: boolean; // Whether a session ticket extension was found
isResumption: boolean; // Whether this appears to be a session resumption
resumedDomain?: string; // The domain associated with the session if resuming
partialExtract?: boolean; // Whether this was only a partial extraction (more data needed)
}
/** /**
* Extracts the SNI (Server Name Indication) from a TLS ClientHello packet. * Extracts the SNI (Server Name Indication) from a TLS ClientHello packet.
* Enhanced for robustness and detailed logging. * Enhanced for robustness and detailed logging.
* Also extracts and tracks TLS Session IDs for session resumption handling.
*
* @param buffer - Buffer containing the TLS ClientHello. * @param buffer - Buffer containing the TLS ClientHello.
* @param enableLogging - Whether to enable detailed logging. * @param enableLogging - Whether to enable detailed logging.
* @returns The server name if found, otherwise undefined. * @returns An object containing SNI and session information, or undefined if parsing fails.
*/ */
function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | undefined { function extractSNIInfo(buffer: Buffer, enableLogging: boolean = false): ISNIExtractResult | undefined {
try { try {
// Check if buffer is too small for TLS // Check if buffer is too small for TLS
if (buffer.length < 5) { if (buffer.length < 5) {
@ -159,9 +222,38 @@ function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | un
offset += 2 + 32; // Skip client version and random offset += 2 + 32; // Skip client version and random
// Session ID // Extract Session ID for session resumption tracking
const sessionIDLength = buffer.readUInt8(offset); const sessionIDLength = buffer.readUInt8(offset);
if (enableLogging) console.log(`Session ID Length: ${sessionIDLength}`); if (enableLogging) console.log(`Session ID Length: ${sessionIDLength}`);
// If there's a session ID, extract it
let sessionId: Buffer | undefined;
let sessionIdKey: string | undefined;
let isResumption = false;
let resumedDomain: string | undefined;
if (sessionIDLength > 0) {
sessionId = Buffer.from(buffer.slice(offset + 1, offset + 1 + sessionIDLength));
// Convert sessionId to a string key for our cache
sessionIdKey = sessionId.toString('hex');
if (enableLogging) {
console.log(`Session ID: ${sessionIdKey}`);
}
// Check if this is a session resumption attempt
if (tlsSessionCache.has(sessionIdKey)) {
const cachedInfo = tlsSessionCache.get(sessionIdKey)!;
resumedDomain = cachedInfo.domain;
isResumption = true;
if (enableLogging) {
console.log(`TLS Session Resumption detected for domain: ${resumedDomain}`);
}
}
}
offset += 1 + sessionIDLength; // Skip session ID offset += 1 + sessionIDLength; // Skip session ID
// Cipher suites // Cipher suites
@ -200,6 +292,10 @@ function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | un
return undefined; return undefined;
} }
// Variables to track session tickets
let hasSessionTicket = false;
let sessionTicketId: string | undefined;
// Parse extensions // Parse extensions
while (offset + 4 <= extensionsEnd) { while (offset + 4 <= extensionsEnd) {
const extensionType = buffer.readUInt16BE(offset); const extensionType = buffer.readUInt16BE(offset);
@ -209,6 +305,33 @@ function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | un
console.log(`Extension Type: 0x${extensionType.toString(16)}, Length: ${extensionLength}`); console.log(`Extension Type: 0x${extensionType.toString(16)}, Length: ${extensionLength}`);
offset += 4; offset += 4;
// Check for Session Ticket extension (type 0x0023)
if (extensionType === 0x0023 && extensionLength > 0) {
hasSessionTicket = true;
// Extract a hash of the ticket for tracking
if (extensionLength > 16) { // Ensure we have enough bytes to create a meaningful ID
const ticketBytes = buffer.slice(offset, offset + Math.min(16, extensionLength));
sessionTicketId = ticketBytes.toString('hex');
if (enableLogging) {
console.log(`Session Ticket found, ID: ${sessionTicketId}`);
// Check if this is a known session ticket
if (tlsSessionCache.has(`ticket:${sessionTicketId}`)) {
const cachedInfo = tlsSessionCache.get(`ticket:${sessionTicketId}`);
console.log(`TLS Session Ticket Resumption detected for domain: ${cachedInfo?.domain}`);
// Set isResumption and resumedDomain if not already set
if (!isResumption && !resumedDomain) {
isResumption = true;
resumedDomain = cachedInfo?.domain;
}
}
}
}
}
if (extensionType === 0x0000) { if (extensionType === 0x0000) {
// SNI extension // SNI extension
@ -251,7 +374,43 @@ function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | un
const serverName = buffer.toString('utf8', offset, offset + nameLen); const serverName = buffer.toString('utf8', offset, offset + nameLen);
if (enableLogging) console.log(`Extracted SNI: ${serverName}`); if (enableLogging) console.log(`Extracted SNI: ${serverName}`);
return serverName;
// Store the session ID to domain mapping for future resumptions
if (sessionIdKey && sessionId && serverName) {
tlsSessionCache.set(sessionIdKey, {
domain: serverName,
sessionId: sessionId,
ticketTimestamp: Date.now()
});
if (enableLogging) {
console.log(`Stored session ${sessionIdKey} for domain ${serverName}`);
}
}
// Also store session ticket information if present
if (sessionTicketId && serverName) {
tlsSessionCache.set(`ticket:${sessionTicketId}`, {
domain: serverName,
ticketId: sessionTicketId,
ticketTimestamp: Date.now()
});
if (enableLogging) {
console.log(`Stored session ticket ${sessionTicketId} for domain ${serverName}`);
}
}
// Return the complete extraction result
return {
serverName,
sessionId,
sessionIdKey,
sessionTicketId,
isResumption,
resumedDomain,
hasSessionTicket
};
} }
offset += nameLen; offset += nameLen;
@ -263,13 +422,46 @@ function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | un
} }
if (enableLogging) console.log('No SNI extension found'); if (enableLogging) console.log('No SNI extension found');
return undefined;
// Even without SNI, we might be dealing with a session resumption
if (isResumption && resumedDomain) {
return {
serverName: resumedDomain, // Use the domain from previous session
sessionId,
sessionIdKey,
sessionTicketId,
hasSessionTicket,
isResumption: true,
resumedDomain
};
}
// Return a basic result with just the session info
return {
isResumption,
sessionId,
sessionIdKey,
sessionTicketId,
hasSessionTicket,
resumedDomain
};
} catch (err) { } catch (err) {
console.log(`Error extracting SNI: ${err}`); console.log(`Error extracting SNI: ${err}`);
return undefined; return undefined;
} }
} }
/**
* Legacy wrapper for extractSNIInfo to maintain backward compatibility
* @param buffer - Buffer containing the TLS ClientHello
* @param enableLogging - Whether to enable detailed logging
* @returns The server name if found, otherwise undefined
*/
function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | undefined {
const result = extractSNIInfo(buffer, enableLogging);
return result?.serverName;
}
// Helper: Check if a port falls within any of the given port ranges // Helper: Check if a port falls within any of the given port ranges
const isPortInRanges = (port: number, ranges: Array<{ from: number; to: number }>): boolean => { const isPortInRanges = (port: number, ranges: Array<{ from: number; to: number }>): boolean => {
return ranges.some((range) => port >= range.from && port <= range.to); return ranges.some((range) => port >= range.from && port <= range.to);
@ -332,7 +524,22 @@ const randomizeTimeout = (baseTimeout: number, variationPercent: number = 5): nu
export class PortProxy { export class PortProxy {
private netServers: plugins.net.Server[] = []; private netServers: plugins.net.Server[] = [];
settings: IPortProxySettings;
// Define the internal settings interface to include all fields, including those removed from the public interface
settings: IPortProxySettings & {
// Internal fields removed from public interface in 3.31.0+
initialDataTimeout: number;
socketTimeout: number;
inactivityCheckInterval: number;
maxConnectionLifetime: number;
inactivityTimeout: number;
disableInactivityCheck: boolean;
enableKeepAliveProbes: boolean;
keepAliveTreatment: 'standard' | 'extended' | 'immortal';
keepAliveInactivityMultiplier: number;
extendedKeepAliveLifetime: number;
};
private connectionRecords: Map<string, IConnectionRecord> = new Map(); private connectionRecords: Map<string, IConnectionRecord> = new Map();
private connectionLogger: NodeJS.Timeout | null = null; private connectionLogger: NodeJS.Timeout | null = null;
private isShuttingDown: boolean = false; private isShuttingDown: boolean = false;
@ -357,42 +564,41 @@ export class PortProxy {
private networkProxies: NetworkProxy[] = []; private networkProxies: NetworkProxy[] = [];
constructor(settingsArg: IPortProxySettings) { constructor(settingsArg: IPortProxySettings) {
// Set reasonable defaults for all settings // Set hardcoded sensible defaults for all settings
this.settings = { this.settings = {
...settingsArg, ...settingsArg,
targetIP: settingsArg.targetIP || 'localhost', targetIP: settingsArg.targetIP || 'localhost',
// Timeout settings with reasonable defaults // Hardcoded timeout settings optimized for TLS safety in all deployment scenarios
initialDataTimeout: settingsArg.initialDataTimeout || 60000, // 60 seconds for initial handshake initialDataTimeout: 60000, // 60 seconds for initial handshake
socketTimeout: ensureSafeTimeout(settingsArg.socketTimeout || 3600000), // 1 hour socket timeout socketTimeout: 1800000, // 30 minutes - short enough for regular certificate refresh
inactivityCheckInterval: settingsArg.inactivityCheckInterval || 60000, // 60 seconds interval inactivityCheckInterval: 60000, // 60 seconds interval for regular cleanup
maxConnectionLifetime: ensureSafeTimeout(settingsArg.maxConnectionLifetime || 86400000), // 24 hours default maxConnectionLifetime: 3600000, // 1 hour maximum lifetime for all connections
inactivityTimeout: ensureSafeTimeout(settingsArg.inactivityTimeout || 14400000), // 4 hours inactivity timeout inactivityTimeout: 1800000, // 30 minutes inactivity timeout
gracefulShutdownTimeout: settingsArg.gracefulShutdownTimeout || 30000, // 30 seconds gracefulShutdownTimeout: settingsArg.gracefulShutdownTimeout || 30000, // 30 seconds
// Socket optimization settings // Socket optimization settings
noDelay: settingsArg.noDelay !== undefined ? settingsArg.noDelay : true, noDelay: settingsArg.noDelay !== undefined ? settingsArg.noDelay : true,
keepAlive: settingsArg.keepAlive !== undefined ? settingsArg.keepAlive : true, keepAlive: settingsArg.keepAlive !== undefined ? settingsArg.keepAlive : true,
keepAliveInitialDelay: settingsArg.keepAliveInitialDelay || 10000, // 10 seconds (reduced for responsiveness) keepAliveInitialDelay: settingsArg.keepAliveInitialDelay || 10000, // 10 seconds
maxPendingDataSize: settingsArg.maxPendingDataSize || 10 * 1024 * 1024, // 10MB to handle large TLS handshakes maxPendingDataSize: settingsArg.maxPendingDataSize || 10 * 1024 * 1024, // 10MB to handle large TLS handshakes
// Feature flags // Feature flags - simplified with sensible defaults
disableInactivityCheck: settingsArg.disableInactivityCheck || false, disableInactivityCheck: false, // Always enable inactivity checks for TLS safety
enableKeepAliveProbes: enableKeepAliveProbes: true, // Always enable keep-alive probes for connection health
settingsArg.enableKeepAliveProbes !== undefined ? settingsArg.enableKeepAliveProbes : true, // Enable by default
enableDetailedLogging: settingsArg.enableDetailedLogging || false, enableDetailedLogging: settingsArg.enableDetailedLogging || false,
enableTlsDebugLogging: settingsArg.enableTlsDebugLogging || false, enableTlsDebugLogging: settingsArg.enableTlsDebugLogging || false,
enableRandomizedTimeouts: settingsArg.enableRandomizedTimeouts || false, // Disable randomization by default enableRandomizedTimeouts: settingsArg.enableRandomizedTimeouts || false,
// Rate limiting defaults // Rate limiting defaults
maxConnectionsPerIP: settingsArg.maxConnectionsPerIP || 100, // 100 connections per IP maxConnectionsPerIP: settingsArg.maxConnectionsPerIP || 100, // 100 connections per IP
connectionRateLimitPerMinute: settingsArg.connectionRateLimitPerMinute || 300, // 300 per minute connectionRateLimitPerMinute: settingsArg.connectionRateLimitPerMinute || 300, // 300 per minute
// Enhanced keep-alive settings // Keep-alive settings with sensible defaults that ensure certificate safety
keepAliveTreatment: settingsArg.keepAliveTreatment || 'extended', // Extended by default keepAliveTreatment: 'standard', // Always use standard treatment for certificate safety
keepAliveInactivityMultiplier: settingsArg.keepAliveInactivityMultiplier || 6, // 6x normal inactivity timeout keepAliveInactivityMultiplier: 2, // 2x normal inactivity timeout for minimal extension
extendedKeepAliveLifetime: settingsArg.extendedKeepAliveLifetime || 7 * 24 * 60 * 60 * 1000, // 7 days extendedKeepAliveLifetime: 3 * 60 * 60 * 1000, // 3 hours maximum (previously was 7 days!)
}; };
// Store NetworkProxy instances if provided // Store NetworkProxy instances if provided
@ -493,26 +699,17 @@ export class PortProxy {
} }
this.cleanupConnection(record, 'client_closed'); this.cleanupConnection(record, 'client_closed');
}); });
// Update activity on data transfer // Special handler for TLS handshake detection with NetworkProxy
socket.on('data', (chunk: Buffer) => { socket.on('data', (chunk: Buffer) => {
this.updateActivity(record); // Check for TLS handshake packets (ContentType.handshake)
// Check for potential TLS renegotiation or reconnection packets
if (chunk.length > 0 && chunk[0] === 22) { if (chunk.length > 0 && chunk[0] === 22) {
// ContentType.handshake console.log(`[${connectionId}] Detected potential TLS handshake with NetworkProxy, updating activity`);
if (this.settings.enableDetailedLogging) { this.updateActivity(record);
console.log(
`[${connectionId}] Detected potential TLS handshake data while connected to NetworkProxy`
);
}
// Let the NetworkProxy handle the TLS renegotiation
// Just update the activity timestamp to prevent timeouts
record.lastActivity = Date.now();
} }
}); });
// Update activity on data transfer from the proxy socket
proxySocket.on('data', () => this.updateActivity(record)); proxySocket.on('data', () => this.updateActivity(record));
if (this.settings.enableDetailedLogging) { if (this.settings.enableDetailedLogging) {
@ -536,15 +733,37 @@ export class PortProxy {
initialChunk?: Buffer, initialChunk?: Buffer,
overridePort?: number overridePort?: number
): void { ): void {
// Enhanced logging for initial connection troubleshooting
if (serverName) {
console.log(`[${connectionId}] Setting up direct connection for domain: ${serverName}`);
} else {
console.log(`[${connectionId}] Setting up direct connection without SNI`);
}
// Log domain config details to help diagnose routing issues
if (domainConfig) {
console.log(`[${connectionId}] Using domain config: ${domainConfig.domains.join(', ')}`);
} else {
console.log(`[${connectionId}] No specific domain config found, using default settings`);
}
// Ensure we maximize connection chances by setting appropriate timeouts
socket.setTimeout(30000); // 30 second initial connect timeout
// Existing connection setup logic // Existing connection setup logic
const targetHost = domainConfig ? this.getTargetIP(domainConfig) : this.settings.targetIP!; const targetHost = domainConfig ? this.getTargetIP(domainConfig) : this.settings.targetIP!;
const connectionOptions: plugins.net.NetConnectOpts = { const connectionOptions: plugins.net.NetConnectOpts = {
host: targetHost, host: targetHost,
port: overridePort !== undefined ? overridePort : this.settings.toPort, port: overridePort !== undefined ? overridePort : this.settings.toPort,
// Add connection timeout to ensure we don't hang indefinitely
timeout: 15000 // 15 second connection timeout
}; };
if (this.settings.preserveSourceIP) { if (this.settings.preserveSourceIP) {
connectionOptions.localAddress = record.remoteIP.replace('::ffff:', ''); connectionOptions.localAddress = record.remoteIP.replace('::ffff:', '');
} }
console.log(`[${connectionId}] Connecting to backend: ${targetHost}:${connectionOptions.port}`);
// Pause the incoming socket to prevent buffer overflows // Pause the incoming socket to prevent buffer overflows
socket.pause(); socket.pause();
@ -585,11 +804,22 @@ export class PortProxy {
// Add the temp handler to capture all incoming data during connection setup // Add the temp handler to capture all incoming data during connection setup
socket.on('data', tempDataHandler); socket.on('data', tempDataHandler);
// Add initial chunk to pending data if present // Add initial chunk to pending data if present - this is critical for SNI forwarding
if (initialChunk) { if (initialChunk) {
record.bytesReceived += initialChunk.length; // Make explicit copy of the buffer to ensure it doesn't get modified
record.pendingData.push(Buffer.from(initialChunk)); const initialDataCopy = Buffer.from(initialChunk);
record.pendingDataSize = initialChunk.length; record.bytesReceived += initialDataCopy.length;
record.pendingData.push(initialDataCopy);
record.pendingDataSize = initialDataCopy.length;
// Log TLS handshake for debug purposes
if (isTlsHandshake(initialChunk)) {
record.isTLS = true;
console.log(`[${connectionId}] Buffered TLS handshake data: ${initialDataCopy.length} bytes, SNI: ${serverName || 'none'}`);
}
} else if (record.isTLS) {
// This shouldn't happen, but log a warning if we have a TLS connection with no initial data
console.log(`[${connectionId}] WARNING: TLS connection without initial handshake data`);
} }
// Create the target socket but don't set up piping immediately // Create the target socket but don't set up piping immediately
@ -624,7 +854,7 @@ export class PortProxy {
} }
} }
// Setup specific error handler for connection phase // Setup specific error handler for connection phase with enhanced retries
targetSocket.once('error', (err) => { targetSocket.once('error', (err) => {
// This handler runs only once during the initial connection phase // This handler runs only once during the initial connection phase
const code = (err as any).code; const code = (err as any).code;
@ -635,6 +865,7 @@ export class PortProxy {
// Resume the incoming socket to prevent it from hanging // Resume the incoming socket to prevent it from hanging
socket.resume(); socket.resume();
// Add detailed logging for connection problems
if (code === 'ECONNREFUSED') { if (code === 'ECONNREFUSED') {
console.log( console.log(
`[${connectionId}] Target ${targetHost}:${connectionOptions.port} refused connection` `[${connectionId}] Target ${targetHost}:${connectionOptions.port} refused connection`
@ -650,6 +881,28 @@ export class PortProxy {
} else if (code === 'EHOSTUNREACH') { } else if (code === 'EHOSTUNREACH') {
console.log(`[${connectionId}] Host ${targetHost} is unreachable`); console.log(`[${connectionId}] Host ${targetHost} is unreachable`);
} }
// Log additional diagnostics
console.log(`[${connectionId}] Connection details - SNI: ${serverName || 'none'}, HasChunk: ${!!initialChunk}, ChunkSize: ${initialChunk ? initialChunk.length : 0}`);
// For TLS connections, provide even more detailed diagnostics
if (record.isTLS) {
console.log(`[${connectionId}] TLS connection failure details - TLS detected: ${record.isTLS}, Server: ${targetHost}:${connectionOptions.port}, Domain config: ${domainConfig ? 'Present' : 'Missing'}`);
}
// For connection refusal or timeouts, try a more aggressive error response
// This helps browsers quickly realize there's an issue rather than waiting
if (code === 'ECONNREFUSED' || code === 'ETIMEDOUT' || code === 'EHOSTUNREACH') {
try {
// Send a RST packet rather than a graceful close
// This signals to browsers to try a new connection immediately
socket.destroy(new Error(`Backend connection failed: ${code}`));
console.log(`[${connectionId}] Forced connection termination to trigger immediate browser retry`);
return; // Skip normal cleanup
} catch (destroyErr) {
console.log(`[${connectionId}] Error during forced connection termination: ${destroyErr}`);
}
}
// Clear any existing error handler after connection phase // Clear any existing error handler after connection phase
targetSocket.removeAllListeners('error'); targetSocket.removeAllListeners('error');
@ -762,12 +1015,105 @@ export class PortProxy {
// Flush all pending data to target // Flush all pending data to target
if (record.pendingData.length > 0) { if (record.pendingData.length > 0) {
const combinedData = Buffer.concat(record.pendingData); const combinedData = Buffer.concat(record.pendingData);
// Add critical debugging for SNI forwarding issues
if (record.isTLS && this.settings.enableTlsDebugLogging) {
console.log(`[${connectionId}] Forwarding TLS handshake data: ${combinedData.length} bytes, SNI: ${serverName || 'none'}`);
// Additional check to verify we're forwarding the ClientHello properly
if (combinedData[0] === 22) { // TLS handshake
console.log(`[${connectionId}] Initial data is a TLS handshake record`);
}
}
// Write the combined data to the target
targetSocket.write(combinedData, (err) => { targetSocket.write(combinedData, (err) => {
if (err) { if (err) {
console.log(`[${connectionId}] Error writing pending data to target: ${err.message}`); console.log(`[${connectionId}] Error writing pending data to target: ${err.message}`);
return this.initiateCleanupOnce(record, 'write_error'); return this.initiateCleanupOnce(record, 'write_error');
} }
if (record.isTLS) {
// Log successful forwarding of initial TLS data
console.log(`[${connectionId}] Successfully forwarded initial TLS data to backend`);
}
// Set up the renegotiation listener *before* piping if this is a TLS connection with SNI
if (serverName && record.isTLS) {
// This listener handles TLS renegotiation detection
socket.on('data', (renegChunk) => {
if (renegChunk.length > 0 && renegChunk.readUInt8(0) === 22) {
// Always update activity timestamp for any handshake packet
this.updateActivity(record);
try {
// Extract all TLS information including session resumption data
const sniInfo = extractSNIInfo(renegChunk, this.settings.enableTlsDebugLogging);
let newSNI = sniInfo?.serverName;
// Handle session resumption - if we recognize the session ID, we know what domain it belongs to
if (sniInfo?.isResumption && sniInfo.resumedDomain) {
console.log(`[${connectionId}] Rehandshake with session resumption for domain: ${sniInfo.resumedDomain}`);
newSNI = sniInfo.resumedDomain;
}
// IMPORTANT: If we can't extract an SNI from renegotiation, we MUST allow it through
if (newSNI === undefined) {
console.log(`[${connectionId}] Rehandshake detected without SNI, allowing it through.`);
return;
}
// Check if the SNI has changed
if (newSNI !== serverName) {
console.log(`[${connectionId}] Rehandshake with different SNI: ${newSNI} vs original ${serverName}`);
// Allow if the new SNI matches existing domain config or find a new matching config
let allowed = false;
if (record.domainConfig) {
allowed = record.domainConfig.domains.some(d => plugins.minimatch(newSNI, d));
}
if (!allowed) {
const newDomainConfig = this.settings.domainConfigs.find((config) =>
config.domains.some((d) => plugins.minimatch(newSNI, d))
);
if (newDomainConfig) {
const effectiveAllowedIPs = [
...newDomainConfig.allowedIPs,
...(this.settings.defaultAllowedIPs || []),
];
const effectiveBlockedIPs = [
...(newDomainConfig.blockedIPs || []),
...(this.settings.defaultBlockedIPs || []),
];
allowed = isGlobIPAllowed(record.remoteIP, effectiveAllowedIPs, effectiveBlockedIPs);
if (allowed) {
record.domainConfig = newDomainConfig;
}
}
}
if (allowed) {
console.log(`[${connectionId}] Updated domain for connection from ${record.remoteIP} to: ${newSNI}`);
record.lockedDomain = newSNI;
} else {
console.log(`[${connectionId}] Rehandshake SNI ${newSNI} not allowed. Terminating connection.`);
this.initiateCleanupOnce(record, 'sni_mismatch');
}
} else {
console.log(`[${connectionId}] Rehandshake with same SNI: ${newSNI}`);
}
} catch (err) {
console.log(`[${connectionId}] Error processing renegotiation: ${err}. Allowing to continue.`);
}
}
});
}
// Now set up piping for future data and resume the socket // Now set up piping for future data and resume the socket
socket.pipe(targetSocket); socket.pipe(targetSocket);
targetSocket.pipe(socket); targetSocket.pipe(socket);
@ -801,7 +1147,83 @@ export class PortProxy {
} }
}); });
} else { } else {
// No pending data, so just set up piping // Set up the renegotiation listener *before* piping if this is a TLS connection with SNI
if (serverName && record.isTLS) {
// This listener handles TLS renegotiation detection
socket.on('data', (renegChunk) => {
if (renegChunk.length > 0 && renegChunk.readUInt8(0) === 22) {
// Always update activity timestamp for any handshake packet
this.updateActivity(record);
try {
// Extract all TLS information including session resumption data
const sniInfo = extractSNIInfo(renegChunk, this.settings.enableTlsDebugLogging);
let newSNI = sniInfo?.serverName;
// Handle session resumption - if we recognize the session ID, we know what domain it belongs to
if (sniInfo?.isResumption && sniInfo.resumedDomain) {
console.log(`[${connectionId}] Rehandshake with session resumption for domain: ${sniInfo.resumedDomain}`);
newSNI = sniInfo.resumedDomain;
}
// IMPORTANT: If we can't extract an SNI from renegotiation, we MUST allow it through
if (newSNI === undefined) {
console.log(`[${connectionId}] Rehandshake detected without SNI, allowing it through.`);
return;
}
// Check if the SNI has changed
if (newSNI !== serverName) {
console.log(`[${connectionId}] Rehandshake with different SNI: ${newSNI} vs original ${serverName}`);
// Allow if the new SNI matches existing domain config or find a new matching config
let allowed = false;
if (record.domainConfig) {
allowed = record.domainConfig.domains.some(d => plugins.minimatch(newSNI, d));
}
if (!allowed) {
const newDomainConfig = this.settings.domainConfigs.find((config) =>
config.domains.some((d) => plugins.minimatch(newSNI, d))
);
if (newDomainConfig) {
const effectiveAllowedIPs = [
...newDomainConfig.allowedIPs,
...(this.settings.defaultAllowedIPs || []),
];
const effectiveBlockedIPs = [
...(newDomainConfig.blockedIPs || []),
...(this.settings.defaultBlockedIPs || []),
];
allowed = isGlobIPAllowed(record.remoteIP, effectiveAllowedIPs, effectiveBlockedIPs);
if (allowed) {
record.domainConfig = newDomainConfig;
}
}
}
if (allowed) {
console.log(`[${connectionId}] Updated domain for connection from ${record.remoteIP} to: ${newSNI}`);
record.lockedDomain = newSNI;
} else {
console.log(`[${connectionId}] Rehandshake SNI ${newSNI} not allowed. Terminating connection.`);
this.initiateCleanupOnce(record, 'sni_mismatch');
}
} else {
console.log(`[${connectionId}] Rehandshake with same SNI: ${newSNI}`);
}
} catch (err) {
console.log(`[${connectionId}] Error processing renegotiation: ${err}. Allowing to continue.`);
}
}
});
}
// Now set up piping
socket.pipe(targetSocket); socket.pipe(targetSocket);
targetSocket.pipe(socket); targetSocket.pipe(socket);
socket.resume(); // Resume the socket after piping is established socket.resume(); // Resume the socket after piping is established
@ -838,31 +1260,8 @@ export class PortProxy {
record.pendingData = []; record.pendingData = [];
record.pendingDataSize = 0; record.pendingDataSize = 0;
// Add the renegotiation listener for SNI validation // Renegotiation detection is now handled before piping is established
if (serverName) { // This ensures the data listener receives all packets properly
socket.on('data', (renegChunk: Buffer) => {
if (renegChunk.length > 0 && renegChunk.readUInt8(0) === 22) {
try {
// Try to extract SNI from potential renegotiation
const newSNI = extractSNI(renegChunk, this.settings.enableTlsDebugLogging);
if (newSNI && newSNI !== record.lockedDomain) {
console.log(
`[${connectionId}] Rehandshake detected with different SNI: ${newSNI} vs locked ${record.lockedDomain}. Terminating connection.`
);
this.initiateCleanupOnce(record, 'sni_mismatch');
} else if (newSNI && this.settings.enableDetailedLogging) {
console.log(
`[${connectionId}] Rehandshake detected with same SNI: ${newSNI}. Allowing.`
);
}
} catch (err) {
console.log(
`[${connectionId}] Error processing potential renegotiation: ${err}. Allowing connection to continue.`
);
}
}
});
}
// Set connection timeout with simpler logic // Set connection timeout with simpler logic
if (record.cleanupTimer) { if (record.cleanupTimer) {
@ -878,22 +1277,23 @@ export class PortProxy {
} }
// No cleanup timer for immortal connections // No cleanup timer for immortal connections
} }
// For TLS keep-alive connections, use a very extended timeout // For TLS keep-alive connections, use a more generous timeout now that
// we've fixed the renegotiation handling issue that was causing certificate problems
else if (record.hasKeepAlive && record.isTLS) { else if (record.hasKeepAlive && record.isTLS) {
// For TLS keep-alive connections, use a very extended timeout // Use a longer timeout for TLS connections now that renegotiation handling is fixed
// This helps prevent certificate errors after sleep/wake cycles // This reduces unnecessary reconnections while still ensuring certificate freshness
const tlsKeepAliveTimeout = 14 * 24 * 60 * 60 * 1000; // 14 days for TLS keep-alive const tlsKeepAliveTimeout = 4 * 60 * 60 * 1000; // 4 hours for TLS keep-alive - increased from 30 minutes
const safeTimeout = ensureSafeTimeout(tlsKeepAliveTimeout); const safeTimeout = ensureSafeTimeout(tlsKeepAliveTimeout);
record.cleanupTimer = setTimeout(() => { record.cleanupTimer = setTimeout(() => {
console.log( console.log(
`[${connectionId}] TLS keep-alive connection from ${ `[${connectionId}] TLS keep-alive connection from ${
record.remoteIP record.remoteIP
} exceeded extended lifetime (${plugins.prettyMs( } exceeded max lifetime (${plugins.prettyMs(
tlsKeepAliveTimeout tlsKeepAliveTimeout
)}), forcing cleanup.` )}), forcing cleanup to refresh certificate context.`
); );
this.initiateCleanupOnce(record, 'tls_extended_lifetime'); this.initiateCleanupOnce(record, 'tls_certificate_refresh');
}, safeTimeout); }, safeTimeout);
// Make sure timeout doesn't keep the process alive // Make sure timeout doesn't keep the process alive
@ -903,7 +1303,7 @@ export class PortProxy {
if (this.settings.enableDetailedLogging) { if (this.settings.enableDetailedLogging) {
console.log( console.log(
`[${connectionId}] TLS keep-alive connection with enhanced protection, lifetime: ${plugins.prettyMs( `[${connectionId}] TLS keep-alive connection with aggressive certificate refresh protection, lifetime: ${plugins.prettyMs(
tlsKeepAliveTimeout tlsKeepAliveTimeout
)}` )}`
); );
@ -1053,15 +1453,41 @@ export class PortProxy {
// For TLS keep-alive connections after sleep/long inactivity, force close // For TLS keep-alive connections after sleep/long inactivity, force close
// to make browser establish a new connection with fresh certificate context // to make browser establish a new connection with fresh certificate context
if (record.isTLS && record.tlsHandshakeComplete) { if (record.isTLS && record.tlsHandshakeComplete) {
if (timeDiff > 4 * 60 * 60 * 1000) { // More generous timeout now that we've fixed the renegotiation handling
// If inactive for more than 4 hours if (timeDiff > 2 * 60 * 60 * 1000) {
// If inactive for more than 2 hours (increased from 20 minutes)
console.log( console.log(
`[${record.id}] TLS connection inactive for ${plugins.prettyMs(timeDiff)}. ` + `[${record.id}] TLS connection inactive for ${plugins.prettyMs(timeDiff)}. ` +
`Closing to force new connection with fresh certificate.` `Closing to force new connection with fresh certificate.`
); );
return this.initiateCleanupOnce(record, 'certificate_refresh_needed'); return this.initiateCleanupOnce(record, 'certificate_refresh_needed');
} else if (timeDiff > 30 * 60 * 1000) {
// For shorter but still significant inactivity (30+ minutes), refresh TLS state
console.log(
`[${record.id}] TLS connection inactive for ${plugins.prettyMs(timeDiff)}. ` +
`Refreshing TLS state.`
);
this.refreshTlsStateAfterSleep(record);
// Add an additional check in 15 minutes if no activity
const refreshCheckId = record.id;
const refreshCheck = setTimeout(() => {
const currentRecord = this.connectionRecords.get(refreshCheckId);
if (currentRecord && Date.now() - currentRecord.lastActivity > 15 * 60 * 1000) {
console.log(
`[${refreshCheckId}] No activity detected after TLS refresh. ` +
`Closing connection to ensure certificate freshness.`
);
this.initiateCleanupOnce(currentRecord, 'tls_refresh_verification_failed');
}
}, 15 * 60 * 1000);
// Make sure timeout doesn't keep the process alive
if (refreshCheck.unref) {
refreshCheck.unref();
}
} else { } else {
// For shorter inactivity periods, try to refresh the TLS state // For shorter inactivity periods, try to refresh the TLS state normally
this.refreshTlsStateAfterSleep(record); this.refreshTlsStateAfterSleep(record);
} }
} }
@ -1097,12 +1523,12 @@ export class PortProxy {
const connectionAge = Date.now() - record.incomingStartTime; const connectionAge = Date.now() - record.incomingStartTime;
const hourInMs = 60 * 60 * 1000; const hourInMs = 60 * 60 * 1000;
// For TLS browser connections that are very old, it's better to force a new connection // For TLS browser connections, use a more generous timeout now that
// rather than trying to refresh the state, to avoid certificate issues // we've fixed the renegotiation handling issues
if (record.isTLS && record.hasKeepAlive && connectionAge > 12 * hourInMs) { if (record.isTLS && record.hasKeepAlive && connectionAge > 8 * hourInMs) { // 8 hours instead of 45 minutes
console.log( console.log(
`[${record.id}] Long-lived TLS connection (${plugins.prettyMs(connectionAge)}). ` + `[${record.id}] Long-lived TLS connection (${plugins.prettyMs(connectionAge)}). ` +
`Closing to ensure proper certificate handling on browser reconnect.` `Closing to ensure proper certificate handling on browser reconnect in proxy chain.`
); );
return this.initiateCleanupOnce(record, 'certificate_context_refresh'); return this.initiateCleanupOnce(record, 'certificate_context_refresh');
} }
@ -1555,16 +1981,47 @@ export class PortProxy {
} }
// If a forcedDomain is provided (port-based routing), use it; otherwise, use SNI-based lookup. // If a forcedDomain is provided (port-based routing), use it; otherwise, use SNI-based lookup.
const domainConfig = forcedDomain let domainConfig = forcedDomain
? forcedDomain ? forcedDomain
: serverName : serverName
? this.settings.domainConfigs.find((config) => ? this.settings.domainConfigs.find((config) =>
config.domains.some((d) => plugins.minimatch(serverName, d)) config.domains.some((d) => plugins.minimatch(serverName, d))
) )
: undefined; : undefined;
// Enhanced logging to diagnose domain config selection issues
if (serverName && !domainConfig) {
console.log(`[${connectionId}] WARNING: No domain config found for SNI: ${serverName}`);
console.log(`[${connectionId}] Available domains:`,
this.settings.domainConfigs.map(config => config.domains.join(',')).join(' | '));
} else if (serverName && domainConfig) {
console.log(`[${connectionId}] Found domain config for SNI: ${serverName} -> ${domainConfig.domains.join(',')}`);
}
// For session resumption, ensure we use the domain config matching the resumed domain
// The resumed domain will be in serverName if this is a session resumption
if (serverName && connectionRecord.lockedDomain === serverName && serverName !== '') {
// Override domain config lookup for session resumption - crucial for certificate selection
const resumedDomainConfig = this.settings.domainConfigs.find((config) =>
config.domains.some((d) => plugins.minimatch(serverName, d))
);
if (resumedDomainConfig) {
domainConfig = resumedDomainConfig;
console.log(`[${connectionId}] Using domain config for resumed session: ${serverName}`);
} else {
console.log(`[${connectionId}] WARNING: Cannot find domain config for resumed domain: ${serverName}`);
}
}
// Save domain config in connection record // Save domain config in connection record
connectionRecord.domainConfig = domainConfig; connectionRecord.domainConfig = domainConfig;
// Always set the lockedDomain, even for non-SNI connections
if (serverName) {
connectionRecord.lockedDomain = serverName;
console.log(`[${connectionId}] Locked connection to domain: ${serverName}`);
}
// IP validation is skipped if allowedIPs is empty // IP validation is skipped if allowedIPs is empty
if (domainConfig) { if (domainConfig) {
@ -1721,19 +2178,49 @@ export class PortProxy {
initialDataReceived = true; initialDataReceived = true;
// Try to extract SNI // Try to extract SNI - with enhanced logging for troubleshooting
let serverName = ''; let serverName = '';
// Record the chunk size for diagnostic purposes
console.log(`[${connectionId}] Received initial data: ${chunk.length} bytes`);
if (isTlsHandshake(chunk)) { if (isTlsHandshake(chunk)) {
connectionRecord.isTLS = true; connectionRecord.isTLS = true;
console.log(`[${connectionId}] Detected TLS handshake`);
if (this.settings.enableTlsDebugLogging) { if (this.settings.enableTlsDebugLogging) {
console.log( console.log(
`[${connectionId}] Extracting SNI from TLS handshake, ${chunk.length} bytes` `[${connectionId}] Extracting SNI from TLS handshake, ${chunk.length} bytes`
); );
} }
serverName = extractSNI(chunk, this.settings.enableTlsDebugLogging) || ''; // Extract all TLS information including session resumption
const sniInfo = extractSNIInfo(chunk, this.settings.enableTlsDebugLogging);
if (sniInfo?.isResumption && sniInfo.resumedDomain) {
// This is a session resumption with a known domain
serverName = sniInfo.resumedDomain;
console.log(`[${connectionId}] TLS Session resumption detected for domain: ${serverName}`);
// When resuming a session, explicitly set the domain in the record to ensure proper routing
// This is CRITICAL for ensuring we select the correct backend/certificate
connectionRecord.lockedDomain = serverName;
// Force detailed logging for resumed sessions to help with troubleshooting
console.log(`[${connectionId}] Resuming TLS session for domain ${serverName} - will use original certificate`);
} else {
// Normal SNI extraction
serverName = sniInfo?.serverName || '';
if (serverName) {
console.log(`[${connectionId}] Extracted SNI domain: ${serverName}`);
} else {
console.log(`[${connectionId}] No SNI found in TLS handshake`);
}
}
} else {
console.log(`[${connectionId}] Non-TLS connection detected`);
} }
// Lock the connection to the negotiated SNI. // Lock the connection to the negotiated SNI.
@ -2049,6 +2536,9 @@ export class PortProxy {
public async stop() { public async stop() {
console.log('PortProxy shutting down...'); console.log('PortProxy shutting down...');
this.isShuttingDown = true; this.isShuttingDown = true;
// Stop the session cleanup timer
stopSessionCleanupTimer();
// Stop accepting new connections // Stop accepting new connections
const closeServerPromises: Promise<void>[] = this.netServers.map( const closeServerPromises: Promise<void>[] = this.netServers.map(