build-telegram-cidr.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Unused secret Telegram backup CIDR, announced by AS62041
  17. '95.161.64.0/20'
  18. ];
  19. const ipcidr6: string[] = [];
  20. for await (const cidr of createReadlineInterfaceFromResponse(resp, true)) {
  21. const v = fastIpVersion(cidr);
  22. if (v === 4) {
  23. ipcidr.push(cidr);
  24. } else if (v === 6) {
  25. ipcidr6.push(cidr);
  26. }
  27. }
  28. const backupIPs = new Set<string>();
  29. // https://github.com/tdlib/td/blob/master/td/telegram/ConfigManager.cpp
  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. try {
  42. // tapv3.stel.com was for testing server
  43. const resp = await client('apv3.stel.com', 'TXT');
  44. const strings = resp.answers.map(i => i.data);
  45. const str = strings[0]!.length > strings[1]!.length
  46. ? strings[0]! + strings[1]!
  47. : strings[1]! + strings[0]!;
  48. const ips = getTelegramBackupIPFromBase64(str);
  49. ips.forEach(i => i && backupIPs.add(i.ip));
  50. } catch {}
  51. }));
  52. // Backup IP Source 2: Firebase Realtime Database
  53. try {
  54. const text = await (await $$fetch('https://reserve-5a846.firebaseio.com/ipconfigv3.json')).json();
  55. if (typeof text === 'string' && text.length === 344) {
  56. const ips = getTelegramBackupIPFromBase64(text);
  57. ips.forEach(i => i && backupIPs.add(i.ip));
  58. }
  59. } catch {
  60. // ignore all errors
  61. }
  62. // Backup IP Source 3: Firebase Value Store
  63. try {
  64. const json = await (await $$fetch('https://firestore.googleapis.com/v1/projects/reserve-5a846/databases/(default)/documents/ipconfig/v3')).json();
  65. if (
  66. json && typeof json === 'object'
  67. && 'fields' in json && typeof json.fields === 'object' && json.fields
  68. && 'data' in json.fields && typeof json.fields.data === 'object' && json.fields.data
  69. && 'stringValue' in json.fields.data && typeof json.fields.data.stringValue === 'string' && json.fields.data.stringValue.length === 344
  70. ) {
  71. const ips = getTelegramBackupIPFromBase64(json.fields.data.stringValue);
  72. ips.forEach(i => i && backupIPs.add(i.ip));
  73. }
  74. } catch {}
  75. // Backup IP Source 4: Google App Engine: https://dns-telegram.appspot.com https://dns-telegram.appspot.com/test
  76. try {
  77. const text = await (await $$fetch('https://dns-telegram.appspot.com')).text();
  78. if (text.length === 344) {
  79. const ips = getTelegramBackupIPFromBase64(text);
  80. ips.forEach(i => i && backupIPs.add(i.ip));
  81. }
  82. } catch {}
  83. // tcdnb.azureedge.net no longer works
  84. console.log(`Found ${backupIPs.size} backup IPs:`, backupIPs);
  85. ipcidr.push(...Array.from(backupIPs).map(i => i + '/32'));
  86. return { date, ipcidr, ipcidr6 };
  87. });
  88. export const buildTelegramCIDR = task(require.main === module, __filename)(async (span) => {
  89. const { date, ipcidr, ipcidr6 } = await span.traceChildAsync('get telegram cidr', getTelegramCIDRPromise);
  90. if (ipcidr.length + ipcidr6.length === 0) {
  91. throw new Error('Failed to fetch data!');
  92. }
  93. const description = [
  94. ...SHARED_DESCRIPTION,
  95. 'Data from:',
  96. ' - https://core.telegram.org/resources/cidr.txt'
  97. ];
  98. return new RulesetOutput(span, 'telegram', 'ip')
  99. .withTitle('Sukka\'s Ruleset - Telegram IP CIDR')
  100. .withDescription(description)
  101. .withDate(date)
  102. .bulkAddCIDR4NoResolve(ipcidr)
  103. .bulkAddCIDR6NoResolve(ipcidr6)
  104. .write();
  105. });