validate-domain-alive.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { SOURCE_DIR } from './constants/dir';
  2. import path from 'node:path';
  3. import { getMethods } from './lib/is-domain-alive';
  4. import { fdir as Fdir } from 'fdir';
  5. import runAgainstSourceFile from './lib/run-against-source-file';
  6. import cliProgress from 'cli-progress';
  7. import { newQueue } from '@henrygd/queue';
  8. const queue = newQueue(32);
  9. const deadDomains: string[] = [];
  10. (async () => {
  11. const [
  12. { isDomainAlive, isRegisterableDomainAlive },
  13. domainSets,
  14. domainRules
  15. ] = await Promise.all([
  16. getMethods(),
  17. new Fdir()
  18. .withFullPaths()
  19. .filter((filePath, isDirectory) => {
  20. if (isDirectory) return false;
  21. const extname = path.extname(filePath);
  22. return extname === '.txt' || extname === '.conf';
  23. })
  24. .crawl(SOURCE_DIR + path.sep + 'domainset')
  25. .withPromise(),
  26. new Fdir()
  27. .withFullPaths()
  28. .filter((filePath, isDirectory) => {
  29. if (isDirectory) return false;
  30. const extname = path.extname(filePath);
  31. return extname === '.txt' || extname === '.conf';
  32. })
  33. .crawl(SOURCE_DIR + path.sep + 'non_ip')
  34. .withPromise()
  35. ]);
  36. const bar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
  37. bar.start(0, 0);
  38. await Promise.all([
  39. ...domainRules,
  40. ...domainSets
  41. ].map(
  42. filepath => runAgainstSourceFile(
  43. filepath,
  44. (domain: string, includeAllSubdomain: boolean) => {
  45. bar.setTotal(bar.getTotal() + 1);
  46. return queue.add(async () => {
  47. let registerableDomainAlive, registerableDomain, alive: boolean | undefined;
  48. if (includeAllSubdomain) {
  49. // we only need to check apex domain, because we don't know if there is any stripped subdomain
  50. ({ alive: registerableDomainAlive, registerableDomain } = await isRegisterableDomainAlive(domain));
  51. } else {
  52. ({ alive, registerableDomainAlive, registerableDomain } = await isDomainAlive(domain));
  53. }
  54. bar.increment();
  55. if (!registerableDomainAlive) {
  56. if (registerableDomain) {
  57. deadDomains.push('.' + registerableDomain);
  58. }
  59. } else if (!includeAllSubdomain && alive != null && !alive) {
  60. deadDomains.push(domain);
  61. }
  62. });
  63. }
  64. ).then(() => console.log('[crawl]', filepath))
  65. ));
  66. await queue.done();
  67. bar.stop();
  68. console.log();
  69. console.log();
  70. console.log(JSON.stringify(deadDomains));
  71. })();