validate-domain-alive.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { SOURCE_DIR } from './constants/dir';
  2. import path from 'node:path';
  3. import { newQueue } from '@henrygd/queue';
  4. import { isDomainAlive, keyedAsyncMutexWithQueue } from './lib/is-domain-alive';
  5. import { fdir as Fdir } from 'fdir';
  6. import runAgainstSourceFile from './lib/run-against-source-file';
  7. const queue = newQueue(24);
  8. const deadDomains: string[] = [];
  9. function onDomain(args: [string, boolean]) {
  10. if (!args[1]) {
  11. deadDomains.push(args[0]);
  12. }
  13. }
  14. (async () => {
  15. const domainSets = await new Fdir()
  16. .withFullPaths()
  17. .filter((filePath, isDirectory) => {
  18. if (isDirectory) return false;
  19. const extname = path.extname(filePath);
  20. return extname === '.txt' || extname === '.conf';
  21. })
  22. .crawl(SOURCE_DIR + path.sep + 'domainset')
  23. .withPromise();
  24. const domainRules = await new Fdir()
  25. .withFullPaths()
  26. .filter((filePath, isDirectory) => {
  27. if (isDirectory) return false;
  28. const extname = path.extname(filePath);
  29. return extname === '.txt' || extname === '.conf';
  30. })
  31. .crawl(SOURCE_DIR + path.sep + 'non_ip')
  32. .withPromise();
  33. await Promise.all([
  34. ...domainSets.map(runAgainstDomainset),
  35. ...domainRules.map(runAgainstRuleset)
  36. ]);
  37. console.log();
  38. console.log();
  39. console.log(JSON.stringify(deadDomains));
  40. })();
  41. export async function runAgainstRuleset(filepath: string) {
  42. const promises: Array<Promise<void>> = [];
  43. await runAgainstSourceFile(
  44. filepath,
  45. (domain: string, includeAllSubdomain: boolean) => queue.add(() => keyedAsyncMutexWithQueue(
  46. domain,
  47. () => isDomainAlive(domain, includeAllSubdomain)
  48. ).then(onDomain))
  49. );
  50. await Promise.all(promises);
  51. console.log('[done]', filepath);
  52. }
  53. export async function runAgainstDomainset(filepath: string) {
  54. const promises: Array<Promise<void>> = [];
  55. await runAgainstSourceFile(
  56. filepath,
  57. (domain: string, includeAllSubdomain: boolean) => queue.add(() => keyedAsyncMutexWithQueue(
  58. domain,
  59. () => isDomainAlive(domain, includeAllSubdomain)
  60. ).then(onDomain))
  61. );
  62. await Promise.all(promises);
  63. console.log('[done]', filepath);
  64. }