Repository navigation
Expand file tree
/
Copy pathFullStream.php
More file actions
130 lines (125 loc) · 4 KB
/
Copy pathFullStream.php
File metadata and controls
130 lines (125 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
/**
* TCP full 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-2025 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\Socket\Socket;
use danog\MadelineProto\Exception;
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.
*
* Manages obfuscated2 encryption/decryption
*
* @author Daniil Gentili <daniil@daniil.it>
*/
final class FullStream implements BufferedStreamInterface, MTProtoBufferInterface
{
private $stream;
private $in_seq_no = -1;
private $out_seq_no = -1;
/**
* Stream to use as data source.
*/
#[\Override]
public function connect(ConnectionContext $ctx, string $header = ''): void
{
$this->in_seq_no = -1;
$this->out_seq_no = -1;
$this->stream = new HashedBufferedStream();
$this->stream->setExtra('crc32b_rev');
$this->stream->connect($ctx, $header);
}
/**
* Async close.
*
* @psalm-mutation-free
*/
#[\Override]
public function disconnect(): void
{
$this->stream->disconnect();
}
/**
* Get write buffer asynchronously.
*
* @param int $length Length of data that is going to be written to the write buffer
*
* @psalm-external-mutation-free
*/
#[\Override]
public function getWriteBuffer(int $length, string $append = ''): \danog\MadelineProto\Stream\WriteBufferInterface
{
$this->stream->startWriteHash();
$this->stream->checkWriteHash($length + 8);
$buffer = $this->stream->getWriteBuffer($length + 12, $append);
$this->out_seq_no++;
$buffer->bufferWrite(pack('VV', $length + 12, $this->out_seq_no));
return $buffer;
}
/**
* Get read buffer asynchronously.
*
* @param int $length Length of payload, as detected by this layer
*/
#[\Override]
public function getReadBuffer(?int &$length): \danog\MadelineProto\Stream\ReadBufferInterface
{
$this->stream->startReadHash();
$buffer = $this->stream->getReadBuffer($l);
$read_length = unpack('V', $buffer->bufferRead(4))[1];
$length = $read_length - 12;
$this->stream->checkReadHash($read_length - 8);
$this->in_seq_no++;
$in_seq_no = unpack('V', $buffer->bufferRead(4))[1];
if ($in_seq_no != $this->in_seq_no) {
throw new Exception('Incoming seq_no mismatch');
}
return $buffer;
}
/**
* {@inheritdoc}
*
* @psalm-mutation-free
*/
#[\Override]
public function getSocket(): Socket
{
return $this->stream->getSocket();
}
/**
* {@inheritDoc}
*
* @psalm-mutation-free
*/
#[\Override]
public function getStream(): RawStreamInterface
{
return $this->stream;
}
/**
* @psalm-pure
*/
#[\Override]
public static function getName(): string
{
return self::class;
}
}