2025-05-04 10:10:07 +00:00
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
import * as paths from '../paths.js';
|
2025-05-07 23:04:54 +00:00
|
|
|
import { SmtpPortConfig, type ISmtpPortSettings } from './classes.smtp.portconfig.js';
|
|
|
|
import { EmailDomainRouter, type IEmailDomainRoutingConfig } from './classes.email.domainrouter.js';
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-07 22:06:55 +00:00
|
|
|
// Certificate types are available via plugins.tsclass
|
2025-05-07 14:33:20 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
// Import the consolidated email config
|
|
|
|
import type { IEmailConfig } from './classes.email.config.js';
|
|
|
|
import { DomainRouter } from './classes.domain.router.js';
|
2025-05-07 23:04:54 +00:00
|
|
|
|
|
|
|
export interface IDcRouterOptions {
|
2025-05-07 23:45:19 +00:00
|
|
|
/**
|
|
|
|
* Direct SmartProxy configuration - gives full control over HTTP/HTTPS and TCP/SNI traffic
|
|
|
|
* This is the preferred way to configure HTTP/HTTPS and general TCP/SNI traffic
|
|
|
|
*/
|
|
|
|
smartProxyConfig?: plugins.smartproxy.ISmartProxyOptions;
|
|
|
|
|
|
|
|
/**
|
2025-05-08 00:12:36 +00:00
|
|
|
* Consolidated email configuration
|
|
|
|
* This enables all email handling with pattern-based routing
|
2025-05-07 23:45:19 +00:00
|
|
|
*/
|
2025-05-08 00:12:36 +00:00
|
|
|
emailConfig?: IEmailConfig;
|
2025-05-07 23:04:54 +00:00
|
|
|
|
|
|
|
/** TLS/certificate configuration */
|
|
|
|
tls?: {
|
|
|
|
/** Contact email for ACME certificates */
|
|
|
|
contactEmail: string;
|
|
|
|
/** Domain for main certificate */
|
|
|
|
domain?: string;
|
|
|
|
/** Path to certificate file (if not using auto-provisioning) */
|
|
|
|
certPath?: string;
|
|
|
|
/** Path to key file (if not using auto-provisioning) */
|
|
|
|
keyPath?: string;
|
|
|
|
};
|
|
|
|
|
2025-05-07 14:33:20 +00:00
|
|
|
/** DNS server configuration */
|
2025-05-04 10:10:07 +00:00
|
|
|
dnsServerConfig?: plugins.smartdns.IDnsServerOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DcRouter can be run on ingress and egress to and from a datacenter site.
|
|
|
|
*/
|
2025-05-07 14:33:20 +00:00
|
|
|
/**
|
|
|
|
* Context passed to HTTP routing rules
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Context passed to port proxy (SmartProxy) routing rules
|
|
|
|
*/
|
|
|
|
export interface PortProxyRuleContext {
|
|
|
|
proxy: plugins.smartproxy.SmartProxy;
|
|
|
|
configs: plugins.smartproxy.IPortProxySettings['domainConfigs'];
|
|
|
|
}
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-04 10:10:07 +00:00
|
|
|
export class DcRouter {
|
2025-05-07 14:33:20 +00:00
|
|
|
public options: IDcRouterOptions;
|
2025-05-07 23:04:54 +00:00
|
|
|
|
|
|
|
// Core services
|
2025-05-07 14:33:20 +00:00
|
|
|
public smartProxy?: plugins.smartproxy.SmartProxy;
|
|
|
|
public dnsServer?: plugins.smartdns.DnsServer;
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
// Unified email components
|
|
|
|
public domainRouter?: DomainRouter;
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-07 23:04:54 +00:00
|
|
|
// Environment access
|
|
|
|
private qenv = new plugins.qenv.Qenv('./', '.nogit/');
|
|
|
|
|
2025-05-04 10:10:07 +00:00
|
|
|
constructor(optionsArg: IDcRouterOptions) {
|
2025-05-07 22:06:55 +00:00
|
|
|
// Set defaults in options
|
|
|
|
this.options = {
|
|
|
|
...optionsArg
|
|
|
|
};
|
2025-05-07 14:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async start() {
|
2025-05-07 23:04:54 +00:00
|
|
|
console.log('Starting DcRouter services...');
|
|
|
|
|
|
|
|
try {
|
2025-05-07 23:45:19 +00:00
|
|
|
// Set up SmartProxy for HTTP/HTTPS and general TCP/SNI traffic
|
|
|
|
if (this.options.smartProxyConfig) {
|
|
|
|
await this.setupSmartProxy();
|
|
|
|
}
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
// Set up unified email handling if configured
|
|
|
|
if (this.options.emailConfig) {
|
|
|
|
await this.setupUnifiedEmailHandling();
|
2025-05-07 23:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Set up DNS server if configured
|
|
|
|
if (this.options.dnsServerConfig) {
|
|
|
|
this.dnsServer = new plugins.smartdns.DnsServer(this.options.dnsServerConfig);
|
|
|
|
await this.dnsServer.start();
|
|
|
|
console.log('DNS server started');
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('DcRouter started successfully');
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error starting DcRouter:', error);
|
|
|
|
// Try to clean up any services that may have started
|
|
|
|
await this.stop();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-05-07 23:45:19 +00:00
|
|
|
* Set up SmartProxy with direct configuration
|
2025-05-07 23:04:54 +00:00
|
|
|
*/
|
2025-05-07 23:45:19 +00:00
|
|
|
private async setupSmartProxy(): Promise<void> {
|
|
|
|
if (!this.options.smartProxyConfig) {
|
2025-05-07 23:04:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
console.log('Setting up SmartProxy with direct configuration');
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
// Create SmartProxy instance with full configuration
|
|
|
|
this.smartProxy = new plugins.smartproxy.SmartProxy(this.options.smartProxyConfig);
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
// Set up event listeners
|
|
|
|
this.smartProxy.on('error', (err) => {
|
|
|
|
console.error('SmartProxy error:', err);
|
2025-05-07 23:04:54 +00:00
|
|
|
});
|
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
if (this.options.smartProxyConfig.acme) {
|
|
|
|
this.smartProxy.on('certificate-issued', (event) => {
|
|
|
|
console.log(`Certificate issued for ${event.domain}, expires ${event.expiryDate}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.smartProxy.on('certificate-renewed', (event) => {
|
|
|
|
console.log(`Certificate renewed for ${event.domain}, expires ${event.expiryDate}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start SmartProxy
|
2025-05-07 23:04:54 +00:00
|
|
|
await this.smartProxy.start();
|
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
console.log('SmartProxy started successfully');
|
2025-05-07 23:04:54 +00:00
|
|
|
}
|
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-04 10:10:07 +00:00
|
|
|
|
2025-05-07 22:06:55 +00:00
|
|
|
/**
|
2025-05-07 23:04:54 +00:00
|
|
|
* Check if a domain matches a pattern (including wildcard support)
|
|
|
|
* @param domain The domain to check
|
|
|
|
* @param pattern The pattern to match against (e.g., "*.example.com")
|
|
|
|
* @returns Whether the domain matches the pattern
|
2025-05-07 22:06:55 +00:00
|
|
|
*/
|
2025-05-07 23:04:54 +00:00
|
|
|
private isDomainMatch(domain: string, pattern: string): boolean {
|
|
|
|
// Normalize inputs
|
|
|
|
domain = domain.toLowerCase();
|
|
|
|
pattern = pattern.toLowerCase();
|
|
|
|
|
|
|
|
// Check for exact match
|
|
|
|
if (domain === pattern) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for wildcard match (*.example.com)
|
|
|
|
if (pattern.startsWith('*.')) {
|
|
|
|
const patternSuffix = pattern.slice(2); // Remove the "*." prefix
|
|
|
|
|
|
|
|
// Check if domain ends with the pattern suffix and has at least one character before it
|
|
|
|
return domain.endsWith(patternSuffix) && domain.length > patternSuffix.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match
|
|
|
|
return false;
|
|
|
|
}
|
2025-05-07 22:06:55 +00:00
|
|
|
|
2025-05-07 23:04:54 +00:00
|
|
|
public async stop() {
|
|
|
|
console.log('Stopping DcRouter services...');
|
|
|
|
|
2025-05-07 22:06:55 +00:00
|
|
|
try {
|
2025-05-07 23:04:54 +00:00
|
|
|
// Stop all services in parallel for faster shutdown
|
|
|
|
await Promise.all([
|
2025-05-08 00:12:36 +00:00
|
|
|
// Stop unified email components if running
|
|
|
|
this.domainRouter ? this.stopUnifiedEmailComponents().catch(err => console.error('Error stopping unified email components:', err)) : Promise.resolve(),
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
// Stop HTTP SmartProxy if running
|
|
|
|
this.smartProxy ? this.smartProxy.stop().catch(err => console.error('Error stopping SmartProxy:', err)) : Promise.resolve(),
|
2025-05-07 23:04:54 +00:00
|
|
|
|
|
|
|
// Stop DNS server if running
|
|
|
|
this.dnsServer ?
|
|
|
|
this.dnsServer.stop().catch(err => console.error('Error stopping DNS server:', err)) :
|
|
|
|
Promise.resolve()
|
|
|
|
]);
|
|
|
|
|
|
|
|
console.log('All DcRouter services stopped');
|
2025-05-07 22:06:55 +00:00
|
|
|
} catch (error) {
|
2025-05-07 23:04:54 +00:00
|
|
|
console.error('Error during DcRouter shutdown:', error);
|
|
|
|
throw error;
|
2025-05-07 22:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-07 23:04:54 +00:00
|
|
|
/**
|
2025-05-07 23:45:19 +00:00
|
|
|
* Update SmartProxy configuration
|
|
|
|
* @param config New SmartProxy configuration
|
2025-05-07 23:04:54 +00:00
|
|
|
*/
|
2025-05-07 23:45:19 +00:00
|
|
|
public async updateSmartProxyConfig(config: plugins.smartproxy.ISmartProxyOptions): Promise<void> {
|
|
|
|
// Stop existing SmartProxy if running
|
2025-05-04 10:10:07 +00:00
|
|
|
if (this.smartProxy) {
|
|
|
|
await this.smartProxy.stop();
|
2025-05-07 23:04:54 +00:00
|
|
|
this.smartProxy = undefined;
|
2025-05-04 10:10:07 +00:00
|
|
|
}
|
2025-05-07 22:06:55 +00:00
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
// Update configuration
|
|
|
|
this.options.smartProxyConfig = config;
|
|
|
|
|
|
|
|
// Start new SmartProxy with updated configuration
|
|
|
|
await this.setupSmartProxy();
|
|
|
|
|
|
|
|
console.log('SmartProxy configuration updated');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
|
2025-05-07 23:45:19 +00:00
|
|
|
/**
|
2025-05-08 00:12:36 +00:00
|
|
|
* Set up unified email handling with pattern-based routing
|
|
|
|
* This implements the consolidated emailConfig approach
|
2025-05-07 23:45:19 +00:00
|
|
|
*/
|
2025-05-08 00:12:36 +00:00
|
|
|
private async setupUnifiedEmailHandling(): Promise<void> {
|
|
|
|
console.log('Setting up unified email handling with pattern-based routing');
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
if (!this.options.emailConfig) {
|
|
|
|
throw new Error('Email configuration is required for unified email handling');
|
|
|
|
}
|
2025-05-07 23:45:19 +00:00
|
|
|
|
|
|
|
try {
|
2025-05-08 00:12:36 +00:00
|
|
|
// 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
|
2025-05-07 23:45:19 +00:00
|
|
|
});
|
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
// TODO: Initialize the full unified email processing pipeline
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
console.log(`Unified email handling configured with ${this.options.emailConfig.domainRules.length} domain rules`);
|
2025-05-07 23:45:19 +00:00
|
|
|
} catch (error) {
|
2025-05-08 00:12:36 +00:00
|
|
|
console.error('Error setting up unified email handling:', error);
|
2025-05-07 23:45:19 +00:00
|
|
|
throw error;
|
|
|
|
}
|
2025-05-07 14:33:20 +00:00
|
|
|
}
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-07 14:33:20 +00:00
|
|
|
/**
|
2025-05-08 00:12:36 +00:00
|
|
|
* Update the unified email configuration
|
|
|
|
* @param config New email configuration
|
2025-05-07 14:33:20 +00:00
|
|
|
*/
|
2025-05-08 00:12:36 +00:00
|
|
|
public async updateEmailConfig(config: IEmailConfig): Promise<void> {
|
|
|
|
// Stop existing email components
|
|
|
|
await this.stopUnifiedEmailComponents();
|
2025-05-07 23:04:54 +00:00
|
|
|
|
|
|
|
// Update configuration
|
2025-05-08 00:12:36 +00:00
|
|
|
this.options.emailConfig = config;
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
// Start email handling with new configuration
|
|
|
|
await this.setupUnifiedEmailHandling();
|
2025-05-07 23:04:54 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
console.log('Unified email configuration updated');
|
2025-05-04 10:10:07 +00:00
|
|
|
}
|
2025-05-07 23:45:19 +00:00
|
|
|
|
|
|
|
/**
|
2025-05-08 00:12:36 +00:00
|
|
|
* Stop all unified email components
|
2025-05-07 23:45:19 +00:00
|
|
|
*/
|
2025-05-08 00:12:36 +00:00
|
|
|
private async stopUnifiedEmailComponents(): Promise<void> {
|
|
|
|
// TODO: Implement stopping all unified email components
|
2025-05-07 23:45:19 +00:00
|
|
|
|
2025-05-08 00:12:36 +00:00
|
|
|
// Clear the domain router
|
|
|
|
this.domainRouter = undefined;
|
2025-05-07 23:45:19 +00:00
|
|
|
}
|
|
|
|
|
2025-05-04 10:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default DcRouter;
|