Skip to content

Commit 4e12c28

Browse files
committed
Add command sending
1 parent cef3e4c commit 4e12c28

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4+
5+
use Illuminate\Http\Response;
6+
use Pterodactyl\Models\Server;
7+
use Psr\Http\Message\ResponseInterface;
8+
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Exception\RequestException;
10+
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
11+
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
12+
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
13+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
14+
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
15+
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
16+
17+
class CommandController extends ClientApiController
18+
{
19+
/**
20+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
21+
*/
22+
private $keyProviderService;
23+
24+
/**
25+
* @var \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface
26+
*/
27+
private $repository;
28+
29+
/**
30+
* CommandController constructor.
31+
*
32+
* @param \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface $repository
33+
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
34+
*/
35+
public function __construct(CommandRepositoryInterface $repository, DaemonKeyProviderService $keyProviderService)
36+
{
37+
parent::__construct();
38+
39+
$this->keyProviderService = $keyProviderService;
40+
$this->repository = $repository;
41+
}
42+
43+
/**
44+
* Send a command to a running server.
45+
*
46+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest $request
47+
* @return \Illuminate\Http\Response
48+
*
49+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
50+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
51+
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
52+
*/
53+
public function index(SendCommandRequest $request): Response
54+
{
55+
$server = $request->getModel(Server::class);
56+
$token = $this->keyProviderService->handle($server, $request->user());
57+
58+
try {
59+
$this->repository->setServer($server)
60+
->setToken($token)
61+
->send($request->input('command'));
62+
} catch (RequestException $exception) {
63+
if ($exception instanceof ClientException) {
64+
if ($exception->getResponse() instanceof ResponseInterface && $exception->getResponse()->getStatusCode() === 412) {
65+
throw new PreconditionFailedHttpException('Server is not online.');
66+
}
67+
}
68+
69+
throw new DaemonConnectionException($exception);
70+
}
71+
72+
return $this->returnNoContent();
73+
}
74+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers;
4+
5+
use Pterodactyl\Models\Server;
6+
7+
class SendCommandRequest extends GetServerRequest
8+
{
9+
/**
10+
* Determine if the API user has permission to perform this action.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize(): bool
15+
{
16+
return $this->user()->can('send-command', $this->getModel(Server::class));
17+
}
18+
19+
/**
20+
* Rules to validate this request aganist.
21+
*
22+
* @return array
23+
*/
24+
public function rules(): array
25+
{
26+
return [
27+
'command' => 'string|min:1',
28+
];
29+
}
30+
}

app/Repositories/Daemon/CommandRepository.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class CommandRepository extends BaseRepository implements CommandRepositoryInter
1212
*
1313
* @param string $command
1414
* @return \Psr\Http\Message\ResponseInterface
15-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
1615
*/
1716
public function send(string $command): ResponseInterface
1817
{

0 commit comments

Comments
 (0)