From 5364ce1a51331eeb3e956e0f339d8d2d6ecaa61a Mon Sep 17 00:00:00 2001 From: DeBuXer Date: Mon, 16 Sep 2024 13:15:35 +0200 Subject: [PATCH] Make alternative TXT also work for local DNS --- src/client.js | 4 ++-- src/util.js | 29 +++++++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/client.js b/src/client.js index 537ed6e..f1593a1 100644 --- a/src/client.js +++ b/src/client.js @@ -59,11 +59,11 @@ async function buildCache(host) { let expand = false; let recordData = await findTxtRecord(host); if (!recordData) { - throw new Error(`The TXT record data for "_.${host}" is missing`); + throw new Error(`The TXT record data for "_.${host}" or "fwd.${host}" is missing`); } let { url, httpStatus = '301' } = recordData; if (url.indexOf('http://') !== 0 && url.indexOf('https://') !== 0) { - throw new Error(`The TXT record data for "_.${host}" is not an absolute URL`); + throw new Error(`The TXT record data for "_.${host}" or "fwd.${host}" is not an absolute URL`); } if (url.endsWith('*')) { url = url.slice(0, -1); diff --git a/src/util.js b/src/util.js index e13ae6b..f7a9579 100644 --- a/src/util.js +++ b/src/util.js @@ -242,18 +242,23 @@ export async function findTxtRecord(host, mockResolve = undefined) { useLocalDNS = process.env.USE_LOCAL_DNS == 'true'; } if (useLocalDNS && !mockResolve) { - const resolve = [ - ...await dns.resolveTxt(`_.${host}`), - ...await dns.resolveTxt(`fwd.${host}`), + const resolvePromises = [ + dns.resolveTxt(`_.${host}`), + dns.resolveTxt(`fwd.${host}`) ]; - for (const record of resolve) { - const joinedRecord = record.join(';'); - const txtData = parseTxtRecordData(joinedRecord); - if (!txtData[recordParamDestUrl]) continue; - return { - url: txtData[recordParamDestUrl], - httpStatus: txtData[recordParamHttpStatus], - }; + + const resolved = await Promise.any(resolvePromises).catch(() => null); + + if (resolved) { + for (const record of resolved) { + const joinedRecord = record.join(';'); + const txtData = parseTxtRecordData(joinedRecord); + if (!txtData[recordParamDestUrl]) continue; + return { + url: txtData[recordParamDestUrl], + httpStatus: txtData[recordParamHttpStatus], + }; + } } } else { /** @@ -335,4 +340,4 @@ export function combineURLs(baseURL, relativeURL) { */ export function isMainProcess(metaURL) { return [process.argv[1], process.env.pm_exec_path].includes(fileURLToPath(metaURL)); -} +} \ No newline at end of file