get-telegram-backup-ip.ts 3.6 KB

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