From 4e12c289ed6bd362314108fac7f4148794dba590 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 27 Feb 2018 22:09:34 -0600 Subject: [PATCH] Add command sending --- .../Api/Client/Servers/CommandController.php | 74 +++++++++++++++++++ .../Api/Client/Servers/SendCommandRequest.php | 30 ++++++++ app/Repositories/Daemon/CommandRepository.php | 1 - 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Api/Client/Servers/CommandController.php create mode 100644 app/Http/Requests/Api/Client/Servers/SendCommandRequest.php diff --git a/app/Http/Controllers/Api/Client/Servers/CommandController.php b/app/Http/Controllers/Api/Client/Servers/CommandController.php new file mode 100644 index 000000000..8a5b951f5 --- /dev/null +++ b/app/Http/Controllers/Api/Client/Servers/CommandController.php @@ -0,0 +1,74 @@ +keyProviderService = $keyProviderService; + $this->repository = $repository; + } + + /** + * Send a command to a running server. + * + * @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest $request + * @return \Illuminate\Http\Response + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function index(SendCommandRequest $request): Response + { + $server = $request->getModel(Server::class); + $token = $this->keyProviderService->handle($server, $request->user()); + + try { + $this->repository->setServer($server) + ->setToken($token) + ->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.'); + } + } + + throw new DaemonConnectionException($exception); + } + + return $this->returnNoContent(); + } +} diff --git a/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php b/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php new file mode 100644 index 000000000..68757658a --- /dev/null +++ b/app/Http/Requests/Api/Client/Servers/SendCommandRequest.php @@ -0,0 +1,30 @@ +user()->can('send-command', $this->getModel(Server::class)); + } + + /** + * Rules to validate this request aganist. + * + * @return array + */ + public function rules(): array + { + return [ + 'command' => 'string|min:1', + ]; + } +} diff --git a/app/Repositories/Daemon/CommandRepository.php b/app/Repositories/Daemon/CommandRepository.php index 31cb6b9b7..7b7577b32 100644 --- a/app/Repositories/Daemon/CommandRepository.php +++ b/app/Repositories/Daemon/CommandRepository.php @@ -12,7 +12,6 @@ class CommandRepository extends BaseRepository implements CommandRepositoryInter * * @param string $command * @return \Psr\Http\Message\ResponseInterface - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function send(string $command): ResponseInterface {