1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 10:19:00 +01:00

Update&fix telethon and pyrogram session conversion logic

This commit is contained in:
Daniil Gentili 2023-09-17 18:51:18 +02:00
parent 385e067e85
commit f17ca784e9
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 53 additions and 39 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@37cc4fd33fe8bbdfca6e056f17fcf2e9c41b9a65"> <files psalm-version="dev-master@7428e49b115a2a837aa29cf0fafd0ca902fe2457">
<file src="src/API.php"> <file src="src/API.php">
<ArgumentTypeCoercion> <ArgumentTypeCoercion>
<code>$settings</code> <code>$settings</code>
@ -124,13 +124,8 @@
<code>$fileName</code> <code>$fileName</code>
<code>$fileName</code> <code>$fileName</code>
<code>$fp</code> <code>$fp</code>
<code>$new_session</code>
<code>$options</code> <code>$options</code>
<code>$options</code> <code>$options</code>
<code>$session</code>
<code>$settings</code>
<code>$settings</code>
<code>$settings</code>
<code>$settings</code> <code>$settings</code>
</MissingParamType> </MissingParamType>
<MissingPropertyType> <MissingPropertyType>
@ -139,15 +134,12 @@
<code>$tdesktop_user_base_path</code> <code>$tdesktop_user_base_path</code>
</MissingPropertyType> </MissingPropertyType>
<MissingReturnType> <MissingReturnType>
<code>pyrogram</code>
<code>tdesktop</code> <code>tdesktop</code>
<code>tdesktop_decrypt</code> <code>tdesktop_decrypt</code>
<code>tdesktop_fopen</code> <code>tdesktop_fopen</code>
<code>tdesktop_fopen_encrypted</code> <code>tdesktop_fopen_encrypted</code>
<code>tdesktop_md5</code> <code>tdesktop_md5</code>
<code>tdesktop_read_bytearray</code> <code>tdesktop_read_bytearray</code>
<code>telethon</code>
<code>zerobias</code>
</MissingReturnType> </MissingReturnType>
<PossiblyUndefinedArrayOffset> <PossiblyUndefinedArrayOffset>
<code>$part_one_md5</code> <code>$part_one_md5</code>
@ -157,9 +149,9 @@
<code>$dc</code> <code>$dc</code>
<code>$main_dc_id</code> <code>$main_dc_id</code>
</PossiblyUndefinedVariable> </PossiblyUndefinedVariable>
<RedundantConditionGivenDocblockType> <TooManyArguments>
<code>notFalse</code> <code>getConfig</code>
</RedundantConditionGivenDocblockType> </TooManyArguments>
</file> </file>
<file src="src/DataCenter.php"> <file src="src/DataCenter.php">
<PossiblyUndefinedArrayOffset> <PossiblyUndefinedArrayOffset>

View File

@ -35,7 +35,7 @@ final class Conversion
* *
* @param array<int, string> $authorization Authorization info * @param array<int, string> $authorization Authorization info
*/ */
public static function importAuthorization(array $authorization, int $main_dc_id, string $session, ?Settings $settings = null): API public static function importAuthorization(array $authorization, int $main_dc_id, string $session, ?SettingsAbstract $settings = null): API
{ {
$settingsFull = new Settings; $settingsFull = new Settings;
if ($settings) { if ($settings) {
@ -45,12 +45,19 @@ final class Conversion
$settings->getLogger()->setLevel(Logger::ULTRA_VERBOSE); $settings->getLogger()->setLevel(Logger::ULTRA_VERBOSE);
$settings->getAuth()->setPfs(true); $settings->getAuth()->setPfs(true);
$MadelineProto = new API($session, $settings); $MadelineProto = new API($session, $settings);
$MadelineProto->help->getConfig(); $MadelineProto->help->getConfig([], ['datacenter' => $main_dc_id]);
$MadelineProto->logger('About to import auth!', Logger::FATAL_ERROR); $MadelineProto->logger("About to import auth to DC $main_dc_id!", Logger::FATAL_ERROR);
$MadelineProto->importAuthorization($authorization, $main_dc_id); $MadelineProto->importAuthorization($authorization, $main_dc_id);
return $MadelineProto; return $MadelineProto;
} }
public static function telethon(string $session, string $new_session, ?Settings $settings = null) /**
* 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')) { if (!\extension_loaded('sqlite3')) {
throw Exception::extension('sqlite3'); throw Exception::extension('sqlite3');
@ -73,25 +80,37 @@ final class Conversion
return self::importAuthorization($dcs, $dc['dc_id'], $new_session, $settings); return self::importAuthorization($dcs, $dc['dc_id'], $new_session, $settings);
} }
public static function pyrogram(string $session, string $new_session, ?Settings $settings = null) /**
* 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']); \set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (!\extension_loaded('sqlite3')) {
throw Exception::extension('sqlite3');
}
Magic::start(light: false);
if (!isset(\pathinfo($session)['extension'])) { if (!isset(\pathinfo($session)['extension'])) {
$session .= '.session'; $session .= '.session';
} }
$session = Tools::absolute($session); $session = Tools::absolute($session);
$session = \json_decode(\file_get_contents($session), true); $sqlite = new PDO("sqlite:$session");
$session['auth_key'] = \base64_decode(\implode('', $session['auth_key'])); $session = $sqlite->query("SELECT * FROM sessions")->fetchAll(PDO::FETCH_ASSOC)[0];
Assert::notFalse($session['auth_key']);
Assert::integer($session['dc_id']);
$settings ??= new Settings; $settingsFull = new Settings;
$settings->getConnection()->setTestMode($session['test_mode']); 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); return self::importAuthorization([$session['dc_id'] => $session['auth_key']], $session['dc_id'], $new_session, $settings);
} }
public static function zerobias($session, $new_session, $settings = []) public static function zerobias(string|array $session, string $new_session, ?SettingsAbstract $settings = null): API
{ {
\set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); \set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (\is_string($session)) { if (\is_string($session)) {

View File

@ -26,10 +26,12 @@ use Amp\DeferredCancellation;
use Amp\DeferredFuture; use Amp\DeferredFuture;
use AssertionError; use AssertionError;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\DataCenter;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\Lang; use danog\MadelineProto\Lang;
use danog\MadelineProto\Logger; use danog\MadelineProto\Logger;
use danog\MadelineProto\MTProto\PermAuthKey; use danog\MadelineProto\MTProto\PermAuthKey;
use danog\MadelineProto\MTProto\TempAuthKey;
use danog\MadelineProto\MTProtoTools\PasswordCalculator; use danog\MadelineProto\MTProtoTools\PasswordCalculator;
use danog\MadelineProto\RPCErrorException; use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Settings; use danog\MadelineProto\Settings;
@ -252,22 +254,23 @@ trait Login
throw new Exception(Lang::$current_lang['already_loggedIn']); throw new Exception(Lang::$current_lang['already_loggedIn']);
} }
$this->logger->logger(Lang::$current_lang['login_auth_key'], Logger::NOTICE); $this->logger->logger(Lang::$current_lang['login_auth_key'], Logger::NOTICE);
foreach ($this->datacenter->getDataCenterConnections() as $connection) {
$connection->resetSession(); $this->datacenter = new DataCenter($this);
$connection->setPermAuthKey(null); $auth_key = $authorization[$mainDcID];
$connection->setTempAuthKey(null); if (!\is_array($auth_key)) {
$connection->authorized(false); $auth_key = ['auth_key' => $auth_key];
}
foreach ($authorization as $dc_id => $auth_key) {
$this->logger->logger("Setting auth key in DC $dc_id", Logger::NOTICE);
if (!\is_array($auth_key)) {
$auth_key = ['auth_key' => $auth_key];
}
$auth_key = new PermAuthKey($auth_key);
$auth_key->authorized(true);
$dataCenterConnection = $this->datacenter->getDataCenterConnection($dc_id);
$dataCenterConnection->setPermAuthKey($auth_key);
} }
$dataCenterConnection = $this->datacenter->getDataCenterConnection($mainDcID);
$this->logger->logger("Setting auth key in DC $mainDcID", Logger::NOTICE);
$auth_key = new PermAuthKey($auth_key);
$auth_key->authorized(true);
$auth_key->setServerSalt(\random_bytes(8));
$dataCenterConnection->setPermAuthKey($auth_key);
$dataCenterConnection->setTempAuthKey(new TempAuthKey());
$dataCenterConnection->bind($this->settings->getAuth()->getPfs());
$this->datacenter->currentDatacenter = $mainDcID;
$this->authorized_dc = $mainDcID; $this->authorized_dc = $mainDcID;
$this->authorized = \danog\MadelineProto\API::LOGGED_IN; $this->authorized = \danog\MadelineProto\API::LOGGED_IN;
$this->getPhoneConfig(); $this->getPhoneConfig();