build-telegram-cidr.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const { fetchWithRetry } = require('./lib/fetch-retry');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { isIPv4, isIPv6 } = require('net');
  5. const { withBanner } = require('./lib/with-banner');
  6. (async () => {
  7. console.time('Total Time - build-telegram-cidr');
  8. /** @type {Response} */
  9. const resp = await fetchWithRetry('https://core.telegram.org/resources/cidr.txt');
  10. const lastModified = resp.headers.get('last-modified');
  11. const date = lastModified ? new Date(lastModified) : new Date();
  12. const res = (await resp.text())
  13. .split('\n')
  14. .filter(line => line.trim() !== '');
  15. if (res.length === 0) {
  16. throw new Error('Failed to fetch data!');
  17. }
  18. await fs.promises.writeFile(
  19. path.resolve(__dirname, '../List/ip/telegram.conf'),
  20. withBanner(
  21. 'Sukka\'s Surge Rules - Telegram IP CIDR',
  22. [
  23. 'License: AGPL 3.0',
  24. 'Homepage: https://ruleset.skk.moe',
  25. 'GitHub: https://github.com/SukkaW/Surge',
  26. 'Data from:',
  27. ' - https://core.telegram.org/resources/cidr.txt'
  28. ],
  29. date,
  30. res.map(ip => {
  31. const [subnet] = ip.split('/');
  32. console.log(` - ${ip}: ${subnet}`);
  33. if (isIPv4(subnet)) {
  34. return `IP-CIDR,${ip},no-resolve`;
  35. }
  36. if (isIPv6(subnet)) {
  37. return `IP-CIDR6,${ip},no-resolve`;
  38. }
  39. return '';
  40. })
  41. )
  42. );
  43. console.timeEnd('Total Time - build-telegram-cidr');
  44. })();