From 4a8826bdad64c8826f83b2574fecf989b10bb4c3 Mon Sep 17 00:00:00 2001 From: danogentili Date: Sun, 7 Aug 2016 23:23:10 +0200 Subject: [PATCH] Started to reorganize class --- Caddyfile | 5 - composer.json | 14 +- README.md => original_telepy/README.md | 0 TL.py => original_telepy/TL.py | 0 .../classes}/__init__.php | 0 .../classes}/__init__.py | 0 {classes => original_telepy/classes}/chat.php | 0 {classes => original_telepy/classes}/chat.py | 0 .../classes}/contact.php | 0 .../classes}/contact.py | 0 {classes => original_telepy/classes}/file.php | 0 {classes => original_telepy/classes}/file.py | 0 .../classes}/libpy2php | 0 .../classes}/message.php | 0 .../classes}/message.py | 0 .../classes}/shell.php | 0 {classes => original_telepy/classes}/shell.py | 0 .../classes}/telepy.php | 0 .../classes}/telepy.py | 0 {classes => original_telepy/classes}/user.php | 0 {classes => original_telepy/classes}/user.py | 0 crypt.py => original_telepy/crypt.py | 0 getpq.py => original_telepy/getpq.py | 0 mtproto.py => original_telepy/mtproto.py | 0 prime.py => original_telepy/prime.py | 0 telepy.py => original_telepy/telepy.py | 0 testing.py => original_telepy/testing.py | 0 {tests => original_telepy/tests}/SHA.php.tmp | 0 .../tests}/Serialization | 0 .../tests}/Serialization and SHA.py | 0 .../tests}/encrypt_decrypt_ige_test.py | 0 {tests => original_telepy/tests}/file.py | 0 {tests => original_telepy/tests}/ige.py | 0 .../tests}/research_decrypt_mode.py | 0 src/danog/MadelineProto/API.php | 20 ++ src/danog/MadelineProto/Exception.php | 14 ++ src/danog/MadelineProto/RSA.php | 0 TL.php => src/danog/MadelineProto/TL.php | 3 +- .../danog/MadelineProto/TL_schema.JSON | 0 .../danog/MadelineProto/aes256.php | 0 src/danog/MadelineProto/connection.php | 63 +++++++ .../danog/MadelineProto/crypt.php | 0 src/danog/MadelineProto/debug_functions.php | 39 ++++ .../MadelineProto/libpy2php}/libpy2php.php | 0 .../danog/MadelineProto/libpy2php}/os.php | 0 .../MadelineProto/libpy2php}/os_path.php | 0 .../MadelineProto/libpy2php}/strict_mode.php | 0 .../danog/MadelineProto/mtproto.php | 176 +++++------------- .../danog/MadelineProto/prime.php | 0 rsa.pub => src/danog/MadelineProto/rsa.pub | 0 .../danog/MadelineProto/telepy.php | 0 src/danog/MadelineProto/tools.php | 52 ++++++ testing.php | 16 +- 53 files changed, 250 insertions(+), 152 deletions(-) delete mode 100644 Caddyfile rename README.md => original_telepy/README.md (100%) rename TL.py => original_telepy/TL.py (100%) rename {classes => original_telepy/classes}/__init__.php (100%) rename {classes => original_telepy/classes}/__init__.py (100%) rename {classes => original_telepy/classes}/chat.php (100%) rename {classes => original_telepy/classes}/chat.py (100%) rename {classes => original_telepy/classes}/contact.php (100%) rename {classes => original_telepy/classes}/contact.py (100%) rename {classes => original_telepy/classes}/file.php (100%) rename {classes => original_telepy/classes}/file.py (100%) rename {classes => original_telepy/classes}/libpy2php (100%) rename {classes => original_telepy/classes}/message.php (100%) rename {classes => original_telepy/classes}/message.py (100%) rename {classes => original_telepy/classes}/shell.php (100%) rename {classes => original_telepy/classes}/shell.py (100%) rename {classes => original_telepy/classes}/telepy.php (100%) rename {classes => original_telepy/classes}/telepy.py (100%) rename {classes => original_telepy/classes}/user.php (100%) rename {classes => original_telepy/classes}/user.py (100%) rename crypt.py => original_telepy/crypt.py (100%) rename getpq.py => original_telepy/getpq.py (100%) rename mtproto.py => original_telepy/mtproto.py (100%) rename prime.py => original_telepy/prime.py (100%) rename telepy.py => original_telepy/telepy.py (100%) rename testing.py => original_telepy/testing.py (100%) rename {tests => original_telepy/tests}/SHA.php.tmp (100%) rename {tests => original_telepy/tests}/Serialization (100%) rename {tests => original_telepy/tests}/Serialization and SHA.py (100%) rename {tests => original_telepy/tests}/encrypt_decrypt_ige_test.py (100%) rename {tests => original_telepy/tests}/file.py (100%) rename {tests => original_telepy/tests}/ige.py (100%) rename {tests => original_telepy/tests}/research_decrypt_mode.py (100%) create mode 100644 src/danog/MadelineProto/API.php create mode 100644 src/danog/MadelineProto/Exception.php create mode 100644 src/danog/MadelineProto/RSA.php rename TL.php => src/danog/MadelineProto/TL.php (99%) rename TL_schema.JSON => src/danog/MadelineProto/TL_schema.JSON (100%) rename aes256.php => src/danog/MadelineProto/aes256.php (100%) create mode 100644 src/danog/MadelineProto/connection.php rename crypt.php => src/danog/MadelineProto/crypt.php (100%) create mode 100644 src/danog/MadelineProto/debug_functions.php rename {libpy2php => src/danog/MadelineProto/libpy2php}/libpy2php.php (100%) rename {libpy2php => src/danog/MadelineProto/libpy2php}/os.php (100%) rename {libpy2php => src/danog/MadelineProto/libpy2php}/os_path.php (100%) rename {libpy2php => src/danog/MadelineProto/libpy2php}/strict_mode.php (100%) rename mtproto.php => src/danog/MadelineProto/mtproto.php (79%) rename prime.php => src/danog/MadelineProto/prime.php (100%) rename rsa.pub => src/danog/MadelineProto/rsa.pub (100%) rename telepy.php => src/danog/MadelineProto/telepy.php (100%) create mode 100644 src/danog/MadelineProto/tools.php diff --git a/Caddyfile b/Caddyfile deleted file mode 100644 index 4a46d614..00000000 --- a/Caddyfile +++ /dev/null @@ -1,5 +0,0 @@ -localhost:80 { - fastcgi / 127.0.0.1:9001 { - index index.php - } -} diff --git a/composer.json b/composer.json index 9f5872a8..ff3906b8 100644 --- a/composer.json +++ b/composer.json @@ -2,18 +2,26 @@ "name": "danog/madelineproto", "description": "PHP implementation of telegram's MTProto protocol.", "type": "project", + "license": "MIT", + "minimum-stability": "stable", + "homepage": "https://daniil.it/MadelineProto", + "keywords": ["telegram", "mtproto", "protocol", "bytes", "telegram", "client", "PHP"], "require": { "danog/phpstruct": "^1.1", "phpseclib/phpseclib": "^2.0", "paragonie/constant_time_encoding": "^2.0", - "paragonie/random_compat": "^2.0" + "paragonie/random_compat": "^2.0", + "php": ">=5.6.0" }, - "license": "MIT", "authors": [ { "name": "Daniil Gentili", "email": "daniil@daniil.it" } ], - "minimum-stability": "stable" + "autoload": { + "psr-0": { + "danog\\MadelineProto\\": "src/" + } + } } diff --git a/README.md b/original_telepy/README.md similarity index 100% rename from README.md rename to original_telepy/README.md diff --git a/TL.py b/original_telepy/TL.py similarity index 100% rename from TL.py rename to original_telepy/TL.py diff --git a/classes/__init__.php b/original_telepy/classes/__init__.php similarity index 100% rename from classes/__init__.php rename to original_telepy/classes/__init__.php diff --git a/classes/__init__.py b/original_telepy/classes/__init__.py similarity index 100% rename from classes/__init__.py rename to original_telepy/classes/__init__.py diff --git a/classes/chat.php b/original_telepy/classes/chat.php similarity index 100% rename from classes/chat.php rename to original_telepy/classes/chat.php diff --git a/classes/chat.py b/original_telepy/classes/chat.py similarity index 100% rename from classes/chat.py rename to original_telepy/classes/chat.py diff --git a/classes/contact.php b/original_telepy/classes/contact.php similarity index 100% rename from classes/contact.php rename to original_telepy/classes/contact.php diff --git a/classes/contact.py b/original_telepy/classes/contact.py similarity index 100% rename from classes/contact.py rename to original_telepy/classes/contact.py diff --git a/classes/file.php b/original_telepy/classes/file.php similarity index 100% rename from classes/file.php rename to original_telepy/classes/file.php diff --git a/classes/file.py b/original_telepy/classes/file.py similarity index 100% rename from classes/file.py rename to original_telepy/classes/file.py diff --git a/classes/libpy2php b/original_telepy/classes/libpy2php similarity index 100% rename from classes/libpy2php rename to original_telepy/classes/libpy2php diff --git a/classes/message.php b/original_telepy/classes/message.php similarity index 100% rename from classes/message.php rename to original_telepy/classes/message.php diff --git a/classes/message.py b/original_telepy/classes/message.py similarity index 100% rename from classes/message.py rename to original_telepy/classes/message.py diff --git a/classes/shell.php b/original_telepy/classes/shell.php similarity index 100% rename from classes/shell.php rename to original_telepy/classes/shell.php diff --git a/classes/shell.py b/original_telepy/classes/shell.py similarity index 100% rename from classes/shell.py rename to original_telepy/classes/shell.py diff --git a/classes/telepy.php b/original_telepy/classes/telepy.php similarity index 100% rename from classes/telepy.php rename to original_telepy/classes/telepy.php diff --git a/classes/telepy.py b/original_telepy/classes/telepy.py similarity index 100% rename from classes/telepy.py rename to original_telepy/classes/telepy.py diff --git a/classes/user.php b/original_telepy/classes/user.php similarity index 100% rename from classes/user.php rename to original_telepy/classes/user.php diff --git a/classes/user.py b/original_telepy/classes/user.py similarity index 100% rename from classes/user.py rename to original_telepy/classes/user.py diff --git a/crypt.py b/original_telepy/crypt.py similarity index 100% rename from crypt.py rename to original_telepy/crypt.py diff --git a/getpq.py b/original_telepy/getpq.py similarity index 100% rename from getpq.py rename to original_telepy/getpq.py diff --git a/mtproto.py b/original_telepy/mtproto.py similarity index 100% rename from mtproto.py rename to original_telepy/mtproto.py diff --git a/prime.py b/original_telepy/prime.py similarity index 100% rename from prime.py rename to original_telepy/prime.py diff --git a/telepy.py b/original_telepy/telepy.py similarity index 100% rename from telepy.py rename to original_telepy/telepy.py diff --git a/testing.py b/original_telepy/testing.py similarity index 100% rename from testing.py rename to original_telepy/testing.py diff --git a/tests/SHA.php.tmp b/original_telepy/tests/SHA.php.tmp similarity index 100% rename from tests/SHA.php.tmp rename to original_telepy/tests/SHA.php.tmp diff --git a/tests/Serialization b/original_telepy/tests/Serialization similarity index 100% rename from tests/Serialization rename to original_telepy/tests/Serialization diff --git a/tests/Serialization and SHA.py b/original_telepy/tests/Serialization and SHA.py similarity index 100% rename from tests/Serialization and SHA.py rename to original_telepy/tests/Serialization and SHA.py diff --git a/tests/encrypt_decrypt_ige_test.py b/original_telepy/tests/encrypt_decrypt_ige_test.py similarity index 100% rename from tests/encrypt_decrypt_ige_test.py rename to original_telepy/tests/encrypt_decrypt_ige_test.py diff --git a/tests/file.py b/original_telepy/tests/file.py similarity index 100% rename from tests/file.py rename to original_telepy/tests/file.py diff --git a/tests/ige.py b/original_telepy/tests/ige.py similarity index 100% rename from tests/ige.py rename to original_telepy/tests/ige.py diff --git a/tests/research_decrypt_mode.py b/original_telepy/tests/research_decrypt_mode.py similarity index 100% rename from tests/research_decrypt_mode.py rename to original_telepy/tests/research_decrypt_mode.py diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php new file mode 100644 index 00000000..1187b0ee --- /dev/null +++ b/src/danog/MadelineProto/API.php @@ -0,0 +1,20 @@ +session = new Session($params); + $this->session->create_auth_key(); + $future_salts = $this->session->method_call('get_future_salts', 3); + pyjslib_printnl($future_salts); + } + + public function __destruct() + { + unset($this->session); + } + public function __call($name, $arguments) { + return $session->method_call($name, $arguments); + } +} \ No newline at end of file diff --git a/src/danog/MadelineProto/Exception.php b/src/danog/MadelineProto/Exception.php new file mode 100644 index 00000000..3342995c --- /dev/null +++ b/src/danog/MadelineProto/Exception.php @@ -0,0 +1,14 @@ +sock = fsockopen('tcp://'.$ip.':'.$port); + $this->protocol = "tcp"; + stream_set_timeout($this->sock, 5); + if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) { + throw new Exception("Connection: couldn't connect to socket."); + } + break; + default: + throw new Exception("Connection: invalid protocol specified."); + break; + } + } + public function __destruct() { + switch ($this->protocol) { + case 'tcp': + fclose($this->sock); + break; + default: + throw new Exception("Connection: invalid protocol specified."); + break; + } + + } + public function write($what, $length = null) { + $what = substr($what, 0, $length); + switch ($this->protocol) { + case 'tcp': + if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) { + throw new Exception("Connection: couldn't connect to socket."); + } + return fwrite($this->sock, $what); + break; + default: + throw new Exception("Connection: invalid protocol specified."); + break; + } + } + public function read($length) { + switch ($this->protocol) { + case 'tcp': + if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) { + throw new Exception("Connection: couldn't connect to socket."); + } + return fread($this->sock, $length); + break; + default: + throw new Exception("Connection: invalid protocol specified."); + break; + } + } +} \ No newline at end of file diff --git a/crypt.php b/src/danog/MadelineProto/crypt.php similarity index 100% rename from crypt.php rename to src/danog/MadelineProto/crypt.php diff --git a/src/danog/MadelineProto/debug_functions.php b/src/danog/MadelineProto/debug_functions.php new file mode 100644 index 00000000..1f8f6074 --- /dev/null +++ b/src/danog/MadelineProto/debug_functions.php @@ -0,0 +1,39 @@ +sock = fsockopen('tcp://'.$ip.':'.$port); - if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) { - throw new Exception("Couldn't connect to socket."); + // Set default settings + $default_settings = ["ip" => "149.154.167.50", "port" => "443", "protocol" => "tcp", "auth_key" => null, "server_salt" => null, "api_id" => 25628, "api_hash" => "1fe17cda7d355166cdaa71f04122873c", "tl_schema" => 'https://core.telegram.org/schema/mtproto-json', "rsa_pub" => __DIR__.'/rsa.pub']; + foreach ($default_settings as $key => $param) { + if(!isset($settings[$key])) { + $settings[$key] = $param; + } } - $this->number = 0; - $this->timedelta = 0; - $this->session_id = \phpseclib\Crypt\Random::string(8); - $this->auth_key = $auth_key; - $this->auth_key_id = $this->auth_key ? substr(sha1($this->auth_key, true), -8) : null; - stream_set_timeout($this->sock, 5); - $this->MAX_RETRY = 5; - $this->AUTH_MAX_RETRY = 5; + $this->settings = $settings; + + // Connect to servers + $this->sock = new Connection($this->settings["ip_address"], $this->settings["ip_address"], $this->settings["protocol"]); + + // Istantiate struct class $this->struct = new \danog\PHP\Struct(); + // Istantiate prime class $this->PrimeModule = new PrimeModule(); + // Istantiate TL class try { - $this->tl = new TL('https://core.telegram.org/schema/mtproto-json'); + $this->tl = new TL($this->settings["tl_schema"]); } catch (Exception $e) { $this->tl = new TL(__DIR__.'/TL_schema.JSON'); } + // Load rsa key + $this->settings["rsa_content"] = file_get_contents($this->rsa_pub); + + // Set some defaults + $this->number = 0; + $this->timedelta = 0; + $this->session_id = \phpseclib\Crypt\Random::string(8); + if(isset($this->settings["auth_key"])) $this->auth_key = $this->settings["auth_key"]; + $this->auth_key_id = $this->auth_key ? substr(sha1($this->auth_key, true), -8) : null; + $this->MAX_RETRY = 5; + $this->AUTH_MAX_RETRY = 5; } public function __destruct() { - fclose($this->sock); + unset($this->sock); + } + /** + * Function to get hex crc32 + * @param $data Data to encode. + */ + function newcrc32($data) + { + return hexdec(hash('crc32b', $data)); } /** @@ -166,7 +81,7 @@ class Session $message = $this->auth_key_id.$message_key.crypt::ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv); } $step1 = $this->struct->pack('number).$message; - $step2 = $step1.$this->struct->pack('struct->pack('newcrc32($step1)); fwrite($this->sock, $step2); $this->number += 1; } @@ -177,13 +92,13 @@ class Session public function recv_message() { $packet_length_data = fread($this->sock, 4); - if (len($packet_length_data) < 4) { + if (strlen($packet_length_data) < 4) { throw new Exception('Nothing in the socket!'); } $packet_length = $this->struct->unpack('sock, ($packet_length - 4)); - if (!(newcrc32($packet_length_data.substr($packet, 0, -4)) == $this->struct->unpack('newcrc32($packet_length_data.substr($packet, 0, -4)) == $this->struct->unpack('struct->unpack('load($f); + $key->load($settings["rsa_content"]); // Make pq request $nonce = \phpseclib\Crypt\Random::string(16); diff --git a/prime.php b/src/danog/MadelineProto/prime.php similarity index 100% rename from prime.php rename to src/danog/MadelineProto/prime.php diff --git a/rsa.pub b/src/danog/MadelineProto/rsa.pub similarity index 100% rename from rsa.pub rename to src/danog/MadelineProto/rsa.pub diff --git a/telepy.php b/src/danog/MadelineProto/telepy.php similarity index 100% rename from telepy.php rename to src/danog/MadelineProto/telepy.php diff --git a/src/danog/MadelineProto/tools.php b/src/danog/MadelineProto/tools.php new file mode 100644 index 00000000..3b68c8f4 --- /dev/null +++ b/src/danog/MadelineProto/tools.php @@ -0,0 +1,52 @@ +create_auth_key(); -$future_salts = $Session->method_call('get_future_salts', 3); -pyjslib_printnl($future_salts); +$MadelineProto = new \danog\MadelineProto\API("393888288264", $config);