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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { fetchRemoteTextAndReadByLine } from './lib/fetch-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. import { exclude } from 'fast-cidr-tools';
  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. export const buildInternalReverseChnCIDR = task(import.meta.path, async () => {
  25. const cidr = (await Promise.all([
  26. processLineFromReadline(await fetchRemoteTextAndReadByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')),
  27. fsp.mkdir(path.resolve(import.meta.dir, '../List/internal'), { recursive: true })
  28. ]))[0];
  29. const reversedCidr = exclude(
  30. [
  31. '0.0.0.0/0',
  32. // https://github.com/misakaio/chnroutes2/issues/25
  33. '223.118.0.0/15',
  34. '223.120.0.0/15'
  35. ],
  36. RESERVED_IPV4_CIDR.concat(cidr),
  37. true
  38. );
  39. return Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
  40. });
  41. if (import.meta.main) {
  42. buildInternalReverseChnCIDR();
  43. }