Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make alternative TXT also work for local DNS #31

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading