2019-09-09 00:48:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-04-07 04:03:00 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2019-09-09 00:48:37 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-03-22 22:31:25 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2020-04-07 04:03:00 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
2020-12-24 17:20:23 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
|
2020-03-22 22:31:25 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
2020-04-17 17:21:15 +00:00
|
|
|
use Pterodactyl\Services\Servers\GetUserPermissionsService;
|
2019-09-09 00:48:37 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
|
|
|
|
class WebsocketController extends ClientApiController
|
|
|
|
{
|
2020-04-07 04:03:00 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeJWTService
|
|
|
|
*/
|
|
|
|
private $jwtService;
|
|
|
|
|
2020-04-17 17:21:15 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\GetUserPermissionsService
|
|
|
|
*/
|
|
|
|
private $permissionsService;
|
|
|
|
|
2019-09-09 00:48:37 +00:00
|
|
|
/**
|
|
|
|
* WebsocketController constructor.
|
|
|
|
*
|
2020-04-07 04:03:00 +00:00
|
|
|
* @param \Pterodactyl\Services\Nodes\NodeJWTService $jwtService
|
2020-04-17 17:21:15 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\GetUserPermissionsService $permissionsService
|
2019-09-09 00:48:37 +00:00
|
|
|
*/
|
2020-04-17 17:21:15 +00:00
|
|
|
public function __construct(
|
|
|
|
NodeJWTService $jwtService,
|
2020-06-28 17:16:15 +00:00
|
|
|
GetUserPermissionsService $permissionsService
|
2020-04-17 17:21:15 +00:00
|
|
|
) {
|
2019-09-09 00:48:37 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2020-04-07 04:03:00 +00:00
|
|
|
$this->jwtService = $jwtService;
|
2020-04-17 17:21:15 +00:00
|
|
|
$this->permissionsService = $permissionsService;
|
2019-09-09 00:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-25 03:20:29 +00:00
|
|
|
* Generates a one-time token that is sent along in every websocket call to the Daemon.
|
|
|
|
* This is a signed JWT that the Daemon then uses the verify the user's identity, and
|
|
|
|
* allows us to continually renew this token and avoid users mainitaining sessions wrongly,
|
|
|
|
* as well as ensure that user's only perform actions they're allowed to.
|
2019-09-09 00:48:37 +00:00
|
|
|
*
|
2020-03-22 22:31:25 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\ClientApiRequest $request
|
2019-09-09 00:48:37 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2020-03-22 22:31:25 +00:00
|
|
|
public function __invoke(ClientApiRequest $request, Server $server)
|
2019-09-09 00:48:37 +00:00
|
|
|
{
|
2020-04-07 04:03:00 +00:00
|
|
|
$user = $request->user();
|
2020-04-17 17:21:15 +00:00
|
|
|
if ($user->cannot(Permission::ACTION_WEBSOCKET_CONNECT, $server)) {
|
2020-12-24 17:20:23 +00:00
|
|
|
throw new HttpForbiddenException('You do not have permission to connect to this server\'s websocket.');
|
2019-09-09 00:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 23:55:44 +00:00
|
|
|
$permissions = $this->permissionsService->handle($server, $user);
|
|
|
|
|
2020-12-24 17:20:23 +00:00
|
|
|
$node = $server->node;
|
2020-12-17 17:34:26 +00:00
|
|
|
if (! is_null($server->transfer)) {
|
2020-12-16 23:55:44 +00:00
|
|
|
// Check if the user has permissions to receive transfer logs.
|
|
|
|
if (! in_array('admin.websocket.transfer', $permissions)) {
|
2020-12-24 17:20:23 +00:00
|
|
|
throw new HttpForbiddenException('You do not have permission to view server transfer logs.');
|
2020-12-16 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 01:54:01 +00:00
|
|
|
// Redirect the websocket request to the new node if the server has been archived.
|
|
|
|
if ($server->transfer->archived) {
|
|
|
|
$node = $server->transfer->newNode;
|
|
|
|
}
|
2020-12-16 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 04:03:00 +00:00
|
|
|
$token = $this->jwtService
|
2020-11-04 05:01:15 +00:00
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(10))
|
2020-04-07 04:03:00 +00:00
|
|
|
->setClaims([
|
|
|
|
'user_id' => $request->user()->id,
|
|
|
|
'server_uuid' => $server->uuid,
|
2020-12-16 23:55:44 +00:00
|
|
|
'permissions' => $permissions,
|
2020-04-07 04:03:00 +00:00
|
|
|
])
|
2020-12-16 23:55:44 +00:00
|
|
|
->handle($node, $user->id . $server->uuid);
|
2019-09-09 00:48:37 +00:00
|
|
|
|
2020-12-16 23:55:44 +00:00
|
|
|
$socket = str_replace(['https://', 'http://'], ['wss://', 'ws://'], $node->getConnectionAddress());
|
2019-09-09 00:48:37 +00:00
|
|
|
|
2020-06-28 17:16:15 +00:00
|
|
|
return new JsonResponse([
|
2019-09-09 00:48:37 +00:00
|
|
|
'data' => [
|
2019-09-25 03:20:29 +00:00
|
|
|
'token' => $token->__toString(),
|
|
|
|
'socket' => $socket . sprintf('/api/servers/%s/ws', $server->uuid),
|
2019-09-09 00:48:37 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|