validate-cdn-conf.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const tldts = require('tldts');
  2. const picocolors = require('picocolors');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const SPECIAL_SUFFIXES = new Set([
  6. 'linodeobjects.com', // only *.linodeobjects.com are public suffix
  7. 'vultrobjects.com', // only *.vultrobjects.com are public suffix
  8. 'dweb.link' // only *.dweb.link are public suffix
  9. ]);
  10. (async () => {
  11. const domainSetContent = await fs.promises.readFile(
  12. path.resolve(__dirname, '../List/domainset/cdn.conf'),
  13. { encoding: 'utf-8' }
  14. );
  15. const domainSetLines = domainSetContent.split('\n');
  16. for (let i = 0, len = domainSetLines.length; i < len; i++) {
  17. const line = domainSetLines[i];
  18. // starts with #
  19. if (line.charCodeAt(0) === 35) {
  20. continue;
  21. }
  22. if (line.trim().length === 0) {
  23. continue;
  24. }
  25. const domain = line.charCodeAt(0) === 46 ? line.slice(1) : line;
  26. const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
  27. if (
  28. (
  29. parsed.isPrivate
  30. || parsed.isIcann
  31. ) && domain === parsed.publicSuffix
  32. ) {
  33. console.error('Domain', picocolors.yellow(domain), picocolors.red('is in public suffix list!'));
  34. }
  35. }
  36. const rulesetContent = await fs.promises.readFile(
  37. path.resolve(__dirname, '../List/non_ip/cdn.conf'),
  38. { encoding: 'utf-8' }
  39. );
  40. const rulesetLines = rulesetContent.split('\n');
  41. for (let i = 0, len = rulesetLines.length; i < len; i++) {
  42. const line = rulesetLines[i];
  43. // starts with #
  44. if (line.charCodeAt(0) === 35) {
  45. continue;
  46. }
  47. if (line.trim().length === 0) {
  48. continue;
  49. }
  50. if (line.startsWith('DOMAIN-SUFFIX')) {
  51. const domain = line.slice(14);
  52. const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
  53. if (domain !== parsed.publicSuffix) {
  54. if (!SPECIAL_SUFFIXES.has(domain)) {
  55. console.error('Domain', picocolors.yellow(domain), picocolors.green('is not in public suffix list!'));
  56. }
  57. }
  58. }
  59. }
  60. })();