validate-global-tld.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import path from 'node:path';
  2. import { readFileByLine } from './lib/fetch-text-by-line';
  3. import { HostnameSmolTrie } from './lib/trie';
  4. import { OUTPUT_SURGE_DIR, SOURCE_DIR } from './constants/dir';
  5. import { ICP_TLD } from './constants/domains';
  6. import tldts from 'tldts-experimental';
  7. import { looseTldtsOpt } from './constants/loose-tldts-opt';
  8. (async () => {
  9. const trie = new HostnameSmolTrie();
  10. const extraWhiteTLDs = new Set<string>();
  11. for await (const line of readFileByLine(path.join(OUTPUT_SURGE_DIR, 'non_ip', 'domestic.conf'))) {
  12. const [type, domain] = line.split(',');
  13. if (type !== 'DOMAIN' && type !== 'DOMAIN-SUFFIX') {
  14. continue;
  15. }
  16. if (domain === 'this_ruleset_is_made_by_sukkaw.ruleset.skk.moe') {
  17. continue;
  18. }
  19. const tld = tldts.getPublicSuffix(domain, looseTldtsOpt);
  20. if (tld) {
  21. extraWhiteTLDs.add(tld);
  22. }
  23. }
  24. for await (const line of readFileByLine(path.join(SOURCE_DIR, 'non_ip', 'global.conf'))) {
  25. const [type, domain] = line.split(',');
  26. switch (type) {
  27. case 'DOMAIN':
  28. trie.add(domain);
  29. break;
  30. case 'DOMAIN-SUFFIX':
  31. trie.add(domain, true);
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. ICP_TLD.forEach(tld => trie.whitelist(tld, true));
  38. extraWhiteTLDs.forEach(tld => trie.whitelist(tld, true));
  39. console.log(trie.dump().join('\n'));
  40. })();