import { Resolver } from 'node:dns/promises'; import type { Observations, DnsInfo } from '../core/types.js'; export async function coletarDns(obs: Observations): Promise { let host: string; try { host = new URL(obs.urlFinal || obs.urlSolicitada).hostname; } catch { return; } const resolver = new Resolver({ timeout: 5000, tries: 2 }); const dns: DnsInfo = {}; await Promise.all([ tentar(() => resolver.resolve4(host)).then((r) => r && (dns.a = r)), tentar(() => resolver.resolve6(host)).then((r) => r && (dns.aaaa = r)), tentar(() => resolver.resolveCname(host)).then((r) => r && (dns.cname = r)), tentar(() => resolver.resolveNs(host)).then((r) => r && (dns.ns = r)), tentar(() => resolver.resolveTxt(host)).then( (r) => r && (dns.txt = r.map((partes) => partes.join(''))), ), tentar(() => resolver.resolveMx(host)).then( (r) => r && (dns.mx = r.map((m) => ({ prioridade: m.priority, servidor: m.exchange }))), ), ]); if (!dns.mx && host.startsWith('www.')) { const raiz = host.slice(4); const mx = await tentar(() => resolver.resolveMx(raiz)); if (mx) dns.mx = mx.map((m) => ({ prioridade: m.priority, servidor: m.exchange })); } obs.dns = dns; } async function tentar(fn: () => Promise): Promise { try { return await fn(); } catch { return undefined; } }