build-telegram-cidr.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // @ts-check
  2. import { createReadlineInterfaceFromResponse } from './lib/fetch-text-by-line';
  3. import { task } from './trace';
  4. import { SHARED_DESCRIPTION } from './constants/description';
  5. import { once } from 'foxts/once';
  6. import { RulesetOutput } from './lib/rules/ruleset';
  7. import { $$fetch } from './lib/fetch-retry';
  8. import { fastIpVersion } from 'foxts/fast-ip-version';
  9. import DNS2 from 'dns2';
  10. import { getTelegramBackupIPFromBase64 } from './lib/get-telegram-backup-ip';
  11. export const getTelegramCIDRPromise = once(async () => {
  12. const resp = await $$fetch('https://core.telegram.org/resources/cidr.txt');
  13. const lastModified = resp.headers.get('last-modified');
  14. const date = lastModified ? new Date(lastModified) : new Date();
  15. const ipcidr: string[] = [
  16. // Telegram secret backup CIDR, announced by AS62041
  17. // see also https://github.com/Telegram-FOSS-Team/Telegram-FOSS/blob/10da5406ed92d30c6add3b25d40b2b3b6995d873/TMessagesProj/src/main/java/org/telegram/tgnet/ConnectionsManager.java#L1157
  18. '95.161.64.0/20'
  19. ];
  20. const ipcidr6: string[] = [];
  21. for await (const cidr of createReadlineInterfaceFromResponse(resp, true)) {
  22. const v = fastIpVersion(cidr);
  23. if (v === 4) {
  24. ipcidr.push(cidr);
  25. } else if (v === 6) {
  26. ipcidr6.push(cidr);
  27. }
  28. }
  29. const backupIPs = new Set<string>();
  30. // Backup IP Source 1 (DoH)
  31. await Promise.all([
  32. DNS2.DOHClient({
  33. dns: '8.8.8.8',
  34. http: false
  35. }),
  36. DNS2.DOHClient({
  37. dns: '1.0.0.1',
  38. http: false
  39. })
  40. ].map(async (client) => {
  41. // tapv3.stel.com was for testing server
  42. const resp = await client('apv3.stel.com', 'TXT');
  43. const strings = resp.answers.map(i => i.data);
  44. const str = strings[0]!.length > strings[1]!.length
  45. ? strings[0]! + strings[1]!
  46. : strings[1]! + strings[0]!;
  47. const ips = getTelegramBackupIPFromBase64(str);
  48. ips.forEach(i => i && backupIPs.add(i.ip));
  49. }));
  50. // Backup IP Source 2: Firebase Storage
  51. try {
  52. const text = await (await $$fetch('https://reserve-5a846.firebaseio.com/ipconfigv3.json')).json();
  53. if (typeof text === 'string' && text.length === 344) {
  54. const ips = getTelegramBackupIPFromBase64(text);
  55. ips.forEach(i => i && backupIPs.add(i.ip));
  56. }
  57. } catch {
  58. // ignore all errors
  59. }
  60. // Backup IP Source 3: Firebase Value Store
  61. try {
  62. const json = await (await $$fetch('https://firestore.googleapis.com/v1/projects/reserve-5a846/databases/(default)/documents/ipconfig/v3')).json();
  63. if (
  64. json && typeof json === 'object'
  65. && 'fields' in json && typeof json.fields === 'object' && json.fields
  66. && 'data' in json.fields && typeof json.fields.data === 'object' && json.fields.data
  67. && 'stringValue' in json.fields.data && typeof json.fields.data.stringValue === 'string' && json.fields.data.stringValue.length === 344
  68. ) {
  69. const ips = getTelegramBackupIPFromBase64(json.fields.data.stringValue);
  70. ips.forEach(i => i && backupIPs.add(i.ip));
  71. }
  72. } catch {}
  73. ipcidr.push(...Array.from(backupIPs).map(i => i + '/32'));
  74. return { date, ipcidr, ipcidr6 };
  75. });
  76. export const buildTelegramCIDR = task(require.main === module, __filename)(async (span) => {
  77. const { date, ipcidr, ipcidr6 } = await span.traceChildAsync('get telegram cidr', getTelegramCIDRPromise);
  78. if (ipcidr.length + ipcidr6.length === 0) {
  79. throw new Error('Failed to fetch data!');
  80. }
  81. const description = [
  82. ...SHARED_DESCRIPTION,
  83. 'Data from:',
  84. ' - https://core.telegram.org/resources/cidr.txt'
  85. ];
  86. return new RulesetOutput(span, 'telegram', 'ip')
  87. .withTitle('Sukka\'s Ruleset - Telegram IP CIDR')
  88. .withDescription(description)
  89. .withDate(date)
  90. .bulkAddCIDR4NoResolve(ipcidr)
  91. .bulkAddCIDR6NoResolve(ipcidr6)
  92. .write();
  93. });