forked from danog/MadelineProto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversion.php
478 lines (422 loc) · 18.9 KB
/
Conversion.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
declare(strict_types=1);
/**
* Conversion module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <[email protected]>
* @copyright 2016-2023 Daniil Gentili <[email protected]>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use danog\MadelineProto\MTProtoTools\Crypt;
use PDO;
use Webmozart\Assert\Assert;
use const DIRECTORY_SEPARATOR;
use function stream_get_contents;
final class Conversion
{
/**
* Import authorization from raw auth key and DC id.
*
* @param array<int, string> $authorization Authorization info, DC ID => auth key
*/
public static function importAuthorization(array $authorization, int $main_dc_id, string $session, ?SettingsAbstract $settings = null): API
{
$settingsFull = new Settings;
if ($settings) {
$settingsFull->merge($settings);
}
$settings = $settingsFull;
$settings->getLogger()->setLevel(Logger::ULTRA_VERBOSE);
$settings->getAuth()->setPfs(true);
$MadelineProto = new API($session, $settings);
/** @var APIWrapper */
$wrapper = Tools::getVar($MadelineProto, 'wrapper');
$wrapper->getAPI()->methodCallAsyncRead('help.getConfig', [], $main_dc_id);
$MadelineProto->logger("About to import auth to DC $main_dc_id!", Logger::FATAL_ERROR);
$MadelineProto->importAuthorization($authorization, $main_dc_id);
return $MadelineProto;
}
/**
* Convert telethon session.
*
* @param string $session Telethon session file
* @param string $new_session MadelineProto session directory to create
* @param SettingsAbstract|null $settings Settings
*/
public static function telethon(string $session, string $new_session, ?SettingsAbstract $settings = null): API
{
if (!\extension_loaded('sqlite3')) {
throw Exception::extension('sqlite3');
}
Magic::start(light: false);
if (!isset(pathinfo($session)['extension'])) {
$session .= '.session';
}
$session = Tools::absolute($session);
$sqlite = new PDO("sqlite:$session");
$sqlite->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sessions = $sqlite->query('SELECT * FROM sessions')->fetchAll();
$dcs = [];
foreach ($sessions as $dc) {
$dcs[$dc['dc_id']] = $dc['auth_key'];
}
return self::importAuthorization($dcs, $dc['dc_id'], $new_session, $settings);
}
/**
* Convert pyrogram session.
*
* @param string $session Pyrogram session file
* @param string $new_session MadelineProto session directory to create
* @param SettingsAbstract|null $settings Settings
*/
public static function pyrogram(string $session, string $new_session, ?SettingsAbstract $settings = null): API
{
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (!\extension_loaded('sqlite3')) {
throw Exception::extension('sqlite3');
}
Magic::start(light: false);
if (!isset(pathinfo($session)['extension'])) {
$session .= '.session';
}
$session = Tools::absolute($session);
$sqlite = new PDO("sqlite:$session");
$session = $sqlite->query("SELECT * FROM sessions")->fetchAll(PDO::FETCH_ASSOC)[0];
$settingsFull = new Settings;
if ($settings) {
$settingsFull->merge($settings);
}
$settingsFull->getConnection()->setTestMode((bool) $session['test_mode']);
return self::importAuthorization([$session['dc_id'] => $session['auth_key']], $session['dc_id'], $new_session, $settings);
}
public static function zerobias(string|array $session, string $new_session, ?SettingsAbstract $settings = null): API
{
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (\is_string($session)) {
$session = json_decode($session, true);
}
$dc = $session['dc'];
$key = hex2bin($session["dc$dc".'_auth_key']);
Assert::integer($dc);
Assert::string($key);
return self::importAuthorization([$dc => $key], $dc, $new_session, $settings);
}
private static function tdesktop_md5($data)
{
$result = '';
foreach (str_split(md5($data), 2) as $byte) {
$result .= strrev($byte);
}
return strtoupper($result);
}
private const FILEOPTION_SAFE = 1;
private const FILEOPTION_USER = 2;
public static $tdesktop_base_path;
public static $tdesktop_user_base_path;
public static $tdesktop_key;
private static function tdesktop_fopen($fileName, $options = self::FILEOPTION_SAFE|self::FILEOPTION_USER)
{
$name = ($options & self::FILEOPTION_USER ? self::$tdesktop_user_base_path : self::$tdesktop_base_path).$fileName;
$totry = [];
foreach (['0', '1', 's'] as $x) {
if (file_exists($name.$x)) {
$totry[] = fopen($name.$x, 'rb');
}
}
foreach ($totry as $fp) {
if (stream_get_contents($fp, 4) !== 'TDF$') {
Logger::log('Wrong magic', Logger::ERROR);
continue;
}
$versionBytes = stream_get_contents($fp, 4);
$version = Tools::unpackSignedInt($versionBytes);
Logger::log("TDesktop version: $version");
$data = stream_get_contents($fp);
$md5 = substr($data, -16);
$data = substr($data, 0, -16);
$length = pack('l', \strlen($data));
$length = Magic::$BIG_ENDIAN ? strrev($length) : $length;
if (md5($data.$length.$versionBytes.'TDF$', true) !== $md5) {
Logger::log('Wrong MD5', Logger::ERROR);
}
$res = fopen('php://memory', 'rw+b');
fwrite($res, $data);
fseek($res, 0);
return $res;
}
throw new Exception("Could not open $fileName");
}
private static function tdesktop_fopen_encrypted($fileName, $options = 3)
{
$f = self::tdesktop_fopen($fileName, $options);
$data = self::tdesktop_read_bytearray($f);
$res = self::tdesktop_decrypt($data, self::$tdesktop_key);
$length = unpack('V', stream_get_contents($res, 4))[1];
if ($length > fstat($res)['size'] || $length < 4) {
throw new Exception('Wrong length');
}
return $res;
}
private static function tdesktop_read_bytearray($fp, bool $asString = false)
{
$length = Tools::unpackSignedInt(strrev(stream_get_contents($fp, 4)));
$data = $length > 0 ? stream_get_contents($fp, $length) : '';
if ($asString) {
return $data;
}
$res = fopen('php://memory', 'rw+b');
fwrite($res, $data);
fseek($res, 0);
return $res;
}
private static function tdesktop_decrypt($data, $auth_key)
{
$message_key = stream_get_contents($data, 16);
$encrypted_data = stream_get_contents($data);
[$aes_key, $aes_iv] = Crypt::oldKdf($message_key, $auth_key, false);
$decrypted_data = Crypt::igeDecrypt($encrypted_data, $aes_key, $aes_iv);
if ($message_key != substr(sha1($decrypted_data, true), 0, 16)) {
throw new SecurityException('msg_key mismatch');
}
$res = fopen('php://memory', 'rw+b');
fwrite($res, $decrypted_data);
fseek($res, 0);
return $res;
}
private const dbiKey = 0x00;
private const dbiUser = 0x01;
private const dbiDcOptionOldOld = 0x02;
private const dbiChatSizeMax = 0x03;
private const dbiMutePeer = 0x04;
private const dbiSendKey = 0x05;
private const dbiAutoStart = 0x06;
private const dbiStartMinimized = 0x07;
private const dbiSoundNotify = 0x08;
private const dbiWorkMode = 0x09;
private const dbiSeenTrayTooltip = 0x0a;
private const dbiDesktopNotify = 0x0b;
private const dbiAutoUpdate = 0x0c;
private const dbiLastUpdateCheck = 0x0d;
private const dbiWindowPosition = 0x0e;
private const dbiConnectionTypeOld = 0x0f;
// 0x10 reserved
private const dbiDefaultAttach = 0x11;
private const dbiCatsAndDogs = 0x12;
private const dbiReplaceEmojis = 0x13;
private const dbiAskDownloadPath = 0x14;
private const dbiDownloadPathOld = 0x15;
private const dbiScale = 0x16;
private const dbiEmojiTabOld = 0x17;
private const dbiRecentEmojiOldOld = 0x18;
private const dbiLoggedPhoneNumber = 0x19;
private const dbiMutedPeers = 0x1a;
// 0x1b reserved
private const dbiNotifyView = 0x1c;
private const dbiSendToMenu = 0x1d;
private const dbiCompressPastedImage = 0x1e;
private const dbiLangOld = 0x1f;
private const dbiLangFileOld = 0x20;
private const dbiTileBackground = 0x21;
private const dbiAutoLock = 0x22;
private const dbiDialogLastPath = 0x23;
private const dbiRecentEmojiOld = 0x24;
private const dbiEmojiVariantsOld = 0x25;
private const dbiRecentStickers = 0x26;
private const dbiDcOptionOld = 0x27;
private const dbiTryIPv6 = 0x28;
private const dbiSongVolume = 0x29;
private const dbiWindowsNotificationsOld = 0x30;
private const dbiIncludeMuted = 0x31;
private const dbiMegagroupSizeMax = 0x32;
private const dbiDownloadPath = 0x33;
private const dbiAutoDownload = 0x34;
private const dbiSavedGifsLimit = 0x35;
private const dbiShowingSavedGifsOld = 0x36;
private const dbiAutoPlay = 0x37;
private const dbiAdaptiveForWide = 0x38;
private const dbiHiddenPinnedMessages = 0x39;
private const dbiRecentEmoji = 0x3a;
private const dbiEmojiVariants = 0x3b;
private const dbiDialogsMode = 0x40;
private const dbiModerateMode = 0x41;
private const dbiVideoVolume = 0x42;
private const dbiStickersRecentLimit = 0x43;
private const dbiNativeNotifications = 0x44;
private const dbiNotificationsCount = 0x45;
private const dbiNotificationsCorner = 0x46;
private const dbiThemeKey = 0x47;
private const dbiDialogsWidthRatioOld = 0x48;
private const dbiUseExternalVideoPlayer = 0x49;
private const dbiDcOptions = 0x4a;
private const dbiMtpAuthorization = 0x4b;
private const dbiLastSeenWarningSeenOld = 0x4c;
private const dbiAuthSessionSettings = 0x4d;
private const dbiLangPackKey = 0x4e;
private const dbiConnectionType = 0x4f;
private const dbiStickersFavedLimit = 0x50;
private const dbiSuggestStickersByEmoji = 0x51;
private const dbiEncryptedWithSalt = 333;
private const dbiEncrypted = 444;
// 500-600 reserved
private const dbiVersion = 666;
private static function tdesktop(string $session, string $new_session, $settings = [])
{
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
$settings['old_session_key'] ??= 'data';
$settings['old_session_passcode'] ??= '';
if (basename($session) !== 'tdata') {
$session .= DIRECTORY_SEPARATOR.'tdata';
}
[$part_one_md5, $part_two_md5] = str_split(self::tdesktop_md5($settings['old_session_key']), 16);
self::$tdesktop_base_path = $session.DIRECTORY_SEPARATOR;
self::$tdesktop_user_base_path = self::$tdesktop_base_path.$part_one_md5.DIRECTORY_SEPARATOR;
$data = self::tdesktop_fopen('map');
$salt = self::tdesktop_read_bytearray($data, true);
$encryptedKey = self::tdesktop_read_bytearray($data);
if (\strlen($salt)) {
$keyIterCount = \strlen($settings['old_session_passcode']) ? 4000 : 4;
$passKey = openssl_pbkdf2($settings['old_session_passcode'], $salt, 256, $keyIterCount);
self::$tdesktop_key = stream_get_contents(
self::tdesktop_read_bytearray(
self::tdesktop_decrypt($encryptedKey, $passKey),
),
);
} else {
$key = 'key_'.$settings['old_session_key'];
$data = self::tdesktop_fopen($key, self::FILEOPTION_SAFE);
$salt = self::tdesktop_read_bytearray($data, true);
if (\strlen($salt) !== 32) {
throw new Exception('Length of salt is wrong!');
}
$encryptedKey = self::tdesktop_read_bytearray($data);
$encryptedInfo = self::tdesktop_read_bytearray($data);
$hash = hash('sha512', $salt.$settings['old_session_passcode'].$salt, true);
$iterCount = \strlen($settings['old_session_passcode']) ? 100000 : 1;
$passKey = openssl_pbkdf2($hash, $salt, 256, $iterCount, 'sha512');
$key = self::tdesktop_read_bytearray(self::tdesktop_decrypt($encryptedKey, $passKey), true);
$info = self::tdesktop_read_bytearray(self::tdesktop_decrypt($encryptedInfo, $key));
self::$tdesktop_key = $key;
$count = Tools::unpackSignedInt(strrev(stream_get_contents($info, 4)));
Logger::log("Number of accounts: $count");
for ($i = 0; $i != $count; $i++) {
$idx = Tools::unpackSignedInt(strrev(stream_get_contents($info, 4)));
if ($idx >= 0) {
$dataName = $settings['old_session_key'];
if ($idx > 0) {
$dataName = $settings['old_session_key'].'#'.($idx+1);
}
[$part_one_md5] = str_split(self::tdesktop_md5($dataName), 16);
}
}
}
$main = self::tdesktop_fopen_encrypted($part_one_md5, self::FILEOPTION_SAFE);
$auth_keys = [];
while (!feof($main)) {
$magic = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
switch ($magic) {
case self::dbiDcOptionOldOld:
stream_get_contents($main, 4);
self::tdesktop_read_bytearray($main);
self::tdesktop_read_bytearray($main);
stream_get_contents($main, 4);
break;
case self::dbiDcOptionOld:
stream_get_contents($main, 8);
self::tdesktop_read_bytearray($main);
stream_get_contents($main, 4);
break;
case self::dbiDcOptions:
self::tdesktop_read_bytearray($main);
break;
case self::dbiUser:
stream_get_contents($main, 4);
$main_dc_id = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
break;
case self::dbiKey:
$auth_keys[Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)))] = stream_get_contents($main, 256);
break;
case self::dbiMtpAuthorization:
$main = self::tdesktop_read_bytearray($main);
//stream_get_contents($main, 4);
$user_id = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
$main_dc_id = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
$length = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
for ($x = 0; $x < $length; $x++) {
$dc = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
$auth_key = stream_get_contents($main, 256);
if ($dc <= 5) {
$auth_keys[$dc] = $auth_key;
}
}
break 2;
case self::dbiAutoDownload:
stream_get_contents($main, 12);
break;
case self::dbiDialogsMode:
stream_get_contents($main, 8);
break;
case self::dbiAuthSessionSettings:
self::tdesktop_read_bytearray($main);
break;
case self::dbiConnectionTypeOld:
switch (Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)))) {
case 2:
case 3:
self::tdesktop_read_bytearray($main);
stream_get_contents($main, 4);
self::tdesktop_read_bytearray($main);
self::tdesktop_read_bytearray($main);
break;
}
break;
case self::dbiConnectionType:
stream_get_contents($main, 8);
self::tdesktop_read_bytearray($main);
stream_get_contents($main, 4);
self::tdesktop_read_bytearray($main);
self::tdesktop_read_bytearray($main);
break;
case self::dbiThemeKey:
case self::dbiLangPackKey:
case self::dbiMutePeer:
stream_get_contents($main, 8);
break;
case self::dbiWindowPosition:
stream_get_contents($main, 24);
break;
case self::dbiLoggedPhoneNumber:
self::tdesktop_read_bytearray($main);
break;
case self::dbiMutedPeers:
$length = Tools::unpackSignedInt(strrev(stream_get_contents($main, 4)));
for ($x = 0; $x < $length; $x++) {
stream_get_contents($main, 8);
}
// no break
case self::dbiDownloadPathOld:
self::tdesktop_read_bytearray($main);
break;
case self::dbiDialogLastPath:
self::tdesktop_read_bytearray($main);
break;
case self::dbiDownloadPath:
self::tdesktop_read_bytearray($main);
self::tdesktop_read_bytearray($main);
break;
default:
throw new \Exception("Unknown type $magic");
break;
}
}
return self::importAuthorization($auth_keys, $main_dc_id, $new_session, $settings);
}
}