Added APIFactory for calling namespaced mtproto methods, cleaned up testing.php and API.php, moved authorization keys, session_id, seq_no and time delta to the Connection class, added close_and_reopen method to the Connection class, improved DataCenter class, renamed Logging class to Logger, added a bit more logging, added setup_logger, switch_dc, init_authorization methods to the MTProto class, added parameter to disable automatic switching to nearest DC in write_client_info, added a try catch block in the create_auth_key method, fixed switching of DCs in wait_for_response method, added arguments check in the method calling methods, added a message id check in MessageHandler, added method_name_namespaced array in TL for APIFactory, renamed a lot of stuff and removed a few checks in the TL class, moved sendCode test call to testing.php
This commit is contained in:
parent
49a555d99f
commit
1ded657739
1
.gitignore
vendored
1
.gitignore
vendored
@ -63,3 +63,4 @@ target/
|
|||||||
vendor
|
vendor
|
||||||
*save
|
*save
|
||||||
*bak
|
*bak
|
||||||
|
number.php
|
||||||
|
@ -30,6 +30,7 @@ src/danog/MadelineProto/
|
|||||||
TLConstructor - Represents a TL Constructor
|
TLConstructor - Represents a TL Constructor
|
||||||
TLMethod - Represents a TL method
|
TLMethod - Represents a TL method
|
||||||
API - Wrapper class that istantiates the MTProto class, sets the error handler, and provides a wrapper for calling mtproto methods directly as class submethods
|
API - Wrapper class that istantiates the MTProto class, sets the error handler, and provides a wrapper for calling mtproto methods directly as class submethods
|
||||||
|
APIFactory - Provides a wrapper for calling namespaced mtproto methods directly as class submethods
|
||||||
Connection - Handles tcp/udp/http connections and wrapping payloads generated by MTProtoTools/MessageHandler into the right message according to the protocol
|
Connection - Handles tcp/udp/http connections and wrapping payloads generated by MTProtoTools/MessageHandler into the right message according to the protocol
|
||||||
DataCenter - Handles mtproto datacenters
|
DataCenter - Handles mtproto datacenters
|
||||||
DebugTools - Various debugging tools
|
DebugTools - Various debugging tools
|
||||||
@ -47,4 +48,3 @@ This project adheres to the [Hacktoberfest](https://hacktoberfest.digitalocean.c
|
|||||||
Browse the issues of this project and help close at least four of them with a pull request to get a free t-shirt!
|
Browse the issues of this project and help close at least four of them with a pull request to get a free t-shirt!
|
||||||
Check out the [Contribution guide first though](https://github.com/danog/MadelineProto/blob/master/CONTRIBUTING.md).
|
Check out the [Contribution guide first though](https://github.com/danog/MadelineProto/blob/master/CONTRIBUTING.md).
|
||||||
|
|
||||||
The name of this project is inspired by [this person](https://s-media-cache-ak0.pinimg.com/736x/f0/a1/70/f0a170718baeb0e3817c612d96f5d1cf.jpg).
|
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
[App data]
|
|
||||||
api_id = 25628
|
|
||||||
api_hash = 1fe17cda7d355166cdaa71f04122873c
|
|
||||||
ip_address = 149.154.167.50
|
|
||||||
port = 443
|
|
@ -20,11 +20,23 @@ class API extends Tools
|
|||||||
{
|
{
|
||||||
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
|
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
|
||||||
$this->session = new MTProto($params);
|
$this->session = new MTProto($params);
|
||||||
$ping_res = $this->ping(3);
|
|
||||||
if (isset($ping['_']) && $ping['_'] == 'pong') {
|
\danog\MadelineProto\Logger::log('Running APIFactory...');
|
||||||
$this->log->log('Pong: '.$ping['ping_id']);
|
foreach ($this->session->tl->method_name_namespaced as $method) {
|
||||||
|
if (isset($method[1])) {
|
||||||
|
if (!isset($this->{$method[0]})) {
|
||||||
|
$this->{$method[0]} = new APIFactory($method[0], $this->session);
|
||||||
}
|
}
|
||||||
|
$this->{$method[0]}->allowed_methods[] = $method[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
\danog\MadelineProto\Logger::log('Ping...');
|
||||||
|
$ping = $this->ping(3);
|
||||||
|
\danog\MadelineProto\Logger::log('Pong: '.$ping['ping_id']);
|
||||||
|
\danog\MadelineProto\Logger::log('Getting future salts...');
|
||||||
$future_salts = $this->get_future_salts(3);
|
$future_salts = $this->get_future_salts(3);
|
||||||
|
\danog\MadelineProto\Logger::log('MadelineProto is ready!');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
@ -35,6 +47,9 @@ class API extends Tools
|
|||||||
|
|
||||||
public function __call($name, $arguments)
|
public function __call($name, $arguments)
|
||||||
{
|
{
|
||||||
|
if (!in_array($name, $this->session->tl->method_name_namespaced)) {
|
||||||
|
throw new Exception("The called method doesn't exist!");
|
||||||
|
}
|
||||||
return $this->session->method_call($name, $arguments);
|
return $this->session->method_call($name, $arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
src/danog/MadelineProto/APIFactory.php
Normal file
34
src/danog/MadelineProto/APIFactory.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
Copyright 2016 Daniil Gentili
|
||||||
|
(https://daniil.it)
|
||||||
|
This file is part of MadelineProto.
|
||||||
|
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
See the GNU Affero General Public License for more details.
|
||||||
|
You should have received a copy of the GNU General Public License along with the MadelineProto.
|
||||||
|
If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace danog\MadelineProto;
|
||||||
|
|
||||||
|
class APIFactory
|
||||||
|
{
|
||||||
|
public $namespace;
|
||||||
|
public $session;
|
||||||
|
public $allowed_methods = [];
|
||||||
|
|
||||||
|
public function __construct($namespace, $session) {
|
||||||
|
$this->namespace = $namespace;
|
||||||
|
$this->session = $session;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __call($name, $arguments)
|
||||||
|
{
|
||||||
|
if (!in_array($name, $this->allowed_methods)) {
|
||||||
|
throw new Exception("The called method doesn't exist!");
|
||||||
|
}
|
||||||
|
return $this->session->method_call($this->namespace.'.'.$name, $arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -18,8 +18,14 @@ namespace danog\MadelineProto;
|
|||||||
class Connection extends Tools
|
class Connection extends Tools
|
||||||
{
|
{
|
||||||
public $sock = null;
|
public $sock = null;
|
||||||
|
|
||||||
public $protocol = null;
|
public $protocol = null;
|
||||||
private $_delta = 0;
|
|
||||||
|
public $time_delta = 0;
|
||||||
|
public $temp_auth_key;
|
||||||
|
public $auth_key;
|
||||||
|
public $session_id;
|
||||||
|
public $seq_no = 0;
|
||||||
|
|
||||||
public function __construct($ip, $port, $protocol = 'tcp_full')
|
public function __construct($ip, $port, $protocol = 'tcp_full')
|
||||||
{
|
{
|
||||||
@ -33,6 +39,8 @@ class Connection extends Tools
|
|||||||
- udp
|
- udp
|
||||||
*/
|
*/
|
||||||
$this->protocol = $protocol;
|
$this->protocol = $protocol;
|
||||||
|
$this->ip = $ip;
|
||||||
|
$this->port = $port;
|
||||||
switch ($this->protocol) {
|
switch ($this->protocol) {
|
||||||
case 'tcp_abridged':
|
case 'tcp_abridged':
|
||||||
$this->sock = fsockopen('tcp://'.$ip.':'.$port);
|
$this->sock = fsockopen('tcp://'.$ip.':'.$port);
|
||||||
@ -59,6 +67,10 @@ class Connection extends Tools
|
|||||||
$this->out_seq_no = -1;
|
$this->out_seq_no = -1;
|
||||||
$this->in_seq_no = -1;
|
$this->in_seq_no = -1;
|
||||||
break;
|
break;
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
case 'udp':
|
||||||
|
throw new Exception("Connection: This protocol wasn't implemented yet.");
|
||||||
default:
|
default:
|
||||||
throw new Exception('Connection: invalid protocol specified.');
|
throw new Exception('Connection: invalid protocol specified.');
|
||||||
break;
|
break;
|
||||||
@ -73,20 +85,19 @@ class Connection extends Tools
|
|||||||
case 'tcp_full':
|
case 'tcp_full':
|
||||||
fclose($this->sock);
|
fclose($this->sock);
|
||||||
break;
|
break;
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
case 'udp':
|
||||||
|
throw new Exception("Connection: This protocol wasn't implemented yet.");
|
||||||
default:
|
default:
|
||||||
throw new Exception('Connection: invalid protocol specified.');
|
throw new Exception('Connection: invalid protocol specified.');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_time_delta($delta)
|
public function close_and_reopen() {
|
||||||
{
|
$this->__destruct();
|
||||||
$this->_delta = $delta;
|
$this->__construct($this->ip, $this->port, $this->protocol);
|
||||||
}
|
|
||||||
|
|
||||||
public function get_time_delta()
|
|
||||||
{
|
|
||||||
return $this->_delta;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,6 +125,10 @@ class Connection extends Tools
|
|||||||
|
|
||||||
return fwrite($this->sock, $what);
|
return fwrite($this->sock, $what);
|
||||||
break;
|
break;
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
case 'udp':
|
||||||
|
throw new Exception("Connection: This protocol wasn't implemented yet.");
|
||||||
default:
|
default:
|
||||||
throw new Exception('Connection: invalid protocol specified.');
|
throw new Exception('Connection: invalid protocol specified.');
|
||||||
break;
|
break;
|
||||||
@ -132,6 +147,10 @@ class Connection extends Tools
|
|||||||
|
|
||||||
return fread($this->sock, $length);
|
return fread($this->sock, $length);
|
||||||
break;
|
break;
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
case 'udp':
|
||||||
|
throw new Exception("Connection: This protocol wasn't implemented yet.");
|
||||||
default:
|
default:
|
||||||
throw new Exception('Connection: invalid protocol specified.');
|
throw new Exception('Connection: invalid protocol specified.');
|
||||||
break;
|
break;
|
||||||
@ -182,6 +201,10 @@ class Connection extends Tools
|
|||||||
$packet = $this->sock->read($packet_length);
|
$packet = $this->sock->read($packet_length);
|
||||||
$payload = $this->fopen_and_write('php://memory', 'rw+b', $packet);
|
$payload = $this->fopen_and_write('php://memory', 'rw+b', $packet);
|
||||||
break;
|
break;
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
case 'udp':
|
||||||
|
throw new Exception("Connection: This protocol wasn't implemented yet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $payload;
|
return $payload;
|
||||||
@ -209,6 +232,10 @@ class Connection extends Tools
|
|||||||
}
|
}
|
||||||
$this->write($step1);
|
$this->write($step1);
|
||||||
break;
|
break;
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
case 'udp':
|
||||||
|
throw new Exception("Connection: This protocol wasn't implemented yet.");
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@ namespace danog\MadelineProto;
|
|||||||
*/
|
*/
|
||||||
class DataCenter extends Tools
|
class DataCenter extends Tools
|
||||||
{
|
{
|
||||||
|
public $referenced_variables = ["time_delta", "temp_auth_key", "auth_key", "session_id", "seq_no"];
|
||||||
|
|
||||||
public function __construct($dclist, $settings)
|
public function __construct($dclist, $settings)
|
||||||
{
|
{
|
||||||
$this->dclist = $dclist;
|
$this->dclist = $dclist;
|
||||||
@ -36,13 +38,12 @@ class DataCenter extends Tools
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->dc_connect($settings['default_dc']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dc_disconnect($dc_number)
|
public function dc_disconnect($dc_number)
|
||||||
{
|
{
|
||||||
if (isset($this->sockets[$dc_number])) {
|
if (isset($this->sockets[$dc_number])) {
|
||||||
\danog\MadelineProto\Logging::log('Disconnecting from DC '.$dc_number.'...');
|
\danog\MadelineProto\Logger::log('Disconnecting from DC '.$dc_number.'...');
|
||||||
unset($this->sockets[$dc_number]);
|
unset($this->sockets[$dc_number]);
|
||||||
unset($this->curdc);
|
unset($this->curdc);
|
||||||
}
|
}
|
||||||
@ -51,9 +52,9 @@ class DataCenter extends Tools
|
|||||||
public function dc_connect($dc_number, $settings = [])
|
public function dc_connect($dc_number, $settings = [])
|
||||||
{
|
{
|
||||||
if (isset($this->sockets[$dc_number])) {
|
if (isset($this->sockets[$dc_number])) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
\danog\MadelineProto\Logging::log('Connecting to DC '.$dc_number.'...');
|
\danog\MadelineProto\Logger::log('Connecting to DC '.$dc_number.'...');
|
||||||
|
|
||||||
if ($settings == []) {
|
if ($settings == []) {
|
||||||
$settings = $this->settings[$dc_number];
|
$settings = $this->settings[$dc_number];
|
||||||
@ -65,47 +66,32 @@ class DataCenter extends Tools
|
|||||||
$address = 'https://'.$subdomain.'.web.telegram.org/'.$path;
|
$address = 'https://'.$subdomain.'.web.telegram.org/'.$path;
|
||||||
}
|
}
|
||||||
$this->sockets[$dc_number] = new Connection($address, $settings['port'], $settings['protocol']);
|
$this->sockets[$dc_number] = new Connection($address, $settings['port'], $settings['protocol']);
|
||||||
|
$this->set_curdc($dc_number);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set_curdc($dc_number) {
|
||||||
$this->curdc = $dc_number;
|
$this->curdc = $dc_number;
|
||||||
|
foreach ($referenced_variables as $key) {
|
||||||
|
$this->{$key} = &$this->sockets[$dc_number]->{$key};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function unset_curdc($dc_number) {
|
||||||
|
unset($this->curdc);
|
||||||
|
foreach ($referenced_variables as $key) {
|
||||||
|
unset($this->sockets[$dc_number]->{$key});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_time_delta($delta, $dc_number = -1)
|
|
||||||
|
public function __call($name, $arguments)
|
||||||
{
|
{
|
||||||
if ($dc_number == -1) {
|
return $this->sockets[$this->curdc]->{$name}(...$arguments);
|
||||||
$dc_number = $this->curdc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->sockets[$dc_number]->set_time_delta($delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_time_delta($dc_number = -1)
|
|
||||||
{
|
|
||||||
if ($dc_number == -1) {
|
|
||||||
$dc_number = $this->curdc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->sockets[$dc_number]->get_time_delta();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function send_message($message, $dc_number = -1)
|
|
||||||
{
|
|
||||||
if ($dc_number == -1) {
|
|
||||||
$dc_number = $this->curdc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->sockets[$dc_number]->send_message($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function read_message($dc_number = -1)
|
|
||||||
{
|
|
||||||
if ($dc_number == -1) {
|
|
||||||
$dc_number = $this->curdc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->sockets[$dc_number]->read_message();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destroy()
|
public function __destroy()
|
||||||
{
|
{
|
||||||
|
$this->unset_curdc();
|
||||||
foreach ($this->sockets as $n => $socket) {
|
foreach ($this->sockets as $n => $socket) {
|
||||||
unset($this->sockets[$n]);
|
unset($this->sockets[$n]);
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ You should have received a copy of the GNU General Public License along with the
|
|||||||
If not, see <http://www.gnu.org/licenses/>.
|
If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
* Logging class
|
* Logger class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace danog\MadelineProto;
|
namespace danog\MadelineProto;
|
||||||
|
|
||||||
class Logging
|
class Logger
|
||||||
{
|
{
|
||||||
public static $mode = null;
|
public static $mode = null;
|
||||||
public static $optional = null;
|
public static $optional = null;
|
||||||
@ -23,9 +23,9 @@ class Logging
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Constructor function
|
* Constructor function
|
||||||
* Accepts various logging modes:
|
* Accepts various logger modes:
|
||||||
* 0 - No logging
|
* 0 - No logger
|
||||||
* 1 - Log to the default logging destination
|
* 1 - Log to the default logger destination
|
||||||
* 2 - Log to file defined in second parameter
|
* 2 - Log to file defined in second parameter
|
||||||
* 3 - Echo logs
|
* 3 - Echo logs
|
||||||
*/
|
*/
|
@ -24,10 +24,7 @@ class MTProto extends MTProtoTools
|
|||||||
// Set default settings
|
// Set default settings
|
||||||
$default_settings = [
|
$default_settings = [
|
||||||
'authorization' => [
|
'authorization' => [
|
||||||
'auth_key' => null,
|
|
||||||
'temp_auth_key' => null,
|
|
||||||
'default_temp_auth_key_expires_in' => 86400,
|
'default_temp_auth_key_expires_in' => 86400,
|
||||||
'session_id' => \phpseclib\Crypt\Random::string(8),
|
|
||||||
'rsa_key' => '-----BEGIN RSA PUBLIC KEY-----
|
'rsa_key' => '-----BEGIN RSA PUBLIC KEY-----
|
||||||
MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6
|
MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6
|
||||||
lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS
|
lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS
|
||||||
@ -61,7 +58,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
|
|||||||
'connection_settings' => [
|
'connection_settings' => [
|
||||||
'all' => [
|
'all' => [
|
||||||
'protocol' => 'tcp_full',
|
'protocol' => 'tcp_full',
|
||||||
'test_mode' => true,
|
'test_mode' => false,
|
||||||
'port' => '443',
|
'port' => '443',
|
||||||
],
|
],
|
||||||
'default_dc' => 2,
|
'default_dc' => 2,
|
||||||
@ -81,10 +78,10 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
|
|||||||
__DIR__.'/TL_telegram_v55.json',
|
__DIR__.'/TL_telegram_v55.json',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'logging' => [
|
'logger' => [
|
||||||
'logging' => 1,
|
'logger' => 1,
|
||||||
'logging_param' => '/tmp/MadelineProto.log',
|
'logger_param' => '/tmp/MadelineProto.log',
|
||||||
'logging' => 3,
|
'logger' => 3,
|
||||||
],
|
],
|
||||||
'max_tries' => [
|
'max_tries' => [
|
||||||
'query' => 5,
|
'query' => 5,
|
||||||
@ -107,42 +104,67 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
// Set up logging class
|
|
||||||
\danog\MadelineProto\Logging::constructor($this->settings['logging']['logging'], $this->settings['logging']['logging_param']);
|
// Setup logger
|
||||||
|
$this->setup_logger();
|
||||||
|
|
||||||
// Connect to servers
|
// Connect to servers
|
||||||
\danog\MadelineProto\Logging::log('Connecting to server...');
|
\danog\MadelineProto\Logger::log('Istantiating DataCenter...');
|
||||||
$this->connection = new DataCenter($this->settings['connection'], $this->settings['connection_settings']);
|
$this->datacenter = new DataCenter($this->settings['connection'], $this->settings['connection_settings']);
|
||||||
|
|
||||||
// Load rsa key
|
// Load rsa key
|
||||||
\danog\MadelineProto\Logging::log('Loading RSA key...');
|
\danog\MadelineProto\Logger::log('Loading RSA key...');
|
||||||
$this->key = new RSA($settings['authorization']['rsa_key']);
|
$this->key = new RSA($settings['authorization']['rsa_key']);
|
||||||
|
|
||||||
// Istantiate TL class
|
// Istantiate TL class
|
||||||
\danog\MadelineProto\Logging::log('Translating tl schemas...');
|
\danog\MadelineProto\Logger::log('Translating tl schemas...');
|
||||||
$this->tl = new TL\TL($this->settings['tl_schema']['src']);
|
$this->tl = new TL\TL($this->settings['tl_schema']['src']);
|
||||||
|
|
||||||
$this->seq_no = 0;
|
|
||||||
$this->incoming_messages = [];
|
$this->incoming_messages = [];
|
||||||
$this->outgoing_messages = [];
|
$this->outgoing_messages = [];
|
||||||
$this->future_salts = [];
|
$this->future_salts = [];
|
||||||
|
|
||||||
if ($this->settings['authorization']['temp_auth_key'] == null || $this->settings['authorization']['auth_key'] == null) {
|
$this->switch_dc($this->settings['connection_settings']['default_dc'], true);
|
||||||
if ($this->settings['authorization']['auth_key'] == null) {
|
|
||||||
\danog\MadelineProto\Logging::log('Generating permanent authorization key...');
|
|
||||||
$this->settings['authorization']['auth_key'] = $this->create_auth_key(-1);
|
|
||||||
}
|
|
||||||
\danog\MadelineProto\Logging::log('Generating temporary authorization key...');
|
|
||||||
$this->settings['authorization']['temp_auth_key'] = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']);
|
|
||||||
}
|
|
||||||
$this->write_client_info();
|
|
||||||
$this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']);
|
|
||||||
\danog\MadelineProto\Logging::log('You may now login to Telegram.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function write_client_info()
|
public function setup_logger()
|
||||||
{
|
{
|
||||||
\danog\MadelineProto\Logging::log('Writing client info...');
|
if (!\danog\MadelineProto\Logger::$constructed) {
|
||||||
|
// Set up logger class
|
||||||
|
\danog\MadelineProto\Logger::constructor($this->settings['logger']['logger'], $this->settings['logger']['logger_param']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switches to a new datacenter and if necessary creates authorization keys, binds them and writes client info
|
||||||
|
public function switch_dc($new_dc, $allow_nearestdc_switch = false)
|
||||||
|
{
|
||||||
|
\danog\MadelineProto\Logger::log('Switching to DC '.$new_dc.'...');
|
||||||
|
if ($this->datacenter->dc_connect($new_dc)) {
|
||||||
|
$this->init_authorization();
|
||||||
|
$this->write_client_info();
|
||||||
|
$this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $allow_nearestdc_switch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates authorization keys
|
||||||
|
public function init_authorization()
|
||||||
|
{
|
||||||
|
if ($this->datacenter->session_id == null) {
|
||||||
|
$this->datacenter->session_id = \phpseclib\Crypt\Random::string(8);
|
||||||
|
}
|
||||||
|
if ($this->datacenter->temp_auth_key == null || $this->datacenter->auth_key == null) {
|
||||||
|
if ($this->datacenter->auth_key == null) {
|
||||||
|
\danog\MadelineProto\Logger::log('Generating permanent authorization key...');
|
||||||
|
$this->datacenter->auth_key = $this->create_auth_key(-1);
|
||||||
|
}
|
||||||
|
\danog\MadelineProto\Logger::log('Generating temporary authorization key...');
|
||||||
|
$this->datacenter->temp_auth_key = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function write_client_info($allow_switch) {
|
||||||
|
|
||||||
|
\danog\MadelineProto\Logger::log('Writing client info...');
|
||||||
$nearestDc = $this->method_call(
|
$nearestDc = $this->method_call(
|
||||||
'invokeWithLayer',
|
'invokeWithLayer',
|
||||||
[
|
[
|
||||||
@ -155,17 +177,17 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
\danog\MadelineProto\Logging::log('Current dc is '.$nearestDc['this_dc'].', nearest dc is '.$nearestDc['nearest_dc'].' in '.$nearestDc['country'].'.');
|
\danog\MadelineProto\Logger::log('Current dc is '.$nearestDc['this_dc'].', nearest dc is '.$nearestDc['nearest_dc'].' in '.$nearestDc['country'].'.');
|
||||||
|
|
||||||
if ($nearestDc['nearest_dc'] != $nearestDc['this_dc']) {
|
if ($nearestDc['nearest_dc'] != $nearestDc['this_dc'] && $allow_switch) {
|
||||||
\danog\MadelineProto\Logging::log('Switching to dc '.$nearestDc['nearest_dc'].'...');
|
$this->switch_dc($nearestDc['nearest_dc']);
|
||||||
$this->connection->dc_connect($nearestDc['nearest_dc']);
|
|
||||||
$this->settings['connection_settings']['default_dc'] = $nearestDc['nearest_dc'];
|
$this->settings['connection_settings']['default_dc'] = $nearestDc['nearest_dc'];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
unset($this->sock);
|
unset($this->datacenter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ class AckHandler extends \danog\MadelineProto\PrimeModule
|
|||||||
|
|
||||||
public function ack_incoming_message_id($message_id)
|
public function ack_incoming_message_id($message_id)
|
||||||
{
|
{
|
||||||
if ($this->settings['authorization']['temp_auth_key']['id'] === null || $this->settings['authorization']['temp_auth_key']['id'] == $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) {
|
if ($this->datacenter->temp_auth_key['id'] === null || $this->datacenter->temp_auth_key['id'] == $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// I let the server know that I received its message
|
// I let the server know that I received its message
|
||||||
|
@ -23,7 +23,8 @@ class AuthKeyHandler extends AckHandler
|
|||||||
public function create_auth_key($expires_in = -1)
|
public function create_auth_key($expires_in = -1)
|
||||||
{
|
{
|
||||||
foreach ($this->range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) {
|
foreach ($this->range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) {
|
||||||
\danog\MadelineProto\Logging::log('Requesting pq');
|
try {
|
||||||
|
\danog\MadelineProto\Logger::log('Requesting pq');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ***********************************************************************
|
* ***********************************************************************
|
||||||
@ -94,7 +95,7 @@ class AuthKeyHandler extends AckHandler
|
|||||||
throw new Exception("couldn't compute p and q.");
|
throw new Exception("couldn't compute p and q.");
|
||||||
}
|
}
|
||||||
|
|
||||||
\danog\MadelineProto\Logging::log('Factorization '.$pq.' = '.$p.' * '.$q);
|
\danog\MadelineProto\Logger::log('Factorization '.$pq.' = '.$p.' * '.$q);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ***********************************************************************
|
* ***********************************************************************
|
||||||
@ -138,7 +139,7 @@ class AuthKeyHandler extends AckHandler
|
|||||||
$to_encrypt = $sha_digest.$data.$random_bytes;
|
$to_encrypt = $sha_digest.$data.$random_bytes;
|
||||||
$encrypted_data = $this->key->encrypt($to_encrypt);
|
$encrypted_data = $this->key->encrypt($to_encrypt);
|
||||||
|
|
||||||
\danog\MadelineProto\Logging::log('Starting Diffie Hellman key exchange');
|
\danog\MadelineProto\Logger::log('Starting Diffie Hellman key exchange');
|
||||||
/*
|
/*
|
||||||
* ***********************************************************************
|
* ***********************************************************************
|
||||||
* Starting Diffie Hellman key exchange, Server authentication
|
* Starting Diffie Hellman key exchange, Server authentication
|
||||||
@ -252,16 +253,16 @@ class AuthKeyHandler extends AckHandler
|
|||||||
* Time delta
|
* Time delta
|
||||||
*/
|
*/
|
||||||
$server_time = $server_DH_inner_data['server_time'];
|
$server_time = $server_DH_inner_data['server_time'];
|
||||||
$this->connection->set_time_delta($server_time - time());
|
$this->datacenter->set_time_delta($server_time - time());
|
||||||
|
|
||||||
\danog\MadelineProto\Logging::log(sprintf('Server-client time delta = %.1f s', $this->connection->get_time_delta()));
|
\danog\MadelineProto\Logger::log(sprintf('Server-client time delta = %.1f s', $this->datacenter->get_time_delta()));
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ***********************************************************************
|
* ***********************************************************************
|
||||||
* Define some needed numbers for BigInteger
|
* Define some needed numbers for BigInteger
|
||||||
*/
|
*/
|
||||||
\danog\MadelineProto\Logging::log('Executing dh_prime checks...');
|
\danog\MadelineProto\Logger::log('Executing dh_prime checks...');
|
||||||
$one = new \phpseclib\Math\BigInteger(1);
|
$one = new \phpseclib\Math\BigInteger(1);
|
||||||
$two = new \phpseclib\Math\BigInteger(2);
|
$two = new \phpseclib\Math\BigInteger(2);
|
||||||
$twoe2047 = new \phpseclib\Math\BigInteger('16158503035655503650357438344334975980222051334857742016065172713762327569433945446598600705761456731844358980460949009747059779575245460547544076193224141560315438683650498045875098875194826053398028819192033784138396109321309878080919047169238085235290822926018152521443787945770532904303776199561965192760957166694834171210342487393282284747428088017663161029038902829665513096354230157075129296432088558362971801859230928678799175576150822952201848806616643615613562842355410104862578550863465661734839271290328348967522998634176499319107762583194718667771801067716614802322659239302476074096777926805529798115328');
|
$twoe2047 = new \phpseclib\Math\BigInteger('16158503035655503650357438344334975980222051334857742016065172713762327569433945446598600705761456731844358980460949009747059779575245460547544076193224141560315438683650498045875098875194826053398028819192033784138396109321309878080919047169238085235290822926018152521443787945770532904303776199561965192760957166694834171210342487393282284747428088017663161029038902829665513096354230157075129296432088558362971801859230928678799175576150822952201848806616643615613562842355410104862578550863465661734839271290328348967522998634176499319107762583194718667771801067716614802322659239302476074096777926805529798115328');
|
||||||
@ -420,7 +421,7 @@ class AuthKeyHandler extends AckHandler
|
|||||||
throw new Exception('wrong new_nonce_hash1');
|
throw new Exception('wrong new_nonce_hash1');
|
||||||
}
|
}
|
||||||
|
|
||||||
\danog\MadelineProto\Logging::log('Diffie Hellman key exchange processed successfully');
|
\danog\MadelineProto\Logger::log('Diffie Hellman key exchange processed successfully');
|
||||||
|
|
||||||
$res_authorization['server_salt'] = \danog\PHP\Struct::unpack('<q', substr($new_nonce, 0, 8 - 0) ^ substr($server_nonce, 0, 8 - 0))[0];
|
$res_authorization['server_salt'] = \danog\PHP\Struct::unpack('<q', substr($new_nonce, 0, 8 - 0) ^ substr($server_nonce, 0, 8 - 0))[0];
|
||||||
$res_authorization['auth_key'] = $auth_key_str;
|
$res_authorization['auth_key'] = $auth_key_str;
|
||||||
@ -430,7 +431,7 @@ class AuthKeyHandler extends AckHandler
|
|||||||
$res_authorization['expires_in'] = $expires_in;
|
$res_authorization['expires_in'] = $expires_in;
|
||||||
}
|
}
|
||||||
|
|
||||||
\danog\MadelineProto\Logging::log('Auth key generated');
|
\danog\MadelineProto\Logger::log('Auth key generated');
|
||||||
|
|
||||||
return $res_authorization;
|
return $res_authorization;
|
||||||
case 'dh_gen_retry':
|
case 'dh_gen_retry':
|
||||||
@ -439,20 +440,23 @@ class AuthKeyHandler extends AckHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
//repeat foreach
|
//repeat foreach
|
||||||
\danog\MadelineProto\Logging::log('Retrying Auth');
|
\danog\MadelineProto\Logger::log('Retrying Auth');
|
||||||
break;
|
break;
|
||||||
case 'dh_gen_fail':
|
case 'dh_gen_fail':
|
||||||
if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) {
|
if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) {
|
||||||
throw new Exception('wrong new_nonce_hash_3');
|
throw new Exception('wrong new_nonce_hash_3');
|
||||||
}
|
}
|
||||||
|
|
||||||
\danog\MadelineProto\Logging::log('Auth Failed');
|
\danog\MadelineProto\Logger::log('Auth Failed');
|
||||||
break 2;
|
break 2;
|
||||||
default:
|
default:
|
||||||
throw new Exception('Response Error');
|
throw new Exception('Response Error');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
\danog\MadelineProto\Logger::log('An exception occurred while generating the authorization key. Retrying...');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Exception('Auth Failed');
|
throw new Exception('Auth Failed');
|
||||||
@ -460,12 +464,12 @@ class AuthKeyHandler extends AckHandler
|
|||||||
|
|
||||||
public function bind_temp_auth_key($expires_in)
|
public function bind_temp_auth_key($expires_in)
|
||||||
{
|
{
|
||||||
\danog\MadelineProto\Logging::log('Binding authorization keys...');
|
\danog\MadelineProto\Logger::log('Binding authorization keys...');
|
||||||
$nonce = \danog\PHP\Struct::unpack('<q', \phpseclib\Crypt\Random::string(8))[0];
|
$nonce = \danog\PHP\Struct::unpack('<q', \phpseclib\Crypt\Random::string(8))[0];
|
||||||
$expires_at = time() + $expires_in;
|
$expires_at = time() + $expires_in;
|
||||||
$temp_auth_key_id = \danog\PHP\Struct::unpack('<q', $this->settings['authorization']['temp_auth_key']['id'])[0];
|
$temp_auth_key_id = \danog\PHP\Struct::unpack('<q', $this->datacenter->temp_auth_key['id'])[0];
|
||||||
$perm_auth_key_id = \danog\PHP\Struct::unpack('<q', $this->settings['authorization']['auth_key']['id'])[0];
|
$perm_auth_key_id = \danog\PHP\Struct::unpack('<q', $this->datacenter->auth_key['id'])[0];
|
||||||
$temp_session_id = \danog\PHP\Struct::unpack('<q', $this->settings['authorization']['session_id'])[0];
|
$temp_session_id = \danog\PHP\Struct::unpack('<q', $this->datacenter->session_id)[0];
|
||||||
$message_data = $this->tl->serialize_obj('bind_auth_key_inner',
|
$message_data = $this->tl->serialize_obj('bind_auth_key_inner',
|
||||||
[
|
[
|
||||||
'nonce' => $nonce,
|
'nonce' => $nonce,
|
||||||
@ -476,18 +480,18 @@ class AuthKeyHandler extends AckHandler
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
$int_message_id = $this->generate_message_id();
|
$int_message_id = $this->generate_message_id();
|
||||||
|
$this->check_message_id($int_message_id, true);
|
||||||
|
|
||||||
$message_id = \danog\PHP\Struct::pack('<Q', $int_message_id);
|
$message_id = \danog\PHP\Struct::pack('<Q', $int_message_id);
|
||||||
$seq_no = 0;
|
$seq_no = 0;
|
||||||
$encrypted_data = \phpseclib\Crypt\Random::string(16).$message_id.\danog\PHP\Struct::pack('<II', $seq_no, strlen($message_data)).$message_data;
|
$encrypted_data = \phpseclib\Crypt\Random::string(16).$message_id.\danog\PHP\Struct::pack('<II', $seq_no, strlen($message_data)).$message_data;
|
||||||
$message_key = substr(sha1($encrypted_data, true), -16);
|
$message_key = substr(sha1($encrypted_data, true), -16);
|
||||||
$padding = \phpseclib\Crypt\Random::string($this->posmod(-strlen($encrypted_data), 16));
|
$padding = \phpseclib\Crypt\Random::string($this->posmod(-strlen($encrypted_data), 16));
|
||||||
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['auth_key']['auth_key']);
|
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->datacenter->auth_key['auth_key']);
|
||||||
$encrypted_message = $this->settings['authorization']['auth_key']['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv);
|
$encrypted_message = $this->datacenter->auth_key['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv);
|
||||||
|
|
||||||
if ($this->method_call('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message], $int_message_id)) {
|
if ($this->method_call('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message], $int_message_id)) {
|
||||||
\danog\MadelineProto\Logging::log('Successfully binded temporary and permanent authorization keys.');
|
\danog\MadelineProto\Logger::log('Successfully binded temporary and permanent authorization keys.');
|
||||||
$this->write_client_info();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
throw new Exception('An error occurred while binding temporary and permanent authorization keys.');
|
throw new Exception('An error occurred while binding temporary and permanent authorization keys.');
|
||||||
|
@ -25,7 +25,7 @@ class CallHandler extends AuthKeyHandler
|
|||||||
$response = null;
|
$response = null;
|
||||||
$count = 0;
|
$count = 0;
|
||||||
while ($response == null && $count++ < $this->settings['max_tries']['response']) {
|
while ($response == null && $count++ < $this->settings['max_tries']['response']) {
|
||||||
\danog\MadelineProto\Logging::log('Getting response (try number '.$count.' for '.$optional_name.')...');
|
\danog\MadelineProto\Logger::log('Getting response (try number '.$count.' for '.$optional_name.')...');
|
||||||
$last_received = $this->recv_message();
|
$last_received = $this->recv_message();
|
||||||
$this->handle_message($last_sent, $last_received);
|
$this->handle_message($last_sent, $last_received);
|
||||||
if (isset($this->outgoing_messages[$last_sent]['response']) && isset($this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'])) {
|
if (isset($this->outgoing_messages[$last_sent]['response']) && isset($this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'])) {
|
||||||
@ -37,8 +37,18 @@ class CallHandler extends AuthKeyHandler
|
|||||||
}
|
}
|
||||||
switch ($response['_']) {
|
switch ($response['_']) {
|
||||||
case 'rpc_error':
|
case 'rpc_error':
|
||||||
|
switch ($response['error_code']) {
|
||||||
|
case 303:
|
||||||
|
$dc = preg_replace('/[^0-9]+/', '', $response['error_message']);
|
||||||
|
$this->switch_dc($dc);
|
||||||
|
return $this->method_call($this->outgoing_messages[$last_sent]['content']['method'], $this->outgoing_messages[$last_sent]['content']['args']);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
throw new Exception('Got rpc error '.$response['error_code'].': '.$response['error_message']);
|
throw new Exception('Got rpc error '.$response['error_code'].': '.$response['error_message']);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return $response;
|
return $response;
|
||||||
break;
|
break;
|
||||||
@ -47,6 +57,9 @@ class CallHandler extends AuthKeyHandler
|
|||||||
|
|
||||||
public function method_call($method, $args, $message_id = null)
|
public function method_call($method, $args, $message_id = null)
|
||||||
{
|
{
|
||||||
|
if (!is_array($args)) {
|
||||||
|
throw new Exception("Arguments aren't an array.");
|
||||||
|
}
|
||||||
foreach (range(1, $this->settings['max_tries']['query']) as $i) {
|
foreach (range(1, $this->settings['max_tries']['query']) as $i) {
|
||||||
try {
|
try {
|
||||||
$args = $this->tl->get_named_method_args($method, $args);
|
$args = $this->tl->get_named_method_args($method, $args);
|
||||||
@ -54,9 +67,8 @@ class CallHandler extends AuthKeyHandler
|
|||||||
$this->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args];
|
$this->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args];
|
||||||
$server_answer = $this->wait_for_response($int_message_id, $method);
|
$server_answer = $this->wait_for_response($int_message_id, $method);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
\danog\MadelineProto\Logging::log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...');
|
\danog\MadelineProto\Logger::log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...');
|
||||||
unset($this->connection);
|
$this->datacenter->close_and_reopen();
|
||||||
$this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($server_answer == null) {
|
if ($server_answer == null) {
|
||||||
@ -70,15 +82,18 @@ class CallHandler extends AuthKeyHandler
|
|||||||
|
|
||||||
public function object_call($object, $args)
|
public function object_call($object, $args)
|
||||||
{
|
{
|
||||||
|
if (!is_array($args)) {
|
||||||
|
throw new Exception("Arguments aren't an array.");
|
||||||
|
}
|
||||||
|
|
||||||
foreach (range(1, $this->settings['max_tries']['query']) as $i) {
|
foreach (range(1, $this->settings['max_tries']['query']) as $i) {
|
||||||
try {
|
try {
|
||||||
$int_message_id = $this->send_message($this->tl->serialize_obj($object, $args), $this->tl->content_related($object));
|
$int_message_id = $this->send_message($this->tl->serialize_obj($object, $args), $this->tl->content_related($object));
|
||||||
$this->outgoing_messages[$int_message_id]['content'] = ['object' => $object, 'args' => $args];
|
$this->outgoing_messages[$int_message_id]['content'] = ['object' => $object, 'args' => $args];
|
||||||
// $server_answer = $this->wait_for_response($int_message_id);
|
// $server_answer = $this->wait_for_response($int_message_id);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
\danog\MadelineProto\Logging::log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...');
|
\danog\MadelineProto\Logger::log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...');
|
||||||
unset($this->connection);
|
$this->datacenter->close_and_reopen();
|
||||||
$this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,22 +26,21 @@ class MessageHandler extends Crypt
|
|||||||
if ($int_message_id == null) {
|
if ($int_message_id == null) {
|
||||||
$int_message_id = $this->generate_message_id();
|
$int_message_id = $this->generate_message_id();
|
||||||
}
|
}
|
||||||
if (!is_int($int_message_id)) {
|
$this->check_message_id($int_message_id, true);
|
||||||
throw new Exception("Given message id isn't an integer!");
|
|
||||||
}
|
|
||||||
$message_id = \danog\PHP\Struct::pack('<Q', $int_message_id);
|
$message_id = \danog\PHP\Struct::pack('<Q', $int_message_id);
|
||||||
if (($this->settings['authorization']['temp_auth_key']['auth_key'] == null) || ($this->settings['authorization']['temp_auth_key']['server_salt'] == null)) {
|
if ($this->datacenter->temp_auth_key['auth_key'] == null || $this->datacenter->temp_auth_key['server_salt'] == null) {
|
||||||
$message = $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.\danog\PHP\Struct::pack('<I', strlen($message_data)).$message_data;
|
$message = $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.\danog\PHP\Struct::pack('<I', strlen($message_data)).$message_data;
|
||||||
} else {
|
} else {
|
||||||
$seq_no = $this->generate_seq_no($content_related);
|
$seq_no = $this->generate_seq_no($content_related);
|
||||||
$encrypted_data = \danog\PHP\Struct::pack('<q', $this->settings['authorization']['temp_auth_key']['server_salt']).$this->settings['authorization']['session_id'].$message_id.\danog\PHP\Struct::pack('<II', $seq_no, strlen($message_data)).$message_data;
|
$encrypted_data = \danog\PHP\Struct::pack('<q', $this->datacenter->temp_auth_key['server_salt']).$this->datacenter->session_id.$message_id.\danog\PHP\Struct::pack('<II', $seq_no, strlen($message_data)).$message_data;
|
||||||
$message_key = substr(sha1($encrypted_data, true), -16);
|
$message_key = substr(sha1($encrypted_data, true), -16);
|
||||||
$padding = \phpseclib\Crypt\Random::string($this->posmod(-strlen($encrypted_data), 16));
|
$padding = \phpseclib\Crypt\Random::string($this->posmod(-strlen($encrypted_data), 16));
|
||||||
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['temp_auth_key']['auth_key']);
|
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->datacenter->temp_auth_key['auth_key']);
|
||||||
$message = $this->settings['authorization']['temp_auth_key']['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv);
|
$message = $this->datacenter->temp_auth_key['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv);
|
||||||
$this->outgoing_messages[$int_message_id]['seq_no'] = $seq_no;
|
$this->outgoing_messages[$int_message_id]['seq_no'] = $seq_no;
|
||||||
}
|
}
|
||||||
$this->connection->send_message($message);
|
$this->datacenter->send_message($message);
|
||||||
|
|
||||||
return $int_message_id;
|
return $int_message_id;
|
||||||
}
|
}
|
||||||
@ -51,7 +50,7 @@ class MessageHandler extends Crypt
|
|||||||
*/
|
*/
|
||||||
public function recv_message()
|
public function recv_message()
|
||||||
{
|
{
|
||||||
$payload = $this->connection->read_message();
|
$payload = $this->datacenter->read_message();
|
||||||
if (fstat($payload)['size'] == 4) {
|
if (fstat($payload)['size'] == 4) {
|
||||||
throw new Exception('Server response error: '.abs(\danog\PHP\Struct::unpack('<i', fread($payload, 4))[0]));
|
throw new Exception('Server response error: '.abs(\danog\PHP\Struct::unpack('<i', fread($payload, 4))[0]));
|
||||||
}
|
}
|
||||||
@ -60,19 +59,19 @@ class MessageHandler extends Crypt
|
|||||||
list($message_id, $message_length) = \danog\PHP\Struct::unpack('<QI', fread($payload, 12));
|
list($message_id, $message_length) = \danog\PHP\Struct::unpack('<QI', fread($payload, 12));
|
||||||
$this->check_message_id($message_id, false);
|
$this->check_message_id($message_id, false);
|
||||||
$message_data = fread($payload, $message_length);
|
$message_data = fread($payload, $message_length);
|
||||||
} elseif ($auth_key_id == $this->settings['authorization']['temp_auth_key']['id']) {
|
} elseif ($auth_key_id == $this->datacenter->temp_auth_key['id']) {
|
||||||
$message_key = fread($payload, 16);
|
$message_key = fread($payload, 16);
|
||||||
$encrypted_data = stream_get_contents($payload);
|
$encrypted_data = stream_get_contents($payload);
|
||||||
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['temp_auth_key']['auth_key'], 'from server');
|
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->datacenter->temp_auth_key['auth_key'], 'from server');
|
||||||
$decrypted_data = $this->ige_decrypt($encrypted_data, $aes_key, $aes_iv);
|
$decrypted_data = $this->ige_decrypt($encrypted_data, $aes_key, $aes_iv);
|
||||||
|
|
||||||
$server_salt = \danog\PHP\Struct::unpack('<q', substr($decrypted_data, 0, 8))[0];
|
$server_salt = \danog\PHP\Struct::unpack('<q', substr($decrypted_data, 0, 8))[0];
|
||||||
if ($server_salt != $this->settings['authorization']['temp_auth_key']['server_salt']) {
|
if ($server_salt != $this->datacenter->temp_auth_key['server_salt']) {
|
||||||
// throw new Exception('Server salt mismatch (my server salt '.$this->settings['authorization']['temp_auth_key']['server_salt'].' is not equal to server server salt '.$server_salt.').');
|
throw new Exception('Server salt mismatch (my server salt '.$this->datacenter->temp_auth_key['server_salt'].' is not equal to server server salt '.$server_salt.').');
|
||||||
}
|
}
|
||||||
|
|
||||||
$session_id = substr($decrypted_data, 8, 8);
|
$session_id = substr($decrypted_data, 8, 8);
|
||||||
if ($session_id != $this->settings['authorization']['session_id']) {
|
if ($session_id != $this->datacenter->session_id) {
|
||||||
throw new Exception('Session id mismatch.');
|
throw new Exception('Session id mismatch.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,10 +19,10 @@ class MsgIdHandler extends MessageHandler
|
|||||||
{
|
{
|
||||||
public function check_message_id($new_message_id, $outgoing, $container = false)
|
public function check_message_id($new_message_id, $outgoing, $container = false)
|
||||||
{
|
{
|
||||||
if (((int) ((time() + $this->connection->get_time_delta() - 300) * pow(2, 30)) * 4) > $new_message_id) {
|
if (((int) ((time() + $this->datacenter->get_time_delta() - 300) * pow(2, 30)) * 4) > $new_message_id) {
|
||||||
throw new Exception('Given message id ('.$new_message_id.') is too old.');
|
throw new Exception('Given message id ('.$new_message_id.') is too old.');
|
||||||
}
|
}
|
||||||
if (((int) ((time() + $this->connection->get_time_delta() + 30) * pow(2, 30)) * 4) < $new_message_id) {
|
if (((int) ((time() + $this->datacenter->get_time_delta() + 30) * pow(2, 30)) * 4) < $new_message_id) {
|
||||||
throw new Exception('Given message id ('.$new_message_id.') is too new.');
|
throw new Exception('Given message id ('.$new_message_id.') is too new.');
|
||||||
}
|
}
|
||||||
if ($outgoing) {
|
if ($outgoing) {
|
||||||
@ -66,7 +66,7 @@ class MsgIdHandler extends MessageHandler
|
|||||||
|
|
||||||
public function generate_message_id()
|
public function generate_message_id()
|
||||||
{
|
{
|
||||||
$int_message_id = (int) ((time() + $this->connection->get_time_delta()) << 32);
|
$int_message_id = (int) ((time() + $this->datacenter->get_time_delta()) << 32);
|
||||||
/* $int_message_id = (int) (
|
/* $int_message_id = (int) (
|
||||||
((int) ($ms_time / 1000) << 32) |
|
((int) ($ms_time / 1000) << 32) |
|
||||||
($this->posmod($ms_time, 1000) << 22) |
|
($this->posmod($ms_time, 1000) << 22) |
|
||||||
@ -78,7 +78,6 @@ class MsgIdHandler extends MessageHandler
|
|||||||
if ($int_message_id <= $keys) {
|
if ($int_message_id <= $keys) {
|
||||||
$int_message_id = $keys + 4;
|
$int_message_id = $keys + 4;
|
||||||
}
|
}
|
||||||
$this->check_message_id($int_message_id, true);
|
|
||||||
|
|
||||||
return $int_message_id;
|
return $int_message_id;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ class ResponseHandler extends MsgIdHandler
|
|||||||
throw new Exception('Received bad_msg_notification for '.$response['bad_msg_id'].': '.$error_codes[$response['error_code']]);
|
throw new Exception('Received bad_msg_notification for '.$response['bad_msg_id'].': '.$error_codes[$response['error_code']]);
|
||||||
break;
|
break;
|
||||||
case 'bad_server_salt':
|
case 'bad_server_salt':
|
||||||
$this->settings['authorization']['temp_auth_key']['server_salt'] = $response['new_server_salt'];
|
$this->datacenter->temp_auth_key['server_salt'] = $response['new_server_salt'];
|
||||||
$this->ack_outgoing_message_id($response['bad_msg_id']); // Acknowledge that the server received my request
|
$this->ack_outgoing_message_id($response['bad_msg_id']); // Acknowledge that the server received my request
|
||||||
$this->outgoing_messages[$response['bad_msg_id']]['response'] = $last_received;
|
$this->outgoing_messages[$response['bad_msg_id']]['response'] = $last_received;
|
||||||
$this->incoming_messages[$last_received]['content'] = $response;
|
$this->incoming_messages[$last_received]['content'] = $response;
|
||||||
@ -73,15 +73,15 @@ class ResponseHandler extends MsgIdHandler
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'new_session_created':
|
case 'new_session_created':
|
||||||
$this->settings['authorization']['temp_auth_key']['server_salt'] = $response['server_salt'];
|
$this->datacenter->temp_auth_key['server_salt'] = $response['server_salt'];
|
||||||
$this->ack_incoming_message_id($last_received); // Acknowledge that I received the server's response
|
$this->ack_incoming_message_id($last_received); // Acknowledge that I received the server's response
|
||||||
\danog\MadelineProto\Logging::log('new session created');
|
\danog\MadelineProto\Logger::log('new session created');
|
||||||
\danog\MadelineProto\Logging::log($response);
|
\danog\MadelineProto\Logger::log($response);
|
||||||
break;
|
break;
|
||||||
case 'msg_container':
|
case 'msg_container':
|
||||||
$responses = [];
|
$responses = [];
|
||||||
\danog\MadelineProto\Logging::log('Received container.');
|
\danog\MadelineProto\Logger::log('Received container.');
|
||||||
\danog\MadelineProto\Logging::log($response['messages']);
|
\danog\MadelineProto\Logger::log($response['messages']);
|
||||||
foreach ($response['messages'] as $message) {
|
foreach ($response['messages'] as $message) {
|
||||||
$this->check_message_id($message['msg_id'], false, true);
|
$this->check_message_id($message['msg_id'], false, true);
|
||||||
$this->incoming_messages[$message['msg_id']] = ['seq_no' => $message['seqno'], 'content' => $message['body']];
|
$this->incoming_messages[$message['msg_id']] = ['seq_no' => $message['seqno'], 'content' => $message['body']];
|
||||||
@ -100,8 +100,8 @@ class ResponseHandler extends MsgIdHandler
|
|||||||
return end($responses);
|
return end($responses);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
\danog\MadelineProto\Logging::log('Received multiple responses, returning last one');
|
\danog\MadelineProto\Logger::log('Received multiple responses, returning last one');
|
||||||
\danog\MadelineProto\Logging::log($responses);
|
\danog\MadelineProto\Logger::log($responses);
|
||||||
|
|
||||||
return end($responses);
|
return end($responses);
|
||||||
break;
|
break;
|
||||||
@ -119,8 +119,8 @@ class ResponseHandler extends MsgIdHandler
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'http_wait':
|
case 'http_wait':
|
||||||
\danog\MadelineProto\Logging::log('Received http wait.');
|
\danog\MadelineProto\Logger::log('Received http wait.');
|
||||||
\danog\MadelineProto\Logging::log($response);
|
\danog\MadelineProto\Logger::log($response);
|
||||||
break;
|
break;
|
||||||
case 'gzip_packed':
|
case 'gzip_packed':
|
||||||
$this->incoming_messages[$last_received]['content'] = gzdecode($response);
|
$this->incoming_messages[$last_received]['content'] = gzdecode($response);
|
||||||
|
@ -26,8 +26,8 @@ class SaltHandler extends ResponseHandler
|
|||||||
|
|
||||||
public function addsalt($valid_since, $valid_until, $salt)
|
public function addsalt($valid_since, $valid_until, $salt)
|
||||||
{
|
{
|
||||||
if (!isset($this->settings['authorization']['temp_auth_key']['salts'][$salt])) {
|
if (!isset($this->datacenter->temp_auth_key['salts'][$salt])) {
|
||||||
$settings['authorization']['temp_auth_key']['salts'][$salt] = ['valid_since' => $valid_since, 'valid_until' => $valid_until];
|
$this->datacenter->temp_auth_key['salts'][$salt] = ['valid_since' => $valid_since, 'valid_until' => $valid_until];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,34 @@ class RSA extends TL\TL
|
|||||||
|
|
||||||
public function __construct($key)
|
public function __construct($key)
|
||||||
{
|
{
|
||||||
|
\danog\MadelineProto\Logger::log('Istantiating \phpseclib\Crypt\RSA...');
|
||||||
$this->key = new \phpseclib\Crypt\RSA();
|
$this->key = new \phpseclib\Crypt\RSA();
|
||||||
|
|
||||||
|
\danog\MadelineProto\Logger::log('Loading key...');
|
||||||
$this->key->loadKey($key);
|
$this->key->loadKey($key);
|
||||||
$this->n = $this->key->modulus;
|
$this->n = $this->key->modulus;
|
||||||
$this->e = $this->key->exponent;
|
$this->e = $this->key->exponent;
|
||||||
$this->fp_bytes = substr(sha1($this->serialize_param('bytes', null, $this->n->toBytes()).$this->serialize_param('bytes', null, $this->e->toBytes()), true), -8);
|
|
||||||
|
\danog\MadelineProto\Logger::log('Computing fingerprint...');
|
||||||
|
$this->fp_bytes = substr(
|
||||||
|
sha1(
|
||||||
|
$this->serialize_param(
|
||||||
|
'bytes',
|
||||||
|
null,
|
||||||
|
$this->n->toBytes()
|
||||||
|
)
|
||||||
|
.
|
||||||
|
$this->serialize_param(
|
||||||
|
'bytes',
|
||||||
|
null,
|
||||||
|
$this->e->toBytes()
|
||||||
|
),
|
||||||
|
true
|
||||||
|
),
|
||||||
|
-8
|
||||||
|
);
|
||||||
|
|
||||||
|
\danog\MadelineProto\Logger::log('Generating BigInteger object for fingerprint...');
|
||||||
$this->fp = new \phpseclib\Math\BigInteger(strrev($this->fp_bytes), -256);
|
$this->fp = new \phpseclib\Math\BigInteger(strrev($this->fp_bytes), -256);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,98 +23,101 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
$TL_dict['methods'] = array_merge(json_decode(file_get_contents($file), true)['methods'], $TL_dict['methods']);
|
$TL_dict['methods'] = array_merge(json_decode(file_get_contents($file), true)['methods'], $TL_dict['methods']);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$TL_dict = json_decode(file_get_contents($file), true);
|
$TL_dict = json_decode(file_get_contents($filename), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\danog\MadelineProto\Logger::log('Translating objects...');
|
||||||
$this->constructors = $TL_dict['constructors'];
|
$this->constructors = $TL_dict['constructors'];
|
||||||
$this->constructor_id = [];
|
$this->constructor_id = [];
|
||||||
$this->constructor_type = [];
|
$this->constructor_type = [];
|
||||||
foreach ($this->constructors as $elem) {
|
foreach ($this->constructors as $elem) {
|
||||||
$z = new TLConstructor($elem);
|
$z = new \danog\MadelineProto\TL\TLConstructor($elem);
|
||||||
$this->constructor_id[$z->id] = $z;
|
$this->constructor_id[$z->id] = $z;
|
||||||
$this->constructor_type[$z->predicate] = $z;
|
$this->constructor_type[$z->predicate] = $z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\danog\MadelineProto\Logger::log('Translating methods...');
|
||||||
$this->methods = $TL_dict['methods'];
|
$this->methods = $TL_dict['methods'];
|
||||||
$this->method_id = [];
|
$this->method_id = [];
|
||||||
$this->method_name = [];
|
$this->method_name = [];
|
||||||
|
$this->method_name_namespaced = [];
|
||||||
foreach ($this->methods as $elem) {
|
foreach ($this->methods as $elem) {
|
||||||
$z = new TLMethod($elem);
|
$z = new \danog\MadelineProto\TL\TLMethod($elem);
|
||||||
$this->method_id[$z->id] = $z;
|
$this->method_id[$z->id] = $z;
|
||||||
$this->method_name[$z->method] = $z;
|
$this->method_name[$z->method] = $z;
|
||||||
|
$this->method_name_namespaced[$z->method] = explode('.', $z->method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function serialize_obj($type_, $kwargs)
|
public function get_named_method_args($method, $arguments)
|
||||||
{
|
{
|
||||||
$bytes_io = '';
|
if (!isset($this->method_name[$method])) {
|
||||||
if (isset($this->constructor_type[$type_])) {
|
throw new Exception('Could not extract type: '.$method);
|
||||||
$tl_constructor = $this->constructor_type[$type_];
|
|
||||||
} else {
|
|
||||||
throw new Exception('Could not extract type: '.$type_);
|
|
||||||
}
|
|
||||||
$bytes_io .= \danog\PHP\Struct::pack('<i', $tl_constructor->id);
|
|
||||||
foreach ($tl_constructor->params as $arg) {
|
|
||||||
$bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]);
|
|
||||||
}
|
}
|
||||||
|
$tl_method = $this->method_name[$method];
|
||||||
|
|
||||||
return $bytes_io;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_named_method_args($type_, $kwargs)
|
if (count(array_filter(array_keys($arguments), 'is_string')) == 0) {
|
||||||
{
|
|
||||||
if (isset($this->method_name[$type_])) {
|
|
||||||
$tl_method = $this->method_name[$type_];
|
|
||||||
} else {
|
|
||||||
throw new Exception('Could not extract type: '.$type_);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count(array_filter(array_keys($kwargs), 'is_string')) == 0) {
|
|
||||||
$argcount = 0;
|
$argcount = 0;
|
||||||
$newargs = [];
|
$newargs = [];
|
||||||
foreach ($tl_method->params as $arg) {
|
foreach ($tl_method->params as $current_argument) {
|
||||||
$newargs[$arg['name']] = $kwargs[$argcount++];
|
$newargs[$current_argument['name']] = $arguments[$argcount++];
|
||||||
}
|
}
|
||||||
$kwargs = $newargs;
|
$arguments = $newargs;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $kwargs;
|
return $arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function serialize_method($type_, $kwargs)
|
public function serialize_obj($object, $arguments)
|
||||||
{
|
{
|
||||||
$bytes_io = '';
|
if (!isset($this->constructor_type[$object])) {
|
||||||
if (isset($this->method_name[$type_])) {
|
throw new Exception('Could not extract type: '.$object);
|
||||||
$tl_method = $this->method_name[$type_];
|
|
||||||
} else {
|
|
||||||
throw new Exception('Could not extract type: '.$type_);
|
|
||||||
}
|
}
|
||||||
$bytes_io .= \danog\PHP\Struct::pack('<i', $tl_method->id);
|
|
||||||
foreach ($tl_method->params as $arg) {
|
$tl_method = $this->constructor_type[$object];
|
||||||
if (!isset($kwargs[$arg['name']])) {
|
$serialized = \danog\PHP\Struct::pack('<i', $tl_constructor->id);
|
||||||
if ($arg['name'] == 'flags') {
|
|
||||||
$kwargs['flags'] = 0;
|
foreach ($tl_constructor->params as $current_argument) {
|
||||||
|
$serialized .= $this->serialize_param($current_argument['type'], $current_argument['subtype'], $arguments[$current_argument['name']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $serialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function serialize_method($method, $arguments)
|
||||||
|
{
|
||||||
|
if (!isset($this->method_name[$method])) {
|
||||||
|
throw new Exception('Could not extract type: '.$method);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tl_method = $this->method_name[$method];
|
||||||
|
$serialized = \danog\PHP\Struct::pack('<i', $tl_method->id);
|
||||||
|
|
||||||
|
foreach ($tl_method->params as $current_argument) {
|
||||||
|
if (!isset($arguments[$current_argument['name']])) {
|
||||||
|
if ($current_argument['name'] == 'flags') {
|
||||||
|
$arguments['flags'] = 0;
|
||||||
} else {
|
} else {
|
||||||
if ($arg['opt']) {
|
if ($current_argument['opt']) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw new Exception('Missing required parameter ('.$arg['name'].')');
|
throw new Exception('Missing required parameter ('.$current_argument['name'].')');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]);
|
$serialized .= $this->serialize_param($current_argument['type'], $current_argument['subtype'], $arguments[$current_argument['name']]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $bytes_io;
|
return $serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function serialize_param($type_, $subtype, $value)
|
public function serialize_param($type, $subtype, $value)
|
||||||
{
|
{
|
||||||
switch ($type_) {
|
switch ($type) {
|
||||||
case 'int':
|
case 'int':
|
||||||
if (!is_numeric($value)) {
|
if (!is_numeric($value)) {
|
||||||
throw new Exception("serialize_param: given value isn't numeric");
|
throw new Exception("serialize_param: given value isn't numeric");
|
||||||
}
|
}
|
||||||
if (!(strlen(decbin($value)) <= 32)) {
|
|
||||||
throw new Exception('Given value is too long.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return \danog\PHP\Struct::pack('<i', $value);
|
return \danog\PHP\Struct::pack('<i', $value);
|
||||||
break;
|
break;
|
||||||
@ -122,9 +125,6 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
if (!is_numeric($value)) {
|
if (!is_numeric($value)) {
|
||||||
throw new Exception("serialize_param: given value isn't numeric");
|
throw new Exception("serialize_param: given value isn't numeric");
|
||||||
}
|
}
|
||||||
if (!(strlen(decbin($value)) <= 32)) {
|
|
||||||
throw new Exception('Given value is too long.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return \danog\PHP\Struct::pack('<I', $value);
|
return \danog\PHP\Struct::pack('<I', $value);
|
||||||
break;
|
break;
|
||||||
@ -137,11 +137,7 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
break;
|
break;
|
||||||
case 'int128':
|
case 'int128':
|
||||||
case 'int256':
|
case 'int256':
|
||||||
if (!is_string($value)) {
|
return (string)$value;
|
||||||
throw new Exception("serialize_param: given value isn't a string");
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
break;
|
break;
|
||||||
case 'double':
|
case 'double':
|
||||||
return \danog\PHP\Struct::pack('<d', $value);
|
return \danog\PHP\Struct::pack('<d', $value);
|
||||||
@ -175,14 +171,14 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
|
|
||||||
return $concat;
|
return $concat;
|
||||||
default:
|
default:
|
||||||
throw new Exception("Couldn't serialize param with type ".$type_);
|
throw new Exception("Couldn't serialize param with type ".$type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_length($bytes_io, $type_ = null, $subtype = null)
|
public function get_length($bytes_io, $type = null, $subtype = null)
|
||||||
{
|
{
|
||||||
$this->deserialize($bytes_io, $type_, $subtype);
|
$this->deserialize($bytes_io, $type, $subtype);
|
||||||
|
|
||||||
return ftell($bytes_io);
|
return ftell($bytes_io);
|
||||||
}
|
}
|
||||||
@ -190,12 +186,12 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
/**
|
/**
|
||||||
* :type bytes_io: io.BytesIO object.
|
* :type bytes_io: io.BytesIO object.
|
||||||
*/
|
*/
|
||||||
public function deserialize($bytes_io, $type_ = null, $subtype = null)
|
public function deserialize($bytes_io, $type = null, $subtype = null)
|
||||||
{
|
{
|
||||||
if (!(get_resource_type($bytes_io) == 'file' || get_resource_type($bytes_io) == 'stream')) {
|
if (!(get_resource_type($bytes_io) == 'file' || get_resource_type($bytes_io) == 'stream')) {
|
||||||
throw new Exception('An invalid bytes_io handle provided.');
|
throw new Exception('An invalid bytes_io handle was provided.');
|
||||||
}
|
}
|
||||||
switch ($type_) {
|
switch ($type) {
|
||||||
case 'int':
|
case 'int':
|
||||||
$x = \danog\PHP\Struct::unpack('<i', fread($bytes_io, 4)) [0];
|
$x = \danog\PHP\Struct::unpack('<i', fread($bytes_io, 4)) [0];
|
||||||
break;
|
break;
|
||||||
@ -249,16 +245,14 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (isset($this->constructor_type[$type_])) {
|
if (isset($this->constructor_type[$type])) {
|
||||||
$tl_elem = $this->constructor_type[$type_];
|
$tl_elem = $this->constructor_type[$type];
|
||||||
} else {
|
} else {
|
||||||
$Idata = fread($bytes_io, 4);
|
$i = \danog\PHP\Struct::unpack('<i', fread($bytes_io, 4)) [0];
|
||||||
$i = \danog\PHP\Struct::unpack('<i', $Idata) [0];
|
if (!isset($this->constructor_id[$i])) {
|
||||||
if (isset($this->constructor_id[$i])) {
|
throw new Exception('Could not extract type: '.$type);
|
||||||
$tl_elem = $this->constructor_id[$i];
|
|
||||||
} else {
|
|
||||||
throw new Exception('Could not extract type: '.$type_);
|
|
||||||
}
|
}
|
||||||
|
$tl_elem = $this->constructor_id[$i];
|
||||||
}
|
}
|
||||||
|
|
||||||
$base_boxed_types = ['Vector t', 'Int', 'Long', 'Double', 'String', 'Int128', 'Int256'];
|
$base_boxed_types = ['Vector t', 'Int', 'Long', 'Double', 'String', 'Int128', 'Int256'];
|
||||||
@ -278,7 +272,9 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
|
|
||||||
public function content_related($method)
|
public function content_related($method)
|
||||||
{
|
{
|
||||||
return !in_array($method, [
|
return !in_array(
|
||||||
|
$method,
|
||||||
|
[
|
||||||
'rpc_result',
|
'rpc_result',
|
||||||
'rpc_error',
|
'rpc_error',
|
||||||
'rpc_drop_answer',
|
'rpc_drop_answer',
|
||||||
@ -309,6 +305,7 @@ class TL extends \danog\MadelineProto\Tools
|
|||||||
'msg_new_detailed_info',
|
'msg_new_detailed_info',
|
||||||
'msg_resend_req',
|
'msg_resend_req',
|
||||||
'msg_resend_ans_req',
|
'msg_resend_ans_req',
|
||||||
]);
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
testing.php
21
testing.php
@ -2,10 +2,19 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
$config = parse_ini_file('credentials', true);
|
|
||||||
if (!$config) {
|
|
||||||
pyjslib_printnl("File 'credentials' seems to not exist.");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$MadelineProto = new \danog\MadelineProto\API($config);
|
$MadelineProto = new \danog\MadelineProto\API();
|
||||||
|
|
||||||
|
if (file_exists('number.php')) {
|
||||||
|
include_once 'number.php';
|
||||||
|
$sendCode = $MadelineProto->auth->sendCode(
|
||||||
|
[
|
||||||
|
'phone_number' => $number,
|
||||||
|
'sms_type' => 5,
|
||||||
|
'api_id' => $this->settings['app_info']['api_id'],
|
||||||
|
'api_hash' => $this->settings['app_info']['api_hash'],
|
||||||
|
'lang_code' => $this->settings['app_info']['lang_code'],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
var_dump($sendCode);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user