import { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js"; import dns2 from "dns2"; import { NOTFOUND, NODATA } from "dns"; import { SPFRecords } from "./SPFRecords.js"; import localDns from "node:dns/promises"; import TimeoutError from "../../../common/errors/TimeoutError.js"; import sleep from "../../../common/sleep.js"; import { jsonCache } from "../../../common/jsonCache.js"; export interface IDKIMRecord { keyType: string; publicKey: string; } const defaultNameServers = ["8.8.8.8", "1.1.1.1", "114.114.114.114"]; const ttlSecondsFailed = (e) => e.code === NOTFOUND ? 15*60 : 60; @RegisterSingleton export default class DnsService { @jsonCache() public async getDomainKey(selector: string, domain: string, nameServers?: string[]): Promise { nameServers ??= []; if (!nameServers.length) { nameServers = defaultNameServers; } const name = `${selector}._domainkey.${domain}`; const dns = new dns2({ nameServers }); const records = await dns.resolve(name, "TXT"); for (const iterator of records.answers) { if (/^v\=DKIM/.test(iterator.data.toString())) { let keyType; let publicKey; const tokens = iterator.data.toString().split(";").slice(1); for (const token of tokens) { const [type, value] = token.split(/[\:\=]/g); switch(type.trim()) { case "p": publicKey = value.trim(); break; case "k": keyType = value.trim(); break; } } return { keyType, publicKey }; } } return; } /** * Will return all acceptable IP addresses * @param domain domain * @param nameServers optional name servers */ @jsonCache() public async getSPF(domain: string, nameServers?: string[]): Promise { nameServers ??= []; if (!nameServers.length) { nameServers = defaultNameServers; } const dns = new dns2({nameServers }); const result = new SPFRecords(); const records = await dns.resolve(domain, "TXT"); for (const iterator of records.answers) { if (!/^v\=spf/.test(iterator.data.toString())) { continue; } const tokens = iterator.data.toString().split(" ").slice(1); for (const token of tokens) { const [type, value] = token.split(/[\:\=]/g); switch(type) { case "redirect": return await this.getSPF(value, nameServers); case "include": const include = await this.getSPF(value, nameServers); result.include(include); break; case "a": const a = await dns.resolveA(value); result.appendIPs(a.answers.map((x) => x.address)); break; case "ip4": result.appendSubNet(value); break; case "~all": result.softFail = true; break; } } } return result; } /** * Will return all acceptable IP addresses * @param domain domain * @param nameServers optional name servers */ @jsonCache({ ttlSecondsFailed }) public async getA(domain: string, nameServers?: string[]): Promise { const mx = await this.try((x) => localDns.resolve(x), domain); return mx; } /** * This will try to get cached mx and cached a record for mx records. * @param domain domain * @param nameServers optional name servers */ @jsonCache({ ttlSecondsFailed }) public async getMXAddresses(domain: string, nameServers?: string[]): Promise<{ host: string, ip: string }[]> { const result = [] as { host: string, ip: string}[]; try { // we should try to get cached result const mxs = await this.getMX(domain, nameServers); await Promise.all(mxs.map( async (x) => { try { const ips = await this.getA(x); for(const ip of ips) { result.push({ host: x, ip }); } } catch (e) { // do nothing... } })); if (result.length) { return result; } } catch (error) { if (error.code === NOTFOUND) { throw error; } } // try a... const r = await this.getA(domain, nameServers); for(const ip of r) { result.push({ host: domain, ip }); } return result; } /** * Will return all acceptable IP addresses * @param domain domain * @param nameServers optional name servers */ @jsonCache({ ttlSecondsFailed }) private async getMX(domain: string, nameServers?: string[]): Promise { const mx = await this.try( (x) => localDns.resolveMx(x), domain); mx.sort((a: any, b: any) => a.priority - b.priority); return mx.map((x) => x.exchange); } private async try(fx: (x: TIn) => Promise, p: TIn): Promise { let lastError = void 0; for(let i=0;i<3;i++) { try { return await fx(p); } catch (error) { lastError = error; if(error.code === NOTFOUND || error.code === NODATA) { break; } } await sleep(500); } if(!lastError) { throw new TimeoutError(); } throw lastError; } }