validate-domainset.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. const rl = readFileByLine(
  15. path.resolve(__dirname, '../List/domainset', filePath)
  16. );
  17. for await (const l of rl) {
  18. // starts with #
  19. const line = processLine(l);
  20. if (!line) {
  21. continue;
  22. }
  23. const domain = line.charCodeAt(0) === 46 ? line.slice(1) : line;
  24. const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
  25. if (
  26. (
  27. parsed.isPrivate
  28. || parsed.isIcann
  29. ) && domain === parsed.publicSuffix
  30. ) {
  31. console.error(`[${filePath}]`, picocolors.yellow(domain), picocolors.red('is in public suffix list!'));
  32. }
  33. }
  34. };
  35. const validateRuleset = async (filePath) => {
  36. const rl = readFileByLine(
  37. path.resolve(__dirname, '../List/non_ip', filePath)
  38. );
  39. console.log(`[${filePath}]`);
  40. for await (const l of rl) {
  41. // starts with #
  42. const line = processLine(l);
  43. if (!line) {
  44. continue;
  45. }
  46. if (!line.startsWith('DOMAIN-SUFFIX,')) {
  47. continue;
  48. }
  49. const domain = line.slice(14);
  50. const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
  51. if (domain !== parsed.publicSuffix) {
  52. if (!SPECIAL_SUFFIXES.has(domain)) {
  53. console.warn(picocolors.yellow(domain), picocolors.green('is not in public suffix list!'));
  54. }
  55. }
  56. }
  57. };
  58. (async () => {
  59. const [domainsetFiles, _rulesetFiles] = await Promise.all([
  60. listDir(path.resolve(__dirname, '../List/domainset')),
  61. listDir(path.resolve(__dirname, '../List/non_ip'))
  62. ]);
  63. await Promise.all(
  64. domainsetFiles.map(file => validateDomainSet(file))
  65. // rulesetFiles.map(file => validateRuleset(file))
  66. );
  67. })();