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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // @ts-check
  2. const { fetchRemoteTextAndCreateReadlineInterface } = require('./lib/fetch-remote-text-by-line');
  3. const { processLineFromReadline } = require('./lib/process-line');
  4. const path = require('path');
  5. const fsp = require('fs/promises');
  6. const { task } = require('./lib/trace-runner');
  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. const buildInternalReverseChnCIDR = task(__filename, async () => {
  25. const [{ exclude }, cidr] = await Promise.all([
  26. import('cidr-tools-wasm'),
  27. processLineFromReadline(await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')),
  28. fsp.mkdir(path.resolve(__dirname, '../List/internal'), { recursive: true })
  29. ]);
  30. const reversedCidr = exclude(
  31. [
  32. '0.0.0.0/0',
  33. // https://github.com/misakaio/chnroutes2/issues/25
  34. '223.118.0.0/15',
  35. '223.120.0.0/15'
  36. ],
  37. RESERVED_IPV4_CIDR.concat(cidr),
  38. true
  39. );
  40. return Bun.write(path.resolve(__dirname, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
  41. });
  42. module.exports.buildInternalReverseChnCIDR = buildInternalReverseChnCIDR;
  43. if (import.meta.main) {
  44. buildInternalReverseChnCIDR();
  45. }