// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import { COREDNS_HOST, type GroupConfig } from './env'; import { HarnessError } from './errors'; import { run, sleep } from './proc'; import type { Recorder } from './recorder'; export interface DigRR { name: string; ttl: number; class: string; type: string; data: string; } export interface DigResult { rcode: string; flags: string[]; answer: DigRR[]; authority: DigRR[]; additional: DigRR[]; raw: string; } export function parseRR(line: string): DigRR { const m = /^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s*(.*)$/.exec(line.trim()); if (!m) throw new HarnessError('protocol', `cannot parse dig record line: ${line}`); return { name: m[1].toLowerCase(), ttl: Number(m[2]), class: m[3], type: m[4], data: m[5].trim() }; } export function parseDig(text: string): DigResult { const header = /->>HEADER<<-\s+opcode:\s*\S+,\s*status:\s*([A-Z]+),/.exec(text); if (!header) throw new HarnessError('protocol', `dig output without a header: ${text.slice(0, 500)}`); const flagsMatch = /;; flags:([^;]*);/.exec(text); const result: DigResult = { rcode: header[1], flags: flagsMatch ? flagsMatch[1].trim().split(/\s+/).filter(Boolean) : [], answer: [], authority: [], additional: [], raw: text, }; let section: 'answer' | 'authority' | 'additional' | null = null; for (const line of text.split('\n')) { const t = line.trim(); const head = /^;; ([A-Z ]+?)\s*(?:PSEUDO)?SECTION:$/.exec(t); if (head) { const name = head[1].trim().toLowerCase(); section = name === 'answer' || name === 'authority' || name === 'additional' ? name : null; continue; } if (t === '' || t.startsWith(';')) continue; if (section) result[section].push(parseRR(t)); } return result; } export interface DigOptions { tcp?: boolean; recorder?: Recorder; } export async function dig(group: GroupConfig, name: string, type: string, opts: DigOptions = {}): Promise { const args = [`@${COREDNS_HOST}`, '-p', String(group.dnsPort), '+norecurse', '+nocookie', '+time=3', '+tries=2', '+noall', '+comments', '+answer', '+authority', '+additional']; if (opts.tcp) args.push('+tcp'); args.push('-t', type, '-q', name); let last = ''; for (let attempt = 0; attempt < 3; attempt++) { const r = await run('dig', args, { timeoutMs: 20_000 }); last = r.stdout + r.stderr; opts.recorder?.addDig(args, last); if (r.code === 0 && /->>HEADER<<-/.test(r.stdout)) return parseDig(r.stdout); await sleep(300); } throw new HarnessError('transport', `dig ${name} ${type} on port ${group.dnsPort} failed: ${last.slice(0, 500)}`); } export function fqdn(name: string, zone: string): string { return name === '' ? zone : `${name}.${zone}`; } export async function waitForZone(group: GroupConfig, zone: string, opts: DigOptions = {}, timeoutMs = 20_000): Promise { const deadline = Date.now() + timeoutMs; let last: DigResult | undefined; for (;;) { last = await dig(group, zone, 'SOA', opts); if (last.rcode === 'NOERROR' && last.flags.includes('aa') && last.answer.some((rr) => rr.type === 'SOA')) return last; if (Date.now() > deadline) throw new HarnessError('timeout', `zone ${zone} not served on port ${group.dnsPort} within ${timeoutMs} ms: ${last.raw.slice(0, 800)}`); await sleep(250); } } export async function waitForZoneGone(group: GroupConfig, zone: string, opts: DigOptions = {}, timeoutMs = 20_000): Promise { const deadline = Date.now() + timeoutMs; for (;;) { const r = await dig(group, zone, 'SOA', opts); if (!r.flags.includes('aa') && !r.answer.some((rr) => rr.type === 'SOA')) return r; if (Date.now() > deadline) throw new HarnessError('timeout', `zone ${zone} still served on port ${group.dnsPort} after ${timeoutMs} ms`); await sleep(250); } } export function soaSerialOf(rr: DigRR): number { const parts = rr.data.split(/\s+/); if (rr.type !== 'SOA' || parts.length !== 7) throw new HarnessError('protocol', `not an SOA record: ${rr.type} ${rr.data}`); return Number(parts[2]); } export async function servedSerial(group: GroupConfig, zone: string, opts: DigOptions = {}): Promise { const r = await dig(group, zone, 'SOA', opts); const soa = r.answer.find((rr) => rr.type === 'SOA'); if (!soa) throw new HarnessError('protocol', `no SOA answer for ${zone}: ${r.raw.slice(0, 500)}`); return soaSerialOf(soa); }