2025-05-08 01:37:38 +00:00
# PlatformService Roadmap
## Latest Changes
2025-05-08 10:39:43 +00:00
### API Type Safety Improvements
- [x] Create comprehensive TypeScript interfaces for all API methods
- [x] Replace any types with specific interfaces in EmailService and MtaConnector
- [x] Align interface types with their implementations
- [x] Document all interface properties with detailed JSDoc comments
- [x] Use type imports to ensure consistency between services
- [x] Fix TypeScript build errors from interface placements
- [x] Add proper method signatures for API endpoint handlers
- [x] Implement type-safe API responses
2025-05-08 10:24:50 +00:00
### Test Stability Improvements
- [x] Fix race conditions in SenderReputationMonitor tests
- [x] Disable filesystem operations during tests to prevent race conditions
- [x] Add proper cleanup of singleton instances and timeouts
- [x] Ensure all tests properly clean up shared resources
- [x] Set consistent test environment with NODE_ENV=test
2025-05-08 10:39:43 +00:00
### Error Handling Improvements
- [ ] Add structured error types for better error reporting
- [ ] Implement consistent error handling patterns across services
- [ ] Add detailed error context for debugging
- [ ] Create retry mechanisms for transient failures
- [ ] Improve error logging with structured data
### Configuration Interface Standardization
- [ ] Define consistent configuration interfaces across services
- [ ] Implement validation for all configuration objects
- [ ] Add default values for optional configuration
- [ ] Create documentation for all configuration options
- [ ] Add migration helpers for configuration format changes
### Logging Enhancements
- [ ] Implement structured logging throughout the codebase
- [ ] Add context information to all log messages
- [ ] Create consistent log levels and usage patterns
- [ ] Add correlation IDs for request tracking
- [ ] Implement log filtering and sampling options
2025-05-08 01:37:38 +00:00
### Mailgun Removal
- [x] Remove Mailgun integration from keywords in package.json and npmextra.json
- [x] Update EmailService comments to remove mentions of Mailgun
- [x] Update error messages to reference MTA instead of generic email providers
- [x] Update the readme email example to use PlatformService reference instead of Mailgun API key
2025-05-08 00:12:36 +00:00
# DcRouter Consolidated Email Configuration Plan
2025-05-07 23:30:04 +00:00
## Overview
2025-05-08 00:12:36 +00:00
This plan outlines the consolidation of all email-related functionality in DcRouter under a unified `emailConfig` interface. This new approach combines MTA, SMTP forwarding, and store-and-forward processing into a single, pattern-based routing system that:
1. Uses glob patterns for domain matching (e.g., `*@task.vc` or `*@*.example.net` )
2. Shares ports across all email handling methods (25, 587, 465)
3. Allows different handling modes based on email domain patterns
4. Provides a flexible configuration interface for all email-related functionality
This consolidated approach simplifies configuration while enhancing flexibility, allowing domain-specific handling where, for example, `*@task.vc` emails are forwarded to another SMTP server while `*@lossless.digital` emails are processed by the MTA for programmatic use.
2025-05-07 23:30:04 +00:00
## 0. Configuration Approaches
### 0.1 Core SmartProxy Direct Configuration
DcRouter should leverage SmartProxy's configuration directly, exposing SmartProxy's full domain configuration options to give users maximum flexibility for all HTTP/HTTPS and TCP/SNI traffic:
```typescript
interface IDcRouterOptions {
// Direct SmartProxy configuration for general HTTP/HTTPS and TCP/SNI traffic
smartProxyConfig?: plugins.smartproxy.ISmartProxyOptions;
2025-05-08 00:12:36 +00:00
// Consolidated email configuration
emailConfig?: IEmailConfig;
2025-05-07 23:30:04 +00:00
// Other DcRouter options...
}
```
This approach allows direct configuration of SmartProxy's powerful domain-based routing, giving full control over HTTP/HTTPS and SNI-based traffic:
```typescript
const dcRouter = new DcRouter({
// Direct SmartProxy configuration for HTTP/HTTPS traffic
smartProxyConfig: {
fromPort: 443,
toPort: 8080,
targetIP: '10.0.0.10',
sniEnabled: true,
acme: {
port: 80,
enabled: true,
autoRenew: true,
useProduction: true,
renewThresholdDays: 30,
accountEmail: 'admin@example .com'
},
globalPortRanges: [
{ from: 80, to: 80 },
{ from: 443, to: 443 }
],
// SmartProxy's full domain configuration flexibility
domainConfigs: [
{
domains: ['example.com', 'www.example.com'],
allowedIPs: ['0.0.0.0/0'],
blockedIPs: ['1.2.3.4/32'],
targetIPs: ['10.0.0.10', '10.0.0.11'],
portRanges: [
{ from: 80, to: 80 },
{ from: 443, to: 443 }
],
connectionTimeout: 60000,
useNetworkProxy: true
},
// Additional domain configurations...
2025-05-08 00:12:36 +00:00
]
2025-05-07 23:30:04 +00:00
},
2025-05-08 00:12:36 +00:00
// Consolidated email configuration
emailConfig: {
2025-05-07 23:30:04 +00:00
// Email handling configuration...
2025-05-08 00:12:36 +00:00
}
});
2025-05-07 23:30:04 +00:00
```
2025-05-08 00:12:36 +00:00
### 0.2 Consolidated Email Configuration
We'll implement a unified email configuration approach that combines MTA, SMTP forwarding, and store-and-forward processing into a single pattern-based routing system:
```typescript
interface IDcRouterOptions {
// HTTP/HTTPS configuration
smartProxyConfig?: plugins.smartproxy.ISmartProxyOptions;
// Consolidated email handling
emailConfig?: {
// Global email server settings
ports: number[];
hostname: string;
maxMessageSize?: number;
// TLS configuration for all email services
tls?: {
certPath?: string;
keyPath?: string;
caPath?: string;
minVersion?: string;
};
// Authentication for inbound connections
auth?: {
required?: boolean;
methods?: ('PLAIN' | 'LOGIN' | 'OAUTH2')[];
users?: Array< {username: string, password: string}>;
};
// Default routing for unmatched domains
defaultMode: 'forward' | 'mta' | 'process';
defaultServer?: string;
defaultPort?: number;
defaultTls?: boolean;
// Domain-specific rules with glob pattern support
domainRules: Array< {
// Domain pattern (e.g., "*@example .com", "*@*.example.net")
pattern: string;
// Handling mode for this pattern
mode: 'forward' | 'mta' | 'process';
// Forward mode configuration
target?: {
server: string;
port?: number;
useTls?: boolean;
authentication?: {
user?: string;
pass?: string;
};
};
// MTA mode configuration
mtaOptions?: {
domain?: string;
allowLocalDelivery?: boolean;
localDeliveryPath?: string;
dkimSign?: boolean;
dkimOptions?: {
domainName: string;
keySelector: string;
privateKey: string;
};
};
// Process mode configuration
contentScanning?: boolean;
scanners?: Array< {
type: 'spam' | 'virus' | 'attachment';
threshold?: number;
action: 'tag' | 'reject';
blockedExtensions?: string[];
}>;
transformations?: Array< {
type: string;
[key: string]: any;
}>;
// Rate limits for this domain
rateLimits?: {
maxMessagesPerMinute?: number;
maxRecipientsPerMessage?: number;
};
}>;
// Queue configuration for all email processing
queue?: {
storageType?: 'memory' | 'disk';
persistentPath?: string;
maxRetries?: number;
baseRetryDelay?: number;
maxRetryDelay?: number;
};
// Advanced MTA settings
mtaGlobalOptions?: {
smtpBanner?: string;
maxConnections?: number;
connTimeout?: number;
spoolDir?: string;
dkimKeyPath?: string;
};
};
}
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
## 1. Core Architecture for Consolidated Email Processing
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
### 1.1 Unified Email Server
2025-05-08 00:39:43 +00:00
- [x] Create a unified email server component
2025-05-08 00:12:36 +00:00
- Build on existing SmtpServer class but with enhanced routing capabilities
- Configure to listen on standard ports (25, 587, 465) for all email handling
- Implement TLS support (STARTTLS and implicit TLS)
- Add support for authentication methods (PLAIN, LOGIN, OAUTH2)
2025-05-07 23:30:04 +00:00
- Set up size limits and connection timeouts
2025-05-08 00:12:36 +00:00
### 1.2 Pattern-Based Domain Router
2025-05-08 00:39:43 +00:00
- [x] Create pattern matching system for email domains
2025-05-08 00:12:36 +00:00
- Implement glob pattern matching for email addresses
- Support patterns like `*@domain.com` , `*@*.domain.com`
- Create priority-based matching system (most specific match wins)
- Build rule evaluation engine to determine processing mode
- Implement a fast lookup mechanism for incoming emails
### 1.3 Multi-Modal Processing System
2025-05-08 00:39:43 +00:00
- [x] Create a unified processing system with multiple modes
2025-05-08 00:12:36 +00:00
- Forward mode: SMTP proxy functionality with enhanced routing
- MTA mode: Programmatic email handling with local delivery options
- Process mode: Full store-and-forward pipeline with content scanning
- Implement mode-specific configuration and handlers
- Create fallback handling for unmatched domains
### 1.4 Shared Infrastructure
2025-05-08 00:39:43 +00:00
- [x] Develop shared components across all email handling modes
- [x] Create unified delivery queue for all outbound email
- [x] Implement shared authentication system
- [x] Build common TLS and certificate management
- [x] Create uniform logging and metrics collection
- [x] Develop shared rate limiting and throttling
2025-05-08 00:12:36 +00:00
## 2. Consolidated Email Processing Features
### 2.1 Pattern-Based Routing
2025-05-08 00:39:43 +00:00
- [x] Implement glob pattern-based email routing
2025-05-08 00:12:36 +00:00
- Create glob pattern matching for both domains and full email addresses
- Support wildcards for domains, subdomains, and local parts (e.g., `*@domain.com` , `user@*.domain.com` )
- Add support for pattern matching priorities (most specific wins)
- Implement cacheable pattern matching for performance
- Create comprehensive test suite for pattern matching
### 2.2 Multi-Modal Processing
2025-05-08 00:39:43 +00:00
- [x] Develop multiple email handling modes
2025-05-08 00:12:36 +00:00
- Forward mode: Simple SMTP forwarding to another server with enhanced routing
- MTA mode: Process with the MTA for programmatic handling and local delivery
- Process mode: Full store-and-forward processing with content scanning
- Add mode-specific configuration validation
- Implement seamless mode transitions based on patterns
### 2.3 Content Inspection and Transformation
2025-05-08 00:39:43 +00:00
- [x] Enhance content inspection for processing mode
- [x] Improve MIME parsing and content extraction capabilities
- [x] Enhance attachment scanning and filtering
- [x] Add text analysis for spam and phishing detection
- [x] Create more robust transformation framework
- [x] Support content-based routing decisions
2025-05-08 00:12:36 +00:00
### 2.4 Unified Rate Limiting and Traffic Control
2025-05-08 00:39:43 +00:00
- [x] Build unified rate limiting across all modes
- [x] Implement pattern-based rate limits
- [x] Create hierarchical rate limiting (global, pattern, IP)
- [x] Add real-time rate limit monitoring
- [x] Develop traffic shaping capabilities
- [x] Implement backpressure mechanisms for overload protection
2025-05-08 00:12:36 +00:00
## 3. DcRouter Integration
### 3.1 Unified Configuration Interface
2025-05-08 00:39:43 +00:00
- [x] Implement the consolidated emailConfig interface
2025-05-08 00:12:36 +00:00
- Create the IEmailConfig interface with all required components
- Replace existing SMTP, forwarding, and MTA configs with unified approach
- Add backward compatibility layer for existing configurations
- Provide comprehensive validation for the new configuration format
- Add clear documentation and examples in code comments
### 3.2 Enhanced Management API
2025-05-08 00:39:43 +00:00
- [x] Develop enhanced management API for consolidated email handling
2025-05-08 00:12:36 +00:00
- Create unified status reporting across all modes
- Implement pattern-based rule management (add, update, remove)
- Add comprehensive queue management across all modes
- Create mode-specific monitoring endpoints
- Implement enhanced configuration update methods
### 3.3 Unified Metrics and Logging
2025-05-08 00:39:43 +00:00
- [x] Create a unified metrics system for all email handling
2025-05-08 00:12:36 +00:00
- Develop pattern-based metrics collection
- Implement mode-specific performance metrics
- Create pattern rule effectiveness measurements
- Add real-time monitoring capabilities
- Design comprehensive logging with correlation IDs
2025-05-07 23:30:04 +00:00
## 4. Detailed Component Specifications
### 4.0 DcRouter Configuration Extension
```typescript
export interface IDcRouterOptions {
// Core configuration options
// Direct SmartProxy configuration - gives full control over all TCP/SNI traffic
// including HTTP, HTTPS, and any other TCP-based protocol
smartProxyConfig?: plugins.smartproxy.ISmartProxyOptions;
2025-05-08 00:12:36 +00:00
// Unified email configuration for all email handling modes
emailConfig?: IEmailConfig;
2025-05-07 23:30:04 +00:00
// Shared configurations
tls?: {
contactEmail: string;
domain?: string;
certPath?: string;
keyPath?: string;
};
// Other DcRouter options
dnsServerConfig?: plugins.smartdns.IDnsServerOptions;
2025-05-08 00:12:36 +00:00
}
/**
* Consolidated email configuration interface
*/
export interface IEmailConfig {
// Email server settings
ports: number[];
hostname: string;
maxMessageSize?: number;
// TLS configuration for email server
tls?: {
certPath?: string;
keyPath?: string;
caPath?: string;
minVersion?: string;
};
// Authentication for inbound connections
auth?: {
required?: boolean;
methods?: ('PLAIN' | 'LOGIN' | 'OAUTH2')[];
users?: Array< {username: string, password: string}>;
};
// Default routing for unmatched domains
defaultMode: EmailProcessingMode;
defaultServer?: string;
defaultPort?: number;
defaultTls?: boolean;
// Domain rules with glob pattern support
domainRules: IDomainRule[];
// Queue configuration for all email processing
queue?: {
storageType?: 'memory' | 'disk';
persistentPath?: string;
maxRetries?: number;
baseRetryDelay?: number;
maxRetryDelay?: number;
};
// Advanced MTA settings
mtaGlobalOptions?: IMtaOptions;
}
/**
* Email processing modes
*/
export type EmailProcessingMode = 'forward' | 'mta' | 'process';
/**
* Domain rule interface for pattern-based routing
*/
export interface IDomainRule {
// Domain pattern (e.g., "*@example .com", "*@*.example.net")
pattern: string;
// Handling mode for this pattern
mode: EmailProcessingMode;
// Forward mode configuration
target?: {
server: string;
port?: number;
useTls?: boolean;
authentication?: {
user?: string;
pass?: string;
};
};
// MTA mode configuration
mtaOptions?: IMtaOptions;
// Process mode configuration
contentScanning?: boolean;
scanners?: IContentScanner[];
transformations?: ITransformation[];
// Rate limits for this domain
rateLimits?: {
maxMessagesPerMinute?: number;
maxRecipientsPerMessage?: number;
};
}
/**
* MTA options interface
*/
export interface IMtaOptions {
domain?: string;
allowLocalDelivery?: boolean;
localDeliveryPath?: string;
dkimSign?: boolean;
dkimOptions?: {
domainName: string;
keySelector: string;
privateKey: string;
};
smtpBanner?: string;
maxConnections?: number;
connTimeout?: number;
spoolDir?: string;
}
/**
* Content scanner interface
*/
export interface IContentScanner {
type: 'spam' | 'virus' | 'attachment';
threshold?: number;
action: 'tag' | 'reject';
blockedExtensions?: string[];
}
/**
* Transformation interface
*/
export interface ITransformation {
type: string;
[key: string]: any;
2025-05-07 23:30:04 +00:00
}
```
2025-05-08 00:12:36 +00:00
### 4.1 UnifiedEmailServer Class
2025-05-07 23:30:04 +00:00
```typescript
2025-05-08 00:12:36 +00:00
/**
* Options for the unified email server
*/
export interface IUnifiedEmailServerOptions {
2025-05-07 23:30:04 +00:00
// Base server options
ports: number[];
hostname: string;
banner?: string;
// Authentication options
2025-05-08 00:12:36 +00:00
auth?: {
required?: boolean;
methods?: ('PLAIN' | 'LOGIN' | 'OAUTH2')[];
users?: Array< {username: string, password: string}>;
};
2025-05-07 23:30:04 +00:00
// TLS options
tls?: {
2025-05-08 00:12:36 +00:00
certPath?: string;
keyPath?: string;
caPath?: string;
2025-05-07 23:30:04 +00:00
minVersion?: string;
2025-05-08 00:12:36 +00:00
ciphers?: string;
2025-05-07 23:30:04 +00:00
};
// Limits
maxMessageSize?: number;
maxClients?: number;
maxConnections?: number;
// Connection options
connectionTimeout?: number;
socketTimeout?: number;
2025-05-08 00:12:36 +00:00
// Domain rules
domainRules: IDomainRule[];
// Default handling for unmatched domains
defaultMode: EmailProcessingMode;
defaultServer?: string;
defaultPort?: number;
defaultTls?: boolean;
2025-05-07 23:30:04 +00:00
}
/**
2025-05-08 00:12:36 +00:00
* Unified email server that handles all email traffic with pattern-based routing
2025-05-07 23:30:04 +00:00
*/
2025-05-08 00:12:36 +00:00
export class UnifiedEmailServer extends EventEmitter {
constructor(options: IUnifiedEmailServerOptions);
2025-05-07 23:30:04 +00:00
// Start and stop the server
2025-05-08 00:12:36 +00:00
public start(): Promise< void > ;
public stop(): Promise< void > ;
// Core event handlers
private onConnect(session: ISmtpSession, callback: (err?: Error) => void): void;
private onAuth(auth: IAuthData, session: ISmtpSession, callback: (err?: Error, user?: any) => void): void;
private onMailFrom(address: {address: string}, session: ISmtpSession, callback: (err?: Error) => void): void;
private onRcptTo(address: {address: string}, session: ISmtpSession, callback: (err?: Error) => void): void;
private onData(stream: Readable, session: ISmtpSession, callback: (err?: Error) => void): void;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
// Pattern matching and routing
private matchDomainRule(address: string): IDomainRule | null;
private determineProcessingMode(session: ISmtpSession): EmailProcessingMode;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
// Mode-specific processing
private handleForwardMode(message: any, session: ISmtpSession, rule: IDomainRule): Promise< void > ;
private handleMtaMode(message: any, session: ISmtpSession, rule: IDomainRule): Promise< void > ;
private handleProcessMode(message: any, session: ISmtpSession, rule: IDomainRule): Promise< void > ;
// Helper methods
private parseEmail(rawData: string): Promise< any > ;
private isIpAllowed(ip: string, rule: IDomainRule): boolean;
private createDeliveryJob(message: any, rule: IDomainRule): IDeliveryJob;
2025-05-07 23:30:04 +00:00
// Configuration updates
2025-05-08 00:12:36 +00:00
public updateOptions(options: Partial< IUnifiedEmailServerOptions > ): void;
public updateDomainRules(rules: IDomainRule[]): void;
2025-05-07 23:30:04 +00:00
// Server stats
2025-05-08 00:12:36 +00:00
public getStats(): IServerStats;
}
```
### 4.2 DomainRouter Class
```typescript
/**
* Options for the domain-based router
*/
export interface IDomainRouterOptions {
// Domain rules with glob pattern matching
domainRules: IDomainRule[];
// Default handling for unmatched domains
defaultMode: EmailProcessingMode;
defaultServer?: string;
defaultPort?: number;
defaultTls?: boolean;
// Pattern matching options
caseSensitive?: boolean;
priorityOrder?: 'most-specific' | 'first-match';
// Cache settings for pattern matching
enableCache?: boolean;
cacheSize?: number;
}
/**
* A pattern matching and routing class for email domains
*/
export class DomainRouter {
constructor(options: IDomainRouterOptions);
/**
* Match an email address against defined rules
* @param email Email address to match
* @returns The matching rule or null if no match
*/
public matchRule(email: string): IDomainRule | null;
/**
* Check if email matches a specific pattern
* @param email Email address to check
* @param pattern Pattern to check against
* @returns True if matching, false otherwise
*/
public matchesPattern(email: string, pattern: string): boolean;
/**
* Get all rules that match an email address
* @param email Email address to match
* @returns Array of matching rules
*/
public getAllMatchingRules(email: string): IDomainRule[];
/**
* Add a new routing rule
* @param rule Domain rule to add
*/
public addRule(rule: IDomainRule): void;
/**
* Update an existing rule
* @param pattern Pattern to update
* @param updates Updates to apply
*/
public updateRule(pattern: string, updates: Partial< IDomainRule > ): boolean;
/**
* Remove a rule
* @param pattern Pattern to remove
*/
public removeRule(pattern: string): boolean;
/**
* Get rule by pattern
* @param pattern Pattern to find
*/
public getRule(pattern: string): IDomainRule | null;
/**
* Get all rules
*/
public getRules(): IDomainRule[];
/**
* Update options
* @param options New options
*/
public updateOptions(options: Partial< IDomainRouterOptions > ): void;
/**
* Clear pattern matching cache
*/
public clearCache(): void;
2025-05-07 23:30:04 +00:00
}
```
2025-05-08 00:12:36 +00:00
### 4.3 MultiModeProcessor Class
2025-05-07 23:30:04 +00:00
```typescript
2025-05-08 00:12:36 +00:00
/**
* Processing modes
*/
export type EmailProcessingMode = 'forward' | 'mta' | 'process';
/**
* Processor options
*/
export interface IMultiModeProcessorOptions {
2025-05-07 23:30:04 +00:00
// Processing options
maxParallelProcessing?: number;
processingTimeout?: number;
2025-05-08 00:12:36 +00:00
// Mode handlers
forwardHandler?: IForwardHandler;
mtaHandler?: IMtaHandler;
processHandler?: IProcessHandler;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
// Queue configuration
queue?: {
storageType?: 'memory' | 'disk';
persistentPath?: string;
maxRetries?: number;
baseRetryDelay?: number;
maxRetryDelay?: number;
};
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
// Shared services
sharedServices?: {
dkimSigner?: IDkimSigner;
contentScanner?: IContentScanner;
rateLimiter?: IRateLimiter;
};
2025-05-07 23:30:04 +00:00
}
/**
2025-05-08 00:12:36 +00:00
* Processes emails using different modes based on domain rules
2025-05-07 23:30:04 +00:00
*/
2025-05-08 00:12:36 +00:00
export class MultiModeProcessor extends EventEmitter {
constructor(options: IMultiModeProcessorOptions);
/**
* Process an email using the appropriate mode
* @param message Parsed email message
* @param session SMTP session
* @param rule Matching domain rule
* @param mode Processing mode
*/
public async processEmail(
message: any,
session: ISmtpSession,
rule: IDomainRule,
mode: EmailProcessingMode
): Promise< IProcessingResult > ;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
/**
* Handle email in forward mode
* @param message Parsed email message
* @param session SMTP session
* @param rule Matching domain rule
*/
private async handleForwardMode(
message: any,
session: ISmtpSession,
rule: IDomainRule
): Promise< IProcessingResult > ;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
/**
* Handle email in MTA mode
* @param message Parsed email message
* @param session SMTP session
* @param rule Matching domain rule
*/
private async handleMtaMode(
message: any,
session: ISmtpSession,
rule: IDomainRule
): Promise< IProcessingResult > ;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
/**
* Handle email in process mode
* @param message Parsed email message
* @param session SMTP session
* @param rule Matching domain rule
*/
private async handleProcessMode(
message: any,
session: ISmtpSession,
rule: IDomainRule
): Promise< IProcessingResult > ;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
/**
* Update processor options
* @param options New options
*/
public updateOptions(options: Partial< IMultiModeProcessorOptions > ): void;
/**
* Get processor statistics
*/
public getStats(): IProcessorStats;
2025-05-07 23:30:04 +00:00
}
```
2025-05-08 00:12:36 +00:00
### 4.4 UnifiedDeliveryQueue Class
2025-05-07 23:30:04 +00:00
```typescript
2025-05-08 00:12:36 +00:00
/**
* Queue item status
*/
export type QueueItemStatus = 'pending' | 'processing' | 'delivered' | 'failed' | 'deferred';
/**
* Queue item interface
*/
export interface IQueueItem {
id: string;
processingMode: EmailProcessingMode;
processingResult: any;
rule: IDomainRule;
status: QueueItemStatus;
attempts: number;
nextAttempt: Date;
lastError?: string;
createdAt: Date;
updatedAt: Date;
deliveredAt?: Date;
}
/**
* Queue options interface
*/
export interface IQueueOptions {
2025-05-07 23:30:04 +00:00
// Storage options
2025-05-08 00:12:36 +00:00
storageType?: 'memory' | 'disk';
persistentPath?: string;
2025-05-07 23:30:04 +00:00
// Queue behavior
checkInterval?: number;
maxQueueSize?: number;
maxPerDestination?: number;
// Delivery attempts
maxRetries?: number;
baseRetryDelay?: number;
maxRetryDelay?: number;
}
/**
2025-05-08 00:12:36 +00:00
* A unified queue for all email modes
2025-05-07 23:30:04 +00:00
*/
2025-05-08 00:12:36 +00:00
export class UnifiedDeliveryQueue extends EventEmitter {
2025-05-07 23:30:04 +00:00
constructor(options: IQueueOptions);
2025-05-08 00:12:36 +00:00
/**
* Initialize the queue
*/
public async initialize(): Promise< void > ;
/**
* Add an item to the queue
* @param processingResult Processing result to queue
* @param mode Processing mode
* @param rule Domain rule
*/
public async enqueue(processingResult: any, mode: EmailProcessingMode, rule: IDomainRule): Promise< string > ;
/**
* Get an item from the queue
* @param id Item ID
*/
public getItem(id: string): IQueueItem | undefined;
/**
* Mark an item as delivered
* @param id Item ID
*/
public async markDelivered(id: string): Promise< boolean > ;
/**
* Mark an item as failed
* @param id Item ID
* @param error Error message
*/
public async markFailed(id: string, error: string): Promise< boolean > ;
/**
* Get queue statistics
*/
public getStats(): IQueueStats;
/**
* Pause queue processing
*/
public pause(): void;
/**
* Resume queue processing
*/
public resume(): void;
/**
* Shutdown the queue
*/
public async shutdown(): Promise< void > ;
2025-05-07 23:30:04 +00:00
}
```
2025-05-08 00:12:36 +00:00
### 4.5 MultiModeDeliverySystem Class
2025-05-07 23:30:04 +00:00
```typescript
2025-05-08 00:12:36 +00:00
/**
* Delivery options
*/
export interface IMultiModeDeliveryOptions {
2025-05-07 23:30:04 +00:00
// Connection options
connectionPoolSize?: number;
socketTimeout?: number;
// Delivery behavior
concurrentDeliveries?: number;
sendTimeout?: number;
// TLS options
verifyCertificates?: boolean;
tlsMinVersion?: string;
2025-05-08 00:12:36 +00:00
// Mode-specific handlers
forwardHandler?: IForwardDeliveryHandler;
mtaHandler?: IMtaDeliveryHandler;
processHandler?: IProcessDeliveryHandler;
2025-05-07 23:30:04 +00:00
// Rate limiting
globalRateLimit?: number;
2025-05-08 00:12:36 +00:00
perPatternRateLimit?: Record< string , number > ;
// Event hooks
onDeliveryStart?: (item: IQueueItem) => Promise< void > ;
onDeliverySuccess?: (item: IQueueItem, result: any) => Promise< void > ;
onDeliveryFailed?: (item: IQueueItem, error: string) => Promise< void > ;
2025-05-07 23:30:04 +00:00
}
/**
2025-05-08 00:12:36 +00:00
* Handles delivery for all email processing modes
2025-05-07 23:30:04 +00:00
*/
2025-05-08 00:12:36 +00:00
export class MultiModeDeliverySystem extends EventEmitter {
constructor(queue: UnifiedDeliveryQueue, options: IMultiModeDeliveryOptions);
/**
* Start the delivery system
*/
public async start(): Promise< void > ;
/**
* Stop the delivery system
*/
public async stop(): Promise< void > ;
/**
* Deliver an item from the queue
* @param item Queue item to deliver
*/
private async deliverItem(item: IQueueItem): Promise< void > ;
/**
* Handle delivery in forward mode
* @param item Queue item
*/
private async handleForwardDelivery(item: IQueueItem): Promise< any > ;
/**
* Handle delivery in MTA mode
* @param item Queue item
*/
private async handleMtaDelivery(item: IQueueItem): Promise< any > ;
/**
* Handle delivery in process mode
* @param item Queue item
*/
private async handleProcessDelivery(item: IQueueItem): Promise< any > ;
/**
* Update delivery options
* @param options New options
*/
public updateOptions(options: Partial< IMultiModeDeliveryOptions > ): void;
/**
* Get delivery statistics
*/
public getStats(): IDeliveryStats;
2025-05-07 23:30:04 +00:00
}
```
2025-05-08 00:12:36 +00:00
### 4.6 DcRouter Integration with EmailConfig
2025-05-07 23:30:04 +00:00
```typescript
2025-05-08 00:12:36 +00:00
/**
* DcRouter options with emailConfig
*/
export interface IDcRouterOptions {
// Direct SmartProxy configuration for general HTTP/HTTPS and TCP/SNI traffic
smartProxyConfig?: plugins.smartproxy.ISmartProxyOptions;
// Consolidated email configuration
emailConfig?: IEmailConfig;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
// Shared TLS configuration
2025-05-07 23:30:04 +00:00
tls?: {
2025-05-08 00:12:36 +00:00
contactEmail: string;
domain?: string;
2025-05-07 23:30:04 +00:00
certPath?: string;
keyPath?: string;
};
2025-05-08 00:12:36 +00:00
// DNS server configuration
dnsServerConfig?: plugins.smartdns.IDnsServerOptions;
}
/**
* DcRouter with consolidated email handling
*/
export class DcRouter {
// Core services
public smartProxy?: plugins.smartproxy.SmartProxy;
public dnsServer?: plugins.smartdns.DnsServer;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
// Unified email components
public emailServer?: UnifiedEmailServer;
public domainRouter?: DomainRouter;
public multiModeProcessor?: MultiModeProcessor;
public deliveryQueue?: UnifiedDeliveryQueue;
public deliverySystem?: MultiModeDeliverySystem;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
constructor(options: IDcRouterOptions);
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
/**
* Start DcRouter services
*/
public async start(): Promise< void > ;
/**
* Stop DcRouter services
*/
public async stop(): Promise< void > ;
/**
* Set up email handling
*/
private async setupEmailHandling(): Promise< void > ;
2025-05-07 23:30:04 +00:00
2025-05-08 00:12:36 +00:00
/**
* Update email configuration
* @param config New email configuration
*/
public async updateEmailConfig(config: IEmailConfig): Promise< void > ;
/**
* Update domain rules
* @param rules New domain rules
*/
public async updateDomainRules(rules: IDomainRule[]): Promise< void > ;
/**
* Get DcRouter statistics
*/
public getStats(): IDcRouterStats;
2025-05-07 23:30:04 +00:00
}
```
## 5. Implementation Phases
2025-05-08 00:12:36 +00:00
### Phase 1: Core Architecture and Pattern Matching
2025-05-08 00:39:43 +00:00
- [x] Create the UnifiedEmailServer class foundation
- [x] Implement the DomainRouter with glob pattern matching
- [x] Build pattern priority system (most specific match first)
- [x] Create pattern caching mechanism for performance
- [x] Implement validation for email patterns
- [x] Build test suite for pattern matching system
2025-05-08 00:12:36 +00:00
### Phase 2: Multi-Modal Processing Framework
2025-05-08 00:39:43 +00:00
- [x] Build the MultiModeProcessor class
- [x] Implement mode-specific handlers (forward, MTA, process)
- [x] Create processing pipeline for each mode
- [x] Implement content scanning for process mode
- [x] Build shared services infrastructure
- [x] Add validation for mode-specific configurations
2025-05-08 00:12:36 +00:00
### Phase 3: Unified Queue and Delivery System
2025-05-08 00:39:43 +00:00
- [x] Implement the UnifiedDeliveryQueue
- [x] Create persistent storage for all processing modes
- [x] Build the MultiModeDeliverySystem
- [x] Implement mode-specific delivery handlers
- [x] Create shared retry logic with exponential backoff
- [x] Add delivery tracking and notification
2025-05-08 00:12:36 +00:00
### Phase 4: DcRouter Integration
2025-05-08 00:39:43 +00:00
- [x] Implement the consolidated emailConfig interface
- [x] Integrate all components into DcRouter
- [x] Add configuration validation
- [x] Create management APIs for updating rules
- [x] Implement migration support for existing configurations
- [x] Build mode-specific metrics and logging
2025-05-08 00:12:36 +00:00
### Phase 5: Testing and Documentation
2025-05-08 01:13:54 +00:00
- [x] Create comprehensive unit tests for all components
- [x] Implement integration tests for all processing modes
- [x] Test pattern matching with complex scenarios
- [x] Create performance tests for high-volume scenarios
- [x] Build detailed documentation and examples
- [x] Identify and document legacy components to be deprecated (EmailDomainRouter)
- [x] Remove deprecated components (EmailDomainRouter)
2025-05-07 23:30:04 +00:00
## 6. Technical Requirements
### 6.1 Dependencies
- SMTP server library (smtp-server or similar)
- Email parsing library (mailparser or similar)
- MIME handling library
- DKIM signing library
- Queue management system (optional Redis support)
- Cryptographic libraries for TLS and authentication
### 6.2 Performance Targets
- Handle 1000+ concurrent SMTP connections
- Process 100+ messages per second on standard hardware
- Support message sizes up to 50MB
- Maintain delivery queue of 100,000+ messages
- Sub-second processing time for standard emails
### 6.3 Security Requirements
- Full TLS support with modern cipher configurations
- Authentication verification and rate limiting
- Input validation for all SMTP commands
- Secure storage of queued emails
- Proper error handling to prevent information leakage
- Access controls based on IP addresses and authentication
## 7. API Examples
### 7.1 Basic DcRouter SMTP Configuration
```typescript
const dcRouter = new DcRouter({
// HTTP configuration...
smtpConfig: {
ports: [25, 587, 465],
hostname: 'mail.example.com',
maxMessageSize: 50 * 1024 * 1024, // 50MB
// TLS configuration
tls: {
certPath: '/path/to/cert.pem',
keyPath: '/path/to/key.pem'
},
// Domain routing
domainConfigs: [
{
domains: ['example.com', '*.example.com'],
targetIPs: ['mail1.example.com', 'mail2.example.com'],
port: 25,
useTls: true
}
],
// Default routing
defaultServer: 'fallback-mail.example.com',
defaultPort: 25,
useTls: true,
// Queue settings
queueStorage: 'disk',
persistentPath: '/var/mail/queue',
maxRetries: 5
}
});
```
### 7.2 Advanced Configuration with Processing
```typescript
const dcRouter = new DcRouter({
// HTTP configuration...
smtpConfig: {
// Basic settings
ports: [25, 587, 465],
hostname: 'mail.example.com',
// Domain routing with advanced features
domainConfigs: [
{
domains: ['example.com', '*.example.com'],
targetIPs: ['mail1.example.com', 'mail2.example.com'],
port: 25,
useTls: true,
// Add custom headers
addHeaders: true,
headerInfo: [
{ name: 'X-Processed-By', value: 'gateway' },
{ name: 'X-Scanned', value: 'true' }
],
// Sign with DKIM
signDkim: true,
dkimOptions: {
domainName: 'example.com',
keySelector: 'mail',
privateKey: '...'
},
// Rate limiting
rateLimits: {
maxMessagesPerMinute: 100,
maxRecipientsPerMessage: 50
}
}
],
// Content scanning
contentScanning: true,
scanners: [
{
type: 'spam',
threshold: 5.0,
action: 'tag'
},
{
type: 'virus',
action: 'reject'
},
{
type: 'attachment',
blockedExtensions: ['.exe', '.bat', '.vbs'],
action: 'reject'
}
],
// Transformations
transformations: [
{
type: 'addHeader',
header: 'X-Gateway',
value: 'DcRouter 1.0'
},
{
type: 'dkimSign',
domains: ['example.com']
}
]
}
});
```
## 8. Extensibility Points
### 8.1 Plugin Architecture
- Custom content scanners
- Custom transformation handlers
- Routing rule extensions
- Authentication providers
- Queue storage backends
### 8.2 Event System
- Connection events (connect, disconnect, error)
- Message events (received, processed, queued, delivered)
- Error events (delivery failure, processing error)
- Performance events (queue size, processing time)
- Security events (authentication failure, policy violation)
## 9. Migration Plan
### 9.1 From Simple Proxy to Store-and-Forward
2025-05-08 00:12:36 +00:00
- [x] Create compatibility layer for existing configurations
- [x] Implement graceful transition from connection proxy to full processing
- [x] Add configuration validation to ensure smooth migration
- [x] Allow components to coexist for flexible deployment options
- [x] Provide documentation with comments for migrating existing deployments
- [x] Remove deprecated files after migration to consolidated approach
2025-05-08 01:13:54 +00:00
- [x] Removed EmailDomainRouter class
- [x] Updated imports and references to use the new DomainRouter
2025-05-07 23:30:04 +00:00
### 9.2 Backward Compatibility
2025-05-08 00:12:36 +00:00
- [x] Maintain support for basic proxy functionality
- [x] Allow MTA, SMTP forwarding, and store-and-forward to work together
- [x] Support multiple concurrent email handling approaches
- [x] Enable hybrid deployments with different services running simultaneously
### 9.3 Enhanced Coexistence Support
- [x] Modified the startup sequence to enable concurrent operation of multiple services:
- MTA service can now run alongside SMTP forwarding and store-and-forward processing
- Store-and-forward SMTP processing can run alongside MTA and SMTP forwarding
- SMTP forwarding now uses its own dedicated SmartProxy instance (smtpProxy)
- [x] Updated component lifecycle management to properly start/stop all services
- [x] Added clear separation between service instances to avoid conflicts
- [x] Ensured configuration updates for one component don't affect others
2025-05-07 23:30:04 +00:00
## 10. SmartProxy Integration
### 10.1 SmartProxy Configuration Handling
2025-05-07 23:45:19 +00:00
- [x] Implement comprehensive support for SmartProxy configuration
- Passed through all SmartProxy options directly in DcRouter's configuration
- Added support for all SmartProxy domain configuration features
- Implemented proper handling of SmartProxy events and callbacks
- [x] Added documentation on SmartProxy configuration:
- Documented how all SmartProxy features are available through DcRouter
- Added examples of different configuration approaches
- Provided guidance in code comments
2025-05-07 23:30:04 +00:00
### 10.2 SMTP Integration with SmartProxy
2025-05-07 23:45:19 +00:00
- [x] Ensured store-and-forward SMTP works alongside SmartProxy
- Handled SMTP ports separately from HTTP/HTTPS ports
- Prevented port conflicts between SmartProxy and SMTP server
- Created code structure showing SmartProxy and SMTP working together
- [x] Implemented combined usage model:
2025-05-07 23:30:04 +00:00
- HTTP/HTTPS traffic using SmartProxy configuration
- SMTP traffic using store-and-forward for advanced processing
2025-05-07 23:45:19 +00:00
- Added support for multi-service environments
2025-05-07 23:30:04 +00:00
## 11. Documentation Requirements
### 11.1 Code Documentation
2025-05-08 01:13:54 +00:00
- [x] Comprehensive JSDoc comments for all classes and methods
- [x] Interface definitions with detailed parameter descriptions
- [x] Example code snippets for common operations
- [x] Architecture documentation with component diagrams
- [x] Decision logs for key design choices
2025-05-07 23:30:04 +00:00
### 11.2 User Documentation
2025-05-08 01:13:54 +00:00
- [x] Getting started guide with configuration approach selection guidance
- [x] Complete configuration reference for both approaches
- [x] Deployment scenarios and examples
- [x] Troubleshooting guide
- [x] Performance tuning recommendations
- [x] Security best practices
2025-05-07 23:30:04 +00:00
### 11.3 Direct SmartProxy Configuration Guide
2025-05-08 01:13:54 +00:00
- [x] Detailed guide on using SmartProxy's domain configuration capabilities
- [x] Examples of complex routing scenarios with SmartProxy
- [x] Performance optimization tips for SmartProxy configurations
- [x] Security settings for SmartProxy deployments