This commit is contained in:
2025-05-22 23:02:37 +00:00
parent f065a9c952
commit 50350bd78d
10 changed files with 633 additions and 779 deletions

View File

@@ -4,7 +4,7 @@
*/
import * as plugins from '../../../plugins.js';
import type { ITlsHandler, ISessionManager } from './interfaces.js';
import type { ITlsHandler, ISmtpServer } from './interfaces.js';
import { SmtpResponseCode, SecurityEventType, SecurityLogLevel } from './constants.js';
import { SmtpLogger } from './utils/logging.js';
import { getSocketDetails, getTlsDetails } from './utils/helpers.js';
@@ -21,19 +21,9 @@ import { SmtpState } from '../interfaces.js';
*/
export class TlsHandler implements ITlsHandler {
/**
* Session manager instance
* Reference to the SMTP server instance
*/
private sessionManager: ISessionManager;
/**
* TLS options
*/
private options: {
key: string;
cert: string;
ca?: string;
rejectUnauthorized?: boolean;
};
private smtpServer: ISmtpServer;
/**
* Certificate data
@@ -42,22 +32,13 @@ export class TlsHandler implements ITlsHandler {
/**
* Creates a new TLS handler
* @param sessionManager - Session manager instance
* @param options - TLS options
* @param smtpServer - SMTP server instance
*/
constructor(
sessionManager: ISessionManager,
options: {
key: string;
cert: string;
ca?: string;
rejectUnauthorized?: boolean;
}
) {
this.sessionManager = sessionManager;
this.options = options;
constructor(smtpServer: ISmtpServer) {
this.smtpServer = smtpServer;
// Initialize certificates
const options = this.smtpServer.getOptions();
try {
// Try to load certificates from provided options
this.certificates = loadCertificatesFromString({
@@ -81,7 +62,7 @@ export class TlsHandler implements ITlsHandler {
*/
public handleStartTls(socket: plugins.net.Socket | plugins.tls.TLSSocket): void {
// Get the session for this socket
const session = this.sessionManager.getSession(socket);
const session = this.smtpServer.getSessionManager().getSession(socket);
if (!session) {
this.sendResponse(socket, `${SmtpResponseCode.LOCAL_ERROR} Internal server error - session not found`);
return;
@@ -129,7 +110,7 @@ export class TlsHandler implements ITlsHandler {
*/
public async startTLS(socket: plugins.net.Socket): Promise<void> {
// Get the session for this socket
const session = this.sessionManager.getSession(socket);
const session = this.smtpServer.getSessionManager().getSession(socket);
try {
// Import the enhanced STARTTLS handler
@@ -139,11 +120,14 @@ export class TlsHandler implements ITlsHandler {
SmtpLogger.info('Using enhanced STARTTLS implementation');
// Use the enhanced STARTTLS handler with better error handling and socket management
const options = this.smtpServer.getOptions();
const tlsSocket = await performStartTLS(socket, {
key: this.options.key,
cert: this.options.cert,
ca: this.options.ca,
key: options.key,
cert: options.cert,
ca: options.ca,
session: session,
sessionManager: this.smtpServer.getSessionManager(),
connectionManager: this.smtpServer.getConnectionManager(),
// Callback for successful upgrade
onSuccess: (secureSocket) => {
SmtpLogger.info('TLS connection successfully established via enhanced STARTTLS', {
@@ -187,7 +171,7 @@ export class TlsHandler implements ITlsHandler {
);
},
// Function to update session state
updateSessionState: this.sessionManager.updateSessionState?.bind(this.sessionManager)
updateSessionState: this.smtpServer.getSessionManager().updateSessionState?.bind(this.smtpServer.getSessionManager())
});
// If STARTTLS failed with the enhanced implementation, log the error
@@ -291,7 +275,8 @@ export class TlsHandler implements ITlsHandler {
* @returns Whether TLS is enabled
*/
public isTlsEnabled(): boolean {
return !!(this.options.key && this.options.cert);
const options = this.smtpServer.getOptions();
return !!(options.key && options.cert);
}
/**
@@ -326,4 +311,13 @@ export class TlsHandler implements ITlsHandler {
socket.destroy();
}
}
/**
* Clean up resources
*/
public destroy(): void {
// Clear any cached certificates or TLS contexts
// TlsHandler doesn't have timers but may have cached resources
SmtpLogger.debug('TlsHandler destroyed');
}
}