Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/CommandController.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Server;
7
use Pterodactyl\Facades\Activity;
8
use GuzzleHttp\Exception\BadResponseException;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
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
15
class CommandController extends ClientApiController
16
{
17
/**
18
* CommandController constructor.
19
*/
20
public function __construct(private DaemonCommandRepository $repository)
21
{
22
parent::__construct();
23
}
24
25
/**
26
* Send a command to a running server.
27
*
28
* @throws DaemonConnectionException
29
*/
30
public function index(SendCommandRequest $request, Server $server): Response
31
{
32
try {
33
$this->repository->setServer($server)->send($request->input('command'));
34
} catch (DaemonConnectionException $exception) {
35
$previous = $exception->getPrevious();
36
37
if ($previous instanceof BadResponseException) {
38
if ($previous->getResponse()->getStatusCode() === Response::HTTP_BAD_GATEWAY) {
39
throw new HttpException(Response::HTTP_BAD_GATEWAY, 'Server must be online in order to send commands.', $exception);
40
}
41
}
42
43
throw $exception;
44
}
45
46
Activity::event('server:console.command')->property('command', $request->input('command'))->log();
47
48
return $this->returnNoContent();
49
}
50
}
51
52