Fixed binding of authorization keys. Finally can use auth.sendCode method.

This commit is contained in:
danogentili 2016-11-15 23:42:52 +03:00
parent 6720199a33
commit 417d9343c2
6 changed files with 36 additions and 20 deletions

View File

@ -19,6 +19,7 @@ class Connection extends Tools
{
public $sock = null;
public $protocol = null;
private $_delta = 0;
public function __construct($ip, $port, $protocol = 'tcp_full')
{
@ -78,6 +79,12 @@ class Connection extends Tools
}
}
public function set_time_delta($delta) {
$this->_delta = $delta;
}
public function get_time_delta() {
return $this->_delta;
}
/**
* Function to get hex crc32.
*

View File

@ -68,6 +68,20 @@ class DataCenter extends Tools
$this->curdc = $dc_number;
}
public function set_time_delta($delta, $dc_number = -1)
{
if ($dc_number == -1) {
$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) {

View File

@ -123,7 +123,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$this->tl = new TL\TL($this->settings['tl_schema']['src']);
$this->seq_no = 0;
$this->timedelta = 0; // time delta
$this->incoming_messages = [];
$this->outgoing_messages = [];
$this->future_salts = [];
@ -138,14 +137,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
}
$this->write_client_info();
$this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']);
$nearestDc = $this->method_call('auth.sendCode', [
'phone_number' => '393373737',
'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($nearestDc);
\danog\MadelineProto\Logging::log('You may now login to Telegram.');
}
public function write_client_info()

View File

@ -252,9 +252,9 @@ class AuthKeyHandler extends AckHandler
* Time delta
*/
$server_time = $server_DH_inner_data['server_time'];
$this->timedelta = $server_time - time();
$this->connection->set_time_delta($server_time - time());
\danog\MadelineProto\Logging::log(sprintf('Server-client time delta = %.1f s', $this->timedelta));
\danog\MadelineProto\Logging::log(sprintf('Server-client time delta = %.1f s', $this->connection->get_time_delta()));
/*
@ -431,7 +431,6 @@ class AuthKeyHandler extends AckHandler
}
\danog\MadelineProto\Logging::log('Auth key generated');
$this->timedelta = 0;
return $res_authorization;
case 'dh_gen_retry':
@ -461,6 +460,7 @@ class AuthKeyHandler extends AckHandler
public function bind_temp_auth_key($expires_in)
{
\danog\MadelineProto\Logging::log('Binding authorization keys...');
$nonce = \danog\PHP\Struct::unpack('<q', \phpseclib\Crypt\Random::string(8))[0];
$expires_at = time() + $expires_in;
$temp_auth_key_id = \danog\PHP\Struct::unpack('<q', $this->settings['authorization']['temp_auth_key']['id'])[0];
@ -484,7 +484,7 @@ class AuthKeyHandler extends AckHandler
list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['auth_key']['auth_key']);
$encrypted_message = $this->settings['authorization']['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], $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.');
$this->write_client_info();

View File

@ -26,6 +26,9 @@ class MessageHandler extends Crypt
if ($int_message_id == null) {
$int_message_id = $this->generate_message_id();
}
if (!is_int($int_message_id)) {
throw new Exception("Given message id isn't an integer!");
}
$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)) {
$message = $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.\danog\PHP\Struct::pack('<I', strlen($message_data)).$message_data;

View File

@ -19,10 +19,10 @@ class MsgIdHandler extends MessageHandler
{
public function check_message_id($new_message_id, $outgoing, $container = false)
{
if (((int) ((time() + $this->timedelta - 300) * pow(2, 30)) * 4) > $new_message_id) {
if (((int) ((time() + $this->connection->get_time_delta() - 300) * pow(2, 30)) * 4) > $new_message_id) {
throw new Exception('Given message id ('.$new_message_id.') is too old.');
}
if (((int) ((time() + $this->timedelta + 30) * pow(2, 30)) * 4) < $new_message_id) {
if (((int) ((time() + $this->connection->get_time_delta() + 30) * pow(2, 30)) * 4) < $new_message_id) {
throw new Exception('Given message id ('.$new_message_id.') is too new.');
}
if ($outgoing) {
@ -66,17 +66,17 @@ class MsgIdHandler extends MessageHandler
public function generate_message_id()
{
$ms_time = (time() + $this->timedelta) * 1000;
$int_message_id = (int) (
$int_message_id = (int) ((time() + $this->connection->get_time_delta()) << 32);
/* $int_message_id = (int) (
((int) ($ms_time / 1000) << 32) |
($this->posmod($ms_time, 1000) << 22) |
rand(0, 524288) << 2
);
);*/
$keys = array_keys($this->outgoing_messages);
asort($keys);
$keys = end($keys);
while ($int_message_id <= $keys) {
$int_message_id += 4;
if ($int_message_id <= $keys) {
$int_message_id = $keys + 4;
}
$this->check_message_id($int_message_id, true);