This commit is contained in:
2025-05-28 18:07:07 +00:00
parent 455b0085ec
commit 6c8458f63c
5 changed files with 393 additions and 5 deletions

View File

@ -116,9 +116,16 @@ export class DcRouter {
// Set up DNS server if configured
if (this.options.dnsServerConfig) {
this.dnsServer = new plugins.smartdns.DnsServer(this.options.dnsServerConfig);
const { records, ...dnsServerOptions } = this.options.dnsServerConfig as any;
this.dnsServer = new plugins.smartdns.DnsServer(dnsServerOptions);
// Register DNS record handlers if records provided
if (records && records.length > 0) {
this.registerDnsRecords(records);
}
await this.dnsServer.start();
console.log('DNS server started');
console.log(`DNS server started on UDP port ${dnsServerOptions.udpPort || 53}`);
}
console.log('DcRouter started successfully');
@ -592,6 +599,70 @@ export class DcRouter {
return true;
}
/**
* Register DNS records with the DNS server
* @param records Array of DNS records to register
*/
private registerDnsRecords(records: Array<{name: string; type: string; value: string; ttl?: number}>): void {
if (!this.dnsServer) return;
// Group records by domain pattern
const recordsByDomain = new Map<string, typeof records>();
for (const record of records) {
const pattern = record.name.includes('*') ? record.name : `*.${record.name}`;
if (!recordsByDomain.has(pattern)) {
recordsByDomain.set(pattern, []);
}
recordsByDomain.get(pattern)!.push(record);
}
// Register handlers for each domain pattern
for (const [domainPattern, domainRecords] of recordsByDomain) {
const recordTypes = [...new Set(domainRecords.map(r => r.type))];
this.dnsServer.registerHandler(domainPattern, recordTypes, (question) => {
const matchingRecord = domainRecords.find(
r => r.name === question.name && r.type === question.type
);
if (matchingRecord) {
return {
name: matchingRecord.name,
type: matchingRecord.type,
class: 'IN',
ttl: matchingRecord.ttl || 300,
data: this.parseDnsRecordData(matchingRecord.type, matchingRecord.value)
};
}
return null;
});
}
}
/**
* Parse DNS record data based on record type
* @param type DNS record type
* @param value DNS record value
* @returns Parsed data for the DNS response
*/
private parseDnsRecordData(type: string, value: string): any {
switch (type) {
case 'A':
return value; // IP address as string
case 'MX':
const [priority, exchange] = value.split(' ');
return { priority: parseInt(priority), exchange };
case 'TXT':
return value;
case 'NS':
return value;
default:
return value;
}
}
}
// Re-export email server types for convenience