build-chn-cidr.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // @ts-check
  2. const { fetchRemoteTextAndCreateReadlineInterface } = require('./lib/fetch-remote-text-by-line');
  3. const { resolve: pathResolve } = require('path');
  4. // This should not use `createRuleset` API since we are going to generate ipcidr for Clash
  5. const { compareAndWriteFile, withBannerArray } = require('./lib/create-file');
  6. const { processLine } = require('./lib/process-line');
  7. const { runner } = require('./lib/trace-runner');
  8. // https://github.com/misakaio/chnroutes2/issues/25
  9. const EXCLUDE_CIDRS = [
  10. '223.118.0.0/15',
  11. '223.120.0.0/15'
  12. ];
  13. runner(__filename, async () => {
  14. const { exclude: excludeCidrs } = await import('cidr-tools-wasm');
  15. /** @type {string[]} */
  16. const cidr = [];
  17. for await (const line of await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')) {
  18. const l = processLine(line);
  19. if (l) {
  20. cidr.push(l);
  21. }
  22. }
  23. console.log('Before Merge:', cidr.length);
  24. const filteredCidr = excludeCidrs(cidr, EXCLUDE_CIDRS, true);
  25. console.log('After Merge:', filteredCidr.length);
  26. const description = [
  27. 'License: CC BY-SA 2.0',
  28. 'Homepage: https://ruleset.skk.moe',
  29. 'GitHub: https://github.com/SukkaW/Surge',
  30. '',
  31. 'Data from https://misaka.io (misakaio @ GitHub)'
  32. ];
  33. await Promise.all([
  34. compareAndWriteFile(
  35. withBannerArray(
  36. 'Sukka\'s Ruleset - Mainland China IPv4 CIDR',
  37. description,
  38. new Date(),
  39. filteredCidr.map(i => `IP-CIDR,${i}`)
  40. ),
  41. pathResolve(__dirname, '../List/ip/china_ip.conf')
  42. ),
  43. compareAndWriteFile(
  44. withBannerArray(
  45. 'Sukka\'s Ruleset - Mainland China IPv4 CIDR',
  46. description,
  47. new Date(),
  48. filteredCidr
  49. ),
  50. pathResolve(__dirname, '../Clash/ip/china_ip.txt')
  51. )
  52. ]);
  53. });