Implement

This commit is contained in:
Daniil Gentili 2019-12-16 21:14:57 +01:00
parent 01a8e2dbf3
commit ee99686d97
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 65 additions and 126 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -1,41 +0,0 @@
<?php
/**
* TON public key module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
* @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;
}
}

View File

@ -1,61 +0,0 @@
<?php
/**
* TON public key module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
* @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;
}
}

View File

@ -1,11 +1,11 @@
{
"liteservers": [
{
"ip": 1137658550,
"port": 4924,
"ip": 861606190,
"port": 9999,
"id": {
"@type": "pub.ed25519",
"key": "peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M="
"key": "T10NJq2tgx6LpTHj734fSNYJ6S2w1hTdFRXJaj5st80"
}
}
],

23
ton/toncustom.json Normal file
View File

@ -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="
}
}
}