Path: blob/1.0-develop/app/Repositories/Wings/DaemonServerRepository.php
7460 views
<?php12namespace Pterodactyl\Repositories\Wings;34use Webmozart\Assert\Assert;5use Pterodactyl\Models\Server;6use GuzzleHttp\Exception\GuzzleException;7use GuzzleHttp\Exception\TransferException;8use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;910/**11* @method \Pterodactyl\Repositories\Wings\DaemonServerRepository setNode(\Pterodactyl\Models\Node $node)12* @method \Pterodactyl\Repositories\Wings\DaemonServerRepository setServer(\Pterodactyl\Models\Server $server)13*/14class DaemonServerRepository extends DaemonRepository15{16/**17* Returns details about a server from the Daemon instance.18*19* @throws DaemonConnectionException20*/21public function getDetails(): array22{23Assert::isInstanceOf($this->server, Server::class);2425try {26$response = $this->getHttpClient()->get(27sprintf('/api/servers/%s', $this->server->uuid)28);29} catch (TransferException $exception) {30throw new DaemonConnectionException($exception, false);31}3233return json_decode($response->getBody()->__toString(), true);34}3536/**37* Creates a new server on the Wings daemon.38*39* @throws DaemonConnectionException40*/41public function create(bool $startOnCompletion = true): void42{43Assert::isInstanceOf($this->server, Server::class);4445try {46$this->getHttpClient()->post('/api/servers', [47'json' => [48'uuid' => $this->server->uuid,49'start_on_completion' => $startOnCompletion,50],51]);52} catch (GuzzleException $exception) {53throw new DaemonConnectionException($exception);54}55}5657/**58* Triggers a server sync on Wings.59*60* @throws DaemonConnectionException61*/62public function sync(): void63{64Assert::isInstanceOf($this->server, Server::class);6566try {67$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/sync");68} catch (GuzzleException $exception) {69throw new DaemonConnectionException($exception);70}71}7273/**74* Delete a server from the daemon, forcibly if passed.75*76* @throws DaemonConnectionException77*/78public function delete(): void79{80Assert::isInstanceOf($this->server, Server::class);8182try {83$this->getHttpClient()->delete('/api/servers/' . $this->server->uuid);84} catch (TransferException $exception) {85throw new DaemonConnectionException($exception);86}87}8889/**90* Reinstall a server on the daemon.91*92* @throws DaemonConnectionException93*/94public function reinstall(): void95{96Assert::isInstanceOf($this->server, Server::class);9798try {99$this->getHttpClient()->post(sprintf(100'/api/servers/%s/reinstall',101$this->server->uuid102));103} catch (TransferException $exception) {104throw new DaemonConnectionException($exception);105}106}107108/**109* Requests the daemon to create a full archive of the server. Once the daemon is finished110* they will send a POST request to "/api/remote/servers/{uuid}/archive" with a boolean.111*112* @throws DaemonConnectionException113*/114public function requestArchive(): void115{116Assert::isInstanceOf($this->server, Server::class);117118try {119$this->getHttpClient()->post(sprintf(120'/api/servers/%s/archive',121$this->server->uuid122));123} catch (TransferException $exception) {124throw new DaemonConnectionException($exception);125}126}127128/**129* Revokes a single user's JTI by using their ID. This is simply a helper function to130* make it easier to revoke tokens on the fly. This ensures that the JTI key is formatted131* correctly and avoids any costly mistakes in the codebase.132*133* @throws DaemonConnectionException134*/135public function revokeUserJTI(int $id): void136{137Assert::isInstanceOf($this->server, Server::class);138139$this->revokeJTIs([md5($id . $this->server->uuid)]);140}141142/**143* Revokes an array of JWT JTI's by marking any token generated before the current time on144* the Wings instance as being invalid.145*146* @throws DaemonConnectionException147*/148protected function revokeJTIs(array $jtis): void149{150Assert::isInstanceOf($this->server, Server::class);151152try {153$this->getHttpClient()154->post(sprintf('/api/servers/%s/ws/deny', $this->server->uuid), [155'json' => ['jtis' => $jtis],156]);157} catch (TransferException $exception) {158throw new DaemonConnectionException($exception);159}160}161}162163164