2016-01-01 19:27:44 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2017-05-01 13:59:33 -04:00
|
|
|
namespace Pterodactyl\Http\Controllers\Daemon;
|
2016-01-01 19:27:44 -05:00
|
|
|
|
2017-05-01 14:21:18 -04:00
|
|
|
use Cache;
|
2016-01-01 19:27:44 -05:00
|
|
|
use Illuminate\Http\Request;
|
2017-05-01 14:21:18 -04:00
|
|
|
use Pterodactyl\Models\Node;
|
2019-03-03 13:44:28 -08:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2019-03-03 13:44:28 -08:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2018-07-01 23:34:40 +02:00
|
|
|
use Pterodactyl\Events\Server\Installed as ServerInstalled;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
|
2019-03-03 13:44:28 -08:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2016-01-01 19:27:44 -05:00
|
|
|
|
2017-05-01 13:59:33 -04:00
|
|
|
class ActionController extends Controller
|
2016-01-01 19:27:44 -05:00
|
|
|
{
|
2018-07-01 23:34:40 +02:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher
|
|
|
|
*/
|
|
|
|
private $eventDispatcher;
|
2019-09-05 21:32:57 -07:00
|
|
|
|
2019-03-03 13:44:28 -08:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
2018-07-01 23:34:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ActionController constructor.
|
|
|
|
*
|
2019-03-03 13:44:28 -08:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
2019-09-05 21:32:57 -07:00
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $eventDispatcher
|
2018-07-01 23:34:40 +02:00
|
|
|
*/
|
2019-03-03 13:44:28 -08:00
|
|
|
public function __construct(ServerRepository $repository, EventDispatcher $eventDispatcher)
|
2018-07-01 23:34:40 +02:00
|
|
|
{
|
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2019-03-03 13:44:28 -08:00
|
|
|
$this->repository = $repository;
|
2018-07-01 23:34:40 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 19:36:50 -04:00
|
|
|
/**
|
|
|
|
* Handles install toggle request from daemon.
|
|
|
|
*
|
2017-08-21 22:10:48 -05:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-19 19:36:50 -04:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2019-03-03 13:44:28 -08:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-03-19 19:36:50 -04:00
|
|
|
*/
|
2019-03-03 13:44:28 -08:00
|
|
|
public function markInstall(Request $request): JsonResponse
|
2016-01-22 20:31:47 -05:00
|
|
|
{
|
2019-03-03 13:44:28 -08:00
|
|
|
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.',
|
2019-03-03 13:44:28 -08:00
|
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $server->relationLoaded('node')) {
|
|
|
|
$server->load('node');
|
2016-01-22 20:31:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$hmac = $request->input('signed');
|
|
|
|
$status = $request->input('installed');
|
|
|
|
|
2019-03-03 13:44:28 -08:00
|
|
|
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.',
|
2019-03-03 13:44:28 -08:00
|
|
|
], Response::HTTP_FORBIDDEN);
|
2016-01-22 20:31:47 -05:00
|
|
|
}
|
|
|
|
|
2019-03-03 13:44:28 -08:00
|
|
|
$this->repository->update($server->id, [
|
|
|
|
'installed' => ($status === 'installed') ? 1 : 2,
|
|
|
|
], true, true);
|
2016-01-22 20:31:47 -05:00
|
|
|
|
2018-07-01 23:34:40 +02:00
|
|
|
// Only fire event if server installed successfully.
|
2019-03-03 13:44:28 -08:00
|
|
|
if ($status === 'installed') {
|
2018-07-01 23:34:40 +02:00
|
|
|
$this->eventDispatcher->dispatch(new ServerInstalled($server));
|
|
|
|
}
|
|
|
|
|
2019-03-03 13:44:28 -08:00
|
|
|
// Don't use a 204 here, the daemon is hard-checking for a 200 code.
|
|
|
|
return JsonResponse::create([]);
|
2016-10-14 16:20:24 -04:00
|
|
|
}
|
2017-01-07 18:10:11 +01:00
|
|
|
|
2017-03-19 19:36:50 -04:00
|
|
|
/**
|
|
|
|
* Handles configuration data request from daemon.
|
|
|
|
*
|
2017-08-21 22:10:48 -05:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-09-05 21:32:57 -07:00
|
|
|
* @param string $token
|
2017-03-19 19:36:50 -04:00
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
|
|
|
*/
|
2017-05-01 13:59:33 -04:00
|
|
|
public function configuration(Request $request, $token)
|
2017-01-07 18:39:41 +01:00
|
|
|
{
|
2018-03-01 18:46:59 -06:00
|
|
|
$nodeId = Cache::pull('Node:Configuration:' . $token);
|
2017-05-01 14:21:18 -04:00
|
|
|
if (is_null($nodeId)) {
|
2017-01-08 15:21:02 +01:00
|
|
|
return response()->json(['error' => 'token_invalid'], 403);
|
2017-01-07 18:10:11 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:21:18 -04:00
|
|
|
$node = Node::findOrFail($nodeId);
|
2017-01-07 18:10:11 +01:00
|
|
|
|
2017-01-08 15:21:02 +01:00
|
|
|
// Manually as getConfigurationAsJson() returns it in correct format already
|
2017-05-01 14:21:18 -04:00
|
|
|
return response($node->getConfigurationAsJson())->header('Content-Type', 'text/json');
|
2017-01-07 18:10:11 +01:00
|
|
|
}
|
2016-01-01 19:27:44 -05:00
|
|
|
}
|