2018-02-28 04:09:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2019-09-06 04:16:36 +00:00
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2019-09-06 04:11:19 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
|
2018-02-28 04:09:34 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
|
|
|
|
class CommandController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
2019-09-06 04:11:19 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonCommandRepository
|
2018-02-28 04:09:34 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CommandController constructor.
|
|
|
|
*
|
2019-09-06 04:11:19 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonCommandRepository $repository
|
2018-02-28 04:09:34 +00:00
|
|
|
*/
|
2019-09-06 04:11:19 +00:00
|
|
|
public function __construct(DaemonCommandRepository $repository)
|
2018-02-28 04:09:34 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a command to a running server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest $request
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2018-02-28 04:09:34 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
2019-09-06 04:31:12 +00:00
|
|
|
public function index(SendCommandRequest $request, Server $server): Response
|
2018-02-28 04:09:34 +00:00
|
|
|
{
|
|
|
|
try {
|
2019-09-06 04:11:19 +00:00
|
|
|
$this->repository->setServer($server)->send($request->input('command'));
|
2018-02-28 04:09:34 +00:00
|
|
|
} catch (RequestException $exception) {
|
2019-09-06 04:16:36 +00:00
|
|
|
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
|
|
|
|
);
|
2018-02-28 04:09:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->returnNoContent();
|
|
|
|
}
|
|
|
|
}
|