2019-09-09 00:48:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
2019-09-25 03:20:29 +00:00
|
|
|
use Lcobucci\JWT\Builder;
|
2019-09-09 00:48:37 +00:00
|
|
|
use Illuminate\Http\Request;
|
2019-09-25 03:20:29 +00:00
|
|
|
use Lcobucci\JWT\Signer\Key;
|
2019-09-09 00:48:37 +00:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-09-25 03:20:29 +00:00
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
2019-09-09 00:48:37 +00:00
|
|
|
use Illuminate\Contracts\Cache\Repository;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
|
|
|
|
class WebsocketController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Repository
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebsocketController constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
|
|
|
*/
|
|
|
|
public function __construct(Repository $cache)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function __invoke(Request $request, Server $server)
|
|
|
|
{
|
|
|
|
if (! $request->user()->can('connect-to-ws', $server)) {
|
|
|
|
throw new HttpException(
|
|
|
|
Response::HTTP_FORBIDDEN, 'You do not have permission to connect to this server\'s websocket.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-25 03:20:29 +00:00
|
|
|
$now = Chronos::now();
|
|
|
|
|
|
|
|
$signer = new Sha256;
|
2019-09-09 00:48:37 +00:00
|
|
|
|
2019-09-25 03:20:29 +00:00
|
|
|
$token = (new Builder)->issuedBy(config('app.url'))
|
|
|
|
->permittedFor($server->node->getConnectionAddress())
|
|
|
|
->identifiedBy(hash('sha256', $request->user()->id . $server->uuid), true)
|
|
|
|
->issuedAt($now->getTimestamp())
|
|
|
|
->canOnlyBeUsedAfter($now->getTimestamp())
|
|
|
|
->expiresAt($now->addMinutes(15)->getTimestamp())
|
|
|
|
->withClaim('user_id', $request->user()->id)
|
|
|
|
->withClaim('server_uuid', $server->uuid)
|
|
|
|
->getToken($signer, new Key($server->node->daemonSecret));
|
2019-09-09 00:48:37 +00:00
|
|
|
|
|
|
|
$socket = str_replace(['https://', 'http://'], ['wss://', 'ws://'], $server->node->getConnectionAddress());
|
|
|
|
|
|
|
|
return JsonResponse::create([
|
|
|
|
'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
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|