Better auth key & DC management

This commit is contained in:
Daniil Gentili 2018-03-01 17:30:27 +00:00
parent 16e0fb4a57
commit 20e2e43d29
5 changed files with 29 additions and 12 deletions

View File

@ -52,11 +52,6 @@ class Logger
if (!defined('\\danog\\MadelineProto\\VoIP::PHP_LIBTGVOIP_VERSION') || \danog\MadelineProto\VoIP::PHP_LIBTGVOIP_VERSION !== '1.1.2') { if (!defined('\\danog\\MadelineProto\\VoIP::PHP_LIBTGVOIP_VERSION') || \danog\MadelineProto\VoIP::PHP_LIBTGVOIP_VERSION !== '1.1.2') {
throw new \danog\MadelineProto\Exception(hex2bin(\danog\MadelineProto\Lang::$current_lang['v_tgerror']), 0, null, 'MadelineProto', 1); throw new \danog\MadelineProto\Exception(hex2bin(\danog\MadelineProto\Lang::$current_lang['v_tgerror']), 0, null, 'MadelineProto', 1);
} }
try {
\Threaded::extend('\\danog\\MadelineProto\\VoIP');
} catch (\RuntimeException $e) {
}
} }
self::$colors[self::ULTRA_VERBOSE] = implode(';', [self::foreground['light_gray'], self::set['dim']]); self::$colors[self::ULTRA_VERBOSE] = implode(';', [self::foreground['light_gray'], self::set['dim']]);
self::$colors[self::VERBOSE] = implode(';', [self::foreground['green'], self::set['bold']]); self::$colors[self::VERBOSE] = implode(';', [self::foreground['green'], self::set['bold']]);
@ -120,8 +115,6 @@ class Logger
case 3: case 3:
echo self::$isatty ? "\33[".self::$colors[$level].'m'.$param."\33[0m".PHP_EOL : $param.PHP_EOL; echo self::$isatty ? "\33[".self::$colors[$level].'m'.$param."\33[0m".PHP_EOL : $param.PHP_EOL;
break; break;
default:
break;
} }
} }
} }

View File

@ -319,7 +319,7 @@ class MTProto
// Set default settings // Set default settings
$default_settings = ['authorization' => [ $default_settings = ['authorization' => [
// Authorization settings // Authorization settings
'default_temp_auth_key_expires_in' => 31557600, 'default_temp_auth_key_expires_in' => 1*24*60*60,
// validity of temporary keys and the binding of the temporary and permanent keys // validity of temporary keys and the binding of the temporary and permanent keys
'rsa_keys' => ["-----B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5Sut6IiBjWozrRQ6n5h2RXNtO7O2qCDqjgB2vBxhV7B+z\nhRbLbCmW0tYMDsvPpX5M8fsO05svN+lKtCAuz1leFns8piZpptpSCFn7bWxiA9/f\nx5x17D7pfah3Sy2pA+NDXyzSlGcKdaUmwQIDAQAB\n-----END RSA PUBLIC KEY-----", "-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAwqjFW0pi4reKGbkc9pK83Eunwj/k0G8ZTioMMPbZmW99GivMibwa\nxDM9RDWabEMyUtGoQC2ZcDeLWRK3W8jMP6dnEKAlvLkDLfC4fXYHzFO5KHEqF06i\nqAqBdmI1iBGdQv/OQCBcbXIWCGDY2AsiqLhlGQfPOI7/vvKc188rTriocgUtoTUc\n/n/sIUzkgwTqRyvWYynWARWzQg0I9olLBBC2q5RQJJlnYXZwyTL3y9tdb7zOHkks\nWV9IMQmZmyZh/N7sMbGWQpt4NMchGpPGeJ2e5gHBjDnlIf2p1yZOYeUYrdbwcS0t\nUiggS4UeE8TzIuXFQxw7fzEIlmhIaq3FnwIDAQAB\n-----END RSA PUBLIC KEY-----"], 'rsa_keys' => ["-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6\nlyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS\nan9tqw3bfUV/nqgbhGX81v/+7RFAEd+RwFnK7a+XYl9sluzHRyVVaTTveB2GazTw\nEfzk2DWgkBluml8OREmvfraX3bkHZJTKX4EQSjBbbdJ2ZXIsRrYOXfaA+xayEGB+\n8hdlLmAjbCVfaigxX0CDqWeR1yFL9kwd9P0NsZRPsmoqVwMbMu7mStFai6aIhc3n\nSlv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB\n-----END RSA PUBLIC KEY-----", "-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAxq7aeLAqJR20tkQQMf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IMQmZmyZh/N7sMbGWQpt4NMchGpPGeJ2e5gHBjDnlIf2p1yZOYeUYrdbwcS0t\nUiggS4UeE8TzIuXFQxw7fzEIlmhIaq3FnwIDAQAB\n-----END RSA PUBLIC KEY-----"],
], 'connection' => [ ], 'connection' => [

View File

@ -215,7 +215,13 @@ trait CallHandler
} }
} catch (\danog\MadelineProto\Exception $e) { } catch (\danog\MadelineProto\Exception $e) {
$last_error = $e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine(); $last_error = $e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine();
\danog\MadelineProto\Logger::log(['An error occurred while calling method '.$method.': '.$last_error.'. Recreating connection and retrying to call method...'], \danog\MadelineProto\Logger::WARNING); if (strpos($e->getMessage(), 'Received request to switch to DC ') === 0) {
if ($this->authorized_dc === -1 && $method === 'users.getUsers' && $args = ['id' => [['_' => 'inputUserSelf']]]) {
$this->authorized_dc = $this->datacenter->curdc;
}
\danog\MadelineProto\Logger::log([$e->getMessage()], \danog\MadelineProto\Logger::WARNING);
continue;
}
$this->close_and_reopen($aargs['datacenter']); $this->close_and_reopen($aargs['datacenter']);
if ($e->getMessage() === 'Re-executing query after server error...') { if ($e->getMessage() === 'Re-executing query after server error...') {
return $this->method_call($method, $args, $aargs); return $this->method_call($method, $args, $aargs);

View File

@ -91,7 +91,9 @@ trait MessageHandler
} }
$payload = $this->datacenter->sockets[$datacenter]->read_message(); $payload = $this->datacenter->sockets[$datacenter]->read_message();
if (strlen($payload) === 4) { if (strlen($payload) === 4) {
return $this->unpack_signed_int($payload); $payload = $this->unpack_signed_int($payload);
\danog\MadelineProto\Logger::log(["Received $payload"], \danog\MadelineProto\Logger::ULTRA_VERBOSE);
return $payload;
} }
$auth_key_id = substr($payload, 0, 8); $auth_key_id = substr($payload, 0, 8);
if ($auth_key_id === "\0\0\0\0\0\0\0\0") { if ($auth_key_id === "\0\0\0\0\0\0\0\0") {

View File

@ -340,6 +340,7 @@ trait ResponseHandler
case 'USER_DEACTIVATED': case 'USER_DEACTIVATED':
case 'SESSION_REVOKED': case 'SESSION_REVOKED':
case 'SESSION_EXPIRED': case 'SESSION_EXPIRED':
\danog\MadelineProto\Logger::log([$server_answer['error_message']], \danog\MadelineProto\Logger::FATAL_ERROR);
foreach ($this->datacenter->sockets as $socket) { foreach ($this->datacenter->sockets as $socket) {
$socket->temp_auth_key = null; $socket->temp_auth_key = null;
$socket->auth_key = null; $socket->auth_key = null;
@ -348,23 +349,38 @@ trait ResponseHandler
$this->authorized = self::NOT_LOGGED_IN; $this->authorized = self::NOT_LOGGED_IN;
$this->authorization = null; $this->authorization = null;
$this->init_authorization(); $this->init_authorization();
// idk
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']); throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
case 'AUTH_KEY_UNREGISTERED': case 'AUTH_KEY_UNREGISTERED':
case 'AUTH_KEY_INVALID': case 'AUTH_KEY_INVALID':
if ($this->authorized !== self::LOGGED_IN) { if ($this->authorized !== self::LOGGED_IN) {
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']); throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
} }
\danog\MadelineProto\Logger::log(['Auth key not registered, resetting temporary and permanent auth keys...'], \danog\MadelineProto\Logger::ERROR);
$this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key = null; $this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key = null;
$this->datacenter->sockets[$aargs['datacenter']]->auth_key = null; $this->datacenter->sockets[$aargs['datacenter']]->auth_key = null;
$this->datacenter->sockets[$aargs['datacenter']]->authorized = false; $this->datacenter->sockets[$aargs['datacenter']]->authorized = false;
if ($this->authorized_dc === $aargs['datacenter'] && $this->authorized === self::LOGGED_IN) {
\danog\MadelineProto\Logger::log(['Permanent auth key was main authorized key, logging out...'], \danog\MadelineProto\Logger::FATAL_ERROR);
foreach ($this->datacenter->sockets as $socket) {
$socket->temp_auth_key = null;
$socket->auth_key = null;
$socket->authorized = false;
}
$this->authorized = self::NOT_LOGGED_IN;
$this->authorization = null;
$this->init_authorization();
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
}
$this->init_authorization(); $this->init_authorization();
// idk
throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key'); throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key');
case 'AUTH_KEY_PERM_EMPTY': case 'AUTH_KEY_PERM_EMPTY':
if ($this->authorized !== self::LOGGED_IN) { if ($this->authorized !== self::LOGGED_IN) {
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']); throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
} }
\danog\MadelineProto\Logger::log(['Temporary auth key not bound, resetting temporary auth key...'], \danog\MadelineProto\Logger::ERROR);
$this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key = null; $this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key = null;
$this->init_authorization(); $this->init_authorization();
// idk // idk