import { Socket } from 'net'; import { EventEmitter } from 'events'; import { MessageBuffer } from '../config/protocol'; import { TCPResponse } from '../types/protocol.types'; import { AuthenticatedUser } from '../../config/Interfaces/Auth/auth.interface'; /** * Connection metadata */ interface ConnectionInfo { socket: Socket; buffer: MessageBuffer; connectedAt: number; lastActivity: number; requestCount: number; /** Remote IP this connection came in on - kept for removeConnection() to clean up connectionsByIp. */ remoteAddress: string; /** Set once a successful AUTHENTICATE command has run on this connection. Lives for the socket's lifetime. */ authUser?: AuthenticatedUser; } /** Reason `addConnection` rejected a new socket, so the caller can send an accurate error response. */ export declare enum ConnectionRejectReason { SERVER_OVERLOAD = "SERVER_OVERLOAD", IP_LIMIT_EXCEEDED = "IP_LIMIT_EXCEEDED", RATE_LIMITED = "RATE_LIMITED" } /** Result of `addConnection` - either the new connection's id, or why it was rejected. */ export type ConnectionAddResult = { connectionId: string; } | { rejected: ConnectionRejectReason; }; /** * Connection Manager - Manages all active TCP connections */ export declare class ConnectionManager extends EventEmitter { private connections; /** Connection ids grouped by remote IP, so addConnection can enforce MAX_CONNECTIONS_PER_IP in O(1). */ private connectionsByIp; private connectionCounter; constructor(); /** * Add new connection */ addConnection(socket: Socket): ConnectionAddResult; /** * Remove connection */ removeConnection(connectionId: string): void; /** * Send response to client */ sendResponse(connectionId: string, response: TCPResponse): boolean; /** * Get connection info */ getConnection(connectionId: string): ConnectionInfo | undefined; /** * Mark a connection as authenticated, following a successful AUTHENTICATE command. */ setAuthUser(connectionId: string, authUser: AuthenticatedUser): void; /** * Get the authenticated identity for a connection, if AUTHENTICATE has succeeded. */ getAuthUser(connectionId: string): AuthenticatedUser | undefined; /** * Clears the cached authenticated identity for every connection belonging to a * given username, following a role change, password reset, or account deletion * elsewhere (see AuthEvents). The connection itself is left open - the next * command on it will simply be treated as unauthenticated again, mirroring how a * revoked GUI session cookie stops working without forcibly closing the browser tab. */ revokeAuthForUser(username: string): void; /** * Get total active connections */ get activeConnections(): number; /** * Get all connection IDs */ getAllConnectionIds(): string[]; /** * Close all connections gracefully */ closeAll(): void; /** * Setup socket event handlers */ private setupSocketHandlers; /** * Generate unique connection ID */ private generateConnectionId; /** * Get connection statistics */ getStats(): Record; } export {};