misc_pterodactyl-panel/app/Http/Controllers/Daemon/ActionController.php

108 lines
3.4 KiB
PHP
Raw Normal View History

2016-01-02 00:27:44 +00:00
<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Daemon;
2016-01-02 00:27:44 +00:00
use Cache;
2016-01-02 00:27:44 +00:00
use Illuminate\Http\Request;
use Pterodactyl\Models\Node;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Events\Server\Installed as ServerInstalled;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
2016-01-02 00:27:44 +00:00
class ActionController extends Controller
2016-01-02 00:27:44 +00:00
{
/**
* @var \Illuminate\Contracts\Events\Dispatcher
*/
private $eventDispatcher;
2019-09-06 04:32:57 +00:00
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $repository;
/**
* ActionController constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Events\Dispatcher $eventDispatcher
*/
public function __construct(ServerRepository $repository, EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
$this->repository = $repository;
}
2017-03-19 23:36:50 +00:00
/**
* Handles install toggle request from daemon.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-19 23:36:50 +00:00
*/
public function markInstall(Request $request): JsonResponse
{
try {
/** @var \Pterodactyl\Models\Server $server */
$server = $this->repository->findFirstWhere([
'uuid' => $request->input('server'),
]);
} catch (RecordNotFoundException $exception) {
return JsonResponse::create([
2016-12-07 22:46:38 +00:00
'error' => 'No server by that ID was found on the system.',
], Response::HTTP_UNPROCESSABLE_ENTITY);
}
if (! $server->relationLoaded('node')) {
$server->load('node');
}
$hmac = $request->input('signed');
$status = $request->input('installed');
if (! hash_equals(base64_decode($hmac), hash_hmac('sha256', $server->uuid, $server->getRelation('node')->daemonSecret, true))) {
return JsonResponse::create([
2016-12-07 22:46:38 +00:00
'error' => 'Signed HMAC was invalid.',
], Response::HTTP_FORBIDDEN);
}
$this->repository->update($server->id, [
'installed' => ($status === 'installed') ? 1 : 2,
], true, true);
// Only fire event if server installed successfully.
if ($status === 'installed') {
$this->eventDispatcher->dispatch(new ServerInstalled($server));
}
// Don't use a 204 here, the daemon is hard-checking for a 200 code.
return JsonResponse::create([]);
}
2017-03-19 23:36:50 +00:00
/**
* Handles configuration data request from daemon.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
2019-09-06 04:32:57 +00:00
* @param string $token
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
*/
public function configuration(Request $request, $token)
2017-01-07 17:39:41 +00:00
{
$nodeId = Cache::pull('Node:Configuration:' . $token);
if (is_null($nodeId)) {
return response()->json(['error' => 'token_invalid'], 403);
}
$node = Node::findOrFail($nodeId);
// Manually as getConfigurationAsJson() returns it in correct format already
2019-12-08 19:29:46 +00:00
return [];
}
2016-01-02 00:27:44 +00:00
}