misc_pterodactyl-panel/app/Repositories/Wings/DaemonServerRepository.php

168 lines
5.1 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\TransferException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class DaemonServerRepository extends DaemonRepository
{
/**
* Returns details about a server from the Daemon instance.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function getDetails(): array
{
Assert::isInstanceOf($this->server, Server::class);
try {
$response = $this->getHttpClient()->get(
sprintf('/api/servers/%s', $this->server->uuid)
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception, false);
}
return json_decode($response->getBody()->__toString(), true);
}
2019-11-16 21:33:01 +00:00
/**
* Creates a new server on the Wings daemon.
*
* @param array $data
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function create(array $data): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post(
'/api/servers', [
'json' => $data,
]
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
* Updates details about a server on the Daemon.
*
* @param array $data
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function update(array $data): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->patch('/api/servers/' . $this->server->uuid, ['json' => $data]);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
2019-12-22 07:26:15 +00:00
* Delete a server from the daemon, forcibly if passed.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
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);
}
}
/**
* Reinstall a server on the daemon.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function reinstall(): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post(sprintf(
'/api/servers/%s/reinstall', $this->server->uuid
));
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
* By default this function will suspend a server instance on the daemon. However, passing
* "true" as the first argument will unsuspend the server.
*
* @param bool $unsuspend
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function suspend(bool $unsuspend = false): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->patch(
'/api/servers/' . $this->server->uuid,
['json' => ['suspended' => ! $unsuspend]]
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
* 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.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function requestArchive(): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post(sprintf(
'/api/servers/%s/archive', $this->server->uuid
));
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
* Revokes an array of JWT JTI's by marking any token generated before the current time on
* the Wings instance as being invalid.
*
* @param array $jtis
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function revokeJTIs(array $jtis): void
{
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);
}
}
}