1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 05:34:42 +01:00

Start implementing TON ADNL

This commit is contained in:
Daniil Gentili 2019-12-14 13:58:04 +01:00
parent 5d70ae57e6
commit 45d12b6a9d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
16 changed files with 259 additions and 12 deletions

View File

@ -128,7 +128,7 @@ class API extends InternalDoc
try {
$unserialized = \danog\Serialization::unserialize($tounserialize);
} catch (\Throwable $e) {
$unserialized = unserialize($tounserialize);
$unserialized = \unserialize($tounserialize);
}
} catch (\Throwable $e) {
Logger::log((string) $e, Logger::ERROR);

View File

@ -0,0 +1,126 @@
<?php
/**
* TCP Intermediate stream wrapper.
*
* 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\Stream\MTProtoTransport;
use Amp\Promise;
use Amp\Socket\EncryptableSocket;
use danog\MadelineProto\Stream\Async\BufferedStream;
use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
use danog\MadelineProto\Tools;
/**
* TCP Intermediate stream wrapper.
*
* Manages obfuscated2 encryption/decryption
*
* @author Daniil Gentili <daniil@daniil.it>
*/
class ADNLStream implements BufferedStreamInterface, MTProtoBufferInterface
{
use BufferedStream;
private $stream;
/**
* Connect to stream.
*
* @param ConnectionContext $ctx The connection context
*
* @return \Generator
*/
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->stream = yield $ctx->getStream($header);
}
/**
* Async close.
*
* @return Promise
*/
public function disconnect()
{
return $this->stream->disconnect();
}
/**
* Get write buffer asynchronously.
*
* @param int $length Length of data that is going to be written to the write buffer
*
* @return Generator
*/
public function getWriteBufferGenerator(int $length, string $append = ''): \Generator
{
$buffer = yield $this->stream->getWriteBuffer($length + 68, $append);
yield $buffer->bufferWrite(\pack('V', $length));
$this->stream->startWriteHash();
$this->stream->checkWriteHash($length + 32);
yield $buffer->bufferWrite(Tools::random(32));
return $buffer;
}
/**
* Get read buffer asynchronously.
*
* @param int $length Length of payload, as detected by this layer
*
* @return Generator
*/
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->checkReadHash($length);
yield $buffer->bufferRead(32);
$length -= 32;
return $buffer;
}
/**
* {@inheritdoc}
*
* @return EncryptableSocket
*/
public function getSocket(): EncryptableSocket
{
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{
return __CLASS__;
}
}

View File

@ -53,4 +53,10 @@ interface BufferedStreamInterface extends StreamInterface
* @return string
*/
public static function getName(): string;
/**
* Get underlying stream resource.
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface;
}

View File

@ -27,7 +27,6 @@ use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\BufferInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\RawStreamInterface;
use function Amp\Socket\connect;
/**
* Buffered raw stream.
@ -236,6 +235,15 @@ class BufferedRawStream implements BufferedStreamInterface, BufferInterface, Raw
{
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
/**
* Get class name.

View File

@ -26,7 +26,7 @@ use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\BufferInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\RawStreamInterface;
use danog\MadelineProto\Stream\StreamInterface;
use phpseclib3\Crypt\AES;
/**
@ -155,7 +155,7 @@ class CtrStream implements BufferedProxyStreamInterface, BufferInterface
}
/**
* Set obfuscation keys/IVs
* Set obfuscation keys/IVs.
*
* @param array $data Keys
*
@ -176,7 +176,12 @@ class CtrStream implements BufferedProxyStreamInterface, BufferInterface
return $this->stream->getSocket();
}
public function getPlainStream(): RawStreamInterface
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}

View File

@ -20,11 +20,12 @@ namespace danog\MadelineProto\Stream\Common;
use Amp\Promise;
use danog\MadelineProto\Stream\Async\BufferedStream;
use danog\MadelineProto\Stream\Async\Stream;
use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\BufferInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* Hash stream wrapper.
*
@ -203,7 +204,7 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf
*
* @return \Generator
*/
public function connectGenerator(ConnectionContext $ctx): \Generator
public function connectGenerator(ConnectionContext $ctx, string $header = ''): \Generator
{
$this->write_hash = null;
$this->write_check_after = 0;
@ -212,7 +213,7 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf
$this->read_check_after = 0;
$this->read_check_pos = 0;
$this->stream = yield $ctx->getStream();
$this->stream = yield $ctx->getStream($header);
}
/**
@ -321,6 +322,15 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{
return __CLASS__;

View File

@ -58,6 +58,15 @@ class SimpleBufferedRawStream extends BufferedRawStream implements BufferedStrea
return \fread($this->memory_stream, $length);
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
/**
* Get class name.

View File

@ -24,6 +24,7 @@ use danog\MadelineProto\Stream\Async\BufferedStream;
use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* Abridged stream wrapper.
@ -108,6 +109,15 @@ class AbridgedStream implements BufferedStreamInterface, MTProtoBufferInterface
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{
return __CLASS__;

View File

@ -25,6 +25,7 @@ use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\Common\HashedBufferedStream;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* TCP full stream wrapper.
@ -118,6 +119,15 @@ class FullStream implements BufferedStreamInterface, MTProtoBufferInterface
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{
return __CLASS__;

View File

@ -25,6 +25,7 @@ use danog\MadelineProto\Stream\Async\BufferedStream;
use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* HTTP stream wrapper.
@ -190,6 +191,15 @@ class HttpStream implements MTProtoBufferInterface, BufferedProxyStreamInterface
{
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{

View File

@ -21,6 +21,8 @@ namespace danog\MadelineProto\Stream\MTProtoTransport;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* HTTPS stream wrapper.
*
@ -40,6 +42,15 @@ class HttpsStream extends HttpStream implements MTProtoBufferInterface
return parent::connectGenerator($ctx->getCtx()->secure(true), $header);
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{
return __CLASS__;

View File

@ -25,6 +25,8 @@ use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* TCP Intermediate stream wrapper.
*
@ -99,6 +101,15 @@ class IntermediatePaddedStream implements BufferedStreamInterface, MTProtoBuffer
{
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{

View File

@ -25,6 +25,8 @@ use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* TCP Intermediate stream wrapper.
*
@ -98,6 +100,15 @@ class IntermediateStream implements BufferedStreamInterface, MTProtoBufferInterf
{
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string

View File

@ -18,13 +18,12 @@
namespace danog\MadelineProto\Stream\MTProtoTransport;
use Amp\Promise;
use Amp\Socket\EncryptableSocket;
use danog\MadelineProto\Stream\Async\Stream;
use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\Common\CtrStream;
use danog\MadelineProto\Stream\ConnectionContext;
/**
* Obfuscated2 stream wrapper.
*
@ -92,7 +91,7 @@ class ObfuscatedStream extends CtrStream implements BufferedProxyStreamInterface
$random = \substr_replace($random, \substr(@$this->getEncryptor()->encrypt($random), 56, 8), 56, 8);
yield $this->getPlainStream()->write($random);
yield $this->getStream()->write($random);
}
@ -116,7 +115,6 @@ class ObfuscatedStream extends CtrStream implements BufferedProxyStreamInterface
$this->extra = $extra;
}
public static function getName(): string
{
return __CLASS__;

View File

@ -26,6 +26,8 @@ use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\RawProxyStreamInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* HTTP proxy stream wrapper.
*
@ -216,6 +218,15 @@ class HttpProxy implements RawProxyStreamInterface, BufferedProxyStreamInterface
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
public static function getName(): string
{
return __CLASS__;

View File

@ -26,6 +26,8 @@ use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\RawProxyStreamInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
/**
* Socks5 stream wrapper.
*
@ -219,6 +221,15 @@ class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterfac
{
$this->extra = $extra;
}
/**
* {@inheritDoc}
*
* @return RawStreamInterface
*/
public function getStream(): RawStreamInterface
{
return $this->stream;
}
/**
* {@inheritdoc}