From f59edfaa53927bf049a66394c914daa541b07bce Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 20 Aug 2023 21:39:43 +0200 Subject: [PATCH] Improve validation --- src/Loop/VoIP/DjLoop.php | 2 +- src/Ogg.php | 7 +++++-- src/VoIP/AuthKeyHandler.php | 9 +++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Loop/VoIP/DjLoop.php b/src/Loop/VoIP/DjLoop.php index 1644913af..41f9aa978 100644 --- a/src/Loop/VoIP/DjLoop.php +++ b/src/Loop/VoIP/DjLoop.php @@ -188,7 +188,7 @@ final class DjLoop extends VoIPLoop private function startPlaying(LocalFile|RemoteUrl|ReadableStream $f, SplQueue $queue, Cancellation $cancellation): void { $it = null; - if ($f instanceof LocalFile) { + if ($f instanceof LocalFile || $f instanceof RemoteUrl) { try { $it = new Ogg($f, $cancellation); if (!\in_array('MADELINE_ENCODER_V=1', $it->comments, true)) { diff --git a/src/Ogg.php b/src/Ogg.php index 426302c3b..83c55fd52 100644 --- a/src/Ogg.php +++ b/src/Ogg.php @@ -21,6 +21,7 @@ namespace danog\MadelineProto; use Amp\ByteStream\ReadableStream; use Amp\ByteStream\WritableStream; use Amp\Cancellation; +use Amp\DeferredCancellation; use Amp\Process\Process; use AssertionError; use Closure; @@ -543,9 +544,11 @@ final class Ogg { $ok = false; try { - $ogg = new self($f); + $cancel = new DeferredCancellation; + $ogg = new self($f, $cancel->getCancellation()); $ok = \in_array('MADELINE_ENCODER_V=1', $ogg->comments, true); - } catch (\Throwable) {} + } catch (\Throwable) { + } if (!$ok) { throw new AssertionError("The passed file was not generated by MadelineProto or @libtgvoipbot, please pre-convert it using @libtgvoip bot or install FFI and ffmpeg to perform realtime conversion!"); } diff --git a/src/VoIP/AuthKeyHandler.php b/src/VoIP/AuthKeyHandler.php index 5e32f9a07..616f9b5de 100644 --- a/src/VoIP/AuthKeyHandler.php +++ b/src/VoIP/AuthKeyHandler.php @@ -22,6 +22,7 @@ namespace danog\MadelineProto\VoIP; use Amp\ByteStream\ReadableStream; use Amp\DeferredFuture; +use AssertionError; use danog\MadelineProto\LocalFile; use danog\MadelineProto\Logger; use danog\MadelineProto\Magic; @@ -163,8 +164,10 @@ trait AuthKeyHandler public function callPlay(int $id, LocalFile|RemoteUrl|ReadableStream $file): void { if (!Magic::canConvertOgg()) { - if ($file instanceof LocalFile) { + if ($file instanceof LocalFile || $file instanceof RemoteUrl) { Ogg::validateOgg($file); + } else { + throw new AssertionError("The passed file was not generated by MadelineProto or @libtgvoipbot, please pre-convert it using @libtgvoip bot or install FFI and ffmpeg to perform realtime conversion!"); } } ($this->calls[$id] ?? null)?->play($file); @@ -235,8 +238,10 @@ trait AuthKeyHandler { if (!Magic::canConvertOgg()) { foreach ($files as $file) { - if ($file instanceof LocalFile) { + if ($file instanceof LocalFile || $file instanceof RemoteUrl) { Ogg::validateOgg($file); + } else { + throw new AssertionError("The passed file was not generated by MadelineProto or @libtgvoipbot, please pre-convert it using @libtgvoip bot or install FFI and ffmpeg to perform realtime conversion!"); } } }