build-internal-reverse-chn-cidr.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // @ts-check
  2. const { fetchRemoteTextAndCreateReadlineInterface } = require('./lib/fetch-remote-text-by-line');
  3. const { processLine } = require('./lib/process-line');
  4. const path = require('path');
  5. const fse = require('fs-extra');
  6. const fs = require('fs');
  7. const RESERVED_IPV4_CIDR = [
  8. '0.0.0.0/8',
  9. '10.0.0.0/8',
  10. '100.64.0.0/10',
  11. '127.0.0.0/8',
  12. '169.254.0.0/16',
  13. '172.16.0.0/12',
  14. '192.0.0.0/24',
  15. '192.0.2.0/24',
  16. '192.168.0.0/16',
  17. '198.18.0.0/15',
  18. '198.51.100.0/24',
  19. '203.0.113.0/24',
  20. '224.0.0.0/4',
  21. '233.252.0.0/24',
  22. '240.0.0.0/4'
  23. ];
  24. (async () => {
  25. const { exclude } = await import('cidr-tools-wasm');
  26. /** @type {Set<string>} */
  27. const cidr = new Set();
  28. for await (const line of await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')) {
  29. const l = processLine(line);
  30. if (l) {
  31. cidr.add(l);
  32. }
  33. }
  34. const reversedCidr = exclude(
  35. ['0.0.0.0/0'],
  36. RESERVED_IPV4_CIDR.concat(Array.from(cidr))
  37. );
  38. await fse.ensureDir(path.resolve(__dirname, '../List/internal'));
  39. await fs.promises.writeFile(
  40. path.resolve(__dirname, '../List/internal/cdn.txt'),
  41. `${reversedCidr.join('\n')}\n`
  42. );
  43. })();