Skip to content

Commit

Permalink
Basic quick ACK support
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 25, 2024
1 parent 0de601b commit 29760e0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/Loop/Connection/ReadLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,25 @@ public function readMessage(): ?int
}
throw $e;
}
if ($payload_length === 4) {
$payload = Tools::unpackSignedInt($buffer->bufferRead(4));
$this->API->logger("Received {$payload} from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
return $payload;
if ($payload_length & (1 << 31)) {
$this->API->logger("Received quick ACK $payload_length from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
return null;
}
if ($payload_length <= 16) {
$code = Tools::unpackSignedInt($buffer->bufferRead(4));
if ($code === -1 && $payload_length >= 8) {
$ack = unpack('V', $buffer->bufferRead(4))[1];
$this->API->logger("Received quick ACK $ack (padded) from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
if ($payload_length > 8) {
$buffer->bufferRead($payload_length-8);
}
return null;
}
if ($payload_length > 4) {
$buffer->bufferRead($payload_length-4);
}
$this->API->logger("Received {$code} from DC ".$this->datacenter, Logger::ULTRA_VERBOSE);
return $code;
}
$this->connection->reading(true);
try {
Expand Down
4 changes: 3 additions & 1 deletion src/Loop/Connection/WriteLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ public function encryptedWriteLoop(): bool
$padding += 16;
}
$padding = Tools::random($padding);
$message_key = substr(hash('sha256', substr($this->shared->getTempAuthKey()->getAuthKey(), 88, 32).$plaintext.$padding, true), 8, 16);
$message_key_large = hash('sha256', substr($this->shared->getTempAuthKey()->getAuthKey(), 88, 32).$plaintext.$padding, true);
$message_key = substr($message_key_large, 8, 16);
//$ack = unpack('V', substr($message_key_large, 0, 4))[1] | (1 << 31);
[$aes_key, $aes_iv] = Crypt::kdf($message_key, $this->shared->getTempAuthKey()->getAuthKey());
$message = $this->shared->getTempAuthKey()->getID().$message_key.Crypt::igeEncrypt($plaintext.$padding, $aes_key, $aes_iv);
$buffer = $this->connection->stream->getWriteBuffer(\strlen($message));
Expand Down
9 changes: 8 additions & 1 deletion src/Stream/MTProtoTransport/AbridgedStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public function getWriteBuffer(int $length, string $append = ''): \danog\Madelin
{
$length >>= 2;
if ($length < 127) {
// $message = \chr($length | (1 << 7));
$message = \chr($length);
} else {
// $message = \chr(255).substr(pack('V', $length), 0, 3);
$message = \chr(127).substr(pack('V', $length), 0, 3);
}
$buffer = $this->stream->getWriteBuffer(\strlen($message) + $length, $append);
Expand All @@ -75,7 +77,12 @@ public function getWriteBuffer(int $length, string $append = ''): \danog\Madelin
public function getReadBuffer(?int &$length): \danog\MadelineProto\Stream\ReadBufferInterface
{
$buffer = $this->stream->getReadBuffer($l);
$length = \ord($buffer->bufferRead(1));
$c = $buffer->bufferRead(1);
$length = \ord($c);
/*if (($length & (1 << 7)) !== 0) {
$length = unpack('V', strrev($c.$buffer->bufferRead(3)))[1];
return $buffer;
}*/
if ($length >= 127) {
$length = unpack('V', ($buffer->bufferRead(3))."\0")[1];
}
Expand Down
1 change: 1 addition & 0 deletions src/Stream/MTProtoTransport/IntermediatePaddedStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function getWriteBuffer(int $length, string $append = ''): \danog\Madelin
{
$padding_length = Tools::randomInt(modulus: 16);
$buffer = $this->stream->getWriteBuffer(4 + $length + $padding_length, $append.Tools::random($padding_length));
//$buffer->bufferWrite(pack('V', ($padding_length + $length) | (1 << 31)));
$buffer->bufferWrite(pack('V', $padding_length + $length));
return $buffer;
}
Expand Down
1 change: 1 addition & 0 deletions src/Stream/MTProtoTransport/IntermediateStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function disconnect(): void
public function getWriteBuffer(int $length, string $append = ''): \danog\MadelineProto\Stream\WriteBufferInterface
{
$buffer = $this->stream->getWriteBuffer($length + 4, $append);
//$buffer->bufferWrite(pack('V', ($length) | (1 << 31)));
$buffer->bufferWrite(pack('V', $length));
return $buffer;
}
Expand Down

0 comments on commit 29760e0

Please sign in to comment.