dcrouter/ts/mail/delivery/smtpserver/create-server.ts
2025-05-21 13:42:12 +00:00

92 lines
2.4 KiB
TypeScript

/**
* SMTP Server Creation Factory
* Provides a simple way to create a complete SMTP server
*/
import { SmtpServer } from './smtp-server.js';
import { SessionManager } from './session-manager.js';
import { ConnectionManager } from './connection-manager.js';
import { CommandHandler } from './command-handler.js';
import { DataHandler } from './data-handler.js';
import { TlsHandler } from './tls-handler.js';
import { SecurityHandler } from './security-handler.js';
import type { ISmtpServerOptions } from './interfaces.js';
import { UnifiedEmailServer } from '../../routing/classes.unified.email.server.js';
/**
* Create a complete SMTP server with all components
* @param emailServer - Email server reference
* @param options - SMTP server options
* @returns Configured SMTP server instance
*/
export function createSmtpServer(emailServer: UnifiedEmailServer, options: ISmtpServerOptions): SmtpServer {
// Create session manager
const sessionManager = new SessionManager({
socketTimeout: options.socketTimeout,
connectionTimeout: options.connectionTimeout,
cleanupInterval: options.cleanupInterval
});
// Create security handler
const securityHandler = new SecurityHandler(
emailServer,
undefined, // IP reputation service
options.auth
);
// Create TLS handler
const tlsHandler = new TlsHandler(
sessionManager,
{
key: options.key,
cert: options.cert,
ca: options.ca
}
);
// Create data handler
const dataHandler = new DataHandler(
sessionManager,
emailServer,
{
size: options.size
}
);
// Create command handler
const commandHandler = new CommandHandler(
sessionManager,
{
hostname: options.hostname,
size: options.size,
maxRecipients: options.maxRecipients,
auth: options.auth
},
dataHandler,
tlsHandler,
securityHandler
);
// Create connection manager
const connectionManager = new ConnectionManager(
sessionManager,
(socket, line) => commandHandler.processCommand(socket, line),
{
hostname: options.hostname,
maxConnections: options.maxConnections,
socketTimeout: options.socketTimeout
}
);
// Create and return SMTP server
return new SmtpServer({
emailServer,
options,
sessionManager,
connectionManager,
commandHandler,
dataHandler,
tlsHandler,
securityHandler
});
}