feat(dcrouter): Implement unified email configuration with pattern‐based routing and consolidated email processing. Migrate SMTP forwarding and store‐and‐forward into a single, configuration-driven system that supports glob pattern matching in domain rules.

This commit is contained in:
2025-05-08 00:12:36 +00:00
parent 13ef31c13f
commit 7f84405279
14 changed files with 1448 additions and 2431 deletions

View File

@ -2,42 +2,12 @@ import * as plugins from '../plugins.js';
import * as paths from '../paths.js';
import { SmtpPortConfig, type ISmtpPortSettings } from './classes.smtp.portconfig.js';
import { EmailDomainRouter, type IEmailDomainRoutingConfig } from './classes.email.domainrouter.js';
import { type IMtaConfig, MtaService } from '../mta/classes.mta.js';
// Import SMTP store-and-forward components
import { SmtpServer } from './classes.smtp.server.js';
import { EmailProcessor, type IProcessingResult } from './classes.email.processor.js';
import { DeliveryQueue } from './classes.delivery.queue.js';
import { DeliverySystem } from './classes.delivery.system.js';
// Certificate types are available via plugins.tsclass
/**
* Configuration for SMTP forwarding functionality
*/
export interface ISmtpForwardingConfig {
/** Whether SMTP forwarding is enabled */
enabled?: boolean;
/** SMTP ports to listen on */
ports?: number[];
/** Default SMTP server hostname */
defaultServer: string;
/** Default SMTP server port */
defaultPort?: number;
/** Whether to use TLS when connecting to the default server */
useTls?: boolean;
/** Preserve source IP address when forwarding */
preserveSourceIp?: boolean;
/** Domain-specific routing rules */
domainRoutes?: Array<{
domain: string;
server: string;
port?: number;
}>;
}
import type { ISmtpConfig } from './classes.smtp.config.js';
// Import the consolidated email config
import type { IEmailConfig } from './classes.email.config.js';
import { DomainRouter } from './classes.domain.router.js';
export interface IDcRouterOptions {
/**
@ -46,24 +16,11 @@ export interface IDcRouterOptions {
*/
smartProxyConfig?: plugins.smartproxy.ISmartProxyOptions;
/**
* SMTP store-and-forward configuration
* This enables advanced email processing capabilities (complementary to smartProxyConfig)
* Consolidated email configuration
* This enables all email handling with pattern-based routing
*/
smtpConfig?: ISmtpConfig;
/**
* Legacy SMTP forwarding configuration
* If smtpConfig is provided, this will be ignored
*/
smtpForwarding?: ISmtpForwardingConfig;
/** MTA service configuration (if not using SMTP forwarding) */
mtaConfig?: IMtaConfig;
/** Existing MTA service instance to use (if not using SMTP forwarding) */
mtaServiceInstance?: MtaService;
emailConfig?: IEmailConfig;
/** TLS/certificate configuration */
tls?: {
@ -100,14 +57,10 @@ export class DcRouter {
// Core services
public smartProxy?: plugins.smartproxy.SmartProxy;
public mta?: MtaService;
public dnsServer?: plugins.smartdns.DnsServer;
// SMTP store-and-forward components
public smtpServer?: SmtpServer;
public emailProcessor?: EmailProcessor;
public deliveryQueue?: DeliveryQueue;
public deliverySystem?: DeliverySystem;
// Unified email components
public domainRouter?: DomainRouter;
// Environment access
private qenv = new plugins.qenv.Qenv('./', '.nogit/');
@ -128,16 +81,9 @@ export class DcRouter {
await this.setupSmartProxy();
}
// 2. Set up SMTP handling
if (this.options.smtpConfig) {
// Set up store-and-forward SMTP processing
await this.setupSmtpProcessing();
} else if (this.options.smtpForwarding?.enabled) {
// Fallback to simple SMTP forwarding for backward compatibility
await this.setupSmtpForwarding();
} else {
// Set up MTA service if no SMTP handling is configured
await this.setupMtaService();
// Set up unified email handling if configured
if (this.options.emailConfig) {
await this.setupUnifiedEmailHandling();
}
// 3. Set up DNS server if configured
@ -191,71 +137,6 @@ export class DcRouter {
}
/**
* Set up the MTA service
*/
private async setupMtaService() {
// Use existing MTA service if provided
if (this.options.mtaServiceInstance) {
this.mta = this.options.mtaServiceInstance;
console.log('Using provided MTA service instance');
} else if (this.options.mtaConfig) {
// Create new MTA service with the provided configuration
this.mta = new MtaService(undefined, this.options.mtaConfig);
console.log('Created new MTA service instance');
// Start the MTA service
await this.mta.start();
console.log('MTA service started');
}
}
/**
* Set up SMTP forwarding with SmartProxy
*/
private async setupSmtpForwarding() {
if (!this.options.smtpForwarding) {
return;
}
const forwarding = this.options.smtpForwarding;
console.log('Setting up SMTP forwarding');
// Determine which ports to listen on
const smtpPorts = forwarding.ports || [25, 587, 465];
// Create SmartProxy instance for SMTP forwarding
const smtpProxyConfig: plugins.smartproxy.ISmartProxyOptions = {
// Listen on the first SMTP port
fromPort: smtpPorts[0],
// Forward to the default server
toPort: forwarding.defaultPort || 25,
targetIP: forwarding.defaultServer,
// Enable SNI if port 465 is included (implicit TLS)
sniEnabled: smtpPorts.includes(465),
// Preserve source IP if requested
preserveSourceIP: forwarding.preserveSourceIp || false,
// Create domain configs for SMTP routing
domainConfigs: forwarding.domainRoutes?.map(route => ({
domains: [route.domain],
allowedIPs: ['0.0.0.0/0'], // Allow from anywhere by default
targetIPs: [route.server]
})) || [],
// Include all SMTP ports in the global port ranges
globalPortRanges: smtpPorts.map(port => ({ from: port, to: port }))
};
// Create a separate SmartProxy instance for SMTP
const smtpProxy = new plugins.smartproxy.SmartProxy(smtpProxyConfig);
// Start the SMTP proxy
await smtpProxy.start();
// Store the SMTP proxy reference
this.smartProxy = smtpProxy;
console.log(`SMTP forwarding configured on ports ${smtpPorts.join(', ')}`);
}
/**
* Check if a domain matches a pattern (including wildcard support)
@ -291,17 +172,12 @@ export class DcRouter {
try {
// Stop all services in parallel for faster shutdown
await Promise.all([
// Stop SMTP components
this.stopSmtpComponents().catch(err => console.error('Error stopping SMTP components:', err)),
// Stop unified email components if running
this.domainRouter ? this.stopUnifiedEmailComponents().catch(err => console.error('Error stopping unified email components:', err)) : Promise.resolve(),
// Stop HTTP SmartProxy if running
this.smartProxy ? this.smartProxy.stop().catch(err => console.error('Error stopping SmartProxy:', err)) : Promise.resolve(),
// Stop MTA service if it's our own (not an external instance)
(this.mta && !this.options.mtaServiceInstance) ?
this.mta.stop().catch(err => console.error('Error stopping MTA service:', err)) :
Promise.resolve(),
// Stop DNS server if running
this.dnsServer ?
this.dnsServer.stop().catch(err => console.error('Error stopping DNS server:', err)) :
@ -336,135 +212,64 @@ export class DcRouter {
}
/**
* Set up SMTP store-and-forward processing
* Set up unified email handling with pattern-based routing
* This implements the consolidated emailConfig approach
*/
private async setupSmtpProcessing(): Promise<void> {
if (!this.options.smtpConfig) {
return;
private async setupUnifiedEmailHandling(): Promise<void> {
console.log('Setting up unified email handling with pattern-based routing');
if (!this.options.emailConfig) {
throw new Error('Email configuration is required for unified email handling');
}
console.log('Setting up SMTP store-and-forward processing');
try {
// 1. Create SMTP server
this.smtpServer = new SmtpServer(this.options.smtpConfig);
// 2. Create email processor
this.emailProcessor = new EmailProcessor(this.options.smtpConfig);
// 3. Create delivery queue
this.deliveryQueue = new DeliveryQueue(this.options.smtpConfig.queue || {});
await this.deliveryQueue.initialize();
// 4. Create delivery system
this.deliverySystem = new DeliverySystem(this.deliveryQueue);
// 5. Connect components
// When a message is received by the SMTP server, process it
this.smtpServer.on('message', async ({ session, mail, rawData }) => {
try {
// Process the message
const processingResult = await this.emailProcessor.processEmail(mail, rawData, session);
// If action is queue, add to delivery queue
if (processingResult.action === 'queue') {
await this.deliveryQueue.enqueue(processingResult);
}
} catch (error) {
console.error('Error processing message:', error);
}
// Create domain router for pattern matching
this.domainRouter = new DomainRouter({
domainRules: this.options.emailConfig.domainRules,
defaultMode: this.options.emailConfig.defaultMode,
defaultServer: this.options.emailConfig.defaultServer,
defaultPort: this.options.emailConfig.defaultPort,
defaultTls: this.options.emailConfig.defaultTls
});
// 6. Start components
await this.smtpServer.start();
await this.deliverySystem.start();
// TODO: Initialize the full unified email processing pipeline
console.log(`SMTP processing started on ports ${this.options.smtpConfig.ports.join(', ')}`);
console.log(`Unified email handling configured with ${this.options.emailConfig.domainRules.length} domain rules`);
} catch (error) {
console.error('Error setting up SMTP processing:', error);
// Clean up any components that were started
if (this.deliverySystem) {
await this.deliverySystem.stop().catch(e => console.error('Error stopping delivery system:', e));
}
if (this.deliveryQueue) {
await this.deliveryQueue.shutdown().catch(e => console.error('Error shutting down delivery queue:', e));
}
if (this.smtpServer) {
await this.smtpServer.stop().catch(e => console.error('Error stopping SMTP server:', e));
}
console.error('Error setting up unified email handling:', error);
throw error;
}
}
/**
* Update SMTP forwarding configuration
* @param config New SMTP forwarding configuration
* Update the unified email configuration
* @param config New email configuration
*/
public async updateSmtpForwarding(config: ISmtpForwardingConfig): Promise<void> {
// Stop existing SMTP components
await this.stopSmtpComponents();
public async updateEmailConfig(config: IEmailConfig): Promise<void> {
// Stop existing email components
await this.stopUnifiedEmailComponents();
// Update configuration
this.options.smtpForwarding = config;
this.options.smtpConfig = undefined; // Clear any store-and-forward config
this.options.emailConfig = config;
// Restart SMTP forwarding if enabled
if (config.enabled) {
await this.setupSmtpForwarding();
}
// Start email handling with new configuration
await this.setupUnifiedEmailHandling();
console.log('SMTP forwarding configuration updated');
console.log('Unified email configuration updated');
}
/**
* Update SMTP processing configuration
* @param config New SMTP config
* Stop all unified email components
*/
public async updateSmtpConfig(config: ISmtpConfig): Promise<void> {
// Stop existing SMTP components
await this.stopSmtpComponents();
private async stopUnifiedEmailComponents(): Promise<void> {
// TODO: Implement stopping all unified email components
// Update configuration
this.options.smtpConfig = config;
this.options.smtpForwarding = undefined; // Clear any forwarding config
// Start SMTP processing
await this.setupSmtpProcessing();
console.log('SMTP processing configuration updated');
// Clear the domain router
this.domainRouter = undefined;
}
/**
* Stop all SMTP components
*/
private async stopSmtpComponents(): Promise<void> {
// Stop delivery system
if (this.deliverySystem) {
await this.deliverySystem.stop().catch(e => console.error('Error stopping delivery system:', e));
this.deliverySystem = undefined;
}
// Stop delivery queue
if (this.deliveryQueue) {
await this.deliveryQueue.shutdown().catch(e => console.error('Error shutting down delivery queue:', e));
this.deliveryQueue = undefined;
}
// Stop SMTP server
if (this.smtpServer) {
await this.smtpServer.stop().catch(e => console.error('Error stopping SMTP server:', e));
this.smtpServer = undefined;
}
// For backward compatibility: legacy SMTP proxy implementation
// This is no longer used with the new implementation
}
}
export default DcRouter;