Skip to content

Commit

Permalink
Make alternative TXT also work for local DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
DeBuXer committed Sep 16, 2024
1 parent 1454766 commit 5364ce1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 17 additions & 12 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 5364ce1

Please sign in to comment.