validate-domain-alive.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(filepath => runAgainstSourceFile(
  42. filepath,
  43. (domain: string, includeAllSubdomain: boolean) => {
  44. bar.setTotal(bar.getTotal() + 1);
  45. return queue.add(async () => {
  46. let registerableDomainAlive, registerableDomain, alive: boolean | undefined;
  47. if (includeAllSubdomain) {
  48. // we only need to check apex domain, because we don't know if there is any stripped subdomain
  49. ({ alive: registerableDomainAlive, registerableDomain } = await isRegisterableDomainAlive(domain));
  50. } else {
  51. ({ alive, registerableDomainAlive, registerableDomain } = await isDomainAlive(domain));
  52. }
  53. bar.increment();
  54. if (!registerableDomainAlive) {
  55. if (registerableDomain) {
  56. deadDomains.push('.' + registerableDomain);
  57. }
  58. } else if (!includeAllSubdomain && alive != null && !alive) {
  59. deadDomains.push(domain);
  60. }
  61. });
  62. }
  63. ).then(() => console.log('[crawl]', filepath))));
  64. await queue.done();
  65. bar.stop();
  66. console.log();
  67. console.log();
  68. console.log(JSON.stringify(deadDomains));
  69. })();