Translating...

This commit is contained in:
Daniil Gentili 2016-06-28 13:38:39 +02:00
parent e6a585d1e4
commit f13efd4e77
4 changed files with 149 additions and 3 deletions

Binary file not shown.

148
mtproto.php.save Normal file
View File

@ -0,0 +1,148 @@
<?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 hexdec(hash("crc32b", $data));
}
/**
* Function to visualize byte streams. Split into bytes, print to console.
* :param bs: BYTE STRING
*/
define('BIG_ENDIAN', pack('L', 1) === pack('N', 1));
function hex_dump($what) { var_dump(bin2hex($what)); };
function oldfile_slice($data, $offset, $length) {
$packet_sliced_handle = fopen_and_write("php://memory", "w+b", $data);
$size = fstat($packet_sliced_handle)["size"];
if($offset < 0) $offset = $size + $offset;
if($length < 0) $length = $size + $length;
if($length == null) $length = $size;
fseek($packet_sliced_handle, $offset);
$packet_sliced = fread($packet_sliced_handle, $length);
fclose($packet_sliced_handle);
return $packet_sliced;
}
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;
}
function pack_le($format, ...$n) {
$res = "";
foreach ($n as $key => $curn) {
if (BIG_ENDIAN) {
$res .= strrev(pack($format[$key], $curn));
} else $res .= pack($format[$key], $curn);
}
return $res;
}
function pack_be($format, ...$n) {
$res = "";
foreach ($n as $key => $curn) {
if (!BIG_ENDIAN) {
$res .= strrev(pack($format[$key], $curn));
} else $res .= pack($format[$key], $curn);
}
return $res;
}
function unpack_le($format, $data) {
if (BIG_ENDIAN) {
return unpack(strrev($format), strrev($data));
} else return unpack($format, $data);
}
function unpack_be($format, $data) {
if (!BIG_ENDIAN) {
return unpack($format, strrev($data));
} else return unpack($format, $data);
}
function vis($bs) {
$bs = bytearray($bs);
$symbols_in_one_line = 8;
$n = floor(strlen($bs) / $symbols_in_one_line);
$i = 0;
foreach (pyjslib_range($n) as $i) {
pyjslib_printnl(
join(pyjslib_str(($i * $symbols_in_one_line)) . ' | ' . ' ',
array_map(function($el) { return "%02X" % $el; }, array_slice($bs,$i*$symbols_in_one_line, ($i+1)*$symbols_in_one_line))
));
}
if (!(((strlen($bs) % $symbols_in_one_line) == 0))) {
pyjslib_printnl(join(pyjslib_str((($i + 1) * $symbols_in_one_line)) . ' | ' . ' ',
array_map(function($el) { return "%02X" % $el; }, array_slice($bs, ($i+1)*$symbols_in_one_line), null)
) . '
');
}
}
/**
* 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 = strlen($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;
}
function fread_all($handle) {
$pos = ftell($handle);
fseek($handle, 0);
$content = fread($handle, fstat($handle)["size"]);
fseek($handle, $pos);
return $content;
}
function fopen_and_write($filename, $mode, $data) {
$handle = fopen($filename, $mode);
fwrite($handle, $data);
rewind($handle);
return $handle;
}
/**
* 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(strlen($s)) as $i ) {
if (($s[$i] != $b('')[0])) {
break;
}
}
$s = array_slice($s, $i, null);
if (($blocksize > 0) && (strlen($s) % $blocksize)) {
$s = ((($blocksize - (strlen($s) % $blocksize)) * $b('')) + $s);
}
return $s;
}
/**
* Manages TCP Transport. encryption and message frames
*/
class Session {
function __construct($ip, $port, $auth_key = null, $server_salt = null) {
$this->sock = fsockopen("tcp://".$ip.":".$port);
$this->number = 0;
$this->timedelta = 0;

BIN
mtproto.php.save.1 Executable file

Binary file not shown.

View File

@ -89,7 +89,6 @@ class Session:
step1 = struct.pack('<II', len(message)+12, self.number) + message
step2 = step1 + struct.pack('<I', crc32(step1))
print "".join("{:02x}".format(ord(c)) for c in struct.pack('<I', crc32(message_data)))
self.sock.send(step2)
self.number += 1
@ -103,11 +102,11 @@ class Session:
raise Exception("Nothing in the socket!")
packet_length = struct.unpack("<I", packet_length_data)[0]
packet = self.sock.recv(packet_length - 4) # read the rest of bytes from socket
# check the CRC32
if not crc32(packet_length_data + packet[0:-4]) == struct.unpack('<I', packet[-4:])[0]:
raise Exception("CRC32 was not correct!")
x = struct.unpack("<I", packet[:4])
print x[0]
auth_key_id = packet[4:12]
if auth_key_id == b'\x00\x00\x00\x00\x00\x00\x00\x00':
# No encryption - Plain text
@ -141,7 +140,6 @@ class Session:
def create_auth_key(self):
nonce = os.urandom(16)
nonce = "a"
print("Requesting pq")
ResPQ = self.method_call('req_pq', nonce=nonce)