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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { fetchRemoteTextAndReadByLine } from './lib/fetch-text-by-line';
  2. import { processLineFromReadline } from './lib/process-line';
  3. import path from 'path';
  4. import { task } from './lib/trace-runner';
  5. import { exclude, merge } from 'fast-cidr-tools';
  6. // https://en.wikipedia.org/wiki/Reserved_IP_addresses
  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.88.99.0 // is currently being broadcast by HE and Comcast
  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. export const buildInternalReverseChnCIDR = task(import.meta.path, async () => {
  26. const cidr = await processLineFromReadline(await fetchRemoteTextAndReadByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt'));
  27. const reversedCidr = merge(
  28. exclude(
  29. ['0.0.0.0/0'],
  30. RESERVED_IPV4_CIDR.concat(cidr),
  31. true
  32. ).concat([
  33. // https://github.com/misakaio/chnroutes2/issues/25
  34. '223.118.0.0/15',
  35. '223.120.0.0/15'
  36. ])
  37. );
  38. return Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
  39. });
  40. if (import.meta.main) {
  41. buildInternalReverseChnCIDR();
  42. }