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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. ['0.0.0.0/0'],
  33. RESERVED_IPV4_CIDR.concat(cidr),
  34. true
  35. );
  36. return fs.promises.writeFile(
  37. path.resolve(__dirname, '../List/internal/reversed-chn-cidr.txt'),
  38. `${reversedCidr.join('\n')}\n`
  39. );
  40. });
  41. module.exports.buildInternalReverseChnCIDR = buildInternalReverseChnCIDR;
  42. if (require.main === module) {
  43. buildInternalReverseChnCIDR();
  44. }