build-chn-cidr.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // @ts-check
  2. const { fetchRemoteTextAndCreateReadlineInterface } = require('./lib/fetch-remote-text-by-line');
  3. const { withBannerArray } = require('./lib/with-banner');
  4. const { resolve: pathResolve } = require('path');
  5. const { compareAndWriteFile } = require('./lib/string-array-compare');
  6. const { processLine } = require('./lib/process-line');
  7. // https://github.com/misakaio/chnroutes2/issues/25
  8. const EXCLUDE_CIDRS = [
  9. '223.118.0.0/15',
  10. '223.120.0.0/15'
  11. ];
  12. (async () => {
  13. console.time('Total Time - build-chnroutes-cidr');
  14. const { exclude: excludeCidrs } = await import('cidr-tools-wasm');
  15. /** @type {Set<string>} */
  16. const cidr = new Set();
  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.add(l);
  21. }
  22. }
  23. console.log('Before Merge:', cidr.size);
  24. const filteredCidr = excludeCidrs(Array.from(cidr), EXCLUDE_CIDRS, true);
  25. console.log('After Merge:', filteredCidr.length);
  26. await Promise.all([
  27. compareAndWriteFile(
  28. withBannerArray(
  29. 'Sukka\'s Ruleset - Mainland China IPv4 CIDR',
  30. [
  31. 'License: CC BY-SA 2.0',
  32. 'Homepage: https://ruleset.skk.moe',
  33. 'GitHub: https://github.com/SukkaW/Surge',
  34. '',
  35. 'Data from https://misaka.io (misakaio @ GitHub)'
  36. ],
  37. new Date(),
  38. filteredCidr.map(i => `IP-CIDR,${i}`)
  39. ),
  40. pathResolve(__dirname, '../List/ip/china_ip.conf')
  41. ),
  42. compareAndWriteFile(
  43. withBannerArray(
  44. 'Sukka\'s Ruleset - Mainland China IPv4 CIDR',
  45. [
  46. 'License: CC BY-SA 2.0',
  47. 'Homepage: https://ruleset.skk.moe',
  48. 'GitHub: https://github.com/SukkaW/Surge',
  49. '',
  50. 'Data from https://misaka.io (misakaio @ GitHub)'
  51. ],
  52. new Date(),
  53. filteredCidr
  54. ),
  55. pathResolve(__dirname, '../Clash/ip/china_ip.txt')
  56. )
  57. ]);
  58. console.timeEnd('Total Time - build-chnroutes-cidr');
  59. })();