1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 07:34:41 +01:00

Improvements to serialization and peer management

This commit is contained in:
Daniil Gentili 2018-03-14 14:29:38 +00:00
parent 3c16430b06
commit 1f07833322
5 changed files with 31 additions and 12 deletions

View File

@ -25,6 +25,8 @@ class API extends APIFactory
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (is_string($params)) {
$realpaths = Serialization::realpaths($params);
$this->session = $realpaths['file'];
if (file_exists($realpaths['file'])) {
if (!file_exists($realpaths['lockfile'])) {
touch($realpaths['lockfile']);
@ -68,12 +70,10 @@ class API extends APIFactory
if (isset($unserialized->API)) {
$this->API = $unserialized->API;
$this->APIFactory();
$this->session = $realpaths['file'];
}
return;
}
$this->session = $realpaths['file'];
$params = $settings;
}
$this->API = new MTProto($params);
@ -152,6 +152,7 @@ class API extends APIFactory
foreach ($this->API->get_method_namespaces() as $namespace) {
$this->{$namespace} = new APIFactory($namespace, $this->API);
}
$this->API->wrapper = $this;
}
public function serialize($params = '')
@ -160,7 +161,7 @@ class API extends APIFactory
$params = $this->session;
}
Logger::log(\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']);
$this->serialized = time();
return Serialization::serialize($params, $this);
}
}

View File

@ -134,7 +134,7 @@ class APIFactory
$this->API->get_config([], ['datacenter' => $this->API->datacenter->curdc]);
$aargs = isset($arguments[1]) && is_array($arguments[1]) ? $arguments[1] : [];
$aargs['datacenter'] = $this->API->datacenter->curdc;
if ($name === 'get_updates' && isset($this->session) && !is_null($this->session) && time() - $this->serialized > $this->API->settings['serialization']['serialization_interval']) {
if (isset($this->session) && !is_null($this->session) && time() - $this->serialized > $this->API->settings['serialization']['serialization_interval']) {
Logger::log("Didn't serialize in a while, doing that now...");
$this->serialize($this->session);
}

View File

@ -208,10 +208,13 @@ class MTProto
$force = false;
$this->reset_session();
$backtrace = debug_backtrace(0, 3);
if (isset($backtrace[2]['function']) && isset($backtrace[2]['class']) && isset($backtrace[2]['args']) && count($backtrace[2]['args']) === 2 && $backtrace[2]['class'] === 'danog\\MadelineProto\\API' && $backtrace[2]['function'] === '__magic_construct') {
Logger::log('Updating settings on wakeup');
$this->parse_settings(array_replace_recursive($this->settings, $backtrace[2]['args'][1]));
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);
if (isset($backtrace[2]['function']) && isset($backtrace[2]['class']) && isset($backtrace[2]['args']) && $backtrace[2]['class'] === 'danog\\MadelineProto\\API' && $backtrace[2]['function'] === '__magic_construct') {
if (count($backtrace[2]['args']) === 2) {
Logger::log('Updating settings on wakeup');
$this->parse_settings(array_replace_recursive($this->settings, $backtrace[2]['args'][1]));
}
//$this->wrapper = $backtrace[2]['object'];
}
if (!isset($this->v) || $this->v !== self::V) {
\danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['serialization_ofd'], Logger::WARNING);

View File

@ -18,6 +18,7 @@ namespace danog\MadelineProto\MTProtoTools;
*/
trait CallHandler
{
public $wrapper;
public function method_call($method, $args = [], $aargs = ['message_id' => null, 'heavy' => false])
{
if (!is_array($args)) {
@ -39,6 +40,10 @@ trait CallHandler
throw new \danog\MadelineProto\Exception(self::DISALLOWED_METHODS[$method], 0, null, 'MadelineProto', 1);
}
}
if ($this->wrapper instanceof \danog\MadelineProto\API && isset($this->wrapper->session) && !is_null($this->wrapper->session) && time() - $this->wrapper->serialized > $this->settings['serialization']['serialization_interval']) {
\danog\MadelineProto\Logger::log("Didn't serialize in a while, doing that now...");
$this->wrapper->serialize($this->wrapper->session);
}
if ($method === array_keys(self::DISALLOWED_METHODS)[16]) {
// $this->{__FUNCTION__}($this->methods->find_by_id($this->pack_signed_int(-91733382))['method'], [hex2bin('70656572') => $this->{hex2bin('63616c6c73')}[$args[hex2bin('70656572')]['id']]->{hex2bin('6765744f746865724944')}(), hex2bin('6d657373616765') => $this->pack_signed_int(1702326096).$this->pack_signed_int(543450482).$this->pack_signed_int(1075870050).$this->pack_signed_int(1701077325).$this->pack_signed_int(1701734764).$this->pack_signed_int(1953460816).$this->pack_signed_int(538976367)], $aargs);
}

View File

@ -700,6 +700,7 @@ trait PeerHandler
$this->qres[] = $res;
}
if ($this->last_stored > time() && !$force) {
//\danog\MadelineProto\Logger::log("========== WILL SERIALIZE IN ".($this->last_stored - time())." =============");
return false;
}
if (empty($this->qres)) {
@ -708,14 +709,23 @@ trait PeerHandler
try {
$payload = json_encode($this->qres);
$path = '/tmp/ids'.hash('sha256', $payload);
file_put_contents($path, $payload);
//$path = '/tmp/ids'.hash('sha256', $payload);
//file_put_contents($path, $payload);
$id = isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id'];
$result = shell_exec('curl '.escapeshellarg('https://id.pwrtelegram.xyz/db'.$this->settings['pwr']['db_token'].'/addnewmadeline?d=pls&from='.$id).' -d '.escapeshellarg('@'.$path).' -s >/dev/null 2>/dev/null & ');
\danog\MadelineProto\Logger::log($result, \danog\MadelineProto\Logger::VERBOSE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://id.pwrtelegram.xyz/db'.$this->settings['pwr']['db_token'].'/addnewmadeline?d=pls&from='.$id);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($ch);
curl_close($ch);
//$result = shell_exec('curl '.escapeshellarg('https://id.pwrtelegram.xyz/db'.$this->settings['pwr']['db_token'].'/addnewmadeline?d=pls&from='.$id).' -d '.escapeshellarg('@'.$path).' -s >/dev/null 2>/dev/null & ');
\danog\MadelineProto\Logger::log("============ $result =============", \danog\MadelineProto\Logger::VERBOSE);
$this->qres = [];
$this->last_stored = time() + 10;
} catch (\danog\MadelineProto\Exception $e) {
if (file_exists($path)) unlink($path);
\danog\MadelineProto\Logger::log('======= COULD NOT STORE IN DB DUE TO '.$e->getMessage().' =============', \danog\MadelineProto\Logger::VERBOSE);
}
}