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

12345678910111213141516171819202122232425262728293031
  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 { writeFile } from './lib/bun';
  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. const outputDir = path.resolve(__dirname, '../Internal');
  20. const outputFile = path.join(outputDir, 'reversed-chn-cidr.txt');
  21. return writeFile(
  22. outputFile,
  23. reversedCidr.join('\n') + '\n'
  24. );
  25. });