mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Chore: prefer Uint8Array
This commit is contained in:
parent
e3320766cc
commit
15955c4128
@ -5,6 +5,8 @@ import crypto from 'node:crypto';
|
|||||||
import { Api, extensions as TgExtensions } from 'telegram';
|
import { Api, extensions as TgExtensions } from 'telegram';
|
||||||
import { bigint2ip } from 'fast-cidr-tools';
|
import { bigint2ip } from 'fast-cidr-tools';
|
||||||
|
|
||||||
|
import { base64ToUint8Array, concatUint8Arrays } from 'foxts/uint8array-utils';
|
||||||
|
|
||||||
const mtptoto_public_rsa = `-----BEGIN RSA PUBLIC KEY-----
|
const mtptoto_public_rsa = `-----BEGIN RSA PUBLIC KEY-----
|
||||||
MIIBCgKCAQEAyr+18Rex2ohtVy8sroGP
|
MIIBCgKCAQEAyr+18Rex2ohtVy8sroGP
|
||||||
BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD
|
BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD
|
||||||
@ -26,7 +28,7 @@ export function getTelegramBackupIPFromBase64(base64: string) {
|
|||||||
// Not needed with Buffer.from
|
// Not needed with Buffer.from
|
||||||
|
|
||||||
// 3. Decode base64 to Buffer
|
// 3. Decode base64 to Buffer
|
||||||
const decoded = Buffer.from(base64, 'base64');
|
const decoded = base64ToUint8Array(base64);
|
||||||
if (decoded.length !== 256) {
|
if (decoded.length !== 256) {
|
||||||
throw new TypeError('Decoded buffer length is not 344 bytes, received ' + decoded.length);
|
throw new TypeError('Decoded buffer length is not 344 bytes, received ' + decoded.length);
|
||||||
}
|
}
|
||||||
@ -55,7 +57,7 @@ export function getTelegramBackupIPFromBase64(base64: string) {
|
|||||||
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
|
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
|
||||||
decipher.setAutoPadding(false);
|
decipher.setAutoPadding(false);
|
||||||
|
|
||||||
const decryptedCbc = Buffer.concat([decipher.update(dataCbc), decipher.final()]);
|
const decryptedCbc = concatUint8Arrays([decipher.update(dataCbc), decipher.final()]);
|
||||||
|
|
||||||
if (decryptedCbc.length !== 224) {
|
if (decryptedCbc.length !== 224) {
|
||||||
throw new Error(`Decrypted AES payload length is not 224 bytes, received ${decryptedCbc.length}`);
|
throw new Error(`Decrypted AES payload length is not 224 bytes, received ${decryptedCbc.length}`);
|
||||||
@ -74,7 +76,7 @@ export function getTelegramBackupIPFromBase64(base64: string) {
|
|||||||
throw new Error('SHA256 hash mismatch');
|
throw new Error('SHA256 hash mismatch');
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser = new TgExtensions.BinaryReader(decryptedCbc);
|
const parser = new TgExtensions.BinaryReader(Buffer.from(decryptedCbc));
|
||||||
const len = parser.readInt();
|
const len = parser.readInt();
|
||||||
if (len < 8 || len > 208) throw new Error(`Invalid TL data length: ${len}`);
|
if (len < 8 || len > 208) throw new Error(`Invalid TL data length: ${len}`);
|
||||||
|
|
||||||
@ -86,7 +88,7 @@ export function getTelegramBackupIPFromBase64(base64: string) {
|
|||||||
|
|
||||||
const payload = decryptedCbc.subarray(8, len);
|
const payload = decryptedCbc.subarray(8, len);
|
||||||
|
|
||||||
const configSimple = Api.help.ConfigSimple.fromReader(new TgExtensions.BinaryReader(payload));
|
const configSimple = Api.help.ConfigSimple.fromReader(new TgExtensions.BinaryReader(Buffer.from(payload)));
|
||||||
|
|
||||||
return configSimple.rules.flatMap(rule => rule.ips.map(ip => {
|
return configSimple.rules.flatMap(rule => rule.ips.map(ip => {
|
||||||
switch (ip.CONSTRUCTOR_ID) {
|
switch (ip.CONSTRUCTOR_ID) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user