Update phpseclib, add support for socket streams, fix localization and other bugfixes

This commit is contained in:
Daniil Gentili 2017-10-05 18:53:56 +03:00
parent 94d186d90f
commit 5de64b4f61
10 changed files with 553 additions and 444 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
SocksProxy.php
magnabroadcast.php magnabroadcast.php
broadcast.php broadcast.php

View File

@ -12,13 +12,12 @@
"php": ">=5.6.0", "php": ">=5.6.0",
"danog/primemodule": "dev-master", "danog/primemodule": "dev-master",
"danog/magicalserializer": "dev-master", "danog/magicalserializer": "dev-master",
"phpseclib/phpseclib": "dev-master#200c2a9", "phpseclib/phpseclib": "dev-master#52c068ac",
"vlucas/phpdotenv": "^2.4", "vlucas/phpdotenv": "^2.4",
"erusev/parsedown": "^1.6", "erusev/parsedown": "^1.6",
"rollbar/rollbar": "~1.1", "rollbar/rollbar": "~1.1",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-curl": "*", "ext-curl": "*",
"ext-sockets": "*",
"ext-xml": "*" "ext-xml": "*"
}, },
"require-dev": { "require-dev": {
@ -44,7 +43,8 @@
"src/Volatile.php", "src/Volatile.php",
"src/Thread.php", "src/Thread.php",
"src/Worker.php", "src/Worker.php",
"src/Pool.php" "src/Pool.php",
"src/BigIntegor.php"
] ]
} }
} }

6
src/BigIntegor.php Normal file
View File

@ -0,0 +1,6 @@
<?php
namespace phpseclib\Math;
class BigIntegor {
}

View File

@ -11,6 +11,7 @@ If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!extension_loaded('pthreads')) { if (!extension_loaded('pthreads')) {
if (extension_loaded('sockets')) {
class Socket class Socket
{ {
private $sock; private $sock;
@ -106,4 +107,100 @@ if (!extension_loaded('pthreads')) {
return $port ? ['host' => $address, 'port' => $port] : ['host' => $address]; return $port ? ['host' => $address, 'port' => $port] : ['host' => $address];
} }
} }
} else {
define('AF_INET', 0);
define('AF_INET6', 1);
define('SOCK_STREAM', 2);
define('SOL_SOCKET', 3);
define('SO_RCVTIMEO', 4);
define('SO_SNDTIMEO', 5);
class Socket
{
private $sock;
private $protocol;
private $timeout = ['sec' => 0, 'usec' => 0];
private $blocking = false;
public function __construct(int $domain, int $type, int $protocol)
{
$this->protocol = getprotobynumber($protocol);
}
public function setOption(int $level, int $name, $value)
{
if (in_array($name, [\SO_RCVTIMEO, \SO_SNDTIMEO])) {
$this->timeout = ['sec' => (int) $value, 'usec' => (int) (($value - (int) $value) * 1000000)];
stream_set_timeout($this->sock, $this->timeout['sec'], $this->timeout['usec']);
return true;
}
throw new \danog\MadelineProto\Exception('Not supported');
}
public function getOption(int $level, int $name)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function setBlocking(bool $blocking)
{
return stream_set_blocking($this->sock, $blocking);
}
public function bind(string $address, int $port = 0)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function listen(int $backlog = 0)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function accept()
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function connect(string $address, int $port = 0)
{
$this->sock = fsockopen($this->protocol.'://'.$address, $port);
return true;
}
public function select(array &$read, array &$write, array &$except, int $tv_sec, int $tv_usec = 0)
{
return stream_select($read, $write, $except, $tv_sec, $tv_usec);
}
public function read(int $length, int $flags = 0)
{
return stream_get_contents($this->sock, $length);
}
public function write(string $buffer, int $length = -1)
{
return $length === -1 ? fwrite($this->sock, $buffer) : fwrite($this->sock, $buffer, $length);
}
public function send(string $data, int $length, int $flags)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function close()
{
return fclose($this->sock);
}
public function getPeerName(bool $port = true)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function getSockName(bool $port = true)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
}
}
} }

View File

@ -82,14 +82,14 @@ class Connection
if ($has_proxy && $this->extra !== []) { if ($has_proxy && $this->extra !== []) {
$this->sock->setExtra($this->extra); $this->sock->setExtra($this->extra);
} }
if (!$this->sock->connect($ip, $port)) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['socket_con_error']);
}
if (!\danog\MadelineProto\Logger::$has_thread) { if (!\danog\MadelineProto\Logger::$has_thread) {
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout); $this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout); $this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
} }
$this->sock->setBlocking(true); $this->sock->setBlocking(true);
if (!$this->sock->connect($ip, $port)) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['socket_con_error']);
}
$this->write(chr(239)); $this->write(chr(239));
break; break;
case 'tcp_intermediate': case 'tcp_intermediate':
@ -97,11 +97,11 @@ class Connection
if ($has_proxy && $this->extra !== []) { if ($has_proxy && $this->extra !== []) {
$this->sock->setExtra($this->extra); $this->sock->setExtra($this->extra);
} }
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock->connect($ip, $port)) { if (!$this->sock->connect($ip, $port)) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['socket_con_error']); throw new Exception(\danog\MadelineProto\Lang::$current_lang['socket_con_error']);
} }
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!\danog\MadelineProto\Logger::$has_thread) { if (!\danog\MadelineProto\Logger::$has_thread) {
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout); $this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout); $this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
@ -146,7 +146,7 @@ class Connection
$random[56] = $random[57] = $random[58] = $random[59] = chr(0xef); $random[56] = $random[57] = $random[58] = $random[59] = chr(0xef);
$reversed = strrev(substr($random, 8, 48)); $reversed = strrev(substr($random, 8, 48));
$this->obfuscated = ['encryption' => new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_CTR), 'decryption' => new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_CTR)]; $this->obfuscated = ['encryption' => new \phpseclib\Crypt\AES('ctr'), 'decryption' => new \phpseclib\Crypt\AES('ctr')];
$this->obfuscated['encryption']->enableContinuousBuffer(); $this->obfuscated['encryption']->enableContinuousBuffer();
$this->obfuscated['decryption']->enableContinuousBuffer(); $this->obfuscated['decryption']->enableContinuousBuffer();
@ -158,7 +158,7 @@ class Connection
$random = substr_replace( $random = substr_replace(
$random, $random,
substr( substr(
$this->obfuscated['encryption']->encrypt($random), @$this->obfuscated['encryption']->encrypt($random),
56, 56,
8 8
), ),
@ -254,7 +254,7 @@ class Connection
switch ($this->protocol) { switch ($this->protocol) {
case 'obfuscated2': case 'obfuscated2':
$what = $this->obfuscated['encryption']->encrypt($what); $what = @$this->obfuscated['encryption']->encrypt($what);
case 'tcp_abridged': case 'tcp_abridged':
case 'tcp_intermediate': case 'tcp_intermediate':
case 'tcp_full': case 'tcp_full':
@ -293,7 +293,7 @@ class Connection
throw new Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['wrong_length_read'], $length, strlen($packet))); throw new Exception(sprintf(\danog\MadelineProto\Lang::$current_lang['wrong_length_read'], $length, strlen($packet)));
} }
return $this->obfuscated['decryption']->encrypt($packet); return @$this->obfuscated['decryption']->encrypt($packet);
case 'tcp_abridged': case 'tcp_abridged':
case 'tcp_intermediate': case 'tcp_intermediate':

View File

@ -14,8 +14,9 @@ namespace danog\MadelineProto;
class Lang class Lang
{ {
public static $lang = [ public static $lang = array (
'it' => [ 'it' =>
array (
'phpseclib_fork' => 'Per favore installa questo fork di phpseclib: https://github.com/danog/phpseclib', 'phpseclib_fork' => 'Per favore installa questo fork di phpseclib: https://github.com/danog/phpseclib',
'inst_dc' => 'Istanziamento dei DataCenter...', 'inst_dc' => 'Istanziamento dei DataCenter...',
'load_rsa' => 'Caricamento delle chiavi RSA...', 'load_rsa' => 'Caricamento delle chiavi RSA...',
@ -151,13 +152,14 @@ class Lang
'resending_unsupported' => 'IL riinvio di messaggi non è ancora supportato', 'resending_unsupported' => 'IL riinvio di messaggi non è ancora supportato',
'unrecognized_dec_msg' => 'È stato ricevuto un messaggio decifrato sconosciuto: ', 'unrecognized_dec_msg' => 'È stato ricevuto un messaggio decifrato sconosciuto: ',
'serializing_madelineproto' => 'Sto serializzando MadelineProto...', 'serializing_madelineproto' => 'Sto serializzando MadelineProto...',
'req_pq' => 'Sto richiedendo p_q...', 'req_pq' => 'Sto richiedendo pq...',
'done' => 'Fatto!', 'done' => 'Fatto!',
'cdn_reupload' => 'Il file non è disponibile sul nostro CDN, richiedo la copia!', 'cdn_reupload' => 'Il file non è disponibile sul nostro CDN, richiedo la copia!',
'stored_on_cdn' => 'Il file è scaricabile tramite CDN!', 'stored_on_cdn' => 'Il file è scaricabile tramite CDN!',
], ),
'en' => [ 'en' =>
'req_pq' => 'Requesting p_q...', array (
'req_pq' => 'Requesting pq...',
'done' => 'Done!', 'done' => 'Done!',
'cdn_reupload' => 'File is not stored on CDN, requesting reupload!', 'cdn_reupload' => 'File is not stored on CDN, requesting reupload!',
'stored_on_cdn' => 'File is stored on CDN!', 'stored_on_cdn' => 'File is stored on CDN!',
@ -296,12 +298,12 @@ class Lang
'rand_bytes_too_short' => 'random_bytes is too short!', 'rand_bytes_too_short' => 'random_bytes is too short!',
'resending_unsupported' => 'Resending of messages is not yet supported', 'resending_unsupported' => 'Resending of messages is not yet supported',
'unrecognized_dec_msg' => 'Unrecognized decrypted message received: ', 'unrecognized_dec_msg' => 'Unrecognized decrypted message received: ',
], ),
]; );
// THIS WILL BE OVERWRITTEN BY $lang["en"] // THIS WILL BE OVERWRITTEN BY $lang["en"]
public static $current_lang = [ public static $current_lang = array (
'req_pq' => 'Requesting p_q...', 'req_pq' => 'Requesting pq...',
'done' => 'Done!', 'done' => 'Done!',
'cdn_reupload' => 'File is not stored on CDN, requesting reupload!', 'cdn_reupload' => 'File is not stored on CDN, requesting reupload!',
'stored_on_cdn' => 'File is stored on CDN!', 'stored_on_cdn' => 'File is stored on CDN!',
@ -440,5 +442,5 @@ class Lang
'rand_bytes_too_short' => 'random_bytes is too short!', 'rand_bytes_too_short' => 'random_bytes is too short!',
'resending_unsupported' => 'Resending of messages is not yet supported', 'resending_unsupported' => 'Resending of messages is not yet supported',
'unrecognized_dec_msg' => 'Unrecognized decrypted message received: ', 'unrecognized_dec_msg' => 'Unrecognized decrypted message received: ',
]; );
} }

View File

@ -46,7 +46,7 @@ class MTProto
/* /*
const V = 71; const V = 71;
*/ */
const V = 74; const V = 75;
const NOT_LOGGED_IN = 0; const NOT_LOGGED_IN = 0;
const WAITING_CODE = 1; const WAITING_CODE = 1;
@ -628,7 +628,7 @@ class MTProto
public function setup_logger() public function setup_logger()
{ {
\Rollbar\Rollbar::init(['environment' => 'production', 'root' => __DIR__, 'access_token' => (isset($this->settings['logger']['rollbar_token']) && !in_array($this->settings['logger']['rollbar_token'], ['f9fff6689aea4905b58eec73f66c791d', '300afd7ccef346ea84d0c185ae831718', '11a8c2fe4c474328b40a28193f8d63f5', 'beef2d426496462ba34dcaad33d44a14'])) || $this->settings['pwr']['pwr'] ? $this->settings['logger']['rollbar_token'] : 'c07d9b2f73c2461297b0beaef6c1662f'], false, false); @\Rollbar\Rollbar::init(['environment' => 'production', 'root' => __DIR__, 'access_token' => (isset($this->settings['logger']['rollbar_token']) && !in_array($this->settings['logger']['rollbar_token'], ['f9fff6689aea4905b58eec73f66c791d', '300afd7ccef346ea84d0c185ae831718', '11a8c2fe4c474328b40a28193f8d63f5', 'beef2d426496462ba34dcaad33d44a14'])) || $this->settings['pwr']['pwr'] ? $this->settings['logger']['rollbar_token'] : 'c07d9b2f73c2461297b0beaef6c1662f'], false, false);
\danog\MadelineProto\Logger::constructor($this->settings['logger']['logger'], $this->settings['logger']['logger_param'], isset($this->authorization['user']) ? (isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id']) : '', isset($this->settings['logger']['logger_level']) ? $this->settings['logger']['logger_level'] : Logger::VERBOSE); \danog\MadelineProto\Logger::constructor($this->settings['logger']['logger'], $this->settings['logger']['logger_param'], isset($this->authorization['user']) ? (isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id']) : '', isset($this->settings['logger']['logger_level']) ? $this->settings['logger']['logger_level'] : Logger::VERBOSE);
} }

View File

@ -29,28 +29,28 @@ trait Crypt
public function ige_encrypt($message, $key, $iv) public function ige_encrypt($message, $key, $iv)
{ {
$cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE); $cipher = new \phpseclib\Crypt\AES('ige');
$cipher->setKey($key); $cipher->setKey($key);
$cipher->setIV($iv); $cipher->setIV($iv);
return $cipher->encrypt($message); return @$cipher->encrypt($message);
} }
public function ctr_encrypt($message, $key, $iv) public function ctr_encrypt($message, $key, $iv)
{ {
$cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_CTR); $cipher = new \phpseclib\Crypt\AES('ctr');
$cipher->setKey($key); $cipher->setKey($key);
$cipher->setIV($iv); $cipher->setIV($iv);
return $cipher->encrypt($message); return @$cipher->encrypt($message);
} }
public function ige_decrypt($message, $key, $iv) public function ige_decrypt($message, $key, $iv)
{ {
$cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE); $cipher = new \phpseclib\Crypt\AES('ige');
$cipher->setKey($key); $cipher->setKey($key);
$cipher->setIV($iv); $cipher->setIV($iv);
return $cipher->decrypt($message); return @$cipher->decrypt($message);
} }
} }

View File

@ -64,6 +64,7 @@ class Serialization
$unserialized = file_get_contents($filename); $unserialized = file_get_contents($filename);
flock($lock, LOCK_UN); flock($lock, LOCK_UN);
fclose($lock); fclose($lock);
$tounserialize = str_replace('O:26:"danog\MadelineProto\Button":', 'O:35:"danog\MadelineProto\TL\Types\Button":', $unserialized); $tounserialize = str_replace('O:26:"danog\MadelineProto\Button":', 'O:35:"danog\MadelineProto\TL\Types\Button":', $unserialized);
foreach (['RSA', 'TL\TLMethod', 'TL\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\Types\Button', 'TL\Types\Bytes', 'APIFactory'] as $class) { foreach (['RSA', 'TL\TLMethod', 'TL\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\Types\Button', 'TL\Types\Bytes', 'APIFactory'] as $class) {
class_exists('\danog\MadelineProto\\'.$class); class_exists('\danog\MadelineProto\\'.$class);
@ -72,6 +73,7 @@ class Serialization
\danog\MadelineProto\Logger::class_exists(); \danog\MadelineProto\Logger::class_exists();
try { try {
// $unserialized = \danog\Serialization::unserialize($tounserialize);
$unserialized = unserialize($tounserialize); $unserialized = unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) { } catch (\danog\MadelineProto\Bug74586Exception $e) {
$unserialized = \danog\Serialization::unserialize($tounserialize); $unserialized = \danog\Serialization::unserialize($tounserialize);
@ -79,6 +81,7 @@ class Serialization
if (Logger::$constructed) { if (Logger::$constructed) {
Logger::log([(string) $e], Logger::ERROR); Logger::log([(string) $e], Logger::ERROR);
} }
if ($e->getMessage() === "Erroneous data format for unserializing 'phpseclib\Math\BigInteger'") $tounserialize = str_replace('phpseclib\Math\BigInteger', 'phpseclib\Math\BigIntegor', $unserialized);
$unserialized = \danog\Serialization::unserialize($tounserialize); $unserialized = \danog\Serialization::unserialize($tounserialize);
} }
if ($unserialized instanceof \danog\PlaceHolder) { if ($unserialized instanceof \danog\PlaceHolder) {

View File

@ -13,7 +13,7 @@ If not, see <http://www.gnu.org/licenses/>.
set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/../').':'.realpath(dirname(__FILE__).'/../MadelineProto/')); set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/../').':'.realpath(dirname(__FILE__).'/../MadelineProto/'));
chdir(dirname(__FILE__).'/../'); chdir(dirname(__FILE__).'/../');
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
//include 'SocksProxy.php';
if (!function_exists('readline')) { if (!function_exists('readline')) {
function readline($prompt = null) function readline($prompt = null)
{ {