Files
panel/app/Http/Controllers/Api/Client/Servers/CommandController.php

46 lines
1.4 KiB
PHP
Raw Normal View History

2018-02-27 22:09:34 -06:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Client\Servers;
2018-02-27 22:09:34 -06:00
use Illuminate\Http\Response;
2024-03-12 22:39:16 -04:00
use App\Models\Server;
use App\Facades\Activity;
2018-02-27 22:09:34 -06:00
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\BadResponseException;
use Symfony\Component\HttpKernel\Exception\HttpException;
2024-03-12 22:39:16 -04:00
use App\Http\Controllers\Api\Client\ClientApiController;
use App\Http\Requests\Api\Client\Servers\SendCommandRequest;
use Exception;
use Illuminate\Http\Client\ConnectionException;
2018-02-27 22:09:34 -06:00
class CommandController extends ClientApiController
{
/**
* Send a command to a running server.
*
* @throws ConnectionException
2018-02-27 22:09:34 -06:00
*/
public function index(SendCommandRequest $request, Server $server): Response
2018-02-27 22:09:34 -06:00
{
try {
2024-03-16 15:11:10 -04:00
$server->send($request->input('command'));
} catch (Exception $exception) {
$previous = $exception->getPrevious();
if ($previous instanceof BadResponseException) {
if ($previous->getResponse() instanceof ResponseInterface && $previous->getResponse()->getStatusCode() === Response::HTTP_BAD_GATEWAY) {
2021-01-23 12:33:34 -08:00
throw new HttpException(Response::HTTP_BAD_GATEWAY, 'Server must be online in order to send commands.', $exception);
2018-02-27 22:09:34 -06:00
}
}
throw $exception;
2018-02-27 22:09:34 -06:00
}
Activity::event('server:console.command')
->property('command', $request->input('command'))
->log();
2018-02-27 22:09:34 -06:00
return $this->returnNoContent();
}
}