validate-domainset.js 2.3 KB

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