diff --git a/src/danog/MadelineProto/Stream/ADNLTransport/ADNLStream.php b/src/danog/MadelineProto/Stream/ADNLTransport/ADNLStream.php index 518490f6..4fdfd8df 100644 --- a/src/danog/MadelineProto/Stream/ADNLTransport/ADNLStream.php +++ b/src/danog/MadelineProto/Stream/ADNLTransport/ADNLStream.php @@ -71,10 +71,11 @@ class ADNLStream implements BufferedStreamInterface, MTProtoBufferInterface */ public function getWriteBufferGenerator(int $length, string $append = ''): \Generator { - $buffer = yield $this->stream->getWriteBuffer($length + 68, $append); + $length += 64; + $buffer = yield $this->stream->getWriteBuffer($length + 4, $append); yield $buffer->bufferWrite(\pack('V', $length)); $this->stream->startWriteHash(); - $this->stream->checkWriteHash($length + 32); + $this->stream->checkWriteHash($length - 32); yield $buffer->bufferWrite(Tools::random(32)); return $buffer; @@ -90,8 +91,8 @@ class ADNLStream implements BufferedStreamInterface, MTProtoBufferInterface public function getReadBufferGenerator(&$length): \Generator { $buffer = yield $this->stream->getReadBuffer($l); - $this->stream->startReadHash(); $length = \unpack('V', yield $buffer->bufferRead(4))[1] - 32; + $this->stream->startReadHash(); $this->stream->checkReadHash($length); yield $buffer->bufferRead(32); $length -= 32; diff --git a/src/danog/MadelineProto/Stream/Common/CtrStream.php b/src/danog/MadelineProto/Stream/Common/CtrStream.php index 1e44fd0c..c5ae6e13 100644 --- a/src/danog/MadelineProto/Stream/Common/CtrStream.php +++ b/src/danog/MadelineProto/Stream/Common/CtrStream.php @@ -108,7 +108,7 @@ class CtrStream implements BufferedProxyStreamInterface, BufferInterface */ public function getReadBufferGenerator(&$length): \Generator { - $this->read_buffer = yield $this->stream->getReadBuffer($l); + $this->read_buffer = yield $this->stream->getReadBuffer($length); return $this; } diff --git a/src/danog/MadelineProto/Stream/Common/HashedBufferedStream.php b/src/danog/MadelineProto/Stream/Common/HashedBufferedStream.php index 3ecf7ee1..63d7e932 100644 --- a/src/danog/MadelineProto/Stream/Common/HashedBufferedStream.php +++ b/src/danog/MadelineProto/Stream/Common/HashedBufferedStream.php @@ -235,13 +235,13 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf */ public function getReadBufferGenerator(&$length): \Generator { - if ($this->read_hash) { + //if ($this->read_hash) { $this->read_buffer = yield $this->stream->getReadBuffer($length); return $this; - } + //} - return yield $this->stream->getReadBuffer($length); + //return yield $this->stream->getReadBuffer($length); } /** @@ -253,13 +253,13 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf */ public function getWriteBufferGenerator(int $length, string $append = ''): \Generator { - if ($this->write_hash) { + //if ($this->write_hash) { $this->write_buffer = yield $this->stream->getWriteBuffer($length, $append); return $this; - } + //} - return yield $this->stream->getWriteBuffer($length, $append); + //return yield $this->stream->getWriteBuffer($length, $append); } /** @@ -290,7 +290,7 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf public function bufferWrite(string $data): Promise { if ($this->write_hash === null) { - return $this->write_buffer->bufferWrite($length); + return $this->write_buffer->bufferWrite($data); } $length = \strlen($data); diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index c4cbfd64..f047baa8 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -223,9 +223,16 @@ class TL $name = \preg_replace(['/#.*/', '/\\s.*/'], '', $line); if (\in_array($name, ['bytes', 'int128', 'int256', 'int512', 'int', 'long', 'double', 'string', 'bytes', 'object', 'function'])) { + /*if (!(\in_array($scheme_type, ['ton_api', 'lite_api']) && $name === 'bytes')) { + continue; + }*/ continue; } - $clean = \preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $line); + if (\in_array($scheme_type, ['ton_api', 'lite_api'])) { + $clean = \preg_replace(['/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/{/', '/}/'], ['', ' ', '', ' ', ' ', ' ', '', '', '', ''], $line); + } else { + $clean = \preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $line); + } $id = \hash('crc32b', $clean); if (\preg_match('/^[^\s]+#([a-f0-9]*)/i', $line, $matches)) { @@ -241,7 +248,7 @@ class TL $dparams = []; } $TL_dict[$type][$key][$type === 'constructors' ? 'predicate' : 'method'] = $name; - $TL_dict[$type][$key]['id'] = \strrev(\hex2bin($id)); + $TL_dict[$type][$key]['id'] = $a = \strrev(\hex2bin($id)); $TL_dict[$type][$key]['params'] = []; $TL_dict[$type][$key]['type'] = \preg_replace(['/.+\\s+=\\s+/', '/;/'], '', $line); if ($layer !== null) { diff --git a/src/danog/MadelineProto/TON/ADNLConnection.php b/src/danog/MadelineProto/TON/ADNLConnection.php index 15cba2fc..b1fb1033 100644 --- a/src/danog/MadelineProto/TON/ADNLConnection.php +++ b/src/danog/MadelineProto/TON/ADNLConnection.php @@ -24,6 +24,7 @@ use danog\MadelineProto\Magic; use danog\MadelineProto\MTProtoTools\Crypt; use danog\MadelineProto\Stream\ADNLTransport\ADNLStream; use danog\MadelineProto\Stream\Common\BufferedRawStream; +use danog\MadelineProto\Stream\Common\CtrStream; use danog\MadelineProto\Stream\Common\HashedBufferedStream; use danog\MadelineProto\Stream\ConnectionContext; use danog\MadelineProto\Stream\MTProtoTransport\ObfuscatedStream; @@ -77,6 +78,7 @@ class ADNLConnection } $random = Tools::random(256 - 32 - 64); + //$random = strrev(hex2bin(strrev('9E7C27765D12CE634414F0875D55CE5C58E7A9D58CD45C57CAB516D1241B7864691E5B0AFC4ECB54BFF2CEFC2060F1D45F5B5DEB76A9EF6471D75816AAAEC83CD7DE39EE99B9E980B6C0D4565A916D00908613E63657D5539118F89A14FD73ABB8ECD3AC26C287EEBD0FA44F52B315F01DD60F486EFF4C5B4D71EA6F443358FF141E7294BBBB5D7C079F16BD46C28A12507E1948722E7121B94C3B5C7832ADE7'))); $s1 = \substr($random, 0, 32); $s2 = \substr($random, 32, 32); $v1 = \substr($random, 64, 16); @@ -120,8 +122,6 @@ class ADNLConnection // Generate secret $secret = DH::computeSecret($private, $peerPublic); - var_dumP($private, $peerPublic, bin2hex($secret)); - // Encrypting random with obf keys $digest = \hash('sha256', $random, true); @@ -136,8 +136,6 @@ class ADNLConnection $payload .= $digest; $payload .= $encryptedRandom; - \var_dump(bin2hex($payload)); - $ip = \long2ip(\unpack('V', Tools::packSignedInt($endpoint['ip']))[1]); $port = $endpoint['port']; $ctx = (new ConnectionContext()) @@ -145,19 +143,20 @@ class ADNLConnection ->setUri("tcp://$ip:$port") ->addStream(DefaultStream::getName()) ->addStream(BufferedRawStream::getName()) - ->addStream(ObfuscatedStream::getName(), $obf) + ->addStream(CtrStream::getName(), $obf) ->addStream(HashedBufferedStream::getName(), 'sha256') ->addStream(ADNLStream::getName()); $this->stream = yield $ctx->getStream($payload); - \var_dump("Connected"); Tools::callFork((function () { - yield Tools::sleep(2); + //yield Tools::sleep(1); while (true) { $buffer = yield $this->stream->getReadBuffer($length); \var_dump($length, "GOT PACKET WITH LENGTH $length"); - \var_dump($length, yield $buffer->bufferRead($length)); + if ($length) { + \var_dump($length, yield $buffer->bufferRead($length)); + } } })()); } @@ -171,7 +170,18 @@ class ADNLConnection */ public function send(array $payload): \Generator { + var_dumP("Sending moar"); $data = yield $this->TL->serializeMethod($payload['_'], $payload); + $data = yield $this->TL->serializeObject( + ['type' => ''], + [ + '_' => 'adnl.message.query', + 'query_id' => Tools::random(32), + 'query' => $data + ], + '' + ); + var_dump(unpack('V*', $data)); (yield $this->stream->getWriteBuffer(\strlen($data)))->bufferWrite($data); } } diff --git a/src/danog/MadelineProto/TON/PrivateKey.php b/src/danog/MadelineProto/TON/PrivateKey.php deleted file mode 100644 index e81488ea..00000000 --- a/src/danog/MadelineProto/TON/PrivateKey.php +++ /dev/null @@ -1,41 +0,0 @@ -. - * - * @author Daniil Gentili - * @copyright 2016-2019 Daniil Gentili - * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 - * - * @link https://docs.madelineproto.xyz MadelineProto documentation - */ - -namespace danog\MadelineProto\TON; - -use phpseclib3\Crypt\EC\Curves\Curve25519; -use phpseclib3\Crypt\EC\PrivateKey as ECPrivateKey; - -class PrivateKey extends ECPrivateKey -{ - public static function load($key, $password = false) - { - self::initialize_static_variables(); - - $components = false; - - $curve = new Curve25519; - $peerPublic = Common::extractPoint($key, $curve); - - $components['format'] = 'TON'; - $components['curve'] = $curve; - - $new = static::onLoad($components); - return $new; - } -} \ No newline at end of file diff --git a/src/danog/MadelineProto/TON/PublicKey.php b/src/danog/MadelineProto/TON/PublicKey.php deleted file mode 100644 index 50c46ec7..00000000 --- a/src/danog/MadelineProto/TON/PublicKey.php +++ /dev/null @@ -1,61 +0,0 @@ -. - * - * @author Daniil Gentili - * @copyright 2016-2019 Daniil Gentili - * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 - * - * @link https://docs.madelineproto.xyz MadelineProto documentation - */ - -namespace danog\MadelineProto\TON; - -use danog\MadelineProto\Magic; -use danog\MadelineProto\Tools; -use phpseclib3\Crypt\EC\Curves\Curve25519; -use phpseclib3\Crypt\EC\Formats\Keys\Common; -use phpseclib3\Crypt\EC\Formats\Keys\MontgomeryPublic; -use phpseclib3\Crypt\EC\PublicKey as PPublicKey; -use phpseclib3\Math\BigInteger; - -class PublicKey extends PPublicKey -{ - public static function load($key, $password = false) - { - self::initialize_static_variables(); - - $components = false; - - // Transpose - $key[31] = $key[31] & chr(127); - - $curve = new Curve25519; - $modulo = Tools::getVar($curve, "modulo"); - $y = new BigInteger(strrev($key), 256); - $y2 = clone $y; - $y = $y->add(Magic::$one); - $y2 = $y2->subtract(Magic::$one); - $y2 = $modulo->subtract($y2)->powMod(Magic::$one, $modulo); - - $y2 = $y2->modInverse($modulo); - - $key = $y->multiply($y2)->powMod(Magic::$one, $modulo)->toBytes(); - - $components = MontgomeryPublic::load($key); - - $components['format'] = 'TON'; - $components['curve'] = $curve; - - $new = static::onLoad($components); - return $new; - } -} diff --git a/ton/ton-lite-client-test1.config.json b/ton/ton-lite-client-test1.config.json index 47ecdb08..3d34832e 100644 --- a/ton/ton-lite-client-test1.config.json +++ b/ton/ton-lite-client-test1.config.json @@ -1,11 +1,11 @@ { "liteservers": [ { - "ip": 1137658550, - "port": 4924, + "ip": 861606190, + "port": 9999, "id": { "@type": "pub.ed25519", - "key": "peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M=" + "key": "T10NJq2tgx6LpTHj734fSNYJ6S2w1hTdFRXJaj5st80" } } ], diff --git a/ton/toncustom.json b/ton/toncustom.json new file mode 100644 index 00000000..329a0cf5 --- /dev/null +++ b/ton/toncustom.json @@ -0,0 +1,23 @@ +{ + "liteservers": [ + { + "ip": 1137658550, + "port": 4924, + "id": { + "@type": "pub.ed25519", + "key": "peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M=" + } + } + ], + "validator": { + "@type": "validator.config.global", + "zero_state": { + "workchain": -1, + "shard": -9223372036854775808, + "seqno": 0, + "root_hash": "F6OpKZKqvqeFp6CQmFomXNMfMj2EnaUSOXN+Mh+wVWk=", + "file_hash": "XplPz01CXAps5qeSWUtxcyBfdAo5zVb1N979KLSKD24=" + } + } + } + \ No newline at end of file