2016-06-23 23:51:08 +02:00
|
|
|
|
<?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
|
|
|
|
|
*/
|
2016-06-24 23:47:10 +02:00
|
|
|
|
|
2016-06-23 23:51:08 +02:00
|
|
|
|
function vis($bs) {
|
|
|
|
|
$bs = bytearray($bs);
|
|
|
|
|
$symbols_in_one_line = 8;
|
2016-06-26 12:59:15 +02:00
|
|
|
|
$n = floor(count($bs) / $symbols_in_one_line);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
foreach (pyjslib_range($n) as $i) {
|
|
|
|
|
pyjslib_printnl(pyjslib_str(($i * $symbols_in_one_line)) . ' | ' . ' '->join(
|
2016-06-24 23:47:10 +02:00
|
|
|
|
array_map(function($el) { return "%02X" % $el; }, array_slice($bs,$i*$symbols_in_one_line, ($i+1)*$symbols_in_one_line))
|
2016-06-23 23:51:08 +02:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
if (!(((count($bs) % $symbols_in_one_line) == 0))) {
|
2016-06-24 23:47:10 +02:00
|
|
|
|
pyjslib_printnl(pyjslib_str((($i + 1) * $symbols_in_one_line)) . ' | ' . ' '->join(
|
|
|
|
|
array_map(function($el) { return "%02X" % $el; }, array_slice($bs, ($i+1)*$symbols_in_one_line), null)
|
2016-06-23 23:51:08 +02:00
|
|
|
|
) . '
|
|
|
|
|
');
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 15:28:39 +02:00
|
|
|
|
/**
|
|
|
|
|
* bytes_to_long(string) : long
|
|
|
|
|
* Convert a byte string to a long integer.
|
|
|
|
|
* This is (essentially) the inverse of long_to_bytes().
|
|
|
|
|
*/
|
|
|
|
|
function bytes_to_long($s) {
|
|
|
|
|
$acc = 0;
|
|
|
|
|
$length = count($s);
|
|
|
|
|
if (($length % 4)) {
|
|
|
|
|
$extra = (4 - ($length % 4));
|
|
|
|
|
$s = (($b('') * $extra) + $s);
|
|
|
|
|
$length = ($length + $extra);
|
|
|
|
|
}
|
|
|
|
|
foreach( pyjslib_range(0, $length, 4) as $i ) {
|
|
|
|
|
$acc = ($acc << 32 + unpack('>I', array_slice($s, $i, ($i + 4) - $i))[0]);
|
|
|
|
|
}
|
|
|
|
|
return $acc;
|
|
|
|
|
}
|
2016-06-24 23:47:10 +02:00
|
|
|
|
|
2016-06-26 15:28:39 +02:00
|
|
|
|
/**
|
|
|
|
|
* long_to_bytes(n:long, blocksize:int) : string
|
|
|
|
|
* Convert a long integer to a byte string.
|
|
|
|
|
* If optional blocksize is given and greater than zero, pad the front of the
|
|
|
|
|
* byte string with binary zeros so that the length is a multiple of
|
|
|
|
|
* blocksize.
|
|
|
|
|
*/
|
|
|
|
|
function long_to_bytes($n,$blocksize=0) {
|
|
|
|
|
$s = $b('');
|
|
|
|
|
$n = long($n);
|
|
|
|
|
while (($n > 0)) {
|
|
|
|
|
$s = (pack('>I', $n & 4294967295) + $s);
|
|
|
|
|
$n = $n >> 32;
|
|
|
|
|
}
|
|
|
|
|
foreach( pyjslib_range(count($s)) as $i ) {
|
|
|
|
|
if (($s[$i] != $b('')[0])) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$s = array_slice($s, $i, null);
|
|
|
|
|
if (($blocksize > 0) && (count($s) % $blocksize)) {
|
|
|
|
|
$s = ((($blocksize - (count($s) % $blocksize)) * $b('')) + $s);
|
|
|
|
|
}
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
2016-06-23 23:51:08 +02:00
|
|
|
|
/**
|
|
|
|
|
* Manages TCP Transport. encryption and message frames
|
|
|
|
|
*/
|
|
|
|
|
class Session {
|
|
|
|
|
function __construct($ip, $port, $auth_key = null, $server_salt = null) {
|
2016-06-26 14:53:16 +02:00
|
|
|
|
$this->sock = fsockopen($ip, $port);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$this->number = 0;
|
|
|
|
|
$this->timedelta = 0;
|
2016-06-26 14:53:16 +02:00
|
|
|
|
$this->session_id = random_bytes(8);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$this->auth_key = $auth_key;
|
|
|
|
|
$this->auth_key_id = $this->auth_key ? array_slice(sha1($this->auth_key, true), -8, null) : null;
|
2016-06-26 14:53:16 +02:00
|
|
|
|
stream_set_timeout($this->sock, 5.0);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$this->MAX_RETRY = 5;
|
|
|
|
|
$this->AUTH_MAX_RETRY = 5;
|
|
|
|
|
}
|
|
|
|
|
function __del__() {
|
2016-06-26 14:53:16 +02:00
|
|
|
|
fclose($this->sock);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 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 = ' |