validate-domain-alive.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { readFileByLine } from './lib/fetch-text-by-line';
  2. import { processLine } from './lib/process-line';
  3. import { SOURCE_DIR } from './constants/dir';
  4. import path from 'node:path';
  5. import { newQueue } from '@henrygd/queue';
  6. import { isDomainAlive, keyedAsyncMutexWithQueue } from './lib/is-domain-alive';
  7. import { fdir as Fdir } from 'fdir';
  8. const queue = newQueue(32);
  9. (async () => {
  10. const domainSets = await new Fdir()
  11. .withFullPaths()
  12. .crawl(SOURCE_DIR + path.sep + 'domainset')
  13. .withPromise();
  14. const domainRules = await new Fdir()
  15. .withFullPaths()
  16. .crawl(SOURCE_DIR + path.sep + 'non_ip')
  17. .withPromise();
  18. await Promise.all([
  19. ...domainSets.map(runAgainstDomainset),
  20. ...domainRules.map(runAgainstRuleset)
  21. ]);
  22. console.log('done');
  23. })();
  24. export async function runAgainstRuleset(filepath: string) {
  25. const extname = path.extname(filepath);
  26. if (extname !== '.conf') {
  27. console.log('[skip]', filepath);
  28. return;
  29. }
  30. const promises: Array<Promise<[string, boolean]>> = [];
  31. for await (const l of readFileByLine(filepath)) {
  32. const line = processLine(l);
  33. if (!line) continue;
  34. const [type, domain] = line.split(',');
  35. switch (type) {
  36. case 'DOMAIN-SUFFIX':
  37. case 'DOMAIN': {
  38. promises.push(queue.add(() => keyedAsyncMutexWithQueue(domain, () => isDomainAlive(domain, type === 'DOMAIN-SUFFIX'))));
  39. break;
  40. }
  41. // no default
  42. }
  43. }
  44. await Promise.all(promises);
  45. console.log('[done]', filepath);
  46. }
  47. export async function runAgainstDomainset(filepath: string) {
  48. const extname = path.extname(filepath);
  49. if (extname !== '.conf') {
  50. console.log('[skip]', filepath);
  51. return;
  52. }
  53. const promises: Array<Promise<[string, boolean]>> = [];
  54. for await (const l of readFileByLine(filepath)) {
  55. const line = processLine(l);
  56. if (!line) continue;
  57. promises.push(queue.add(() => keyedAsyncMutexWithQueue(line, () => isDomainAlive(line, line[0] === '.'))));
  58. }
  59. await Promise.all(promises);
  60. console.log('[done]', filepath);
  61. }