build-chn-cidr.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { fetchRemoteTextAndCreateReadlineInterface } from './lib/fetch-remote-text-by-line';
  2. import { resolve as pathResolve } from 'path';
  3. import { compareAndWriteFile, withBannerArray } from './lib/create-file';
  4. import { processLineFromReadline } from './lib/process-line';
  5. import { task } from './lib/trace-runner';
  6. // https://github.com/misakaio/chnroutes2/issues/25
  7. const EXCLUDE_CIDRS = [
  8. '223.118.0.0/15',
  9. '223.120.0.0/15'
  10. ];
  11. // preload the module
  12. import('cidr-tools-wasm');
  13. export const buildChnCidr = task(import.meta.path, async () => {
  14. const [{ exclude }, cidr] = await Promise.all([
  15. import('cidr-tools-wasm'),
  16. processLineFromReadline(await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt'))
  17. ]);
  18. const filteredCidr = exclude(cidr, EXCLUDE_CIDRS, true);
  19. // Can not use SHARED_DESCRIPTION here as different license
  20. const description = [
  21. 'License: CC BY-SA 2.0',
  22. 'Homepage: https://ruleset.skk.moe',
  23. 'GitHub: https://github.com/SukkaW/Surge',
  24. '',
  25. 'Data from https://misaka.io (misakaio @ GitHub)'
  26. ];
  27. return Promise.all([
  28. compareAndWriteFile(
  29. withBannerArray(
  30. 'Sukka\'s Ruleset - Mainland China IPv4 CIDR',
  31. description,
  32. new Date(),
  33. filteredCidr.map(i => `IP-CIDR,${i}`)
  34. ),
  35. pathResolve(import.meta.dir, '../List/ip/china_ip.conf')
  36. ),
  37. compareAndWriteFile(
  38. withBannerArray(
  39. 'Sukka\'s Ruleset - Mainland China IPv4 CIDR',
  40. description,
  41. new Date(),
  42. filteredCidr
  43. ),
  44. pathResolve(import.meta.dir, '../Clash/ip/china_ip.txt')
  45. )
  46. ]);
  47. });
  48. if (import.meta.main) {
  49. buildChnCidr();
  50. }