Files
panel/app/Repositories/Daemon/DaemonServerRepository.php

218 lines
6.9 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Repositories\Daemon;
2024-04-19 22:32:52 -04:00
use App\Enums\ContainerStatus;
2024-05-07 20:00:02 -04:00
use App\Enums\HttpStatusCode;
2024-04-19 22:32:52 -04:00
use Exception;
2024-05-07 20:00:02 -04:00
use Filament\Notifications\Notification;
use Illuminate\Http\Client\RequestException;
use Webmozart\Assert\Assert;
2024-03-12 22:39:16 -04:00
use App\Models\Server;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\TransferException;
2024-03-12 22:39:16 -04:00
use App\Exceptions\Http\Connection\DaemonConnectionException;
class DaemonServerRepository extends DaemonRepository
{
/**
* Returns details about a server from the Daemon instance.
*
2024-03-12 22:39:16 -04:00
* @throws \App\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)
2024-05-07 20:00:02 -04:00
)->throw();
} catch (RequestException $exception) {
$cfId = $exception->response->header('Cf-Ray');
$cfCache = $exception->response->header('Cf-Cache-Status');
$code = HttpStatusCode::tryFrom($exception->getCode());
$requestFromCloudflare = !empty($cfId);
$requestCachedFromCloudflare = !empty($cfCache);
$requestBadGateway = $code === HttpStatusCode::BadGateway;
if ($requestBadGateway && $requestFromCloudflare && !$requestCachedFromCloudflare) {
Notification::make()
->title('Cloudflare Issue')
->body('Your Node is not accessible by Cloudflare')
->danger()
->send();
}
} catch (Exception $exception) {
report($exception);
}
2024-05-07 19:59:41 -04:00
return $response?->json() ?? ['state' => ContainerStatus::Missing->value];
}
2019-11-16 13:33:01 -08:00
/**
2024-03-23 16:10:47 -04:00
* Creates a new server on the daemon.
2019-11-16 13:33:01 -08:00
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
2019-11-16 13:33:01 -08:00
*/
public function create(bool $startOnCompletion = true): void
2019-11-16 13:33:01 -08:00
{
Assert::isInstanceOf($this->server, Server::class);
try {
2024-04-15 01:34:17 -04:00
$response = $this->getHttpClient()->post('/api/servers', [
'uuid' => $this->server->uuid,
'start_on_completion' => $startOnCompletion,
]);
} catch (GuzzleException $exception) {
2019-11-16 13:33:01 -08:00
throw new DaemonConnectionException($exception);
}
}
/**
2024-03-12 22:39:16 -04:00
* Triggers a server sync on daemon.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function sync(): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/sync");
} catch (GuzzleException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
2019-12-21 23:26:15 -08:00
* Delete a server from the daemon, forcibly if passed.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function delete(): void
{
2019-12-21 23:26:15 -08: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.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function reinstall(): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post(sprintf(
2021-01-23 12:33:34 -08:00
'/api/servers/%s/reinstall',
$this->server->uuid
));
} 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.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function requestArchive(): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()->post(sprintf(
2021-01-23 12:33:34 -08:00
'/api/servers/%s/archive',
$this->server->uuid
));
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
/**
* Cancels a server transfer.
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function cancelTransfer(): void
{
Assert::isInstanceOf($this->server, Server::class);
if ($transfer = $this->server->transfer) {
// Source node
$this->setNode($transfer->oldNode);
try {
$this->getHttpClient()->delete(sprintf(
'/api/servers/%s/transfer',
$this->server->uuid
));
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
// Destination node
$this->setNode($transfer->newNode);
try {
$this->getHttpClient()->delete('/api/transfer', [
'json' => [
'server_id' => $this->server->uuid,
'server' => [
'uuid' => $this->server->uuid,
],
],
]);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
}
/**
* 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.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function revokeUserJTI(int $id): void
{
Assert::isInstanceOf($this->server, Server::class);
$this->revokeJTIs([md5($id . $this->server->uuid)]);
}
/**
* Revokes an array of JWT JTI's by marking any token generated before the current time on
2024-03-12 22:39:16 -04:00
* the daemon instance as being invalid.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
protected function revokeJTIs(array $jtis): void
{
Assert::isInstanceOf($this->server, Server::class);
try {
$this->getHttpClient()
->post(sprintf('/api/servers/%s/ws/deny', $this->server->uuid), [
'jtis' => $jtis,
]);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
}