chore: shuffle servers when checking alive domains
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy to Cloudflare Pages (3.114.9) (push) Has been cancelled
Build / Deploy to GitHub and GitLab (push) Has been cancelled

This commit is contained in:
SukkaW 2025-07-20 23:07:59 +08:00
parent d3179cc2cd
commit f4d0531fb3

View File

@ -3,11 +3,11 @@ import asyncRetry from 'async-retry';
import picocolors from 'picocolors'; import picocolors from 'picocolors';
import { looseTldtsOpt } from '../constants/loose-tldts-opt'; import { looseTldtsOpt } from '../constants/loose-tldts-opt';
import { createKeyedAsyncMutex } from './keyed-async-mutex'; import { createKeyedAsyncMutex } from './keyed-async-mutex';
import { pickRandom, pickOne } from 'foxts/pick-random';
import tldts from 'tldts-experimental'; import tldts from 'tldts-experimental';
import * as whoiser from 'whoiser'; import * as whoiser from 'whoiser';
import process from 'node:process'; import process from 'node:process';
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie'; import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
import { shuffleArray } from 'foxts/shuffle-array';
const domainAliveMap = new Map<string, boolean>(); const domainAliveMap = new Map<string, boolean>();
@ -110,9 +110,9 @@ export async function isDomainAlive(
const aaaaDns: string[] = []; const aaaaDns: string[] = [];
// test 2 times before make sure record is empty // test 2 times before make sure record is empty
const servers = pickRandom(dohServers, 2); const servers = shuffleArray(dohServers, { copy: true });
// TODO: increase limit when there is an error
for (let i = 0; i < 2; i++) { for (let i = 0, len = servers.length; i < len; i++) {
try { try {
// eslint-disable-next-line no-await-in-loop -- sequential // eslint-disable-next-line no-await-in-loop -- sequential
const aRecords = (await $resolve(domain, 'A', servers[i])); const aRecords = (await $resolve(domain, 'A', servers[i]));
@ -123,8 +123,13 @@ export async function isDomainAlive(
aDns.push(aRecords.dns); aDns.push(aRecords.dns);
} catch {} } catch {}
if (aDns.length >= 2) {
break; // we only need to test 2 times
}
} }
for (let i = 0; i < 2; i++) {
for (let i = 0, len = servers.length; i < len; i++) {
try { try {
// eslint-disable-next-line no-await-in-loop -- sequential // eslint-disable-next-line no-await-in-loop -- sequential
const aaaaRecords = await $resolve(domain, 'AAAA', servers[i]); const aaaaRecords = await $resolve(domain, 'AAAA', servers[i]);
@ -135,30 +140,43 @@ export async function isDomainAlive(
aaaaDns.push(aaaaRecords.dns); aaaaDns.push(aaaaRecords.dns);
} catch {} } catch {}
if (aaaaDns.length >= 2) {
break; // we only need to test 2 times
}
} }
// only then, let's test twice with domesticDohServers // only then, let's test twice with domesticDohServers
for (let i = 0; i < 2; i++) { const domesticServers = shuffleArray(domesticDohServers, { copy: true });
for (let i = 0, len = domesticServers.length; i < len; i++) {
try { try {
// eslint-disable-next-line no-await-in-loop -- sequential // eslint-disable-next-line no-await-in-loop -- sequential
const aRecords = (await $resolve(domain, 'A', pickOne(domesticDohServers))); const aRecords = await $resolve(domain, 'A', domesticServers[i]);
if (aRecords.answers.length > 0) { if (aRecords.answers.length > 0) {
domainAliveMap.set(domain, true); domainAliveMap.set(domain, true);
return true; return true;
} }
aDns.push(aRecords.dns); aDns.push(aRecords.dns);
} catch {} } catch {}
if (aDns.length >= 2) {
break; // we only need to test 2 times
}
} }
for (let i = 0; i < 2; i++) {
for (let i = 0, len = domesticServers.length; i < len; i++) {
try { try {
// eslint-disable-next-line no-await-in-loop -- sequential // eslint-disable-next-line no-await-in-loop -- sequential
const aaaaRecords = (await $resolve(domain, 'AAAA', pickOne(domesticDohServers))); const aaaaRecords = await $resolve(domain, 'AAAA', domesticServers[i]);
if (aaaaRecords.answers.length > 0) { if (aaaaRecords.answers.length > 0) {
domainAliveMap.set(domain, true); domainAliveMap.set(domain, true);
return true; return true;
} }
aaaaDns.push(aaaaRecords.dns); aaaaDns.push(aaaaRecords.dns);
} catch {} } catch {}
if (aaaaDns.length >= 2) {
break; // we only need to test 2 times
}
} }
console.log(picocolors.red('[domain dead]'), 'no A/AAAA records', { domain, a: aDns, aaaa: aaaaDns }); console.log(picocolors.red('[domain dead]'), 'no A/AAAA records', { domain, a: aDns, aaaa: aaaaDns });
@ -176,16 +194,26 @@ function isApexDomainAlive(apexDomain: string) {
} }
return apexDomainMap.acquire(apexDomain, async () => { return apexDomainMap.acquire(apexDomain, async () => {
const servers = pickRandom(dohServers, 2); const servers = shuffleArray(dohServers, { copy: true });
let nsSuccess = 0;
for (let i = 0, len = servers.length; i < len; i++) { for (let i = 0, len = servers.length; i < len; i++) {
const server = servers[i]; const server = servers[i];
try { try {
// eslint-disable-next-line no-await-in-loop -- one by one // eslint-disable-next-line no-await-in-loop -- one by one
const resp = await $resolve(apexDomain, 'NS', server); const resp = await $resolve(apexDomain, 'NS', server);
if (resp.answers.length > 0) { if (resp.answers.length > 0) {
domainAliveMap.set(apexDomain, true); domainAliveMap.set(apexDomain, true);
return true; return true;
} }
nsSuccess++;
if (nsSuccess >= 2) {
// we only need to test 2 times
break;
}
} catch {} } catch {}
} }