platformservice/readme.plan.md

172 lines
7.9 KiB
Markdown

# DcRouter Improvement Plan
## Objective
Create a new version of DcRouter that doesn't rely on SzPlatformService but instead uses SmartProxy and the complete email stack directly. This will make DcRouter more modular, lightweight, and capable of SMTP forwarding with the email stack.
## 1. Core Architecture Changes
### 1.1 Remove SzPlatformService Dependency
- [x] Remove the `platformServiceInstance` option from `IDcRouterOptions`
- Update `classes.dcrouter.ts` to remove platformServiceInstance from the options interface
- Create a new utility class `DcRouterEnvironment` to handle environment variables directly using qenv
- Replace all uses of SzDcRouterConnector with direct calls to DcRouterEnvironment
### 1.2 Direct Integration with Email Stack
- [x] Add options for direct MtaService integration
- Enhance the existing mtaConfig/mtaServiceInstance options to include all necessary MTA configuration
- Add new options for email forwarding capabilities that will be passed to the MTA service
- Implement initializers for setting up MTA with or without existing instances
- [x] Create a new SMTP forwarding configuration interface
- Create `ISmtpForwardingConfig` interface with fields for destination domains, routing rules, and authentication
- Implement domain-to-server mapping for routing emails to appropriate SMTP servers
- Add options for SMTP authentication methods (PLAIN, LOGIN, OAUTH2)
- [x] Implement proper connection between SmartProxy and MTA service
- Update `configureSmtpProxy()` method to create bidirectional communication with MTA service
- Implement proxy protocol support for preserving client IP addresses during SMTP forwarding
- Create listener for MTA status changes to adjust proxy settings dynamically
## 2. SmartProxy Configuration
### 2.1 Enhanced SmartProxy Integration
- [x] Update `SmartProxy` configuration for better TCP/SNI handling
- Modify initialization to support more advanced SNI-based routing decisions
- [x] Add specific configuration for SMTP ports (25, 465, 587)
- Create a `SmtpPortConfig` class to manage SMTP-specific port settings
- Add TLS termination options specific to SMTP protocols (STARTTLS vs. implicit TLS)
- Implement connection rate limiting and concurrent connection management for SMTP ports
### 2.2 Routing Configuration
- [x] Allow domain-based routing for email traffic
- Add domain matching patterns with wildcard support for inbound email routing
- Implement sender domain-based routing for outbound emails
- Create domain groups for applying consistent rules across related domains
- [x] Implement IP-based allow/block lists for advanced filtering
- Develop IP range and CIDR notation support for filtering
- Create separate lists for inbound and outbound connections
- Implement geo-based filtering using IP geolocation
## 3. SMTP Forwarding Functionality
### 3.1 SMTP Routing Engine
- [x] Enhance the SMTP rule engine to support advanced forwarding scenarios
- Extend email routing capabilities with SmartProxy configuration
- Add context information to routing for making informed routing decisions
- Implement domain-based routing for traffic management
- [x] Create efficient routing for common email patterns
- Develop email forwarding configuration for common use cases
- Implement domain-to-server mapping for email routing
- Create simple but effective routing mechanisms
- [x] Implement per-domain routing configuration
- Create domain configuration support in SmtpForwardingConfig
- Implement dynamic updating of domain routes
- Add domain-level connection handling
### 3.2 MTA Integration
- [x] Configure MTA service for use with DcRouter
- Extend DcRouter to work with existing MTA configuration
- Implement proper MTA service initialization and startup
- Create clean integration between DcRouter and MTA
- [x] Implement SMTP forwarding as alternative to MTA
- Add SMTP forwarding configuration for simpler deployments
- Implement SmartProxy configuration for SMTP traffic
- Create clean separation between MTA and forwarding modes
- [x] Maintain email traffic integrity
- Ensure proper handling of connections between services
- Implement source IP preservation for proper tracking
- Create configuration options for security settings
## 4. Implementation Tasks
### 4.1 Create New Classes
- [x] Create utility classes to handle configuration
- Implement `DcRouterEnvironment` for environment variable access
- Create `SmtpPortConfig` for managing SMTP port settings
- Implement `EmailDomainRouter` for email domain routing
- [x] Develop SMTP-specific functionality
- Create specialized configuration for SMTP traffic
- Implement port configuration for different SMTP protocols
- Add TLS options handling for SMTP ports
- [x] Implement certificate management
- Utilize SmartProxy's Port80Handler for ACME certificate management
- Add certificate application to NetworkProxy
- Create certificate event handling
### 4.2 Update Existing Components
- [x] Refactor `DcRouter` class to remove SzPlatformService dependencies
- Remove all direct references to SzPlatformService
- Update constructor to use new configuration system
- Refactor initialization logic to work independently
- [x] Update certificate handling to use SmartACME directly
- Implement Port80Handler for ACME certificate management
- Add automatic certificate renewal with event handling
- Apply certificates to appropriate services
- [x] Enhance SmartProxy configuration for better SMTP support
- Implement separate SmartProxy instances for different protocols
- Add TLS settings for different SMTP ports (STARTTLS vs. implicit TLS)
- Create clean service lifecycle management
### 4.3 Configuration Interface
- [x] Create a clean, declarative configuration interface
- Design structured TypeScript interfaces for all configuration options
- Implement simple, focused configuration objects
- Create clean separation between different component configurations
- [x] Support environment variables and programmatic configuration
- Create DcRouterEnvironment for environment variable access
- Implement environment variable caching for better performance
- Allow programmatic configuration updates
- [x] Implement well-defined configuration APIs
- Add typed interfaces for all configuration options
- Create clear documentation in interface comments
- Implement runtime configuration updating
## 5. Testing and Documentation
### 5.1 Code Implementation
- [x] Implement core components
- Create new classes for configuration and domain routing
- Update existing DcRouter with new functionality
- Implement environment variable handling
- [x] Implement SMTP functionality
- Add SMTP forwarding configuration
- Implement port-specific settings
- Create domain-based email routing
- [x] Implement HTTP/HTTPS functionality
- Add NetworkProxy integration
- Implement certificate management
- Create domain-based HTTP routing
### 5.2 Quality and Performance
- [x] Ensure code quality
- Fix all TypeScript errors
- Implement clean interfaces
- Create well-documented code
- [x] Optimize for performance
- Implement parallel service shutdown
- Use environment variable caching
- Create efficient routing lookups
- [x] Maintain compatibility
- Ensure backward compatibility where possible
- Create clean extension points
- Maintain consistent APIs
## 6. Future Enhancements (Pending)
### 6.1 Testing
- [ ] Create unit tests for all components
- Test environment variable handling
- Test domain routing logic
- Test certificate management
- [ ] Create integration tests
- Test email forwarding between domains
- Test HTTP/HTTPS routing
- Test TLS connections
### 6.2 Documentation
- [ ] Create comprehensive user documentation
- Add setup guide for common scenarios
- Document all configuration options
- Provide example configurations
- [ ] Create API documentation
- Document all public interfaces
- Add usage examples
- Create integration examples