validate-domainset.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Surge Domain Set can not include root domain from public suffix list.
  2. const tldts = require('tldts');
  3. const picocolors = require('picocolors');
  4. const path = require('path');
  5. const listDir = require('@sukka/listdir');
  6. const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
  7. const { processLine } = require('./lib/process-line');
  8. const SPECIAL_SUFFIXES = new Set([
  9. 'linodeobjects.com', // only *.linodeobjects.com are public suffix
  10. 'vultrobjects.com', // only *.vultrobjects.com are public suffix
  11. 'dweb.link' // only *.dweb.link are public suffix
  12. ]);
  13. const validateDomainSet = async (filePath) => {
  14. for await (const l of readFileByLine(path.resolve(__dirname, '../List/domainset', filePath))) {
  15. // starts with #
  16. const line = processLine(l);
  17. if (!line) {
  18. continue;
  19. }
  20. const domain = line.charCodeAt(0) === 46 ? line.slice(1) : line;
  21. const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
  22. if (
  23. (
  24. parsed.isPrivate
  25. || parsed.isIcann
  26. ) && domain === parsed.publicSuffix
  27. ) {
  28. console.error(`[${filePath}]`, picocolors.yellow(domain), picocolors.red('is in public suffix list!'));
  29. }
  30. }
  31. };
  32. const _validateRuleset = async (filePath) => {
  33. console.log(`[${filePath}]`);
  34. for await (const l of readFileByLine(path.resolve(__dirname, '../List/non_ip', filePath))) {
  35. // starts with #
  36. const line = processLine(l);
  37. if (!line) {
  38. continue;
  39. }
  40. if (!line.startsWith('DOMAIN-SUFFIX,')) {
  41. continue;
  42. }
  43. const domain = line.slice(14);
  44. const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
  45. if (domain !== parsed.publicSuffix) {
  46. if (!SPECIAL_SUFFIXES.has(domain)) {
  47. console.warn(picocolors.yellow(domain), picocolors.green('is not in public suffix list!'));
  48. }
  49. }
  50. }
  51. };
  52. (async () => {
  53. const [domainsetFiles, _rulesetFiles] = await Promise.all([
  54. listDir(path.resolve(__dirname, '../List/domainset')),
  55. listDir(path.resolve(__dirname, '../List/non_ip'))
  56. ]);
  57. await Promise.all(
  58. domainsetFiles.map(file => validateDomainSet(file))
  59. // rulesetFiles.map(file => validateRuleset(file))
  60. );
  61. })();