2019-08-17 23:03:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Wings;
|
|
|
|
|
2019-09-06 03:33:27 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Pterodactyl\Models\Server;
|
2021-08-29 20:32:55 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2019-08-17 23:03:10 +00:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
|
2019-09-06 03:33:27 +00:00
|
|
|
class DaemonServerRepository extends DaemonRepository
|
2019-08-17 23:03:10 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns details about a server from the Daemon instance.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
|
|
|
public function getDetails(): array
|
|
|
|
{
|
2019-09-06 04:16:36 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
2019-09-06 03:33:27 +00:00
|
|
|
|
2019-08-17 23:03:10 +00:00
|
|
|
try {
|
|
|
|
$response = $this->getHttpClient()->get(
|
2019-09-06 03:33:27 +00:00
|
|
|
sprintf('/api/servers/%s', $this->server->uuid)
|
2019-08-17 23:03:10 +00:00
|
|
|
);
|
|
|
|
} catch (TransferException $exception) {
|
2020-07-18 17:23:28 +00:00
|
|
|
throw new DaemonConnectionException($exception, false);
|
2019-08-17 23:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json_decode($response->getBody()->__toString(), true);
|
|
|
|
}
|
2019-11-16 21:33:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new server on the Wings daemon.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
2021-08-29 21:09:43 +00:00
|
|
|
public function create(bool $startOnCompletion = true): void
|
2019-11-16 21:33:01 +00:00
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
2021-08-29 21:09:43 +00:00
|
|
|
$this->getHttpClient()->post('/api/servers', [
|
|
|
|
'json' => [
|
|
|
|
'uuid' => $this->server->uuid,
|
|
|
|
'start_on_completion' => $startOnCompletion,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
} catch (GuzzleException $exception) {
|
2019-11-16 21:33:01 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:08:54 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-29 20:32:55 +00:00
|
|
|
* Triggers a server sync on Wings.
|
2019-11-24 23:08:54 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
2021-08-29 20:32:55 +00:00
|
|
|
public function sync(): void
|
2019-11-24 23:08:54 +00:00
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
2021-08-29 20:32:55 +00:00
|
|
|
$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/sync");
|
|
|
|
} catch (GuzzleException $exception) {
|
2019-11-24 23:08:54 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 04:19:31 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-22 07:26:15 +00:00
|
|
|
* Delete a server from the daemon, forcibly if passed.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-11-25 04:19:31 +00:00
|
|
|
*/
|
|
|
|
public function delete(): void
|
|
|
|
{
|
2019-12-22 07:26:15 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->getHttpClient()->delete('/api/servers/' . $this->server->uuid);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-11-25 04:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reinstall a server on the daemon.
|
2020-04-03 20:46:45 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2019-11-25 04:19:31 +00:00
|
|
|
*/
|
|
|
|
public function reinstall(): void
|
|
|
|
{
|
2020-04-03 20:46:45 +00:00
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->getHttpClient()->post(sprintf(
|
2021-01-23 20:33:34 +00:00
|
|
|
'/api/servers/%s/reinstall',
|
|
|
|
$this->server->uuid
|
2020-04-03 20:46:45 +00:00
|
|
|
));
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2019-11-25 04:19:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
2020-11-04 05:01:15 +00:00
|
|
|
* Requests the daemon to create a full archive of the server. Once the daemon is finished
|
|
|
|
* they will send a POST request to "/api/remote/servers/{uuid}/archive" with a boolean.
|
2020-04-04 06:50:06 +00:00
|
|
|
*
|
2020-11-04 05:01:15 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2020-04-04 06:50:06 +00:00
|
|
|
*/
|
|
|
|
public function requestArchive(): void
|
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->getHttpClient()->post(sprintf(
|
2021-01-23 20:33:34 +00:00
|
|
|
'/api/servers/%s/archive',
|
|
|
|
$this->server->uuid
|
2020-04-04 06:50:06 +00:00
|
|
|
));
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 05:01:15 +00:00
|
|
|
|
2020-12-06 20:16:12 +00:00
|
|
|
/**
|
|
|
|
* Revokes a single user's JTI by using their ID. This is simply a helper function to
|
|
|
|
* make it easier to revoke tokens on the fly. This ensures that the JTI key is formatted
|
|
|
|
* correctly and avoids any costly mistakes in the codebase.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
|
|
|
public function revokeUserJTI(int $id): void
|
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->revokeJTIs([md5($id . $this->server->uuid)]);
|
2020-12-06 20:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 05:01:15 +00:00
|
|
|
/**
|
|
|
|
* Revokes an array of JWT JTI's by marking any token generated before the current time on
|
|
|
|
* the Wings instance as being invalid.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
2020-12-06 20:16:12 +00:00
|
|
|
protected function revokeJTIs(array $jtis): void
|
2020-11-04 05:01:15 +00:00
|
|
|
{
|
|
|
|
Assert::isInstanceOf($this->server, Server::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->getHttpClient()
|
|
|
|
->post(sprintf('/api/servers/%s/ws/deny', $this->server->uuid), [
|
|
|
|
'json' => ['jtis' => $jtis],
|
|
|
|
]);
|
|
|
|
} catch (TransferException $exception) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 23:03:10 +00:00
|
|
|
}
|