Update command sending from server API to use new daemon code

This commit is contained in:
Dane Everitt 2019-09-05 21:11:19 -07:00
parent 161e0f6165
commit ee0da206c1
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 26 additions and 8 deletions

View file

@ -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) {

View file

@ -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]],
]
);
}
}