From 62cd03d6843da98994c81ebdece40e52e1dee696 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 5 Sep 2019 21:16:36 -0700 Subject: [PATCH] Fix command sending error handling and bad assertion order --- .../Api/Client/Servers/CommandController.php | 15 ++++++++++----- .../Wings/DaemonCommandRepository.php | 2 +- app/Repositories/Wings/DaemonFileRepository.php | 14 +++++++------- app/Repositories/Wings/DaemonRepository.php | 2 +- app/Repositories/Wings/DaemonServerRepository.php | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/CommandController.php b/app/Http/Controllers/Api/Client/Servers/CommandController.php index ac0c3d8f8..97e767311 100644 --- a/app/Http/Controllers/Api/Client/Servers/CommandController.php +++ b/app/Http/Controllers/Api/Client/Servers/CommandController.php @@ -5,13 +5,13 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; use Illuminate\Http\Response; use Pterodactyl\Models\Server; use Psr\Http\Message\ResponseInterface; -use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Exception\BadResponseException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Pterodactyl\Repositories\Wings\DaemonCommandRepository; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; -use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException; class CommandController extends ClientApiController { @@ -47,9 +47,14 @@ class CommandController extends ClientApiController try { $this->repository->setServer($server)->send($request->input('command')); } catch (RequestException $exception) { - if ($exception instanceof ClientException) { - if ($exception->getResponse() instanceof ResponseInterface && $exception->getResponse()->getStatusCode() === 412) { - throw new PreconditionFailedHttpException('Server is not online.'); + if ($exception instanceof BadResponseException) { + if ( + $exception->getResponse() instanceof ResponseInterface + && $exception->getResponse()->getStatusCode() === Response::HTTP_BAD_GATEWAY + ) { + throw new HttpException( + Response::HTTP_BAD_GATEWAY, 'Server must be online in order to send commands.', $exception + ); } } diff --git a/app/Repositories/Wings/DaemonCommandRepository.php b/app/Repositories/Wings/DaemonCommandRepository.php index 5b75e4ab9..644bb024c 100644 --- a/app/Repositories/Wings/DaemonCommandRepository.php +++ b/app/Repositories/Wings/DaemonCommandRepository.php @@ -16,7 +16,7 @@ class DaemonCommandRepository extends DaemonRepository */ public function send($command): ResponseInterface { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); return $this->getHttpClient()->post( sprintf('/api/servers/%s/commands', $this->server->uuid), diff --git a/app/Repositories/Wings/DaemonFileRepository.php b/app/Repositories/Wings/DaemonFileRepository.php index 3c362667b..94f7599e3 100644 --- a/app/Repositories/Wings/DaemonFileRepository.php +++ b/app/Repositories/Wings/DaemonFileRepository.php @@ -37,7 +37,7 @@ class DaemonFileRepository extends DaemonRepository */ public function getContent(string $path, int $notLargerThan = null): string { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); $response = $this->getHttpClient()->get( sprintf('/api/servers/%s/files/contents', $this->server->uuid), @@ -69,7 +69,7 @@ class DaemonFileRepository extends DaemonRepository */ public function putContent(string $path, string $content): ResponseInterface { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); return $this->getHttpClient()->post( sprintf('/api/servers/%s/files/write', $this->server->uuid), @@ -90,7 +90,7 @@ class DaemonFileRepository extends DaemonRepository */ public function getDirectory(string $path): array { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); $response = $this->getHttpClient()->get( sprintf('/api/servers/%s/files/list-directory', $this->server->uuid), @@ -111,7 +111,7 @@ class DaemonFileRepository extends DaemonRepository */ public function createDirectory(string $name, string $path): ResponseInterface { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); return $this->getHttpClient()->post( sprintf('/api/servers/%s/files/create-directory', $this->server->uuid), @@ -133,7 +133,7 @@ class DaemonFileRepository extends DaemonRepository */ public function renameFile(string $from, string $to): ResponseInterface { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); return $this->getHttpClient()->put( sprintf('/api/servers/%s/files/rename', $this->server->uuid), @@ -154,7 +154,7 @@ class DaemonFileRepository extends DaemonRepository */ public function copyFile(string $location): ResponseInterface { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); return $this->getHttpClient()->post( sprintf('/api/servers/%s/files/copy', $this->server->uuid), @@ -174,7 +174,7 @@ class DaemonFileRepository extends DaemonRepository */ public function deleteFile(string $location): ResponseInterface { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); return $this->getHttpClient()->post( sprintf('/api/servers/%s/files/delete', $this->server->uuid), diff --git a/app/Repositories/Wings/DaemonRepository.php b/app/Repositories/Wings/DaemonRepository.php index 435bcc515..b2535b3a9 100644 --- a/app/Repositories/Wings/DaemonRepository.php +++ b/app/Repositories/Wings/DaemonRepository.php @@ -71,7 +71,7 @@ abstract class DaemonRepository */ public function getHttpClient(array $headers = []): Client { - Assert::isInstanceOf(Node::class, $this->node); + Assert::isInstanceOf($this->node, Node::class); return new Client([ 'verify' => $this->app->environment('production'), diff --git a/app/Repositories/Wings/DaemonServerRepository.php b/app/Repositories/Wings/DaemonServerRepository.php index 49cf9f298..f77cc7f06 100644 --- a/app/Repositories/Wings/DaemonServerRepository.php +++ b/app/Repositories/Wings/DaemonServerRepository.php @@ -17,7 +17,7 @@ class DaemonServerRepository extends DaemonRepository */ public function getDetails(): array { - Assert::isInstanceOf(Server::class, $this->server); + Assert::isInstanceOf($this->server, Server::class); try { $response = $this->getHttpClient()->get(