1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 03:14:39 +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"?>
<files psalm-version="dev-master@37cc4fd33fe8bbdfca6e056f17fcf2e9c41b9a65">
<files psalm-version="dev-master@7428e49b115a2a837aa29cf0fafd0ca902fe2457">
<file src="src/API.php">
<ArgumentTypeCoercion>
<code>$settings</code>
@ -124,13 +124,8 @@
<code>$fileName</code>
<code>$fileName</code>
<code>$fp</code>
<code>$new_session</code>
<code>$options</code>
<code>$options</code>
<code>$session</code>
<code>$settings</code>
<code>$settings</code>
<code>$settings</code>
<code>$settings</code>
</MissingParamType>
<MissingPropertyType>
@ -139,15 +134,12 @@
<code>$tdesktop_user_base_path</code>
</MissingPropertyType>
<MissingReturnType>
<code>pyrogram</code>
<code>tdesktop</code>
<code>tdesktop_decrypt</code>
<code>tdesktop_fopen</code>
<code>tdesktop_fopen_encrypted</code>
<code>tdesktop_md5</code>
<code>tdesktop_read_bytearray</code>
<code>telethon</code>
<code>zerobias</code>
</MissingReturnType>
<PossiblyUndefinedArrayOffset>
<code>$part_one_md5</code>
@ -157,9 +149,9 @@
<code>$dc</code>
<code>$main_dc_id</code>
</PossiblyUndefinedVariable>
<RedundantConditionGivenDocblockType>
<code>notFalse</code>
</RedundantConditionGivenDocblockType>
<TooManyArguments>
<code>getConfig</code>
</TooManyArguments>
</file>
<file src="src/DataCenter.php">
<PossiblyUndefinedArrayOffset>

View File

@ -35,7 +35,7 @@ final class Conversion
*
* @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;
if ($settings) {
@ -45,12 +45,19 @@ final class Conversion
$settings->getLogger()->setLevel(Logger::ULTRA_VERBOSE);
$settings->getAuth()->setPfs(true);
$MadelineProto = new API($session, $settings);
$MadelineProto->help->getConfig();
$MadelineProto->logger('About to import auth!', Logger::FATAL_ERROR);
$MadelineProto->help->getConfig([], ['datacenter' => $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;
}
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')) {
throw Exception::extension('sqlite3');
@ -73,25 +80,37 @@ final class Conversion
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']);
if (!\extension_loaded('sqlite3')) {
throw Exception::extension('sqlite3');
}
Magic::start(light: false);
if (!isset(\pathinfo($session)['extension'])) {
$session .= '.session';
}
$session = Tools::absolute($session);
$session = \json_decode(\file_get_contents($session), true);
$session['auth_key'] = \base64_decode(\implode('', $session['auth_key']));
Assert::notFalse($session['auth_key']);
Assert::integer($session['dc_id']);
$sqlite = new PDO("sqlite:$session");
$session = $sqlite->query("SELECT * FROM sessions")->fetchAll(PDO::FETCH_ASSOC)[0];
$settings ??= new Settings;
$settings->getConnection()->setTestMode($session['test_mode']);
$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($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']);
if (\is_string($session)) {

View File

@ -26,10 +26,12 @@ use Amp\DeferredCancellation;
use Amp\DeferredFuture;
use AssertionError;
use danog\MadelineProto\API;
use danog\MadelineProto\DataCenter;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Lang;
use danog\MadelineProto\Logger;
use danog\MadelineProto\MTProto\PermAuthKey;
use danog\MadelineProto\MTProto\TempAuthKey;
use danog\MadelineProto\MTProtoTools\PasswordCalculator;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Settings;
@ -252,22 +254,23 @@ trait Login
throw new Exception(Lang::$current_lang['already_loggedIn']);
}
$this->logger->logger(Lang::$current_lang['login_auth_key'], Logger::NOTICE);
foreach ($this->datacenter->getDataCenterConnections() as $connection) {
$connection->resetSession();
$connection->setPermAuthKey(null);
$connection->setTempAuthKey(null);
$connection->authorized(false);
}
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);
$this->datacenter = new DataCenter($this);
$auth_key = $authorization[$mainDcID];
if (!\is_array($auth_key)) {
$auth_key = ['auth_key' => $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 = \danog\MadelineProto\API::LOGGED_IN;
$this->getPhoneConfig();