build-internal-reverse-chn-cidr.ts 879 B

1234567891011121314151617181920212223242526272829
  1. import path from 'path';
  2. import { task } from './trace';
  3. import { exclude, merge } from 'fast-cidr-tools';
  4. import { getChnCidrPromise } from './build-chn-cidr';
  5. import { NON_CN_CIDR_INCLUDED_IN_CHNROUTE, RESERVED_IPV4_CIDR } from './constants/cidr';
  6. import fsp from 'fs/promises';
  7. export const buildInternalReverseChnCIDR = task(typeof Bun !== 'undefined' ? Bun.main === __filename : require.main === module, __filename)(async () => {
  8. const cidr = await getChnCidrPromise();
  9. const reversedCidr = merge(
  10. exclude(
  11. ['0.0.0.0/0'],
  12. RESERVED_IPV4_CIDR.concat(cidr),
  13. true
  14. ).concat(
  15. // https://github.com/misakaio/chnroutes2/issues/25
  16. NON_CN_CIDR_INCLUDED_IN_CHNROUTE
  17. )
  18. );
  19. return fsp.writeFile(
  20. path.resolve(__dirname, '../Internal/reversed-chn-cidr.txt'),
  21. reversedCidr.join('\n') + '\n',
  22. { encoding: 'utf-8' }
  23. );
  24. });