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