160 lines
4.5 KiB
TypeScript
160 lines
4.5 KiB
TypeScript
/**
|
|
* SMTP Connection Manager
|
|
* Responsible for managing socket connections to the SMTP server
|
|
*/
|
|
import * as plugins from '../../../plugins.js';
|
|
import type { IConnectionManager, ISmtpServer } from './interfaces.js';
|
|
/**
|
|
* Manager for SMTP connections
|
|
* Handles connection setup, event listeners, and lifecycle management
|
|
* Provides resource management, connection tracking, and monitoring
|
|
*/
|
|
export declare class ConnectionManager implements IConnectionManager {
|
|
/**
|
|
* Reference to the SMTP server instance
|
|
*/
|
|
private smtpServer;
|
|
/**
|
|
* Set of active socket connections
|
|
*/
|
|
private activeConnections;
|
|
/**
|
|
* Connection tracking for resource management
|
|
*/
|
|
private connectionStats;
|
|
/**
|
|
* Per-IP connection tracking for rate limiting
|
|
*/
|
|
private ipConnections;
|
|
/**
|
|
* Resource monitoring interval
|
|
*/
|
|
private resourceCheckInterval;
|
|
/**
|
|
* Track cleanup timers so we can clear them
|
|
*/
|
|
private cleanupTimers;
|
|
/**
|
|
* SMTP server options with enhanced resource controls
|
|
*/
|
|
private options;
|
|
/**
|
|
* Creates a new connection manager with enhanced resource management
|
|
* @param smtpServer - SMTP server instance
|
|
*/
|
|
constructor(smtpServer: ISmtpServer);
|
|
/**
|
|
* Start resource monitoring interval to check resource usage
|
|
*/
|
|
private startResourceMonitoring;
|
|
/**
|
|
* Monitor resource usage and log statistics
|
|
*/
|
|
private monitorResourceUsage;
|
|
/**
|
|
* Clean up expired IP rate limits and perform additional resource monitoring
|
|
*/
|
|
private cleanupIpRateLimits;
|
|
/**
|
|
* Validate and repair resource tracking to prevent leaks
|
|
*/
|
|
private validateResourceTracking;
|
|
/**
|
|
* Handle a new connection with resource management
|
|
* @param socket - Client socket
|
|
*/
|
|
handleNewConnection(socket: plugins.net.Socket): Promise<void>;
|
|
/**
|
|
* Check if an IP has exceeded the rate limit
|
|
* @param ip - Client IP address
|
|
* @returns True if rate limited
|
|
*/
|
|
private isIPRateLimited;
|
|
/**
|
|
* Track a new connection from an IP
|
|
* @param ip - Client IP address
|
|
*/
|
|
private trackIPConnection;
|
|
/**
|
|
* Check if an IP has reached its connection limit
|
|
* @param ip - Client IP address
|
|
* @returns True if limit reached
|
|
*/
|
|
private hasReachedIPConnectionLimit;
|
|
/**
|
|
* Handle a new secure TLS connection with resource management
|
|
* @param socket - Client TLS socket
|
|
*/
|
|
handleNewSecureConnection(socket: plugins.tls.TLSSocket): Promise<void>;
|
|
/**
|
|
* Set up event handlers for a socket with enhanced resource management
|
|
* @param socket - Client socket
|
|
*/
|
|
setupSocketEventHandlers(socket: plugins.net.Socket | plugins.tls.TLSSocket): void;
|
|
/**
|
|
* Get the current connection count
|
|
* @returns Number of active connections
|
|
*/
|
|
getConnectionCount(): number;
|
|
/**
|
|
* Check if the server has reached the maximum number of connections
|
|
* @returns True if max connections reached
|
|
*/
|
|
hasReachedMaxConnections(): boolean;
|
|
/**
|
|
* Close all active connections
|
|
*/
|
|
closeAllConnections(): void;
|
|
/**
|
|
* Handle socket close event
|
|
* @param socket - Client socket
|
|
* @param hadError - Whether the socket was closed due to error
|
|
*/
|
|
private handleSocketClose;
|
|
/**
|
|
* Handle socket error event
|
|
* @param socket - Client socket
|
|
* @param error - Error object
|
|
*/
|
|
private handleSocketError;
|
|
/**
|
|
* Handle socket timeout event
|
|
* @param socket - Client socket
|
|
*/
|
|
private handleSocketTimeout;
|
|
/**
|
|
* Reject a connection
|
|
* @param socket - Client socket
|
|
* @param reason - Reason for rejection
|
|
*/
|
|
private rejectConnection;
|
|
/**
|
|
* Send greeting message
|
|
* @param socket - Client socket
|
|
*/
|
|
private sendGreeting;
|
|
/**
|
|
* Send service closing notification
|
|
* @param socket - Client socket
|
|
*/
|
|
private sendServiceClosing;
|
|
/**
|
|
* Send response to client
|
|
* @param socket - Client socket
|
|
* @param response - Response to send
|
|
*/
|
|
private sendResponse;
|
|
/**
|
|
* Handle a new connection (interface requirement)
|
|
*/
|
|
handleConnection(socket: plugins.net.Socket | plugins.tls.TLSSocket, secure: boolean): Promise<void>;
|
|
/**
|
|
* Check if accepting new connections (interface requirement)
|
|
*/
|
|
canAcceptConnection(): boolean;
|
|
/**
|
|
* Clean up resources
|
|
*/
|
|
destroy(): void;
|
|
}
|