Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
MadelineProto performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Mar 9, 2018
1 parent 8f83dfa commit e0e544d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion index.php
Expand Up @@ -56,7 +56,7 @@ function parseTLTrace($trace)
} catch (\danog\MadelineProto\RPCErrorException $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log(parseTLTrace($e->getTLTrace()));
if (in_array($e->rpc, ['SESSION_REVOKED', 'AUTH_KEY_UNREGISTERED'])) {
if (in_array($e->rpc, ['SESSION_REVOKED', 'AUTH_KEY_UNREGISTERED', 'USER_DEACTIVATED'])) {
foreach (glob($API->madeline_path.'*') as $path) {
unlink($path);
}
Expand Down
8 changes: 4 additions & 4 deletions src/PWRTelegram/PWRTelegram/API.php
Expand Up @@ -30,7 +30,7 @@ public function madeline_connect()
require_once $this->pwrhomedir.'/vendor/autoload.php';

try {
$this->madeline = \danog\MadelineProto\Serialization::deserialize($this->madeline_path);
$this->madeline = new \danog\MadelineProto\API($this->madeline_path, ['logger' => ['logger_level' => 5], 'connection_settings' => ['all' => ['protocol' => 'tcp_abridged']]]);
if (is_object($this->madeline)) {
//if (!$this->check_worker()) {
// $this->start_worker();
Expand All @@ -52,11 +52,11 @@ public function madeline_connect()
if ($this->user) {
$this->jsonexit(['ok' => false, 'error_code' => 400, 'description' => 'Please login again.']);
}
$this->madeline = new \danog\MadelineProto\API(['logger' => ['logger' => 1], 'pwr' => ['pwr' => true, 'db_token' => $this->db_token, 'strict' => true], 'app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'connection_settings' => ['all' => ['test_mode' => $this->deep]]]);
$this->madeline = new \danog\MadelineProto\API(['logger' => ['logger' => 1, 'logger_level' => 5], 'pwr' => ['pwr' => true, 'db_token' => $this->db_token, 'strict' => true], 'app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'connection_settings' => ['all' => ['protocol' => 'tcp_abridged', 'test_mode' => $this->deep]]]);
$this->madeline->bot_login($this->real_token);
$this->madeline->API->get_updates_difference();
$this->madeline->API->store_db([], true);
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $this->madeline);
$this->madeline->session = $this->madeline_path;
}
}

Expand All @@ -68,7 +68,7 @@ public function madeline_connect_backend()
}
require_once $this->pwrhomedir.'/vendor/autoload.php';

$this->madeline_backend = \danog\MadelineProto\Serialization::deserialize($this->madeline_backend_path);
$this->madeline_backend = new \danog\MadelineProto\API($this->madeline_backend_path, ['logger' => ['logger_level' => 5], 'connection_settings' => ['all' => ['protocol' => 'tcp_abridged']]]);
if ($this->madeline_backend === false) {
$this->jsonexit(['ok' => false, 'error_code' => 400, 'description' => 'Reset the custom backend to use the PWRTelegram API. Instructions available @ https://pwrtelegram.xyz']);
}
Expand Down
18 changes: 10 additions & 8 deletions src/PWRTelegram/PWRTelegram/Main.php
Expand Up @@ -137,12 +137,12 @@ public function __construct($vars)
}
$this->get_me();
require 'vendor/autoload.php';
$madeline = new \danog\MadelineProto\API(['logger' => ['logger' => 1, 'logger_level' => 5], 'pwr' => ['pwr' => true, 'db_token' => $this->db_token, 'strict' => true], 'app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'connection_settings' => ['all' => ['test_mode' => $this->deep]]]);
$madeline = new \danog\MadelineProto\API(['logger' => ['logger' => 1, 'logger_level' => 5], 'pwr' => ['pwr' => true, 'db_token' => $this->db_token, 'strict' => true], 'app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'connection_settings' => ['all' => ['protocol' => 'tcp_abridged', 'test_mode' => $this->deep]]]);
$madeline->bot_login($this->token);
$madeline->API->get_updates_difference();
$madeline->API->store_db([], true);
$madeline->API->reset_session();
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $madeline);
$madeline->session = $this->madeline_path;
}
if ($this->real_token !== '' && $this->user) {
$this->madeline_connect();
Expand Down Expand Up @@ -363,7 +363,8 @@ public function run_methods()
$this->madeline->API->settings['pwr']['update_handler'] = 'pwr_webhook';
$this->madeline->API->store_db([], true);
$this->madeline->API->reset_session();
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $this->madeline);
$this->madeline->session = $this->madeline_path;
$this->serialize();
$this->start_worker();
$this->jsonexit(['ok' => true, 'result' => true]);
}
Expand Down Expand Up @@ -403,10 +404,11 @@ public function run_methods()
require 'vendor/autoload.php';
$this->real_token = $this->base64url_encode(\phpseclib\Crypt\Random::string(32));
$this->madeline_path = $this->homedir.'sessions/pwrusertemp_'.hash('sha256', $this->real_token).'.madeline';
$madeline = new \danog\MadelineProto\API(['logger' => ['logger' => 1, 'logger_level' => 5], 'pwr' => ['pwr' => true, 'db_token' => $this->db_token, 'strict' => true], 'app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'connection_settings' => ['all' => ['test_mode' => $this->deep]]]);
$madeline = new \danog\MadelineProto\API(['logger' => ['logger' => 1, 'logger_level' => 5], 'pwr' => ['pwr' => true, 'db_token' => $this->db_token, 'strict' => true], 'app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'connection_settings' => ['protocol' => 'tcp_abridged', 'all' => ['protocol' => 'tcp_abridged', 'test_mode' => $this->deep]]]);
//$madeline->API->settings['pwr']['update_handler'] = $madeline->API->settings['updates']['callback'];
$madeline->phone_login($this->REQUEST['phone']);
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $madeline);
$madeline->session = $this->madeline_path;
$madeline->serialize();
$this->jsonexit(['ok' => true, 'result' => $this->real_token]);
break;
case '/completephonelogin':
Expand All @@ -418,11 +420,11 @@ public function run_methods()
$this->jsonexit(['ok' => false, 'error_code' => 400, 'description' => '2FA is enabled but no password is set.']);
}
if ($authorization['_'] === 'account.password') {
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $this->madeline);
$this->madeline->serialize($this->madeline_path);
$this->jsonexit(['ok' => false, 'error_code' => 401, 'description' => '2FA is enabled: call the complete2FALogin method with the password as password parameter (hint: '.$authorization['hint'].')']);
}
if ($authorization['_'] === 'account.needSignup') {
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $this->madeline);
$this->madeline->serialize($this->madeline_path);
$this->jsonexit(['ok' => false, 'error_code' => 401, 'description' => 'Need to signup: call the completesignup method.']);
}
$this->real_token = $authorization['user']['id'].':'.$this->real_token;
Expand Down Expand Up @@ -568,7 +570,7 @@ public function run_methods()
$this->madeline->API->settings['pwr']['update_handler'] = 'pwr_webhook';
$this->madeline->API->store_db([], true);
$this->madeline->API->reset_session();
\danog\MadelineProto\Serialization::serialize($this->madeline_path, $this->madeline);
$this->madeline->serialize($this->madeline_path);
$this->start_worker();
$this->jsonexit(['ok' => true, 'result' => true]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PWRTelegram/PWRTelegram/Tools.php
Expand Up @@ -149,8 +149,8 @@ public function jsonexit($wut, $options = 0)
public function exit($text = '', $ok = true)
{
echo $text;
exit();
ignore_user_abort(true);
header('Connection: close');
flush();
fastcgi_finish_request();

Expand Down
3 changes: 2 additions & 1 deletion storage.php
Expand Up @@ -19,6 +19,7 @@ function no_cache($status, $wut)
}
function analytics($ok, $uri, $bot_id, $user, $pass)
{
return;
require_once '../storage_url.php';
if (!isset($official_pwr)) {
return;
Expand Down Expand Up @@ -153,7 +154,7 @@ function analytics($ok, $uri, $bot_id, $user, $pass)
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>'.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()).'</p></body></html>');
} catch (\danog\MadelineProto\RPCErrorException $e) {
if (in_array($e->rpc, ['AUTH_KEY_UNREGISTERED', 'SESSION_REVOKED'])) {
if (in_array($e->rpc, ['AUTH_KEY_UNREGISTERED', 'SESSION_REVOKED', 'USER_DEACTIVATED'])) {
foreach (glob($madeline.'*') as $file) {
unlink($file);
}
Expand Down

0 comments on commit e0e544d

Please sign in to comment.