|
|
|
@ -62,6 +62,11 @@ export interface IPortProxySettings extends plugins.tls.TlsOptions {
|
|
|
|
|
|
|
|
|
|
// New property for NetworkProxy integration
|
|
|
|
|
networkProxies?: NetworkProxy[]; // Array of NetworkProxy instances to use for TLS termination
|
|
|
|
|
|
|
|
|
|
// Browser optimization settings
|
|
|
|
|
browserFriendlyMode?: boolean; // Optimizes handling for browser connections
|
|
|
|
|
allowRenegotiationWithDifferentSNI?: boolean; // Allows SNI changes during renegotiation
|
|
|
|
|
relatedDomainPatterns?: string[][]; // Patterns for domains that should be allowed to share connections
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -101,9 +106,12 @@ interface IConnectionRecord {
|
|
|
|
|
usingNetworkProxy?: boolean; // Whether this connection is using a NetworkProxy
|
|
|
|
|
networkProxyIndex?: number; // Which NetworkProxy instance is being used
|
|
|
|
|
|
|
|
|
|
// Sleep detection fields
|
|
|
|
|
possibleSystemSleep?: boolean; // Flag to indicate a possible system sleep was detected
|
|
|
|
|
lastSleepDetection?: number; // Timestamp of the last sleep detection
|
|
|
|
|
// New field for renegotiation handler
|
|
|
|
|
renegotiationHandler?: (chunk: Buffer) => void; // Handler for renegotiation detection
|
|
|
|
|
|
|
|
|
|
// Browser connection tracking
|
|
|
|
|
isBrowserConnection?: boolean; // Whether this connection appears to be from a browser
|
|
|
|
|
domainSwitches?: number; // Number of times the domain has been switched on this connection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -270,6 +278,58 @@ function extractSNI(buffer: Buffer, enableLogging: boolean = false): string | un
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if a TLS record is a proper ClientHello message (more accurate than just checking record type)
|
|
|
|
|
* @param buffer - Buffer containing the TLS record
|
|
|
|
|
* @returns true if the buffer contains a proper ClientHello message
|
|
|
|
|
*/
|
|
|
|
|
function isClientHello(buffer: Buffer): boolean {
|
|
|
|
|
try {
|
|
|
|
|
if (buffer.length < 9) return false; // Too small for a proper ClientHello
|
|
|
|
|
|
|
|
|
|
// Check record type (has to be handshake - 22)
|
|
|
|
|
if (buffer.readUInt8(0) !== 22) return false;
|
|
|
|
|
|
|
|
|
|
// After the TLS record header (5 bytes), check the handshake type (1 for ClientHello)
|
|
|
|
|
if (buffer.readUInt8(5) !== 1) return false;
|
|
|
|
|
|
|
|
|
|
// Basic checks passed, this appears to be a ClientHello
|
|
|
|
|
return true;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(`Error checking for ClientHello: ${err}`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if two domains are related based on configured patterns
|
|
|
|
|
* @param domain1 - First domain name
|
|
|
|
|
* @param domain2 - Second domain name
|
|
|
|
|
* @param relatedPatterns - Array of domain pattern groups where domains in the same group are considered related
|
|
|
|
|
* @returns true if domains are related, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
function areDomainsRelated(
|
|
|
|
|
domain1: string,
|
|
|
|
|
domain2: string,
|
|
|
|
|
relatedPatterns?: string[][]
|
|
|
|
|
): boolean {
|
|
|
|
|
// Only exact same domains or empty domains are automatically related
|
|
|
|
|
if (!domain1 || !domain2 || domain1 === domain2) return true;
|
|
|
|
|
|
|
|
|
|
// Check against configured related domain patterns - the ONLY source of truth
|
|
|
|
|
if (relatedPatterns && relatedPatterns.length > 0) {
|
|
|
|
|
for (const patternGroup of relatedPatterns) {
|
|
|
|
|
const domain1Matches = patternGroup.some((pattern) => plugins.minimatch(domain1, pattern));
|
|
|
|
|
const domain2Matches = patternGroup.some((pattern) => plugins.minimatch(domain2, pattern));
|
|
|
|
|
|
|
|
|
|
if (domain1Matches && domain2Matches) return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no patterns match, domains are not related
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper: Check if a port falls within any of the given port ranges
|
|
|
|
|
const isPortInRanges = (port: number, ranges: Array<{ from: number; to: number }>): boolean => {
|
|
|
|
|
return ranges.some((range) => port >= range.from && port <= range.to);
|
|
|
|
@ -379,8 +439,8 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
// Feature flags
|
|
|
|
|
disableInactivityCheck: settingsArg.disableInactivityCheck || false,
|
|
|
|
|
enableKeepAliveProbes: settingsArg.enableKeepAliveProbes !== undefined
|
|
|
|
|
? settingsArg.enableKeepAliveProbes : true, // Enable by default
|
|
|
|
|
enableKeepAliveProbes:
|
|
|
|
|
settingsArg.enableKeepAliveProbes !== undefined ? settingsArg.enableKeepAliveProbes : true, // Enable by default
|
|
|
|
|
enableDetailedLogging: settingsArg.enableDetailedLogging || false,
|
|
|
|
|
enableTlsDebugLogging: settingsArg.enableTlsDebugLogging || false,
|
|
|
|
|
enableRandomizedTimeouts: settingsArg.enableRandomizedTimeouts || false, // Disable randomization by default
|
|
|
|
@ -393,6 +453,11 @@ export class PortProxy {
|
|
|
|
|
keepAliveTreatment: settingsArg.keepAliveTreatment || 'extended', // Extended by default
|
|
|
|
|
keepAliveInactivityMultiplier: settingsArg.keepAliveInactivityMultiplier || 6, // 6x normal inactivity timeout
|
|
|
|
|
extendedKeepAliveLifetime: settingsArg.extendedKeepAliveLifetime || 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
|
|
|
|
|
|
|
|
// Browser optimization settings (new)
|
|
|
|
|
browserFriendlyMode: settingsArg.browserFriendlyMode || true, // On by default
|
|
|
|
|
allowRenegotiationWithDifferentSNI: settingsArg.allowRenegotiationWithDifferentSNI || false, // Off by default
|
|
|
|
|
relatedDomainPatterns: settingsArg.relatedDomainPatterns || [], // Empty by default
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Store NetworkProxy instances if provided
|
|
|
|
@ -417,15 +482,23 @@ export class PortProxy {
|
|
|
|
|
serverName?: string
|
|
|
|
|
): void {
|
|
|
|
|
// Determine which NetworkProxy to use
|
|
|
|
|
const proxyIndex = domainConfig.networkProxyIndex !== undefined
|
|
|
|
|
? domainConfig.networkProxyIndex
|
|
|
|
|
: 0;
|
|
|
|
|
const proxyIndex =
|
|
|
|
|
domainConfig.networkProxyIndex !== undefined ? domainConfig.networkProxyIndex : 0;
|
|
|
|
|
|
|
|
|
|
// Validate the NetworkProxy index
|
|
|
|
|
if (proxyIndex < 0 || proxyIndex >= this.networkProxies.length) {
|
|
|
|
|
console.log(`[${connectionId}] Invalid NetworkProxy index: ${proxyIndex}. Using fallback direct connection.`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Invalid NetworkProxy index: ${proxyIndex}. Using fallback direct connection.`
|
|
|
|
|
);
|
|
|
|
|
// Fall back to direct connection
|
|
|
|
|
return this.setupDirectConnection(connectionId, socket, record, domainConfig, serverName, initialData);
|
|
|
|
|
return this.setupDirectConnection(
|
|
|
|
|
connectionId,
|
|
|
|
|
socket,
|
|
|
|
|
record,
|
|
|
|
|
domainConfig,
|
|
|
|
|
serverName,
|
|
|
|
|
initialData
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const networkProxy = this.networkProxies[proxyIndex];
|
|
|
|
@ -441,7 +514,7 @@ export class PortProxy {
|
|
|
|
|
// Create a connection to the NetworkProxy
|
|
|
|
|
const proxySocket = plugins.net.connect({
|
|
|
|
|
host: proxyHost,
|
|
|
|
|
port: proxyPort
|
|
|
|
|
port: proxyPort,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Store the outgoing socket in the record
|
|
|
|
@ -479,27 +552,15 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
socket.on('close', () => {
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] Client connection closed after forwarding to NetworkProxy`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Client connection closed after forwarding to NetworkProxy`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
this.cleanupConnection(record, 'client_closed');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Update activity on data transfer
|
|
|
|
|
socket.on('data', (chunk: Buffer) => {
|
|
|
|
|
this.updateActivity(record);
|
|
|
|
|
|
|
|
|
|
// Check for potential TLS renegotiation or reconnection packets
|
|
|
|
|
if (chunk.length > 0 && chunk[0] === 22) { // ContentType.handshake
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('data', () => this.updateActivity(record));
|
|
|
|
|
proxySocket.on('data', () => this.updateActivity(record));
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
@ -603,7 +664,9 @@ export class PortProxy {
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Ignore errors - these are optional enhancements
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] Enhanced TCP keep-alive not supported for outgoing socket: ${err}`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Enhanced TCP keep-alive not supported for outgoing socket: ${err}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -660,7 +723,9 @@ export class PortProxy {
|
|
|
|
|
// For keep-alive connections, just log a warning instead of closing
|
|
|
|
|
if (record.hasKeepAlive) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Timeout event on incoming keep-alive connection from ${record.remoteIP} after ${plugins.prettyMs(
|
|
|
|
|
`[${connectionId}] Timeout event on incoming keep-alive connection from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} after ${plugins.prettyMs(
|
|
|
|
|
this.settings.socketTimeout || 3600000
|
|
|
|
|
)}. Connection preserved.`
|
|
|
|
|
);
|
|
|
|
@ -670,9 +735,9 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
// For non-keep-alive connections, proceed with normal cleanup
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Timeout on incoming side from ${record.remoteIP} after ${plugins.prettyMs(
|
|
|
|
|
this.settings.socketTimeout || 3600000
|
|
|
|
|
)}`
|
|
|
|
|
`[${connectionId}] Timeout on incoming side from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} after ${plugins.prettyMs(this.settings.socketTimeout || 3600000)}`
|
|
|
|
|
);
|
|
|
|
|
if (record.incomingTerminationReason === null) {
|
|
|
|
|
record.incomingTerminationReason = 'timeout';
|
|
|
|
@ -685,7 +750,9 @@ export class PortProxy {
|
|
|
|
|
// For keep-alive connections, just log a warning instead of closing
|
|
|
|
|
if (record.hasKeepAlive) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Timeout event on outgoing keep-alive connection from ${record.remoteIP} after ${plugins.prettyMs(
|
|
|
|
|
`[${connectionId}] Timeout event on outgoing keep-alive connection from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} after ${plugins.prettyMs(
|
|
|
|
|
this.settings.socketTimeout || 3600000
|
|
|
|
|
)}. Connection preserved.`
|
|
|
|
|
);
|
|
|
|
@ -695,9 +762,9 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
// For non-keep-alive connections, proceed with normal cleanup
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Timeout on outgoing side from ${record.remoteIP} after ${plugins.prettyMs(
|
|
|
|
|
this.settings.socketTimeout || 3600000
|
|
|
|
|
)}`
|
|
|
|
|
`[${connectionId}] Timeout on outgoing side from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} after ${plugins.prettyMs(this.settings.socketTimeout || 3600000)}`
|
|
|
|
|
);
|
|
|
|
|
if (record.outgoingTerminationReason === null) {
|
|
|
|
|
record.outgoingTerminationReason = 'timeout';
|
|
|
|
@ -713,7 +780,9 @@ export class PortProxy {
|
|
|
|
|
targetSocket.setTimeout(0);
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] Disabled socket timeouts for immortal keep-alive connection`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Disabled socket timeouts for immortal keep-alive connection`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Set normal timeouts for other connections
|
|
|
|
@ -743,9 +812,7 @@ export class PortProxy {
|
|
|
|
|
const combinedData = Buffer.concat(record.pendingData);
|
|
|
|
|
targetSocket.write(combinedData, (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');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -764,7 +831,9 @@ export class PortProxy {
|
|
|
|
|
? ` (Port-based for domain: ${domainConfig.domains.join(', ')})`
|
|
|
|
|
: ''
|
|
|
|
|
}` +
|
|
|
|
|
` TLS: ${record.isTLS ? 'Yes' : 'No'}, Keep-Alive: ${record.hasKeepAlive ? 'Yes' : 'No'}`
|
|
|
|
|
` TLS: ${record.isTLS ? 'Yes' : 'No'}, Keep-Alive: ${
|
|
|
|
|
record.hasKeepAlive ? 'Yes' : 'No'
|
|
|
|
|
}`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(
|
|
|
|
@ -795,7 +864,9 @@ export class PortProxy {
|
|
|
|
|
? ` (Port-based for domain: ${domainConfig.domains.join(', ')})`
|
|
|
|
|
: ''
|
|
|
|
|
}` +
|
|
|
|
|
` TLS: ${record.isTLS ? 'Yes' : 'No'}, Keep-Alive: ${record.hasKeepAlive ? 'Yes' : 'No'}`
|
|
|
|
|
` TLS: ${record.isTLS ? 'Yes' : 'No'}, Keep-Alive: ${
|
|
|
|
|
record.hasKeepAlive ? 'Yes' : 'No'
|
|
|
|
|
}`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(
|
|
|
|
@ -815,30 +886,75 @@ export class PortProxy {
|
|
|
|
|
record.pendingData = [];
|
|
|
|
|
record.pendingDataSize = 0;
|
|
|
|
|
|
|
|
|
|
// Add the renegotiation listener for SNI validation
|
|
|
|
|
// Add the renegotiation handler for SNI validation, with browser-friendly improvements
|
|
|
|
|
if (serverName) {
|
|
|
|
|
socket.on('data', (renegChunk: Buffer) => {
|
|
|
|
|
if (renegChunk.length > 0 && renegChunk.readUInt8(0) === 22) {
|
|
|
|
|
// Define a handler for checking renegotiation with improved detection
|
|
|
|
|
const renegotiationHandler = (renegChunk: Buffer) => {
|
|
|
|
|
// Only process if this looks like a TLS ClientHello (more precise than just checking for type 22)
|
|
|
|
|
if (isClientHello(renegChunk)) {
|
|
|
|
|
try {
|
|
|
|
|
// Try to extract SNI from potential renegotiation
|
|
|
|
|
// Extract SNI from ClientHello
|
|
|
|
|
const newSNI = extractSNI(renegChunk, this.settings.enableTlsDebugLogging);
|
|
|
|
|
if (newSNI && newSNI !== record.lockedDomain) {
|
|
|
|
|
|
|
|
|
|
// Skip if no SNI was found
|
|
|
|
|
if (!newSNI) return;
|
|
|
|
|
|
|
|
|
|
// Handle SNI change during renegotiation
|
|
|
|
|
if (newSNI !== record.lockedDomain) {
|
|
|
|
|
// Track domain switches for browser connections
|
|
|
|
|
if (!record.domainSwitches) record.domainSwitches = 0;
|
|
|
|
|
record.domainSwitches++;
|
|
|
|
|
|
|
|
|
|
// Check if this is a normal behavior of browser connection reuse
|
|
|
|
|
const isRelatedDomain = areDomainsRelated(
|
|
|
|
|
newSNI,
|
|
|
|
|
record.lockedDomain || '',
|
|
|
|
|
this.settings.relatedDomainPatterns
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Decide how to handle the SNI change based on settings
|
|
|
|
|
if (this.settings.browserFriendlyMode && isRelatedDomain) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Rehandshake detected with different SNI: ${newSNI} vs locked ${record.lockedDomain}. Terminating connection.`
|
|
|
|
|
`[${connectionId}] Browser domain switch detected: ${record.lockedDomain} -> ${newSNI}. ` +
|
|
|
|
|
`Domains are related, allowing connection to continue (domain switch #${record.domainSwitches}).`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Update the locked domain to the new one
|
|
|
|
|
record.lockedDomain = newSNI;
|
|
|
|
|
} else if (this.settings.allowRenegotiationWithDifferentSNI) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Renegotiation with different SNI: ${record.lockedDomain} -> ${newSNI}. ` +
|
|
|
|
|
`Allowing due to allowRenegotiationWithDifferentSNI setting.`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Update the locked domain to the new one
|
|
|
|
|
record.lockedDomain = newSNI;
|
|
|
|
|
} else {
|
|
|
|
|
// Standard strict behavior - terminate connection on SNI mismatch
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Renegotiation with different SNI: ${record.lockedDomain} -> ${newSNI}. ` +
|
|
|
|
|
`Terminating connection. Enable browserFriendlyMode to allow this.`
|
|
|
|
|
);
|
|
|
|
|
this.initiateCleanupOnce(record, 'sni_mismatch');
|
|
|
|
|
} else if (newSNI && this.settings.enableDetailedLogging) {
|
|
|
|
|
}
|
|
|
|
|
} else if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Rehandshake detected with same SNI: ${newSNI}. Allowing.`
|
|
|
|
|
`[${connectionId}] Renegotiation detected with same SNI: ${newSNI}. Allowing.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Error processing potential renegotiation: ${err}. Allowing connection to continue.`
|
|
|
|
|
`[${connectionId}] Error processing ClientHello: ${err}. Allowing connection to continue.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Store the handler in the connection record so we can remove it during cleanup
|
|
|
|
|
record.renegotiationHandler = renegotiationHandler;
|
|
|
|
|
|
|
|
|
|
// Add the listener
|
|
|
|
|
socket.on('data', renegotiationHandler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set connection timeout with simpler logic
|
|
|
|
@ -849,35 +965,12 @@ export class PortProxy {
|
|
|
|
|
// For immortal keep-alive connections, skip setting a timeout completely
|
|
|
|
|
if (record.hasKeepAlive && this.settings.keepAliveTreatment === 'immortal') {
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] Keep-alive connection with immortal treatment - no max lifetime`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Keep-alive connection with immortal treatment - no max lifetime`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// No cleanup timer for immortal connections
|
|
|
|
|
}
|
|
|
|
|
// For TLS keep-alive connections, use a very extended timeout
|
|
|
|
|
else if (record.hasKeepAlive && record.isTLS) {
|
|
|
|
|
// For TLS keep-alive connections, use a very extended timeout
|
|
|
|
|
// This helps prevent certificate errors after sleep/wake cycles
|
|
|
|
|
const tlsKeepAliveTimeout = 14 * 24 * 60 * 60 * 1000; // 14 days for TLS keep-alive
|
|
|
|
|
const safeTimeout = ensureSafeTimeout(tlsKeepAliveTimeout);
|
|
|
|
|
|
|
|
|
|
record.cleanupTimer = setTimeout(() => {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] TLS keep-alive connection from ${record.remoteIP} exceeded extended lifetime (${plugins.prettyMs(
|
|
|
|
|
tlsKeepAliveTimeout
|
|
|
|
|
)}), forcing cleanup.`
|
|
|
|
|
);
|
|
|
|
|
this.initiateCleanupOnce(record, 'tls_extended_lifetime');
|
|
|
|
|
}, safeTimeout);
|
|
|
|
|
|
|
|
|
|
// Make sure timeout doesn't keep the process alive
|
|
|
|
|
if (record.cleanupTimer.unref) {
|
|
|
|
|
record.cleanupTimer.unref();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] TLS keep-alive connection with enhanced protection, lifetime: ${plugins.prettyMs(tlsKeepAliveTimeout)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// For extended keep-alive connections, use extended timeout
|
|
|
|
|
else if (record.hasKeepAlive && this.settings.keepAliveTreatment === 'extended') {
|
|
|
|
|
const extendedTimeout = this.settings.extendedKeepAliveLifetime || 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
|
|
@ -885,9 +978,9 @@ export class PortProxy {
|
|
|
|
|
|
|
|
|
|
record.cleanupTimer = setTimeout(() => {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Keep-alive connection from ${record.remoteIP} exceeded extended lifetime (${plugins.prettyMs(
|
|
|
|
|
extendedTimeout
|
|
|
|
|
)}), forcing cleanup.`
|
|
|
|
|
`[${connectionId}] Keep-alive connection from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} exceeded extended lifetime (${plugins.prettyMs(extendedTimeout)}), forcing cleanup.`
|
|
|
|
|
);
|
|
|
|
|
this.initiateCleanupOnce(record, 'extended_lifetime');
|
|
|
|
|
}, safeTimeout);
|
|
|
|
@ -898,20 +991,25 @@ export class PortProxy {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] Keep-alive connection with extended lifetime of ${plugins.prettyMs(extendedTimeout)}`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Keep-alive connection with extended lifetime of ${plugins.prettyMs(
|
|
|
|
|
extendedTimeout
|
|
|
|
|
)}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// For standard connections, use normal timeout
|
|
|
|
|
else {
|
|
|
|
|
// Use domain-specific timeout if available, otherwise use default
|
|
|
|
|
const connectionTimeout = record.domainConfig?.connectionTimeout || this.settings.maxConnectionLifetime!;
|
|
|
|
|
const connectionTimeout =
|
|
|
|
|
record.domainConfig?.connectionTimeout || this.settings.maxConnectionLifetime!;
|
|
|
|
|
const safeTimeout = ensureSafeTimeout(connectionTimeout);
|
|
|
|
|
|
|
|
|
|
record.cleanupTimer = setTimeout(() => {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Connection from ${record.remoteIP} exceeded max lifetime (${plugins.prettyMs(
|
|
|
|
|
connectionTimeout
|
|
|
|
|
)}), forcing cleanup.`
|
|
|
|
|
`[${connectionId}] Connection from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} exceeded max lifetime (${plugins.prettyMs(connectionTimeout)}), forcing cleanup.`
|
|
|
|
|
);
|
|
|
|
|
this.initiateCleanupOnce(record, 'connection_timeout');
|
|
|
|
|
}, safeTimeout);
|
|
|
|
@ -993,74 +1091,6 @@ export class PortProxy {
|
|
|
|
|
this.terminationStats[side][reason] = (this.terminationStats[side][reason] || 0) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update connection activity timestamp with sleep detection
|
|
|
|
|
*/
|
|
|
|
|
private updateActivity(record: IConnectionRecord): void {
|
|
|
|
|
// Get the current time
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
|
|
// Check if there was a large time gap that suggests system sleep
|
|
|
|
|
if (record.lastActivity > 0) {
|
|
|
|
|
const timeDiff = now - record.lastActivity;
|
|
|
|
|
|
|
|
|
|
// If time difference is very large (> 30 minutes) and this is a keep-alive connection,
|
|
|
|
|
// this might indicate system sleep rather than just inactivity
|
|
|
|
|
if (timeDiff > 30 * 60 * 1000 && record.hasKeepAlive) {
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${record.id}] Detected possible system sleep for ${plugins.prettyMs(timeDiff)}. ` +
|
|
|
|
|
`Preserving keep-alive connection.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For keep-alive connections after sleep, we should refresh the TLS state if needed
|
|
|
|
|
if (record.isTLS && record.tlsHandshakeComplete) {
|
|
|
|
|
this.refreshTlsStateAfterSleep(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mark that we detected sleep
|
|
|
|
|
record.possibleSystemSleep = true;
|
|
|
|
|
record.lastSleepDetection = now;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the activity timestamp
|
|
|
|
|
record.lastActivity = now;
|
|
|
|
|
|
|
|
|
|
// Clear any inactivity warning
|
|
|
|
|
if (record.inactivityWarningIssued) {
|
|
|
|
|
record.inactivityWarningIssued = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Refresh TLS state after sleep detection
|
|
|
|
|
*/
|
|
|
|
|
private refreshTlsStateAfterSleep(record: IConnectionRecord): void {
|
|
|
|
|
// Skip if we're using a NetworkProxy as it handles its own TLS state
|
|
|
|
|
if (record.usingNetworkProxy) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// For outgoing connections that might need to be refreshed
|
|
|
|
|
if (record.outgoing && !record.outgoing.destroyed) {
|
|
|
|
|
// Send a zero-byte packet to test the connection
|
|
|
|
|
record.outgoing.write(Buffer.alloc(0));
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${record.id}] Sent refresh packet after sleep detection`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(`[${record.id}] Error refreshing TLS state: ${err}`);
|
|
|
|
|
|
|
|
|
|
// If we can't refresh, don't terminate - client will re-establish if needed
|
|
|
|
|
// Just log the issue but preserve the connection
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cleans up a connection record.
|
|
|
|
|
* Destroys both incoming and outgoing sockets, clears timers, and removes the record.
|
|
|
|
@ -1084,6 +1114,16 @@ export class PortProxy {
|
|
|
|
|
const bytesReceived = record.bytesReceived;
|
|
|
|
|
const bytesSent = record.bytesSent;
|
|
|
|
|
|
|
|
|
|
// Remove the renegotiation handler if present
|
|
|
|
|
if (record.renegotiationHandler && record.incoming) {
|
|
|
|
|
try {
|
|
|
|
|
record.incoming.removeListener('data', record.renegotiationHandler);
|
|
|
|
|
record.renegotiationHandler = undefined;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(`[${record.id}] Error removing renegotiation handler: ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!record.incoming.destroyed) {
|
|
|
|
|
// Try graceful shutdown first, then force destroy after a short timeout
|
|
|
|
@ -1158,8 +1198,11 @@ export class PortProxy {
|
|
|
|
|
` Duration: ${plugins.prettyMs(
|
|
|
|
|
duration
|
|
|
|
|
)}, Bytes IN: ${bytesReceived}, OUT: ${bytesSent}, ` +
|
|
|
|
|
`TLS: ${record.isTLS ? 'Yes' : 'No'}, Keep-Alive: ${record.hasKeepAlive ? 'Yes' : 'No'}` +
|
|
|
|
|
`${record.usingNetworkProxy ? `, NetworkProxy: ${record.networkProxyIndex}` : ''}`
|
|
|
|
|
`TLS: ${record.isTLS ? 'Yes' : 'No'}, Keep-Alive: ${
|
|
|
|
|
record.hasKeepAlive ? 'Yes' : 'No'
|
|
|
|
|
}` +
|
|
|
|
|
`${record.usingNetworkProxy ? `, NetworkProxy: ${record.networkProxyIndex}` : ''}` +
|
|
|
|
|
`${record.domainSwitches ? `, Domain switches: ${record.domainSwitches}` : ''}`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(
|
|
|
|
@ -1169,6 +1212,18 @@ export class PortProxy {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update connection activity timestamp
|
|
|
|
|
*/
|
|
|
|
|
private updateActivity(record: IConnectionRecord): void {
|
|
|
|
|
record.lastActivity = Date.now();
|
|
|
|
|
|
|
|
|
|
// Clear any inactivity warning
|
|
|
|
|
if (record.inactivityWarningIssued) {
|
|
|
|
|
record.inactivityWarningIssued = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get target IP with round-robin support
|
|
|
|
|
*/
|
|
|
|
@ -1190,7 +1245,10 @@ export class PortProxy {
|
|
|
|
|
console.log(`[${record.id}] Connection cleanup initiated for ${record.remoteIP} (${reason})`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (record.incomingTerminationReason === null || record.incomingTerminationReason === undefined) {
|
|
|
|
|
if (
|
|
|
|
|
record.incomingTerminationReason === null ||
|
|
|
|
|
record.incomingTerminationReason === undefined
|
|
|
|
|
) {
|
|
|
|
|
record.incomingTerminationReason = reason;
|
|
|
|
|
this.incrementTerminationStat('incoming', reason);
|
|
|
|
|
}
|
|
|
|
@ -1346,8 +1404,9 @@ export class PortProxy {
|
|
|
|
|
// Initialize NetworkProxy tracking fields
|
|
|
|
|
usingNetworkProxy: false,
|
|
|
|
|
|
|
|
|
|
// Initialize sleep detection fields
|
|
|
|
|
possibleSystemSleep: false
|
|
|
|
|
// Initialize browser connection tracking
|
|
|
|
|
isBrowserConnection: this.settings.browserFriendlyMode, // Assume browser if browserFriendlyMode is enabled
|
|
|
|
|
domainSwitches: 0, // Track domain switches
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Apply keep-alive settings if enabled
|
|
|
|
@ -1368,7 +1427,9 @@ export class PortProxy {
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Ignore errors - these are optional enhancements
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${connectionId}] Enhanced TCP keep-alive settings not supported: ${err}`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Enhanced TCP keep-alive settings not supported: ${err}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -1382,6 +1443,7 @@ export class PortProxy {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] New connection from ${remoteIP} on port ${localPort}. ` +
|
|
|
|
|
`Keep-Alive: ${connectionRecord.hasKeepAlive ? 'Enabled' : 'Disabled'}. ` +
|
|
|
|
|
`Mode: ${this.settings.browserFriendlyMode ? 'Browser-friendly' : 'Standard'}. ` +
|
|
|
|
|
`Active connections: ${this.connectionRecords.size}`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
@ -1552,6 +1614,11 @@ export class PortProxy {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the initial SNI for browser connection management
|
|
|
|
|
if (serverName) {
|
|
|
|
|
connectionRecord.lockedDomain = serverName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we didn't forward to NetworkProxy, proceed with direct connection
|
|
|
|
|
return this.setupDirectConnection(
|
|
|
|
|
connectionId,
|
|
|
|
@ -1724,7 +1791,9 @@ export class PortProxy {
|
|
|
|
|
console.log(
|
|
|
|
|
`PortProxy -> OK: Now listening on port ${port}${
|
|
|
|
|
this.settings.sniEnabled ? ' (SNI passthrough enabled)' : ''
|
|
|
|
|
}${this.networkProxies.length > 0 ? ' (NetworkProxy integration enabled)' : ''}`
|
|
|
|
|
}${this.networkProxies.length > 0 ? ' (NetworkProxy integration enabled)' : ''}${
|
|
|
|
|
this.settings.browserFriendlyMode ? ' (Browser-friendly mode enabled)' : ''
|
|
|
|
|
}`
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
this.netServers.push(server);
|
|
|
|
@ -1744,6 +1813,7 @@ export class PortProxy {
|
|
|
|
|
let pendingTlsHandshakes = 0;
|
|
|
|
|
let keepAliveConnections = 0;
|
|
|
|
|
let networkProxyConnections = 0;
|
|
|
|
|
let domainSwitchedConnections = 0;
|
|
|
|
|
|
|
|
|
|
// Create a copy of the keys to avoid modification during iteration
|
|
|
|
|
const connectionIds = [...this.connectionRecords.keys()];
|
|
|
|
@ -1772,11 +1842,14 @@ export class PortProxy {
|
|
|
|
|
networkProxyConnections++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (record.domainSwitches && record.domainSwitches > 0) {
|
|
|
|
|
domainSwitchedConnections++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxIncoming = Math.max(maxIncoming, now - record.incomingStartTime);
|
|
|
|
|
if (record.outgoingStartTime) {
|
|
|
|
|
maxOutgoing = Math.max(maxOutgoing, now - record.outgoingStartTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parity check: if outgoing socket closed and incoming remains active
|
|
|
|
|
if (
|
|
|
|
|
record.outgoingClosedTime &&
|
|
|
|
@ -1808,47 +1881,12 @@ export class PortProxy {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip inactivity check if disabled or for immortal keep-alive connections
|
|
|
|
|
if (!this.settings.disableInactivityCheck &&
|
|
|
|
|
!(record.hasKeepAlive && this.settings.keepAliveTreatment === 'immortal')) {
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!this.settings.disableInactivityCheck &&
|
|
|
|
|
!(record.hasKeepAlive && this.settings.keepAliveTreatment === 'immortal')
|
|
|
|
|
) {
|
|
|
|
|
const inactivityTime = now - record.lastActivity;
|
|
|
|
|
|
|
|
|
|
// Special handling for TLS keep-alive connections
|
|
|
|
|
if (record.hasKeepAlive && record.isTLS && inactivityTime > this.settings.inactivityTimeout! / 2) {
|
|
|
|
|
// For TLS keep-alive connections that are getting stale, try to refresh before closing
|
|
|
|
|
if (!record.inactivityWarningIssued) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${id}] TLS keep-alive connection from ${record.remoteIP} inactive for ${plugins.prettyMs(inactivityTime)}. ` +
|
|
|
|
|
`Attempting to preserve connection.`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Set warning flag but give a much longer grace period for TLS connections
|
|
|
|
|
record.inactivityWarningIssued = true;
|
|
|
|
|
|
|
|
|
|
// For TLS connections, extend the last activity time considerably
|
|
|
|
|
// This gives browsers more time to re-establish the connection properly
|
|
|
|
|
record.lastActivity = now - (this.settings.inactivityTimeout! / 3);
|
|
|
|
|
|
|
|
|
|
// Try to stimulate the connection with a probe packet
|
|
|
|
|
if (record.outgoing && !record.outgoing.destroyed) {
|
|
|
|
|
try {
|
|
|
|
|
// For TLS connections, send a proper TLS heartbeat-like packet
|
|
|
|
|
// This is just a small empty buffer that won't affect the TLS session
|
|
|
|
|
record.outgoing.write(Buffer.alloc(0));
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${id}] Sent TLS keep-alive probe packet`);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(`[${id}] Error sending TLS probe packet: ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't proceed to the normal inactivity check logic
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use extended timeout for extended-treatment keep-alive connections
|
|
|
|
|
let effectiveTimeout = this.settings.inactivityTimeout!;
|
|
|
|
|
if (record.hasKeepAlive && this.settings.keepAliveTreatment === 'extended') {
|
|
|
|
@ -1860,7 +1898,9 @@ export class PortProxy {
|
|
|
|
|
// For keep-alive connections, issue a warning first
|
|
|
|
|
if (record.hasKeepAlive && !record.inactivityWarningIssued) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${id}] Warning: Keep-alive connection from ${record.remoteIP} inactive for ${plugins.prettyMs(inactivityTime)}. ` +
|
|
|
|
|
`[${id}] Warning: Keep-alive connection from ${
|
|
|
|
|
record.remoteIP
|
|
|
|
|
} inactive for ${plugins.prettyMs(inactivityTime)}. ` +
|
|
|
|
|
`Will close in 10 minutes if no activity.`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
@ -1880,18 +1920,6 @@ export class PortProxy {
|
|
|
|
|
console.log(`[${id}] Error sending probe packet: ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// MODIFIED: For TLS connections, be more lenient before closing
|
|
|
|
|
if (record.isTLS && record.hasKeepAlive) {
|
|
|
|
|
// For TLS keep-alive connections, add additional grace period
|
|
|
|
|
// This helps with browsers reconnecting after sleep
|
|
|
|
|
console.log(
|
|
|
|
|
`[${id}] TLS keep-alive connection from ${record.remoteIP} inactive for ${plugins.prettyMs(inactivityTime)}. ` +
|
|
|
|
|
`Adding extra grace period.`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Give additional time for browsers to reconnect properly
|
|
|
|
|
record.lastActivity = now - (effectiveTimeout / 2);
|
|
|
|
|
} else {
|
|
|
|
|
// For non-keep-alive or after warning, close the connection
|
|
|
|
|
console.log(
|
|
|
|
@ -1901,11 +1929,12 @@ export class PortProxy {
|
|
|
|
|
);
|
|
|
|
|
this.cleanupConnection(record, 'inactivity');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (inactivityTime <= effectiveTimeout && record.inactivityWarningIssued) {
|
|
|
|
|
// If activity detected after warning, clear the warning
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(`[${id}] Connection activity detected after inactivity warning, resetting warning`);
|
|
|
|
|
console.log(
|
|
|
|
|
`[${id}] Connection activity detected after inactivity warning, resetting warning`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
record.inactivityWarningIssued = false;
|
|
|
|
|
}
|
|
|
|
@ -1916,7 +1945,8 @@ export class PortProxy {
|
|
|
|
|
console.log(
|
|
|
|
|
`Active connections: ${this.connectionRecords.size}. ` +
|
|
|
|
|
`Types: TLS=${tlsConnections} (Completed=${completedTlsHandshakes}, Pending=${pendingTlsHandshakes}), ` +
|
|
|
|
|
`Non-TLS=${nonTlsConnections}, KeepAlive=${keepAliveConnections}, NetworkProxy=${networkProxyConnections}. ` +
|
|
|
|
|
`Non-TLS=${nonTlsConnections}, KeepAlive=${keepAliveConnections}, NetworkProxy=${networkProxyConnections}, ` +
|
|
|
|
|
`DomainSwitched=${domainSwitchedConnections}. ` +
|
|
|
|
|
`Longest running: IN=${plugins.prettyMs(maxIncoming)}, OUT=${plugins.prettyMs(
|
|
|
|
|
maxOutgoing
|
|
|
|
|
)}. ` +
|
|
|
|
|