-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoSessionStr.js
More file actions
135 lines (111 loc) · 4.36 KB
/
Copy pathtoSessionStr.js
File metadata and controls
135 lines (111 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const CURRENT_VERSION = "1";
function readWTelegramSessionFile(filename, apiHash) {
const apiHashBytes = Uint8Array.from(
Array.from({length: apiHash.length / 2}, (_, i) =>
parseInt(apiHash.substring(i * 2, i * 2 + 2), 16)
)
);
const fs = require('fs');
const crypto = require('crypto');
let store;
try {
store = fs.openSync(filename, 'r');
const stats = fs.fstatSync(store);
const totalLength = stats.size;
if (totalLength === 0) {
fs.closeSync(store);
return "";
}
const header = Buffer.alloc(8);
const headerBytesRead = fs.readSync(store, header, 0, 8, 0);
if (headerBytesRead !== 8) {
throw new Error(`Can't read session header`);
}
const position = header.readUInt32LE(0);
const dataLength = header.readUInt32LE(4);
if (position + dataLength > totalLength) {
throw new Error(`Invalid session file: position (${position}) + length (${dataLength}) exceeds file size (${totalLength})`);
}
const encrypted = Buffer.alloc(dataLength);
const bytesRead = fs.readSync(store, encrypted, 0, dataLength, position);
if (bytesRead !== dataLength) {
throw new Error(`Can't read session block (${position}, ${dataLength})`);
}
const iv = encrypted.subarray(0, 16);
const encryptedPayload = encrypted.subarray(16);
const decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(apiHashBytes), iv);
const decrypted = Buffer.concat([
decipher.update(encryptedPayload),
decipher.final()
]);
const sha256 = crypto.createHash('sha256');
sha256.update(decrypted.subarray(32));
const hash = sha256.digest();
const storedHash = decrypted.subarray(0, 32);
if (!storedHash.equals(hash)) {
throw new Error("Integrity check failed in session loading");
}
fs.closeSync(store);
return decrypted.subarray(32).toString('utf8');
} catch (ex) {
if (store !== undefined) fs.closeSync(store);
throw new Error(`Exception while reading session file: ${ex.message}\nUse the correct api_hash/id/key, or delete the file to start a new session`);
}
}
function toSessionStr(authkey, setveraddr, port, dcId) {
const _key = authkey;
const dcBuffer = Buffer.from([dcId]);
const addressBuffer = Buffer.from(setveraddr);
const addressLengthBuffer = Buffer.alloc(2);
addressLengthBuffer.writeInt16BE(addressBuffer.length, 0);
const portBuffer = Buffer.alloc(2);
portBuffer.writeInt16BE(port, 0);
return (
CURRENT_VERSION +
encode(
Buffer.concat([
dcBuffer,
addressLengthBuffer,
addressBuffer,
portBuffer,
_key,
])
)
);
}
function encode(x) {
return x.toString("base64");
}
const args = process.argv.slice(2);
if (args.length < 2) {
console.log("WTelegram session file to GramJS session string converter");
console.log("--------------------------------");
console.log("This tool converts a WTelegram session file to a GramJS session string.");
console.log("--------------------------------");
console.log("Usage: node toSessionStr.js <sessionFile> <apiHash>");
console.log("Example: node toSessionStr.js wtelegram.session XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
process.exit(1);
}
const sessionFile = args[0];
const apiHash = args[1];
try {
const sessionStr = readWTelegramSessionFile(sessionFile, apiHash);
const sessionData = JSON.parse(sessionStr);
const mainDc = sessionData.MainDC;
const dcSession = sessionData.DCSessions[mainDc];
if (!dcSession) {
console.error("DC session not found for MainDC:", mainDc);
process.exit(1);
}
const authKeyBase64 = dcSession.AuthKey;
const authKeyBytes = Buffer.from(authKeyBase64, 'base64');
const serverAddress = dcSession.DataCenter.ip_address;
const port = dcSession.DataCenter.port;
const dcId = dcSession.DataCenter.id;
const sessionString = toSessionStr(authKeyBytes, serverAddress, port, dcId);
console.log("GramJS Session String:");
console.log(sessionString);
} catch (error) {
console.error("Error:", error.message);
process.exit(1);
}