fix(server): Require Rust bridge for DNS packet processing; remove synchronous TypeScript fallback; change handler API to accept IDnsQuestion and adjust query API
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import { RustDnsBridge, type IDnsQueryEvent, type IIpcDnsAnswer, type IRustDnsConfig } from './classes.rustdnsbridge.js';
|
||||
import * as dnsPacket from 'dns-packet';
|
||||
|
||||
export interface IDnsServerOptions {
|
||||
httpsKey: string;
|
||||
@@ -27,10 +26,16 @@ export interface DnsAnswer {
|
||||
data: any;
|
||||
}
|
||||
|
||||
export interface IDnsQuestion {
|
||||
name: string;
|
||||
type: string;
|
||||
class?: string;
|
||||
}
|
||||
|
||||
export interface IDnsHandler {
|
||||
domainPattern: string;
|
||||
recordTypes: string[];
|
||||
handler: (question: dnsPacket.Question) => DnsAnswer | null;
|
||||
handler: (question: IDnsQuestion) => DnsAnswer | null;
|
||||
}
|
||||
|
||||
// Let's Encrypt related interfaces
|
||||
@@ -80,7 +85,7 @@ export class DnsServer {
|
||||
public registerHandler(
|
||||
domainPattern: string,
|
||||
recordTypes: string[],
|
||||
handler: (question: dnsPacket.Question) => DnsAnswer | null
|
||||
handler: (question: IDnsQuestion) => DnsAnswer | null
|
||||
): void {
|
||||
this.handlers.push({ domainPattern, recordTypes, handler });
|
||||
}
|
||||
@@ -225,146 +230,14 @@ export class DnsServer {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a raw DNS packet and return the response.
|
||||
* Synchronous version using the TypeScript fallback (for backward compatibility).
|
||||
*/
|
||||
public processRawDnsPacket(packet: Buffer): Buffer {
|
||||
// Synchronous fallback — process locally using TypeScript handler logic
|
||||
// This is needed for backward-compatible callers that expect sync results
|
||||
try {
|
||||
const request = dnsPacket.decode(packet);
|
||||
const response = this.processDnsRequest(request);
|
||||
return dnsPacket.encode(response) as unknown as Buffer;
|
||||
} catch (err) {
|
||||
console.error('Error processing raw DNS packet:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a raw DNS packet asynchronously via Rust bridge.
|
||||
*/
|
||||
public async processRawDnsPacketAsync(packet: Buffer): Promise<Buffer> {
|
||||
if (this.bridgeSpawned) {
|
||||
return this.bridge.processPacket(packet);
|
||||
if (!this.bridgeSpawned) {
|
||||
throw new Error('DNS server not started — call start() first');
|
||||
}
|
||||
// Fallback to local processing if bridge not spawned
|
||||
return this.processRawDnsPacket(packet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a DNS request locally (TypeScript handler resolution).
|
||||
* Used as fallback and for pre-bridge-spawn calls.
|
||||
*/
|
||||
public processDnsRequest(request: dnsPacket.Packet): dnsPacket.Packet {
|
||||
const response: dnsPacket.Packet = {
|
||||
type: 'response',
|
||||
id: request.id,
|
||||
flags:
|
||||
dnsPacket.AUTHORITATIVE_ANSWER |
|
||||
dnsPacket.RECURSION_AVAILABLE |
|
||||
(request.flags & dnsPacket.RECURSION_DESIRED ? dnsPacket.RECURSION_DESIRED : 0),
|
||||
questions: request.questions,
|
||||
answers: [],
|
||||
additionals: [],
|
||||
};
|
||||
|
||||
for (const question of request.questions) {
|
||||
let answered = false;
|
||||
const recordsForQuestion: DnsAnswer[] = [];
|
||||
|
||||
// Built-in handling for localhost and reverse localhost (RFC 6761)
|
||||
const enableLocal = this.options.enableLocalhostHandling !== false;
|
||||
if (enableLocal) {
|
||||
const qnameLower = (question.name || '').toLowerCase();
|
||||
const qnameTrimmed = qnameLower.endsWith('.') ? qnameLower.slice(0, -1) : qnameLower;
|
||||
|
||||
if (qnameTrimmed === 'localhost') {
|
||||
if (question.type === 'A') {
|
||||
recordsForQuestion.push({
|
||||
name: question.name,
|
||||
type: 'A',
|
||||
class: 'IN',
|
||||
ttl: 0,
|
||||
data: '127.0.0.1',
|
||||
});
|
||||
answered = true;
|
||||
} else if (question.type === 'AAAA') {
|
||||
recordsForQuestion.push({
|
||||
name: question.name,
|
||||
type: 'AAAA',
|
||||
class: 'IN',
|
||||
ttl: 0,
|
||||
data: '::1',
|
||||
});
|
||||
answered = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!answered) {
|
||||
const reverseLocalhostV4 = '1.0.0.127.in-addr.arpa';
|
||||
if (qnameTrimmed === reverseLocalhostV4 && question.type === 'PTR') {
|
||||
recordsForQuestion.push({
|
||||
name: question.name,
|
||||
type: 'PTR',
|
||||
class: 'IN',
|
||||
ttl: 0,
|
||||
data: 'localhost.',
|
||||
});
|
||||
answered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Collect all matching records from handlers
|
||||
if (!answered) {
|
||||
for (const handlerEntry of this.handlers) {
|
||||
if (
|
||||
plugins.minimatch.minimatch(question.name, handlerEntry.domainPattern) &&
|
||||
handlerEntry.recordTypes.includes(question.type)
|
||||
) {
|
||||
const answer = handlerEntry.handler(question);
|
||||
if (answer) {
|
||||
const dnsAnswer: DnsAnswer = {
|
||||
...answer,
|
||||
ttl: answer.ttl || 300,
|
||||
class: answer.class || 'IN',
|
||||
};
|
||||
recordsForQuestion.push(dnsAnswer);
|
||||
answered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (recordsForQuestion.length > 0) {
|
||||
for (const record of recordsForQuestion) {
|
||||
response.answers.push(record as plugins.dnsPacket.Answer);
|
||||
}
|
||||
}
|
||||
|
||||
if (!answered) {
|
||||
const soaAnswer: DnsAnswer = {
|
||||
name: question.name,
|
||||
type: 'SOA',
|
||||
class: 'IN',
|
||||
ttl: 3600,
|
||||
data: {
|
||||
mname: this.options.primaryNameserver || `ns1.${this.options.dnssecZone}`,
|
||||
rname: `hostmaster.${this.options.dnssecZone}`,
|
||||
serial: Math.floor(Date.now() / 1000),
|
||||
refresh: 3600,
|
||||
retry: 600,
|
||||
expire: 604800,
|
||||
minimum: 86400,
|
||||
},
|
||||
};
|
||||
response.answers.push(soaAnswer as plugins.dnsPacket.Answer);
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
return this.bridge.processPacket(packet);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -451,7 +324,7 @@ export class DnsServer {
|
||||
this.registerHandler(
|
||||
challengeDomain,
|
||||
['TXT'],
|
||||
(question: dnsPacket.Question): DnsAnswer | null => {
|
||||
(question: IDnsQuestion): DnsAnswer | null => {
|
||||
if (question.name === challengeDomain && question.type === 'TXT') {
|
||||
return {
|
||||
name: question.name,
|
||||
@@ -554,10 +427,10 @@ export class DnsServer {
|
||||
let answered = false;
|
||||
|
||||
for (const q of event.questions) {
|
||||
const question: dnsPacket.Question = {
|
||||
const question: IDnsQuestion = {
|
||||
name: q.name,
|
||||
type: q.type as any,
|
||||
class: q.class as any,
|
||||
type: q.type,
|
||||
class: q.class,
|
||||
};
|
||||
|
||||
for (const handlerEntry of this.handlers) {
|
||||
|
||||
Reference in New Issue
Block a user