fix(tests): fix tests

This commit is contained in:
Juergen Kunz
2025-06-23 08:38:14 +00:00
parent 1aead55296
commit be58700a2f
14 changed files with 153 additions and 107 deletions

View File

@ -52,6 +52,9 @@ export class WrappedSocket {
if (prop === 'setProxyInfo') {
return target.setProxyInfo.bind(target);
}
if (prop === 'remoteFamily') {
return target.remoteFamily;
}
// For all other properties/methods, delegate to the underlying socket
const value = target.socket[prop as keyof plugins.net.Socket];
@ -89,6 +92,21 @@ export class WrappedSocket {
return !!this.realClientIP;
}
/**
* Returns the address family of the remote IP
*/
get remoteFamily(): string | undefined {
const ip = this.realClientIP || this.socket.remoteAddress;
if (!ip) return undefined;
// Check if it's IPv6
if (ip.includes(':')) {
return 'IPv6';
}
// Otherwise assume IPv4
return 'IPv4';
}
/**
* Updates the real client information (called after parsing PROXY protocol)
*/

View File

@ -95,7 +95,8 @@ export class PathMatcher implements IMatcher<IPathMatchResult> {
if (normalizedPattern.includes('*') && match.length > paramNames.length + 1) {
const wildcardCapture = match[match.length - 1];
if (wildcardCapture) {
pathRemainder = wildcardCapture;
// Ensure pathRemainder includes leading slash if it had one
pathRemainder = wildcardCapture.startsWith('/') ? wildcardCapture : '/' + wildcardCapture;
pathMatch = normalizedPath.substring(0, normalizedPath.length - wildcardCapture.length);
}
}

View File

@ -274,10 +274,16 @@ export class MetricsCollector implements IMetrics {
lastUpdate: now
});
// Cleanup old request timestamps (keep last minute only)
if (this.requestTimestamps.length > 1000) {
// Cleanup old request timestamps
if (this.requestTimestamps.length > 5000) {
// First try to clean up old timestamps (older than 1 minute)
const cutoff = now - 60000;
this.requestTimestamps = this.requestTimestamps.filter(ts => ts > cutoff);
// If still too many, enforce hard cap of 5000 most recent
if (this.requestTimestamps.length > 5000) {
this.requestTimestamps = this.requestTimestamps.slice(-5000);
}
}
}

View File

@ -149,7 +149,7 @@ export interface IConnectionRecord {
outgoingClosedTime?: number;
lockedDomain?: string; // Used to lock this connection to the initial SNI
connectionClosed: boolean; // Flag to prevent multiple cleanup attempts
cleanupTimer?: NodeJS.Timeout; // Timer for max lifetime/inactivity
cleanupTimer?: NodeJS.Timeout | null; // Timer for max lifetime/inactivity
alertFallbackTimeout?: NodeJS.Timeout; // Timer for fallback after alert
lastActivity: number; // Last activity timestamp for inactivity detection
pendingData: Buffer[]; // Buffer to hold data during connection setup

View File

@ -1302,10 +1302,13 @@ export class RouteConnectionHandler {
enableHalfOpen: false // Default: close both when one closes (required for proxy chains)
});
// Apply timeouts if keep-alive is enabled
if (record.hasKeepAlive) {
socket.setTimeout(this.smartProxy.settings.socketTimeout || 3600000);
targetSocket.setTimeout(this.smartProxy.settings.socketTimeout || 3600000);
// Apply timeouts using TimeoutManager
const timeout = this.smartProxy.timeoutManager.getEffectiveInactivityTimeout(record);
// Skip timeout for immortal connections (MAX_SAFE_INTEGER would cause issues)
if (timeout !== Number.MAX_SAFE_INTEGER) {
const safeTimeout = this.smartProxy.timeoutManager.ensureSafeTimeout(timeout);
socket.setTimeout(safeTimeout);
targetSocket.setTimeout(safeTimeout);
}
// Log successful connection

View File

@ -94,12 +94,17 @@ export class TimeoutManager {
public setupConnectionTimeout(
record: IConnectionRecord,
onTimeout: (record: IConnectionRecord, reason: string) => void
): NodeJS.Timeout {
): NodeJS.Timeout | null {
// Clear any existing timer
if (record.cleanupTimer) {
clearTimeout(record.cleanupTimer);
}
// Skip timeout for immortal keep-alive connections
if (record.hasKeepAlive && this.smartProxy.settings.keepAliveTreatment === 'immortal') {
return null;
}
// Calculate effective timeout
const effectiveLifetime = this.getEffectiveMaxLifetime(record);

View File

@ -168,7 +168,7 @@ export class HttpRouter {
if (pathResult.matches) {
return {
route,
pathMatch: path,
pathMatch: pathResult.pathMatch || path,
pathParams: pathResult.params,
pathRemainder: pathResult.pathRemainder
};