141 lines
4.0 KiB
TypeScript
141 lines
4.0 KiB
TypeScript
/**
|
|
* SMTP Session Manager
|
|
* Responsible for creating, managing, and cleaning up SMTP sessions
|
|
*/
|
|
import * as plugins from '../../../plugins.js';
|
|
import { SmtpState } from './interfaces.js';
|
|
import type { ISmtpSession } from './interfaces.js';
|
|
import type { ISessionManager, ISessionEvents } from './interfaces.js';
|
|
/**
|
|
* Manager for SMTP sessions
|
|
* Handles session creation, tracking, timeout management, and cleanup
|
|
*/
|
|
export declare class SessionManager implements ISessionManager {
|
|
/**
|
|
* Map of socket ID to session
|
|
*/
|
|
private sessions;
|
|
/**
|
|
* Map of socket to socket ID
|
|
*/
|
|
private socketIds;
|
|
/**
|
|
* SMTP server options
|
|
*/
|
|
private options;
|
|
/**
|
|
* Event listeners
|
|
*/
|
|
private eventListeners;
|
|
/**
|
|
* Timer for cleanup interval
|
|
*/
|
|
private cleanupTimer;
|
|
/**
|
|
* Creates a new session manager
|
|
* @param options - Session manager options
|
|
*/
|
|
constructor(options?: {
|
|
socketTimeout?: number;
|
|
connectionTimeout?: number;
|
|
cleanupInterval?: number;
|
|
});
|
|
/**
|
|
* Creates a new session for a socket connection
|
|
* @param socket - Client socket
|
|
* @param secure - Whether the connection is secure (TLS)
|
|
* @returns New SMTP session
|
|
*/
|
|
createSession(socket: plugins.net.Socket | plugins.tls.TLSSocket, secure: boolean): ISmtpSession;
|
|
/**
|
|
* Updates the session state
|
|
* @param session - SMTP session
|
|
* @param newState - New state
|
|
*/
|
|
updateSessionState(session: ISmtpSession, newState: SmtpState): void;
|
|
/**
|
|
* Updates the session's last activity timestamp
|
|
* @param session - SMTP session
|
|
*/
|
|
updateSessionActivity(session: ISmtpSession): void;
|
|
/**
|
|
* Removes a session
|
|
* @param socket - Client socket
|
|
*/
|
|
removeSession(socket: plugins.net.Socket | plugins.tls.TLSSocket): void;
|
|
/**
|
|
* Gets a session for a socket
|
|
* @param socket - Client socket
|
|
* @returns SMTP session or undefined if not found
|
|
*/
|
|
getSession(socket: plugins.net.Socket | plugins.tls.TLSSocket): ISmtpSession | undefined;
|
|
/**
|
|
* Cleans up idle sessions
|
|
*/
|
|
cleanupIdleSessions(): void;
|
|
/**
|
|
* Gets the current number of active sessions
|
|
* @returns Number of active sessions
|
|
*/
|
|
getSessionCount(): number;
|
|
/**
|
|
* Clears all sessions (used when shutting down)
|
|
*/
|
|
clearAllSessions(): void;
|
|
/**
|
|
* Register an event listener
|
|
* @param event - Event name
|
|
* @param listener - Event listener function
|
|
*/
|
|
on<K extends keyof ISessionEvents>(event: K, listener: ISessionEvents[K]): void;
|
|
/**
|
|
* Remove an event listener
|
|
* @param event - Event name
|
|
* @param listener - Event listener function
|
|
*/
|
|
off<K extends keyof ISessionEvents>(event: K, listener: ISessionEvents[K]): void;
|
|
/**
|
|
* Emit an event to registered listeners
|
|
* @param event - Event name
|
|
* @param args - Event arguments
|
|
*/
|
|
private emitEvent;
|
|
/**
|
|
* Start the cleanup timer
|
|
*/
|
|
private startCleanupTimer;
|
|
/**
|
|
* Stop the cleanup timer
|
|
*/
|
|
private stopCleanupTimer;
|
|
/**
|
|
* Replace socket mapping for STARTTLS upgrades
|
|
* @param oldSocket - Original plain socket
|
|
* @param newSocket - New TLS socket
|
|
* @returns Whether the replacement was successful
|
|
*/
|
|
replaceSocket(oldSocket: plugins.net.Socket | plugins.tls.TLSSocket, newSocket: plugins.net.Socket | plugins.tls.TLSSocket): boolean;
|
|
/**
|
|
* Gets a unique key for a socket
|
|
* @param socket - Client socket
|
|
* @returns Socket key
|
|
*/
|
|
private getSocketKey;
|
|
/**
|
|
* Get all active sessions
|
|
*/
|
|
getAllSessions(): ISmtpSession[];
|
|
/**
|
|
* Update last activity for a session by socket
|
|
*/
|
|
updateLastActivity(socket: plugins.net.Socket | plugins.tls.TLSSocket): void;
|
|
/**
|
|
* Check for timed out sessions
|
|
*/
|
|
checkTimeouts(timeoutMs: number): ISmtpSession[];
|
|
/**
|
|
* Clean up resources
|
|
*/
|
|
destroy(): void;
|
|
}
|