# DcRouter SMTP Store-and-Forward Implementation Plan
## Overview
This plan outlines the implementation of a store-and-forward SMTP proxy within DcRouter that receives emails, processes them, and forwards them to the appropriate destinations. This capability expands DcRouter beyond simple connection proxying to provide full control over email flow, including content inspection, transformation, and reliable delivery.
## 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
// SMTP-specific configurations - can be used alongside smartProxyConfig
// SMTP Store-and-forward configuration for advanced email processing
smtpConfig?: ISmtpConfig;
// 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...
],
// Additional SmartProxy options...
},
// Email-specific configuration (complementary to smartProxyConfig)
smtpConfig: {
// Email handling configuration...
},
// Other DcRouter configuration...
}
```
### 0.2 Store-and-Forward SMTP Implementation
For advanced email handling, we'll build a complete store-and-forward SMTP system to work alongside the direct SmartProxy configuration. This provides full control over email processing while maintaining SmartProxy's flexibility for HTTP/HTTPS traffic:
## 1. Core Architecture
### 1.1 SMTP Server Implementation
- [ ] Integrate an SMTP server library (like `smtp-server`) to accept incoming mail
- Create a wrapper class that initializes and manages the SMTP server instance
- Configure to listen on standard ports (25, 587, 465)
- Implement TLS support (STARTTLS and implicit TLS)
- Support authentication methods (PLAIN, LOGIN, OAUTH2)
- Set up size limits and connection timeouts
### 1.2 Email Processing Pipeline
- [ ] Create a modular processing pipeline for emails
- Build the core pipeline executor that manages the processing workflow
- Implement plugin architecture for extensible processing steps
- Create interfaces for each processing stage
- Add metrics and logging points throughout the pipeline
### 1.3 Queue Management
- [ ] Develop a persistent queue system for email delivery
- Implement in-memory queue for immediate delivery attempts
- Create persistent storage for delivery retry queue
- Build queue manager with priority, sorting, and scheduling capabilities
- Add transaction support to prevent message loss during crashes
### 1.4 Email Delivery System
- [ ] Create a robust delivery system for outbound email
- Implement connection pool for outbound SMTP connections
- Add retry logic with configurable exponential backoff
- Create delivery status tracking and notification
- Set up bounce handling and processing
## 2. Email Processing Features
### 2.1 Routing and Forwarding
- [ ] Implement flexible email routing based on various criteria
- Create domain-based routing rules
- Support pattern matching for domains (exact match, wildcard)
- Implement sender and recipient-based routing
- Add load balancing across multiple target servers
- Create failover support for high availability
### 2.2 Content Inspection
- [ ] Develop content inspection capabilities
- Add MIME parsing and content extraction
- Implement attachment scanning and filtering
- Create text content analysis framework
- Add integration points for external scanners (spam, virus)
- Implement policy enforcement based on content
### 2.3 Email Transformation
- [ ] Create tools for modifying emails during transit
- Implement header addition, modification, and removal
- Add DKIM signing capability
- Support email rewriting (address, content)
- Create tools for attachment handling (remove, sanitize)
- Support for adding tracking or compliance information
### 2.4 Rate Limiting and Traffic Control
- [ ] Build advanced rate limiting controls
- Implement per-IP, per-sender, and per-domain rate limits
- Create tiered rate limiting with different thresholds
- Add traffic shaping capabilities to prevent spikes
- Implement quota enforcement with customizable time windows
- Create alert system for exceeding thresholds
## 3. Integration with DcRouter
### 3.1 Configuration Interface
- [ ] Extend DcRouter's configuration schema
- Create comprehensive SMTP configuration section in IDcRouterOptions
- Define interfaces for each SMTP feature set
- Add validation rules for configuration values
- Implement defaults for all configuration options
- Document configuration parameters thoroughly
### 3.2 Management API
- [ ] Develop management APIs for runtime control
- Create methods to update routing rules without restart