Skip to content

Commit 29760e0

Browse files
committed
Basic quick ACK support
1 parent 0de601b commit 29760e0

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

src/Loop/Connection/ReadLoop.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,25 @@ public function readMessage(): ?int
139139
}
140140
throw $e;
141141
}
142-
if ($payload_length === 4) {
143-
$payload = Tools::unpackSignedInt($buffer->bufferRead(4));
144-
$this->API->logger("Received {$payload} from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
145-
return $payload;
142+
if ($payload_length & (1 << 31)) {
143+
$this->API->logger("Received quick ACK $payload_length from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
144+
return null;
145+
}
146+
if ($payload_length <= 16) {
147+
$code = Tools::unpackSignedInt($buffer->bufferRead(4));
148+
if ($code === -1 && $payload_length >= 8) {
149+
$ack = unpack('V', $buffer->bufferRead(4))[1];
150+
$this->API->logger("Received quick ACK $ack (padded) from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
151+
if ($payload_length > 8) {
152+
$buffer->bufferRead($payload_length-8);
153+
}
154+
return null;
155+
}
156+
if ($payload_length > 4) {
157+
$buffer->bufferRead($payload_length-4);
158+
}
159+
$this->API->logger("Received {$code} from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
160+
return $code;
146161
}
147162
$this->connection->reading(true);
148163
try {

src/Loop/Connection/WriteLoop.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ public function encryptedWriteLoop(): bool
342342
$padding += 16;
343343
}
344344
$padding = Tools::random($padding);
345-
$message_key = substr(hash('sha256', substr($this->shared->getTempAuthKey()->getAuthKey(), 88, 32).$plaintext.$padding, true), 8, 16);
345+
$message_key_large = hash('sha256', substr($this->shared->getTempAuthKey()->getAuthKey(), 88, 32).$plaintext.$padding, true);
346+
$message_key = substr($message_key_large, 8, 16);
347+
//$ack = unpack('V', substr($message_key_large, 0, 4))[1] | (1 << 31);
346348
[$aes_key, $aes_iv] = Crypt::kdf($message_key, $this->shared->getTempAuthKey()->getAuthKey());
347349
$message = $this->shared->getTempAuthKey()->getID().$message_key.Crypt::igeEncrypt($plaintext.$padding, $aes_key, $aes_iv);
348350
$buffer = $this->connection->stream->getWriteBuffer(\strlen($message));

src/Stream/MTProtoTransport/AbridgedStream.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ public function getWriteBuffer(int $length, string $append = ''): \danog\Madelin
5959
{
6060
$length >>= 2;
6161
if ($length < 127) {
62+
// $message = \chr($length | (1 << 7));
6263
$message = \chr($length);
6364
} else {
65+
// $message = \chr(255).substr(pack('V', $length), 0, 3);
6466
$message = \chr(127).substr(pack('V', $length), 0, 3);
6567
}
6668
$buffer = $this->stream->getWriteBuffer(\strlen($message) + $length, $append);
@@ -75,7 +77,12 @@ public function getWriteBuffer(int $length, string $append = ''): \danog\Madelin
7577
public function getReadBuffer(?int &$length): \danog\MadelineProto\Stream\ReadBufferInterface
7678
{
7779
$buffer = $this->stream->getReadBuffer($l);
78-
$length = \ord($buffer->bufferRead(1));
80+
$c = $buffer->bufferRead(1);
81+
$length = \ord($c);
82+
/*if (($length & (1 << 7)) !== 0) {
83+
$length = unpack('V', strrev($c.$buffer->bufferRead(3)))[1];
84+
return $buffer;
85+
}*/
7986
if ($length >= 127) {
8087
$length = unpack('V', ($buffer->bufferRead(3))."\0")[1];
8188
}

src/Stream/MTProtoTransport/IntermediatePaddedStream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function getWriteBuffer(int $length, string $append = ''): \danog\Madelin
6262
{
6363
$padding_length = Tools::randomInt(modulus: 16);
6464
$buffer = $this->stream->getWriteBuffer(4 + $length + $padding_length, $append.Tools::random($padding_length));
65+
//$buffer->bufferWrite(pack('V', ($padding_length + $length) | (1 << 31)));
6566
$buffer->bufferWrite(pack('V', $padding_length + $length));
6667
return $buffer;
6768
}

src/Stream/MTProtoTransport/IntermediateStream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function disconnect(): void
6060
public function getWriteBuffer(int $length, string $append = ''): \danog\MadelineProto\Stream\WriteBufferInterface
6161
{
6262
$buffer = $this->stream->getWriteBuffer($length + 4, $append);
63+
//$buffer->bufferWrite(pack('V', ($length) | (1 << 31)));
6364
$buffer->bufferWrite(pack('V', $length));
6465
return $buffer;
6566
}

0 commit comments

Comments
 (0)