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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { fetchRemoteTextAndCreateReadlineInterface } from './lib/fetch-remote-text-by-line';
  2. import { processLineFromReadline } from './lib/process-line';
  3. import path from 'path';
  4. import fsp from 'fs/promises';
  5. import { task } from './lib/trace-runner';
  6. const RESERVED_IPV4_CIDR = [
  7. '0.0.0.0/8',
  8. '10.0.0.0/8',
  9. '100.64.0.0/10',
  10. '127.0.0.0/8',
  11. '169.254.0.0/16',
  12. '172.16.0.0/12',
  13. '192.0.0.0/24',
  14. '192.0.2.0/24',
  15. '192.168.0.0/16',
  16. '198.18.0.0/15',
  17. '198.51.100.0/24',
  18. '203.0.113.0/24',
  19. '224.0.0.0/4',
  20. '233.252.0.0/24',
  21. '240.0.0.0/4'
  22. ];
  23. // preload the module
  24. import('cidr-tools-wasm');
  25. export const buildInternalReverseChnCIDR = task(import.meta.path, 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(import.meta.dir, '../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 Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
  42. });
  43. if (import.meta.main) {
  44. buildInternalReverseChnCIDR();
  45. }