better logging
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { SmartProxy } from './smart-proxy.js';
|
||||
import { logger } from '../../core/utils/logger.js';
|
||||
import { connectionLogDeduplicator } from '../../core/utils/log-deduplicator.js';
|
||||
|
||||
/**
|
||||
* Handles security aspects like IP tracking, rate limiting, and authorization
|
||||
@@ -7,8 +9,12 @@ import type { SmartProxy } from './smart-proxy.js';
|
||||
export class SecurityManager {
|
||||
private connectionsByIP: Map<string, Set<string>> = new Map();
|
||||
private connectionRateByIP: Map<string, number[]> = new Map();
|
||||
private cleanupInterval: NodeJS.Timeout | null = null;
|
||||
|
||||
constructor(private smartProxy: SmartProxy) {}
|
||||
constructor(private smartProxy: SmartProxy) {
|
||||
// Start periodic cleanup every 60 seconds
|
||||
this.startPeriodicCleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connections count by IP
|
||||
@@ -164,7 +170,76 @@ export class SecurityManager {
|
||||
* Clears all IP tracking data (for shutdown)
|
||||
*/
|
||||
public clearIPTracking(): void {
|
||||
if (this.cleanupInterval) {
|
||||
clearInterval(this.cleanupInterval);
|
||||
this.cleanupInterval = null;
|
||||
}
|
||||
this.connectionsByIP.clear();
|
||||
this.connectionRateByIP.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start periodic cleanup of expired data
|
||||
*/
|
||||
private startPeriodicCleanup(): void {
|
||||
this.cleanupInterval = setInterval(() => {
|
||||
this.performCleanup();
|
||||
}, 60000); // Run every minute
|
||||
|
||||
// Unref the timer so it doesn't keep the process alive
|
||||
if (this.cleanupInterval.unref) {
|
||||
this.cleanupInterval.unref();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform cleanup of expired rate limits and empty IP entries
|
||||
*/
|
||||
private performCleanup(): void {
|
||||
const now = Date.now();
|
||||
const minute = 60 * 1000;
|
||||
let cleanedRateLimits = 0;
|
||||
let cleanedIPs = 0;
|
||||
|
||||
// Clean up expired rate limit timestamps
|
||||
for (const [ip, timestamps] of this.connectionRateByIP.entries()) {
|
||||
const validTimestamps = timestamps.filter(time => now - time < minute);
|
||||
|
||||
if (validTimestamps.length === 0) {
|
||||
// No valid timestamps, remove the IP entry
|
||||
this.connectionRateByIP.delete(ip);
|
||||
cleanedRateLimits++;
|
||||
} else if (validTimestamps.length < timestamps.length) {
|
||||
// Some timestamps expired, update with valid ones
|
||||
this.connectionRateByIP.set(ip, validTimestamps);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up IPs with no active connections
|
||||
for (const [ip, connections] of this.connectionsByIP.entries()) {
|
||||
if (connections.size === 0) {
|
||||
this.connectionsByIP.delete(ip);
|
||||
cleanedIPs++;
|
||||
}
|
||||
}
|
||||
|
||||
// Log cleanup stats if anything was cleaned
|
||||
if (cleanedRateLimits > 0 || cleanedIPs > 0) {
|
||||
if (this.smartProxy.settings.enableDetailedLogging) {
|
||||
connectionLogDeduplicator.log(
|
||||
'ip-cleanup',
|
||||
'debug',
|
||||
'IP tracking cleanup completed',
|
||||
{
|
||||
cleanedRateLimits,
|
||||
cleanedIPs,
|
||||
remainingIPs: this.connectionsByIP.size,
|
||||
remainingRateLimits: this.connectionRateByIP.size,
|
||||
component: 'security-manager'
|
||||
},
|
||||
'periodic-cleanup'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user