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');
|
2016-07-13 21:39:24 +02:00
|
|
|
|
require_once ('Struct.php');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to get hex crc32
|
|
|
|
|
* :param data: Data to encode
|
|
|
|
|
*/
|
2016-06-23 23:51:08 +02:00
|
|
|
|
function newcrc32($data) {
|
2016-06-28 03:10:15 +02:00
|
|
|
|
return hexdec(hash("crc32b", $data));
|
2016-06-23 23:51:08 +02:00
|
|
|
|
}
|
2016-07-13 21:39:24 +02:00
|
|
|
|
|
2016-06-23 23:51:08 +02:00
|
|
|
|
/**
|
2016-07-13 21:39:24 +02:00
|
|
|
|
* Function to dump the hex version of a string.
|
|
|
|
|
* :param what: What to dump
|
2016-06-23 23:51:08 +02:00
|
|
|
|
*/
|
2016-06-28 03:40:31 +02:00
|
|
|
|
function hex_dump($what) { var_dump(bin2hex($what)); };
|
2016-07-13 21:39:24 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function slice a certain string. Works just like array_slice, only with a string.
|
|
|
|
|
* :param data: data to slice
|
|
|
|
|
* :param offset: If offset is non-negative, the sequence will start at that offset in the string. If offset is negative, the sequence will start that far from the end of the string.
|
|
|
|
|
* :param length: If length is given and is positive, then the sequence will have up to that many chars in it. If the string is shorter than the length, then only the available string chars will be present. If length is given and is negative then the sequence will stop that many chars from the end of the string. If it is omitted, then the sequence will have everything from offset up until the end of the string.
|
|
|
|
|
*/
|
2016-06-28 13:38:39 +02:00
|
|
|
|
function file_slice($data, $offset, $length) {
|
|
|
|
|
$size = strlen($data);
|
|
|
|
|
if($offset < 0) $offset = $size + $offset;
|
|
|
|
|
if($length < 0) $length = $size + $length;
|
|
|
|
|
if($length == null) $length = $size;
|
|
|
|
|
if($offset == null) $offset = 0;
|
|
|
|
|
$packet_sliced = "";
|
|
|
|
|
while ($length != 0) { if($offset == $size) break; $packet_sliced .= $data[$offset]; $offset++; $length--; }
|
|
|
|
|
return $packet_sliced;
|
|
|
|
|
}
|
2016-07-13 21:39:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* len.
|
|
|
|
|
*
|
|
|
|
|
* Get the length of a string or of an array
|
|
|
|
|
*
|
|
|
|
|
* @param $input String or array to parse
|
|
|
|
|
*
|
|
|
|
|
* @return int with the length
|
|
|
|
|
**/
|
|
|
|
|
function len($input)
|
|
|
|
|
{
|
|
|
|
|
if (is_array($input)) {
|
|
|
|
|
return count($input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strlen($input);
|
2016-06-28 03:10:15 +02:00
|
|
|
|
}
|
2016-07-13 21:39:24 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to visualize byte streams. Split into bytes, print to console.
|
|
|
|
|
* :param bs: BYTE STRING
|
|
|
|
|
*/
|
2016-06-23 23:51:08 +02:00
|
|
|
|
function vis($bs) {
|
2016-07-13 21:39:24 +02:00
|
|
|
|
$bs = str_split($bs);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$symbols_in_one_line = 8;
|
2016-07-13 21:39:24 +02:00
|
|
|
|
$n = floor(len($bs) / $symbols_in_one_line);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
foreach (pyjslib_range($n) as $i) {
|
2016-07-13 21:39:24 +02:00
|
|
|
|
echo join($i * $symbols_in_one_line . ' | ' . ' ',
|
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-07-13 21:39:24 +02:00
|
|
|
|
) . PHP_EOL;
|
2016-06-23 23:51:08 +02:00
|
|
|
|
}
|
2016-07-13 21:39:24 +02:00
|
|
|
|
if (!(len($bs) % $symbols_in_one_line) == 0) {
|
|
|
|
|
echo join((($i + 1) * $symbols_in_one_line) . ' | ' . ' ',
|
2016-06-24 23:47:10 +02:00
|
|
|
|
array_map(function($el) { return "%02X" % $el; }, array_slice($bs, ($i+1)*$symbols_in_one_line), null)
|
2016-07-13 21:39:24 +02:00
|
|
|
|
) . PHP_EOL;
|
2016-06-23 23:51:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-13 21:39:24 +02:00
|
|
|
|
vis("jndjN");
|
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;
|
2016-06-28 03:10:15 +02:00
|
|
|
|
$length = strlen($s);
|
2016-06-26 15:28:39 +02:00
|
|
|
|
if (($length % 4)) {
|
|
|
|
|
$extra = (4 - ($length % 4));
|
|
|
|
|
$s = (($b('') * $extra) + $s);
|
|
|
|
|
$length = ($length + $extra);
|
|
|
|
|
}
|
|
|
|
|
foreach( pyjslib_range(0, $length, 4) as $i ) {
|
2016-07-13 21:39:24 +02:00
|
|
|
|
$acc = ($acc << 32 + $GLOBALS["struct"]->unpack('I', array_slice($s, $i, ($i + 4) - $i))[0]);
|
2016-06-26 15:28:39 +02:00
|
|
|
|
}
|
|
|
|
|
return $acc;
|
|
|
|
|
}
|
2016-06-24 23:47:10 +02:00
|
|
|
|
|
2016-06-27 16:52:28 +02:00
|
|
|
|
function fread_all($handle) {
|
|
|
|
|
$pos = ftell($handle);
|
|
|
|
|
fseek($handle, 0);
|
|
|
|
|
$content = fread($handle, fstat($handle)["size"]);
|
|
|
|
|
fseek($handle, $pos);
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
2016-06-28 03:37:04 +02:00
|
|
|
|
function fopen_and_write($filename, $mode, $data) {
|
|
|
|
|
$handle = fopen($filename, $mode);
|
|
|
|
|
fwrite($handle, $data);
|
|
|
|
|
rewind($handle);
|
|
|
|
|
return $handle;
|
|
|
|
|
}
|
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)) {
|
2016-07-13 21:39:24 +02:00
|
|
|
|
$s = ($GLOBALS["struct"]->pack('I', $n & 4294967295) + $s);
|
2016-06-26 15:28:39 +02:00
|
|
|
|
$n = $n >> 32;
|
|
|
|
|
}
|
2016-06-28 03:10:15 +02:00
|
|
|
|
foreach( pyjslib_range(strlen($s)) as $i ) {
|
2016-06-26 15:28:39 +02:00
|
|
|
|
if (($s[$i] != $b('')[0])) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$s = array_slice($s, $i, null);
|
2016-06-28 03:10:15 +02:00
|
|
|
|
if (($blocksize > 0) && (strlen($s) % $blocksize)) {
|
|
|
|
|
$s = ((($blocksize - (strlen($s) % $blocksize)) * $b('')) + $s);
|
2016-06-26 15:28:39 +02:00
|
|
|
|
}
|
|
|
|
|
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-27 22:52:07 +02:00
|
|
|
|
$this->sock = fsockopen("tcp://".$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-27 22:52:07 +02:00
|
|
|
|
stream_set_timeout($this->sock, 5);
|
2016-06-23 23:51:08 +02:00
|
|
|
|
$this->MAX_RETRY = 5;
|
|
|
|
|
$this->AUTH_MAX_RETRY = 5;
|
|
|
|
|
}
|
2016-06-27 22:52:07 +02:00
|
|
|
|
function __destruct() {
|
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) {
|
2016-07-13 21:39:24 +02:00
|
|
|
|
$message_id = $GLOBALS["struct"]->pack('<Q', (pyjslib_int(((time() + $this->timedelta) * pow(2, 30))) * 4));
|
2016-06-28 03:10:15 +02:00
|
|
|
|
|
2016-06-23 23:51:08 +02:00
|
|
|
|
if (($this->auth_key == null) || ($this->server_salt == null)) {
|
2016-07-13 21:39:24 +02:00
|
|
|
|
$message = ' |