feat(routes,email): persist system DNS routes with runtime hydration and add reusable email ops DNS helpers
This commit is contained in:
@@ -30,7 +30,8 @@ import { SecurityLogger, ContentScanner, IPReputationChecker } from './security/
|
||||
import { type IHttp3Config, augmentRoutesWithHttp3 } from './http3/index.js';
|
||||
import { DnsManager } from './dns/manager.dns.js';
|
||||
import { AcmeConfigManager } from './acme/manager.acme-config.js';
|
||||
import { EmailDomainManager, SmartMtaStorageManager } from './email/index.js';
|
||||
import { EmailDomainManager, SmartMtaStorageManager, buildEmailDnsRecords } from './email/index.js';
|
||||
import type { IRoute } from '../ts_interfaces/data/route-management.js';
|
||||
|
||||
export interface IDcRouterOptions {
|
||||
/** Base directory for all dcrouter data. Defaults to ~/.serve.zone/dcrouter */
|
||||
@@ -314,7 +315,8 @@ export class DcRouter {
|
||||
// Seed routes assembled during setupSmartProxy, passed to RouteConfigManager for DB seeding
|
||||
private seedConfigRoutes: plugins.smartproxy.IRouteConfig[] = [];
|
||||
private seedEmailRoutes: plugins.smartproxy.IRouteConfig[] = [];
|
||||
// Runtime-only DoH routes. These carry live socket handlers and must never be persisted.
|
||||
private seedDnsRoutes: plugins.smartproxy.IRouteConfig[] = [];
|
||||
// Live DoH routes used during SmartProxy bootstrap before RouteConfigManager re-applies stored routes.
|
||||
private runtimeDnsRoutes: plugins.smartproxy.IRouteConfig[] = [];
|
||||
|
||||
// Environment access
|
||||
@@ -588,13 +590,15 @@ export class DcRouter {
|
||||
this.tunnelManager.syncAllowedEdges();
|
||||
}
|
||||
},
|
||||
() => this.runtimeDnsRoutes,
|
||||
undefined,
|
||||
(storedRoute: IRoute) => this.hydrateStoredRouteForRuntime(storedRoute),
|
||||
);
|
||||
this.apiTokenManager = new ApiTokenManager();
|
||||
await this.apiTokenManager.initialize();
|
||||
await this.routeConfigManager.initialize(
|
||||
this.seedConfigRoutes as import('../ts_interfaces/data/remoteingress.js').IDcRouterRouteConfig[],
|
||||
this.seedEmailRoutes as import('../ts_interfaces/data/remoteingress.js').IDcRouterRouteConfig[],
|
||||
this.seedDnsRoutes as import('../ts_interfaces/data/remoteingress.js').IDcRouterRouteConfig[],
|
||||
);
|
||||
await this.targetProfileManager.normalizeAllRouteRefs();
|
||||
|
||||
@@ -912,10 +916,12 @@ export class DcRouter {
|
||||
logger.log('debug', 'Email routes generated', { routes: JSON.stringify(this.seedEmailRoutes) });
|
||||
}
|
||||
|
||||
this.seedDnsRoutes = [];
|
||||
this.runtimeDnsRoutes = [];
|
||||
if (this.options.dnsNsDomains && this.options.dnsNsDomains.length > 0) {
|
||||
this.runtimeDnsRoutes = this.generateDnsRoutes();
|
||||
logger.log('debug', `DNS routes for nameservers ${this.options.dnsNsDomains.join(', ')}`, { routes: JSON.stringify(this.runtimeDnsRoutes) });
|
||||
this.seedDnsRoutes = this.generateDnsRoutes({ includeSocketHandler: false });
|
||||
this.runtimeDnsRoutes = this.generateDnsRoutes({ includeSocketHandler: true });
|
||||
logger.log('debug', `DNS routes for nameservers ${this.options.dnsNsDomains.join(', ')}`, { routes: JSON.stringify(this.seedDnsRoutes) });
|
||||
}
|
||||
|
||||
// Combined routes for SmartProxy bootstrap (before DB routes are loaded)
|
||||
@@ -1338,19 +1344,20 @@ export class DcRouter {
|
||||
/**
|
||||
* Generate SmartProxy routes for DNS configuration
|
||||
*/
|
||||
private generateDnsRoutes(): plugins.smartproxy.IRouteConfig[] {
|
||||
private generateDnsRoutes(options?: { includeSocketHandler?: boolean }): plugins.smartproxy.IRouteConfig[] {
|
||||
if (!this.options.dnsNsDomains || this.options.dnsNsDomains.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
const includeSocketHandler = options?.includeSocketHandler !== false;
|
||||
const dnsRoutes: plugins.smartproxy.IRouteConfig[] = [];
|
||||
|
||||
|
||||
// Create routes for DNS-over-HTTPS paths
|
||||
const dohPaths = ['/dns-query', '/resolve'];
|
||||
|
||||
|
||||
// Use the first nameserver domain for DoH routes
|
||||
const primaryNameserver = this.options.dnsNsDomains[0];
|
||||
|
||||
|
||||
for (const path of dohPaths) {
|
||||
const dohRoute: plugins.smartproxy.IRouteConfig = {
|
||||
name: `dns-over-https-${path.replace('/', '')}`,
|
||||
@@ -1359,18 +1366,42 @@ export class DcRouter {
|
||||
domains: [primaryNameserver],
|
||||
path: path
|
||||
},
|
||||
action: {
|
||||
type: 'socket-handler' as any,
|
||||
socketHandler: this.createDnsSocketHandler()
|
||||
} as any
|
||||
action: includeSocketHandler
|
||||
? {
|
||||
type: 'socket-handler' as any,
|
||||
socketHandler: this.createDnsSocketHandler()
|
||||
} as any
|
||||
: {
|
||||
type: 'socket-handler' as any,
|
||||
} as any
|
||||
};
|
||||
|
||||
|
||||
dnsRoutes.push(dohRoute);
|
||||
}
|
||||
|
||||
|
||||
return dnsRoutes;
|
||||
}
|
||||
|
||||
private hydrateStoredRouteForRuntime(storedRoute: IRoute): plugins.smartproxy.IRouteConfig | undefined {
|
||||
const routeName = storedRoute.route.name || '';
|
||||
const isDohRoute = storedRoute.origin === 'dns'
|
||||
&& storedRoute.route.action?.type === 'socket-handler'
|
||||
&& routeName.startsWith('dns-over-https-');
|
||||
|
||||
if (!isDohRoute) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
...storedRoute.route,
|
||||
action: {
|
||||
...storedRoute.route.action,
|
||||
type: 'socket-handler' as any,
|
||||
socketHandler: this.createDnsSocketHandler(),
|
||||
} as any,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a domain matches a pattern (including wildcard support)
|
||||
* @param domain The domain to check
|
||||
@@ -1939,37 +1970,20 @@ export class DcRouter {
|
||||
for (const domainConfig of internalDnsDomains) {
|
||||
const domain = domainConfig.domain;
|
||||
const ttl = domainConfig.dns?.internal?.ttl || 3600;
|
||||
const mxPriority = domainConfig.dns?.internal?.mxPriority || 10;
|
||||
|
||||
// MX record - points to the domain itself for email handling
|
||||
records.push({
|
||||
name: domain,
|
||||
type: 'MX',
|
||||
value: `${mxPriority} ${domain}`,
|
||||
ttl
|
||||
});
|
||||
|
||||
// SPF record - using sensible defaults
|
||||
const spfRecord = 'v=spf1 a mx ~all';
|
||||
records.push({
|
||||
name: domain,
|
||||
type: 'TXT',
|
||||
value: spfRecord,
|
||||
ttl
|
||||
});
|
||||
|
||||
// DMARC record - using sensible defaults
|
||||
const dmarcPolicy = 'none'; // Start with 'none' policy for monitoring
|
||||
const dmarcEmail = `dmarc@${domain}`;
|
||||
records.push({
|
||||
name: `_dmarc.${domain}`,
|
||||
type: 'TXT',
|
||||
value: `v=DMARC1; p=${dmarcPolicy}; rua=mailto:${dmarcEmail}`,
|
||||
ttl
|
||||
});
|
||||
|
||||
// Note: DKIM records will be generated later when DKIM keys are available
|
||||
// They require the DKIMCreator which is part of the email server
|
||||
const requiredRecords = buildEmailDnsRecords({
|
||||
domain,
|
||||
hostname: this.options.emailConfig.hostname,
|
||||
mxPriority: domainConfig.dns?.internal?.mxPriority,
|
||||
}).filter((record) => !record.name.includes('._domainkey.'));
|
||||
|
||||
for (const record of requiredRecords) {
|
||||
records.push({
|
||||
name: record.name,
|
||||
type: record.type,
|
||||
value: record.value,
|
||||
ttl,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
logger.log('info', `Generated ${records.length} email DNS records for ${internalDnsDomains.length} internal-dns domains`);
|
||||
|
||||
Reference in New Issue
Block a user