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

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