2019-08-17 16:03:10 -07:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Repositories\Daemon;
|
2019-08-17 16:03:10 -07:00
|
|
|
|
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;
|
2025-01-01 15:20:16 -05:00
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
2024-05-07 20:00:02 -04:00
|
|
|
use Illuminate\Http\Client\RequestException;
|
2025-09-08 08:56:59 +02:00
|
|
|
use Illuminate\Http\Client\Response;
|
2019-08-17 16:03:10 -07:00
|
|
|
|
2019-09-05 20:33:27 -07:00
|
|
|
class DaemonServerRepository extends DaemonRepository
|
2019-08-17 16:03:10 -07:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Returns details about a server from the Daemon instance.
|
2025-03-03 14:41:19 -05:00
|
|
|
*
|
|
|
|
|
* @return array<string, mixed>
|
2019-08-17 16:03:10 -07:00
|
|
|
*/
|
|
|
|
|
public function getDetails(): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-06-03 14:33:43 +02:00
|
|
|
return $this->getHttpClient()->connectTimeout(1)->timeout(1)->get("/api/servers/{$this->server->uuid}")->throw()->json();
|
2024-05-07 20:00:02 -04:00
|
|
|
} 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()
|
2025-09-04 20:17:59 +02:00
|
|
|
->title(trans('admin/node.cloudflare_issue.title'))
|
|
|
|
|
->body(trans('admin/node.cloudflare_issue.body'))
|
2024-05-07 20:00:02 -04:00
|
|
|
->danger()
|
|
|
|
|
->send();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
report($exception);
|
2019-08-17 16:03:10 -07:00
|
|
|
}
|
|
|
|
|
|
2024-06-11 21:00:51 +02:00
|
|
|
return ['state' => ContainerStatus::Missing->value];
|
2019-08-17 16:03:10 -07:00
|
|
|
}
|
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
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2019-11-16 13:33:01 -08:00
|
|
|
*/
|
2021-08-29 14:09:43 -07:00
|
|
|
public function create(bool $startOnCompletion = true): void
|
2019-11-16 13:33:01 -08:00
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->getHttpClient()->post('/api/servers', [
|
|
|
|
|
'uuid' => $this->server->uuid,
|
|
|
|
|
'start_on_completion' => $startOnCompletion,
|
|
|
|
|
]);
|
2019-11-16 13:33:01 -08:00
|
|
|
}
|
2019-11-24 15:08:54 -08:00
|
|
|
|
|
|
|
|
/**
|
2024-03-12 22:39:16 -04:00
|
|
|
* Triggers a server sync on daemon.
|
2019-11-24 15:08:54 -08:00
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2019-11-24 15:08:54 -08:00
|
|
|
*/
|
2021-08-29 13:32:55 -07:00
|
|
|
public function sync(): void
|
2019-11-24 15:08:54 -08:00
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/sync");
|
2019-11-24 15:08:54 -08:00
|
|
|
}
|
2019-11-24 20:19:31 -08:00
|
|
|
|
|
|
|
|
/**
|
2019-12-21 23:26:15 -08:00
|
|
|
* Delete a server from the daemon, forcibly if passed.
|
|
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2019-11-24 20:19:31 -08:00
|
|
|
*/
|
|
|
|
|
public function delete(): void
|
|
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->getHttpClient()->delete("/api/servers/{$this->server->uuid}");
|
2019-11-24 20:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reinstall a server on the daemon.
|
2020-04-03 13:46:45 -07:00
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2019-11-24 20:19:31 -08:00
|
|
|
*/
|
|
|
|
|
public function reinstall(): void
|
|
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/reinstall");
|
2019-11-24 20:19:31 -08:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 00:50:06 -06:00
|
|
|
/**
|
2020-11-03 21:01:15 -08: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 00:50:06 -06:00
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2020-04-04 00:50:06 -06:00
|
|
|
*/
|
|
|
|
|
public function requestArchive(): void
|
|
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/archive");
|
2020-04-04 00:50:06 -06:00
|
|
|
}
|
2020-11-03 21:01:15 -08:00
|
|
|
|
2024-05-15 16:37:56 +02:00
|
|
|
/**
|
|
|
|
|
* Cancels a server transfer.
|
|
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2024-05-15 16:37:56 +02:00
|
|
|
*/
|
|
|
|
|
public function cancelTransfer(): void
|
|
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$transfer = $this->server->transfer;
|
|
|
|
|
if (!$transfer) {
|
|
|
|
|
return;
|
2024-05-15 16:37:56 +02:00
|
|
|
}
|
2025-01-01 15:20:16 -05:00
|
|
|
|
|
|
|
|
// Source node
|
|
|
|
|
$this->setNode($transfer->oldNode);
|
|
|
|
|
|
|
|
|
|
$this->getHttpClient()->delete("/api/servers/{$this->server->uuid}/transfer");
|
|
|
|
|
|
|
|
|
|
// Destination node
|
|
|
|
|
$this->setNode($transfer->newNode);
|
|
|
|
|
|
|
|
|
|
$this->getHttpClient()->delete('/api/transfer', [
|
|
|
|
|
'json' => [
|
|
|
|
|
'server_id' => $this->server->uuid,
|
|
|
|
|
'server' => [
|
|
|
|
|
'uuid' => $this->server->uuid,
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]);
|
2024-05-15 16:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-06 12:16:12 -08: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.
|
|
|
|
|
*
|
2026-01-13 08:39:50 +01:00
|
|
|
* @deprecated
|
|
|
|
|
* @see self::deauthorize()
|
|
|
|
|
*
|
2025-01-01 15:20:16 -05:00
|
|
|
* @throws ConnectionException
|
2020-12-06 12:16:12 -08:00
|
|
|
*/
|
|
|
|
|
public function revokeUserJTI(int $id): void
|
|
|
|
|
{
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->getHttpClient()
|
|
|
|
|
->post("/api/servers/{$this->server->uuid}/ws/deny", [
|
|
|
|
|
'jtis' => [md5($id . $this->server->uuid)],
|
|
|
|
|
]);
|
2020-11-03 21:01:15 -08:00
|
|
|
}
|
2025-05-09 21:03:32 +02:00
|
|
|
|
2026-01-13 08:39:50 +01:00
|
|
|
/**
|
|
|
|
|
* Deauthorizes a user (disconnects websockets and SFTP) on the Wings instance for the server.
|
|
|
|
|
*
|
|
|
|
|
* @throws ConnectionException
|
|
|
|
|
*/
|
|
|
|
|
public function deauthorize(string $user): void
|
|
|
|
|
{
|
|
|
|
|
$this->getHttpClient()->post('/api/deauthorize-user', [
|
|
|
|
|
'json' => [
|
|
|
|
|
'user' => $user,
|
|
|
|
|
'servers' => [$this->server->uuid],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 21:03:32 +02:00
|
|
|
public function getInstallLogs(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->getHttpClient()
|
|
|
|
|
->get("/api/servers/{$this->server->uuid}/install-logs")
|
|
|
|
|
->throw()
|
|
|
|
|
->json('data');
|
|
|
|
|
}
|
2025-09-08 08:56:59 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a power action to the server instance.
|
|
|
|
|
*
|
|
|
|
|
* @throws ConnectionException
|
|
|
|
|
*/
|
|
|
|
|
public function power(string $action): Response
|
|
|
|
|
{
|
|
|
|
|
return $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/power",
|
|
|
|
|
['action' => $action],
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-17 16:03:10 -07:00
|
|
|
}
|