validate-domain-alive.ts 6.9 KB

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