validate-domain-alive.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import DNS2 from 'dns2';
  2. import { readFileByLine } from './lib/fetch-text-by-line';
  3. import { processLine } from './lib/process-line';
  4. import tldts from 'tldts-experimental';
  5. import { looseTldtsOpt } from './constants/loose-tldts-opt';
  6. import { fdir as Fdir } from 'fdir';
  7. import { SOURCE_DIR } from './constants/dir';
  8. import path from 'node:path';
  9. import { newQueue } from '@henrygd/queue';
  10. import asyncRetry from 'async-retry';
  11. import * as whoiser from 'whoiser';
  12. import picocolors from 'picocolors';
  13. import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
  14. import './lib/fetch-retry';
  15. const dohServers: Array<[string, DNS2.DnsResolver]> = ([
  16. '8.8.8.8',
  17. '8.8.4.4',
  18. '1.0.0.1',
  19. '1.1.1.1',
  20. '162.159.36.1',
  21. '162.159.46.1',
  22. '101.101.101.101', // TWNIC
  23. '185.222.222.222', // DNS.SB
  24. '45.11.45.11', // DNS.SB
  25. 'dns10.quad9.net', // Quad9 unfiltered
  26. 'doh.sandbox.opendns.com', // OpenDNS sandbox (unfiltered)
  27. 'unfiltered.adguard-dns.com',
  28. // '0ms.dev', // Proxy Cloudflare
  29. // '76.76.2.0', // ControlD unfiltered, path not /dns-query
  30. // '76.76.10.0', // ControlD unfiltered, path not /dns-query
  31. // 'dns.bebasid.com', // BebasID, path not /dns-query but /unfiltered
  32. // '193.110.81.0', // dns0.eu
  33. // '185.253.5.0', // dns0.eu
  34. // 'zero.dns0.eu',
  35. 'dns.nextdns.io',
  36. 'anycast.dns.nextdns.io',
  37. 'wikimedia-dns.org',
  38. // 'ordns.he.net',
  39. // 'dns.mullvad.net',
  40. 'basic.rethinkdns.com'
  41. // 'ada.openbld.net',
  42. // 'dns.rabbitdns.org'
  43. ] as const).map(dns => [
  44. dns,
  45. DNS2.DOHClient({
  46. dns,
  47. http: false
  48. // get: (url: string) => undici.request(url).then(r => r.body)
  49. })
  50. ] as const);
  51. const domesticDohServers: Array<[string, DNS2.DnsResolver]> = ([
  52. '223.5.5.5',
  53. '223.6.6.6',
  54. '120.53.53.53',
  55. '1.12.12.12'
  56. ] as const).map(dns => [
  57. dns,
  58. DNS2.DOHClient({
  59. dns,
  60. http: false
  61. // get: (url: string) => undici.request(url).then(r => r.body)
  62. })
  63. ] as const);
  64. const queue = newQueue(32);
  65. const mutex = new Map<string, Promise<unknown>>();
  66. function keyedAsyncMutexWithQueue<T>(key: string, fn: () => Promise<T>) {
  67. if (mutex.has(key)) {
  68. return mutex.get(key) as Promise<T>;
  69. }
  70. const promise = queue.add(() => fn());
  71. mutex.set(key, promise);
  72. return promise;
  73. }
  74. class DnsError extends Error {
  75. name = 'DnsError';
  76. constructor(readonly message: string, public readonly server: string) {
  77. super(message);
  78. }
  79. }
  80. interface DnsResponse extends DNS2.$DnsResponse {
  81. dns: string
  82. }
  83. function createResolve(server: Array<[string, DNS2.DnsResolver]>): DNS2.DnsResolver<DnsResponse> {
  84. return async (...args) => {
  85. try {
  86. return await asyncRetry(async () => {
  87. const [dohServer, dohClient] = server[Math.floor(Math.random() * server.length)];
  88. try {
  89. return {
  90. ...await dohClient(...args),
  91. dns: dohServer
  92. } satisfies DnsResponse;
  93. } catch (e) {
  94. console.error(e);
  95. throw new DnsError((e as Error).message, dohServer);
  96. }
  97. }, { retries: 5 });
  98. } catch (e) {
  99. console.log('[doh error]', ...args, e);
  100. throw e;
  101. }
  102. };
  103. }
  104. const resolve = createResolve(dohServers);
  105. const domesticResolve = createResolve(domesticDohServers);
  106. async function getWhois(domain: string) {
  107. return asyncRetry(() => whoiser.domain(domain), { retries: 5 });
  108. }
  109. (async () => {
  110. const domainSets = await new Fdir()
  111. .withFullPaths()
  112. .crawl(SOURCE_DIR + path.sep + 'domainset')
  113. .withPromise();
  114. const domainRules = await new Fdir()
  115. .withFullPaths()
  116. .crawl(SOURCE_DIR + path.sep + 'non_ip')
  117. .withPromise();
  118. await Promise.all([
  119. ...domainSets.map(runAgainstDomainset),
  120. ...domainRules.map(runAgainstRuleset)
  121. ]);
  122. console.log('done');
  123. })();
  124. const whoisNotFoundKeywordTest = createKeywordFilter([
  125. 'no match for',
  126. 'does not exist',
  127. 'not found'
  128. ]);
  129. const domainAliveMap = new Map<string, boolean>();
  130. async function isApexDomainAlive(apexDomain: string): Promise<[string, boolean]> {
  131. if (domainAliveMap.has(apexDomain)) {
  132. return [apexDomain, domainAliveMap.get(apexDomain)!];
  133. }
  134. const resp = await resolve(apexDomain, 'NS');
  135. if (resp.answers.length > 0) {
  136. return [apexDomain, true];
  137. }
  138. let whois;
  139. try {
  140. whois = await getWhois(apexDomain);
  141. } catch (e) {
  142. console.log('[whois fail]', 'whois error', { domain: apexDomain }, e);
  143. return [apexDomain, true];
  144. }
  145. if (Object.keys(whois).length > 0) {
  146. // TODO: this is a workaround for https://github.com/LayeredStudio/whoiser/issues/117
  147. if ('text' in whois && Array.isArray(whois.text) && whois.text.some(value => whoisNotFoundKeywordTest(value.toLowerCase()))) {
  148. console.log(picocolors.red('[domain dead]'), 'whois not found', { domain: apexDomain });
  149. domainAliveMap.set(apexDomain, false);
  150. return [apexDomain, false];
  151. }
  152. return [apexDomain, true];
  153. }
  154. if (!('dns' in whois)) {
  155. console.log({ whois });
  156. }
  157. console.log(picocolors.red('[domain dead]'), 'whois not found', { domain: apexDomain });
  158. domainAliveMap.set(apexDomain, false);
  159. return [apexDomain, false];
  160. }
  161. export async function isDomainAlive(domain: string, isSuffix: boolean): Promise<[string, boolean]> {
  162. if (domainAliveMap.has(domain)) {
  163. return [domain, domainAliveMap.get(domain)!];
  164. }
  165. const apexDomain = tldts.getDomain(domain, looseTldtsOpt);
  166. if (!apexDomain) {
  167. console.log('[domain invalid]', 'no apex domain', { domain });
  168. domainAliveMap.set(domain, true);
  169. return [domain, true] as const;
  170. }
  171. const apexDomainAlive = await isApexDomainAlive(apexDomain);
  172. if (!apexDomainAlive[1]) {
  173. domainAliveMap.set(domain, false);
  174. return [domain, false] as const;
  175. }
  176. const $domain = domain[0] === '.' ? domain.slice(1) : domain;
  177. if (!isSuffix) {
  178. const aDns: string[] = [];
  179. const aaaaDns: string[] = [];
  180. // test 2 times before make sure record is empty
  181. for (let i = 0; i < 2; i++) {
  182. // eslint-disable-next-line no-await-in-loop -- sequential
  183. const aRecords = (await resolve($domain, 'A'));
  184. if (aRecords.answers.length !== 0) {
  185. domainAliveMap.set(domain, true);
  186. return [domain, true] as const;
  187. }
  188. aDns.push(aRecords.dns);
  189. }
  190. for (let i = 0; i < 2; i++) {
  191. // eslint-disable-next-line no-await-in-loop -- sequential
  192. const aaaaRecords = (await resolve($domain, 'AAAA'));
  193. if (aaaaRecords.answers.length !== 0) {
  194. domainAliveMap.set(domain, true);
  195. return [domain, true] as const;
  196. }
  197. aaaaDns.push(aaaaRecords.dns);
  198. }
  199. // only then, let's test once with domesticDohServers
  200. const aRecords = (await domesticResolve($domain, 'A'));
  201. if (aRecords.answers.length !== 0) {
  202. domainAliveMap.set(domain, true);
  203. return [domain, true] as const;
  204. }
  205. aDns.push(aRecords.dns);
  206. const aaaaRecords = (await domesticResolve($domain, 'AAAA'));
  207. if (aaaaRecords.answers.length !== 0) {
  208. domainAliveMap.set(domain, true);
  209. return [domain, true] as const;
  210. }
  211. aaaaDns.push(aaaaRecords.dns);
  212. console.log(picocolors.red('[domain dead]'), 'no A/AAAA records', { domain, a: aDns, aaaa: aaaaDns });
  213. }
  214. domainAliveMap.set($domain, false);
  215. return [domain, false] as const;
  216. }
  217. export async function runAgainstRuleset(filepath: string) {
  218. const extname = path.extname(filepath);
  219. if (extname !== '.conf') {
  220. console.log('[skip]', filepath);
  221. return;
  222. }
  223. const promises: Array<Promise<[string, boolean]>> = [];
  224. for await (const l of readFileByLine(filepath)) {
  225. const line = processLine(l);
  226. if (!line) continue;
  227. const [type, domain] = line.split(',');
  228. switch (type) {
  229. case 'DOMAIN-SUFFIX':
  230. case 'DOMAIN': {
  231. promises.push(keyedAsyncMutexWithQueue(domain, () => isDomainAlive(domain, type === 'DOMAIN-SUFFIX')));
  232. break;
  233. }
  234. // no default
  235. }
  236. }
  237. await Promise.all(promises);
  238. console.log('[done]', filepath);
  239. }
  240. export async function runAgainstDomainset(filepath: string) {
  241. const extname = path.extname(filepath);
  242. if (extname !== '.conf') {
  243. console.log('[skip]', filepath);
  244. return;
  245. }
  246. const promises: Array<Promise<[string, boolean]>> = [];
  247. for await (const l of readFileByLine(filepath)) {
  248. const line = processLine(l);
  249. if (!line) continue;
  250. promises.push(keyedAsyncMutexWithQueue(line, () => isDomainAlive(line, line[0] === '.')));
  251. }
  252. await Promise.all(promises);
  253. console.log('[done]', filepath);
  254. }