219 lines
12 KiB
PHP
219 lines
12 KiB
PHP
|
<?php
|
|||
|
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'libpy2php');
|
|||
|
require_once ('libpy2php.php');
|
|||
|
require_once ('os_path.php');
|
|||
|
require_once ('crypt.php');
|
|||
|
require_once ('prime.php');
|
|||
|
require_once ('TL.php');
|
|||
|
function newcrc32($data) {
|
|||
|
return $originalcrc32($data) & 4294967295;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Function to visualize byte streams. Split into bytes, print to console.
|
|||
|
* :param bs: BYTE STRING
|
|||
|
*/
|
|||
|
/*
|
|||
|
function vis($bs) {
|
|||
|
$bs = bytearray($bs);
|
|||
|
$symbols_in_one_line = 8;
|
|||
|
$n = (count($bs) / $symbols_in_one_line);
|
|||
|
$i = 0;
|
|||
|
foreach (pyjslib_range($n) as $i) {
|
|||
|
pyjslib_printnl(pyjslib_str(($i * $symbols_in_one_line)) . ' | ' . ' '->join(
|
|||
|
array_map(function($el) { return "%02X" % $el; }, $input);
|
|||
|
["%02X" % b for b in bs[i*symbols_in_one_line:(i+1)*symbols_in_one_line]]
|
|||
|
|
|||
|
));
|
|||
|
}
|
|||
|
if (!(((count($bs) % $symbols_in_one_line) == 0))) {
|
|||
|
pyjslib_printnl(pyjslib_str((($i + 1) * $symbols_in_one_line)) . ' | ' . ' '->join(
|
|||
|
) . '
|
|||
|
');
|
|||
|
}
|
|||
|
}
|
|||
|
*/
|
|||
|
/**
|
|||
|
* Manages TCP Transport. encryption and message frames
|
|||
|
*/
|
|||
|
class Session {
|
|||
|
function __construct($ip, $port, $auth_key = null, $server_salt = null) {
|
|||
|
$this->sock = socket::socket();
|
|||
|
$this->sock->connect([$ip, $port]);
|
|||
|
$this->number = 0;
|
|||
|
$this->timedelta = 0;
|
|||
|
$this->session_id = $os->urandom(8);
|
|||
|
$this->auth_key = $auth_key;
|
|||
|
$this->auth_key_id = $this->auth_key ? array_slice(sha1($this->auth_key, true), -8, null) : null;
|
|||
|
$this->sock->settimeout(5.0);
|
|||
|
$this->MAX_RETRY = 5;
|
|||
|
$this->AUTH_MAX_RETRY = 5;
|
|||
|
}
|
|||
|
function __del__() {
|
|||
|
$this->sock->close();
|
|||
|
}
|
|||
|
/**
|
|||
|
* Forming the message frame and sending message to server
|
|||
|
* :param message: byte string to send
|
|||
|
*/
|
|||
|
function send_message($message_data) {
|
|||
|
$message_id = struct::pack('<Q', (pyjslib_int(((new time() + $this->timedelta) * pow(2, 30))) * 4));
|
|||
|
if (($this->auth_key == null) || ($this->server_salt == null)) {
|
|||
|
$message = ' |