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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 fs = require('fs');
  6. const fsp = require('fs/promises');
  7. const { task } = 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. const buildInternalReverseChnCIDR = task(__filename, async () => {
  26. const [{ exclude }, cidr] = await Promise.all([
  27. import('cidr-tools-wasm'),
  28. processLineFromReadline(await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')),
  29. fsp.mkdir(path.resolve(__dirname, '../List/internal'), { recursive: true })
  30. ]);
  31. const reversedCidr = exclude(
  32. [
  33. '0.0.0.0/0',
  34. // https://github.com/misakaio/chnroutes2/issues/25
  35. '223.118.0.0/15',
  36. '223.120.0.0/15'
  37. ],
  38. RESERVED_IPV4_CIDR.concat(cidr),
  39. true
  40. );
  41. return fs.promises.writeFile(
  42. path.resolve(__dirname, '../List/internal/reversed-chn-cidr.txt'),
  43. `${reversedCidr.join('\n')}\n`
  44. );
  45. });
  46. module.exports.buildInternalReverseChnCIDR = buildInternalReverseChnCIDR;
  47. if (require.main === module) {
  48. buildInternalReverseChnCIDR();
  49. }