build-telegram-cidr.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // @ts-check
  2. import { fetchWithRetry } from './lib/fetch-retry';
  3. import { createReadlineInterfaceFromResponse } from './lib/fetch-remote-text-by-line';
  4. import path from 'path';
  5. import { isIPv4, isIPv6 } from 'net';
  6. import { processLine } from './lib/process-line';
  7. import { createRuleset } from './lib/create-file';
  8. import { task } from './lib/trace-runner';
  9. export const buildTelegramCIDR = task(__filename, async () => {
  10. /** @type {Response} */
  11. const resp = await fetchWithRetry('https://core.telegram.org/resources/cidr.txt');
  12. const lastModified = resp.headers.get('last-modified');
  13. const date = lastModified ? new Date(lastModified) : new Date();
  14. /** @type {string[]} */
  15. const results = [];
  16. for await (const line of createReadlineInterfaceFromResponse(resp)) {
  17. const cidr = processLine(line);
  18. if (!cidr) continue;
  19. const [subnet] = cidr.split('/');
  20. if (isIPv4(subnet)) {
  21. results.push(`IP-CIDR,${cidr},no-resolve`);
  22. }
  23. if (isIPv6(subnet)) {
  24. results.push(`IP-CIDR6,${cidr},no-resolve`);
  25. }
  26. }
  27. if (results.length === 0) {
  28. throw new Error('Failed to fetch data!');
  29. }
  30. const description = [
  31. 'License: AGPL 3.0',
  32. 'Homepage: https://ruleset.skk.moe',
  33. 'GitHub: https://github.com/SukkaW/Surge',
  34. 'Data from:',
  35. ' - https://core.telegram.org/resources/cidr.txt'
  36. ];
  37. return Promise.all(createRuleset(
  38. 'Sukka\'s Ruleset - Telegram IP CIDR',
  39. description,
  40. date,
  41. results,
  42. 'ruleset',
  43. path.resolve(__dirname, '../List/ip/telegram.conf'),
  44. path.resolve(__dirname, '../Clash/ip/telegram.txt')
  45. ));
  46. });
  47. if (import.meta.main) {
  48. buildTelegramCIDR();
  49. }