From ee0da206c1b87389d2066b2cd10db788b31ab12d Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 5 Sep 2019 21:11:19 -0700 Subject: [PATCH] Update command sending from server API to use new daemon code --- .../Api/Client/Servers/CommandController.php | 13 +++++------- .../Wings/DaemonCommandRepository.php | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/CommandController.php b/app/Http/Controllers/Api/Client/Servers/CommandController.php index 881548fa0..ac0c3d8f8 100644 --- a/app/Http/Controllers/Api/Client/Servers/CommandController.php +++ b/app/Http/Controllers/Api/Client/Servers/CommandController.php @@ -7,25 +7,25 @@ use Pterodactyl\Models\Server; use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\RequestException; +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 Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface; use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException; class CommandController extends ClientApiController { /** - * @var \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface + * @var \Pterodactyl\Repositories\Wings\DaemonCommandRepository */ private $repository; /** * CommandController constructor. * - * @param \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface $repository + * @param \Pterodactyl\Repositories\Wings\DaemonCommandRepository $repository */ - public function __construct(CommandRepositoryInterface $repository) + public function __construct(DaemonCommandRepository $repository) { parent::__construct(); @@ -43,12 +43,9 @@ class CommandController extends ClientApiController public function index(SendCommandRequest $request): Response { $server = $request->getModel(Server::class); - $token = $request->attributes->get('server_token'); try { - $this->repository->setServer($server) - ->setToken($token) - ->send($request->input('command')); + $this->repository->setServer($server)->send($request->input('command')); } catch (RequestException $exception) { if ($exception instanceof ClientException) { if ($exception->getResponse() instanceof ResponseInterface && $exception->getResponse()->getStatusCode() === 412) { diff --git a/app/Repositories/Wings/DaemonCommandRepository.php b/app/Repositories/Wings/DaemonCommandRepository.php index 4a067e928..5b75e4ab9 100644 --- a/app/Repositories/Wings/DaemonCommandRepository.php +++ b/app/Repositories/Wings/DaemonCommandRepository.php @@ -2,6 +2,27 @@ namespace Pterodactyl\Repositories\Wings; +use Webmozart\Assert\Assert; +use Pterodactyl\Models\Server; +use Psr\Http\Message\ResponseInterface; + class DaemonCommandRepository extends DaemonRepository { + /** + * Sends a command or multiple commands to a running server instance. + * + * @param string|string[] $command + * @return \Psr\Http\Message\ResponseInterface + */ + public function send($command): ResponseInterface + { + Assert::isInstanceOf(Server::class, $this->server); + + return $this->getHttpClient()->post( + sprintf('/api/servers/%s/commands', $this->server->uuid), + [ + 'json' => ['commands' => is_array($command) ? $command : [$command]], + ] + ); + } }