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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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, merge } from 'fast-cidr-tools';
  7. // https://en.wikipedia.org/wiki/Reserved_IP_addresses
  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.88.99.0 // is currently being broadcast by HE and Comcast
  18. '192.168.0.0/16',
  19. '198.18.0.0/15',
  20. '198.51.100.0/24',
  21. '203.0.113.0/24',
  22. '224.0.0.0/4',
  23. '233.252.0.0/24',
  24. '240.0.0.0/4'
  25. ];
  26. export const buildInternalReverseChnCIDR = task(import.meta.path, async () => {
  27. const cidr = (await Promise.all([
  28. processLineFromReadline(await fetchRemoteTextAndReadByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')),
  29. fsp.mkdir(path.resolve(import.meta.dir, '../List/internal'), { recursive: true })
  30. ]))[0];
  31. const reversedCidr = merge(
  32. exclude(
  33. ['0.0.0.0/0'],
  34. RESERVED_IPV4_CIDR.concat(cidr),
  35. true
  36. ).concat([
  37. // https://github.com/misakaio/chnroutes2/issues/25
  38. '223.118.0.0/15',
  39. '223.120.0.0/15'
  40. ])
  41. );
  42. return Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
  43. });
  44. if (import.meta.main) {
  45. buildInternalReverseChnCIDR();
  46. }