get-telegram-backup-ip.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // https://reserve-5a846.firebaseio.com/ipconfigv3.json
  2. // apv3.stel.com tapv3.stel.com
  3. import { Buffer } from 'node:buffer';
  4. import crypto from 'node:crypto';
  5. import { Api, extensions as TgExtensions } from 'telegram';
  6. import { bigint2ip } from 'fast-cidr-tools';
  7. const mtptoto_public_rsa = `-----BEGIN RSA PUBLIC KEY-----
  8. MIIBCgKCAQEAyr+18Rex2ohtVy8sroGP
  9. BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD
  10. eQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd192xRGreuXIaUKmkwlM9JID9WS2jUsTp
  11. zQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c9G5IzzBm+otMS/YKwmR1olzRCyEkyAEj
  12. XWqBI9Ftv5eG8m0VkBzOG655WIYdyV0HfDK/NWcvGqa0w/nriMD6mDjKOryamw0O
  13. P9QuYgMN0C9xMW9y8SmP4h92OAWodTYgY1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWY
  14. xwIDAQAB
  15. -----END RSA PUBLIC KEY-----
  16. `;
  17. export function getTelegramBackupIPFromBase64(base64: string) {
  18. // 1. Check base64 size
  19. if (base64.length !== 344) {
  20. throw new TypeError('Invalid base64 length');
  21. }
  22. // 2. Filter to base64 and check length
  23. // Not needed with Buffer.from
  24. // 3. Decode base64 to Buffer
  25. const decoded = Buffer.from(base64, 'base64');
  26. if (decoded.length !== 256) {
  27. throw new TypeError('Decoded buffer length is not 344 bytes, received ' + decoded.length);
  28. }
  29. // 4. RSA decrypt (public key, "decrypt signature" - usually means "verify and extract")
  30. // In Node.js, publicDecrypt is used for signature verification (Note that no padding is needed)
  31. const publicKey = crypto.createPublicKey(mtptoto_public_rsa);
  32. const decrypted = crypto.publicDecrypt(
  33. {
  34. key: publicKey,
  35. padding: crypto.constants.RSA_NO_PADDING
  36. },
  37. decoded
  38. );
  39. // 5. Extract AES key/iv and encrypted payload
  40. const key = decrypted.subarray(0, 32);
  41. const iv = decrypted.subarray(16, 32);
  42. const dataCbc = decrypted.subarray(32); // 224 bytes
  43. if (dataCbc.length !== 224) {
  44. throw new Error(`Invalid AES payload length: ${dataCbc.length}`);
  45. }
  46. // 6. AES-CBC decrypt
  47. const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  48. decipher.setAutoPadding(false);
  49. const decryptedCbc = Buffer.concat([decipher.update(dataCbc), decipher.final()]);
  50. if (decryptedCbc.length !== 224) {
  51. throw new Error(`Decrypted AES payload length is not 224 bytes, received ${decryptedCbc.length}`);
  52. }
  53. // 7. SHA256 check
  54. const currentHash = crypto
  55. .createHash('sha256')
  56. .update(decryptedCbc.subarray(0, 208))
  57. .digest()
  58. .subarray(0, 16);
  59. const expectedHash = decryptedCbc.subarray(208, 224);
  60. // check if hash matches
  61. if (!currentHash.equals(expectedHash)) {
  62. throw new Error('SHA256 hash mismatch');
  63. }
  64. const parser = new TgExtensions.BinaryReader(decryptedCbc);
  65. const len = parser.readInt();
  66. if (len < 8 || len > 208) throw new Error(`Invalid TL data length: ${len}`);
  67. const constructorId = parser.readInt();
  68. if (constructorId !== Api.help.ConfigSimple.CONSTRUCTOR_ID) {
  69. throw new Error(`Invalid constructor ID: ${constructorId.toString(16)}`);
  70. }
  71. const payload = decryptedCbc.subarray(8, len);
  72. const configSimple = Api.help.ConfigSimple.fromReader(new TgExtensions.BinaryReader(payload));
  73. return configSimple.rules.flatMap(rule => rule.ips.map(ip => {
  74. switch (ip.CONSTRUCTOR_ID) {
  75. case Api.IpPort.CONSTRUCTOR_ID:
  76. case Api.IpPortSecret.CONSTRUCTOR_ID:
  77. return {
  78. ip: bigint2ip(
  79. ip.ipv4 > 0
  80. ? BigInt(ip.ipv4)
  81. : (2n ** 32n) + BigInt(ip.ipv4),
  82. 4
  83. ),
  84. port: ip.port
  85. };
  86. default:
  87. throw new TypeError(`Unknown IP type: 0x${ip.CONSTRUCTOR_ID.toString(16)}`);
  88. }
  89. }));
  90. }