From 1f07833322a915b104f6f70e8bd1350ed3b7f879 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 14 Mar 2018 14:29:38 +0000 Subject: [PATCH] Improvements to serialization and peer management --- src/danog/MadelineProto/API.php | 7 ++++--- src/danog/MadelineProto/APIFactory.php | 2 +- src/danog/MadelineProto/MTProto.php | 11 +++++++---- .../MadelineProto/MTProtoTools/CallHandler.php | 5 +++++ .../MadelineProto/MTProtoTools/PeerHandler.php | 18 ++++++++++++++---- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 428d64b2..9ba58656 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -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); } } diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index a19140f7..fa37489f 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -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); } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index da9fcb97..605eceb5 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -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); diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index d60d2d72..9ebf3277 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -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); } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 2e430cdf..504ffaa5 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -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); } }