Reduce overhead

This commit is contained in:
Daniil Gentili 2019-12-14 12:10:09 +01:00
parent 7b2b69e97d
commit 9f4e245c8d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -32,7 +32,7 @@ use danog\MadelineProto\Stream\ConnectionContext;
*
* @author Daniil Gentili <daniil@daniil.it>
*/
class ObfuscatedStream implements BufferedProxyStreamInterface
class ObfuscatedStream extends CtrStream implements BufferedProxyStreamInterface
{
use Stream;
@ -76,8 +76,8 @@ class ObfuscatedStream implements BufferedProxyStreamInterface
$iv = \substr($random, 40, 16);
$ivRev = \substr($reversed, 40, 16);
$this->stream = new CtrStream;
$this->stream->setExtra([
parent::setExtra(
[
'encrypt' => [
'key' => $key,
'iv' => $iv
@ -86,47 +86,13 @@ class ObfuscatedStream implements BufferedProxyStreamInterface
'key' => $keyRev,
'iv' => $ivRev
]
]);
yield $this->stream->connect($ctx);
]
);
yield from parent::connectGenerator($ctx);
$random = \substr_replace($random, \substr(@$this->stream->getEncryptor()->encrypt($random), 56, 8), 56, 8);
$random = \substr_replace($random, \substr(@$this->getEncryptor()->encrypt($random), 56, 8), 56, 8);
yield $this->stream->getPlainStream()->write($random);
}
/**
* Async close.
*
* @return Promise
*/
public function disconnect()
{
return $this->stream->disconnect();
}
/**
* Get read buffer asynchronously.
*
* @param int $length Length of payload, as detected by this layer
*
* @return Promise
*/
public function getReadBuffer(&$length): Promise
{
return $this->stream->getReadBuffer($length);
}
/**
* Get write buffer asynchronously.
*
* @param int $length Total length of data that is going to be piped in the buffer
*
* @return Promise
*/
public function getWriteBuffer(int $length, string $append = ''): Promise
{
return $this->stream->getWriteBuffer($length, $append);
yield $this->getPlainStream()->write($random);
}
@ -151,16 +117,6 @@ class ObfuscatedStream implements BufferedProxyStreamInterface
$this->extra = $extra;
}
/**
* {@inheritdoc}
*
* @return EncryptableSocket
*/
public function getSocket(): EncryptableSocket
{
return $this->stream->getSocket();
}
public static function getName(): string
{
return __CLASS__;