Fix command sending error handling and bad assertion order

This commit is contained in:
Dane Everitt 2019-09-05 21:16:36 -07:00
parent ee0da206c1
commit 62cd03d684
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 20 additions and 15 deletions

View File

@ -5,13 +5,13 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\BadResponseException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Pterodactyl\Repositories\Wings\DaemonCommandRepository; use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
class CommandController extends ClientApiController class CommandController extends ClientApiController
{ {
@ -47,9 +47,14 @@ class CommandController extends ClientApiController
try { try {
$this->repository->setServer($server)->send($request->input('command')); $this->repository->setServer($server)->send($request->input('command'));
} catch (RequestException $exception) { } catch (RequestException $exception) {
if ($exception instanceof ClientException) { if ($exception instanceof BadResponseException) {
if ($exception->getResponse() instanceof ResponseInterface && $exception->getResponse()->getStatusCode() === 412) { if (
throw new PreconditionFailedHttpException('Server is not online.'); $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
);
} }
} }

View File

@ -16,7 +16,7 @@ class DaemonCommandRepository extends DaemonRepository
*/ */
public function send($command): ResponseInterface public function send($command): ResponseInterface
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->post( return $this->getHttpClient()->post(
sprintf('/api/servers/%s/commands', $this->server->uuid), sprintf('/api/servers/%s/commands', $this->server->uuid),

View File

@ -37,7 +37,7 @@ class DaemonFileRepository extends DaemonRepository
*/ */
public function getContent(string $path, int $notLargerThan = null): string 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( $response = $this->getHttpClient()->get(
sprintf('/api/servers/%s/files/contents', $this->server->uuid), 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 public function putContent(string $path, string $content): ResponseInterface
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->post( return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/write', $this->server->uuid), sprintf('/api/servers/%s/files/write', $this->server->uuid),
@ -90,7 +90,7 @@ class DaemonFileRepository extends DaemonRepository
*/ */
public function getDirectory(string $path): array public function getDirectory(string $path): array
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
$response = $this->getHttpClient()->get( $response = $this->getHttpClient()->get(
sprintf('/api/servers/%s/files/list-directory', $this->server->uuid), 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 public function createDirectory(string $name, string $path): ResponseInterface
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->post( return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/create-directory', $this->server->uuid), 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 public function renameFile(string $from, string $to): ResponseInterface
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->put( return $this->getHttpClient()->put(
sprintf('/api/servers/%s/files/rename', $this->server->uuid), sprintf('/api/servers/%s/files/rename', $this->server->uuid),
@ -154,7 +154,7 @@ class DaemonFileRepository extends DaemonRepository
*/ */
public function copyFile(string $location): ResponseInterface public function copyFile(string $location): ResponseInterface
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->post( return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/copy', $this->server->uuid), sprintf('/api/servers/%s/files/copy', $this->server->uuid),
@ -174,7 +174,7 @@ class DaemonFileRepository extends DaemonRepository
*/ */
public function deleteFile(string $location): ResponseInterface public function deleteFile(string $location): ResponseInterface
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
return $this->getHttpClient()->post( return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/delete', $this->server->uuid), sprintf('/api/servers/%s/files/delete', $this->server->uuid),

View File

@ -71,7 +71,7 @@ abstract class DaemonRepository
*/ */
public function getHttpClient(array $headers = []): Client public function getHttpClient(array $headers = []): Client
{ {
Assert::isInstanceOf(Node::class, $this->node); Assert::isInstanceOf($this->node, Node::class);
return new Client([ return new Client([
'verify' => $this->app->environment('production'), 'verify' => $this->app->environment('production'),

View File

@ -17,7 +17,7 @@ class DaemonServerRepository extends DaemonRepository
*/ */
public function getDetails(): array public function getDetails(): array
{ {
Assert::isInstanceOf(Server::class, $this->server); Assert::isInstanceOf($this->server, Server::class);
try { try {
$response = $this->getHttpClient()->get( $response = $this->getHttpClient()->get(