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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { fetchRemoteTextByLine } from './lib/fetch-text-by-line';
  2. import { processLineFromReadline } from './lib/process-line';
  3. import path from 'path';
  4. import { task } from './trace';
  5. import { exclude, merge } from 'fast-cidr-tools';
  6. import { getChnCidrPromise } from './build-chn-cidr';
  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 (span) => {
  27. const cidrPromise = getChnCidrPromise();
  28. const peeked = Bun.peek(cidrPromise);
  29. const cidr: string[] = peeked === cidrPromise
  30. ? await span.traceChildPromise('download chnroutes2', cidrPromise)
  31. : (peeked as string[]);
  32. const reversedCidr = span.traceChildSync('build reversed chn cidr', () => merge(
  33. exclude(
  34. ['0.0.0.0/0'],
  35. RESERVED_IPV4_CIDR.concat(cidr),
  36. true
  37. ).concat([
  38. // https://github.com/misakaio/chnroutes2/issues/25
  39. '223.118.0.0/15',
  40. '223.120.0.0/15'
  41. ])
  42. ));
  43. return Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
  44. });
  45. if (import.meta.main) {
  46. buildInternalReverseChnCIDR();
  47. }