From a2108c3d9119e3715bb895f7e5d1004c4e87744b Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 20 Oct 2024 15:23:46 -0400 Subject: [PATCH] Various improvements --- .../Resources/ServerResource/Pages/CreateServer.php | 7 +++++-- .../Resources/ServerResource/Pages/EditServer.php | 7 +++++-- .../Resources/UserResource/Pages/EditProfile.php | 2 +- app/Livewire/EndpointSynth.php | 9 +++++---- app/Models/Node.php | 6 +++++- app/Models/Objects/Endpoint.php | 10 +++++++++- app/Models/Server.php | 1 + app/Services/Servers/ServerCreationService.php | 13 ++----------- app/Services/Servers/VariableValidatorService.php | 2 +- 9 files changed, 34 insertions(+), 23 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/CreateServer.php b/app/Filament/Resources/ServerResource/Pages/CreateServer.php index bc615d40e..041a6d8f0 100644 --- a/app/Filament/Resources/ServerResource/Pages/CreateServer.php +++ b/app/Filament/Resources/ServerResource/Pages/CreateServer.php @@ -46,8 +46,11 @@ class CreateServer extends CreateRecord protected static bool $canCreateAnother = false; public ?Node $node = null; + public ?Egg $egg = null; + public array $ports = []; + public array $eggDefaultPorts = []; private ServerCreationService $serverCreationService; @@ -769,7 +772,7 @@ class CreateServer extends CreateRecord ->all(); } - public function ports($state, Forms\Set $set) + public function ports(array $state, Forms\Set $set): void { $ports = collect(); foreach ($state as $portEntry) { @@ -804,7 +807,7 @@ class CreateServer extends CreateRecord $this->ports = $ports->all(); } - public function resetEggVariables(Forms\Set $set, Forms\Get $get) + public function resetEggVariables(Forms\Set $set, Forms\Get $get): void { $set('assignments', []); diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index f47329a86..989ae92bf 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -47,8 +47,11 @@ use Webbingbrasil\FilamentCopyActions\Forms\Actions\CopyAction; class EditServer extends EditRecord { public ?Node $node = null; + public ?Egg $egg = null; + public array $ports = []; + public array $eggDefaultPorts = []; protected static string $resource = ServerResource::class; @@ -901,7 +904,7 @@ class EditServer extends EditRecord ->all(); } - public function ports($state, Forms\Set $set) + public function ports(array $state, Forms\Set $set): void { $ports = collect(); @@ -949,7 +952,7 @@ class EditServer extends EditRecord $this->ports = $ports->all(); } - public function portOptions(Egg $egg, string $startup = null): array + public function portOptions(Egg $egg, ?string $startup = null): array { if (empty($startup)) { $startup = $egg->startup; diff --git a/app/Filament/Resources/UserResource/Pages/EditProfile.php b/app/Filament/Resources/UserResource/Pages/EditProfile.php index d7942eff2..67c611b2d 100644 --- a/app/Filament/Resources/UserResource/Pages/EditProfile.php +++ b/app/Filament/Resources/UserResource/Pages/EditProfile.php @@ -289,7 +289,7 @@ class EditProfile extends \Filament\Pages\Auth\EditProfile if ($token = $data['2facode'] ?? null) { $tokens = $this->toggleTwoFactorService->handle($record, $token, true); - cache()->set("users.$record->id.2fa.tokens", implode("\n", $tokens), now()->addSeconds(15)); + cache()->set("users.$record->id.2fa.tokens", implode("\n", $tokens), 15); $this->redirectRoute('filament.admin.auth.profile', ['tab' => '-2fa-tab']); } diff --git a/app/Livewire/EndpointSynth.php b/app/Livewire/EndpointSynth.php index 87352dbd2..a359e4b07 100644 --- a/app/Livewire/EndpointSynth.php +++ b/app/Livewire/EndpointSynth.php @@ -4,22 +4,23 @@ namespace App\Livewire; use App\Models\Objects\Endpoint; use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth; +use Stringable; class EndpointSynth extends Synth { - public static $key = 'endpoint'; + public static string $key = 'endpoint'; - public static function match($target) + public static function match(mixed $target): bool { return $target instanceof Endpoint; } - public function dehydrate($target) + public function dehydrate(Stringable $target): string { return (string) $target; } - public function hydrate($value) + public function hydrate(mixed $value): ?Endpoint { if (!is_string($value) && !is_int($value)) { return null; diff --git a/app/Models/Node.php b/app/Models/Node.php index c2b03d54f..3f4eda6a3 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -53,6 +53,7 @@ class Node extends Model public const RESOURCE_NAME = 'node'; public const DAEMON_TOKEN_ID_LENGTH = 16; + public const DAEMON_TOKEN_LENGTH = 64; /** @@ -135,7 +136,9 @@ class Node extends Model } public int $servers_sum_memory = 0; + public int $servers_sum_disk = 0; + public int $servers_sum_cpu = 0; public function getRouteKeyName(): string @@ -264,6 +267,7 @@ class Node extends Model { return once(function () { try { + // @phpstan-ignore-next-line return resolve(DaemonConfigurationRepository::class) ->setNode($this) ->getSystemInformation(connectTimeout: 3); @@ -299,7 +303,7 @@ class Node extends Model return $statuses; } - public function statistics() + public function statistics(): array { $default = [ 'memory_total' => 0, diff --git a/app/Models/Objects/Endpoint.php b/app/Models/Objects/Endpoint.php index 18b80b71b..68361156d 100644 --- a/app/Models/Objects/Endpoint.php +++ b/app/Models/Objects/Endpoint.php @@ -8,18 +8,26 @@ use InvalidArgumentException; class Endpoint implements Jsonable { public const CIDR_MAX_BITS = 27; + public const CIDR_MIN_BITS = 32; + public const PORT_FLOOR = 1024; + public const PORT_CEIL = 65535; + public const PORT_RANGE_LIMIT = 1000; + public const PORT_RANGE_REGEX = '/^(\d{4,5})-(\d{4,5})$/'; + public const INADDR_ANY = '0.0.0.0'; + public const INADDR_LOOPBACK = '127.0.0.1'; public int $port; + public string $ip; - public function __construct(string|int $port, string $ip = null) + public function __construct(string|int $port, ?string $ip = null) { $this->ip = $ip ?? self::INADDR_ANY; $this->port = (int) $port; diff --git a/app/Models/Server.php b/app/Models/Server.php index 12bec8a3a..cbbf32d70 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -433,6 +433,7 @@ class Server extends Model return $this->status->color(); } + public function getPrimaryEndpoint(): ?Endpoint { $endpoint = $this->ports->first(); diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 5392e5011..af9e06d8d 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -18,9 +18,6 @@ use App\Models\Egg; class ServerCreationService { - /** - * ServerCreationService constructor. - */ public function __construct( private ConnectionInterface $connection, private DaemonServerRepository $daemonServerRepository, @@ -32,19 +29,13 @@ class ServerCreationService /** * Create a server on the Panel and trigger a request to the Daemon to begin the server creation process. * This function will attempt to set as many additional values as possible given the input data. - * - * @throws \Throwable - * @throws \App\Exceptions\DisplayException - * @throws \Illuminate\Validation\ValidationException - * @throws \App\Exceptions\Service\Deployment\NoViableAllocationException */ - public function handle(array $data, ?DeploymentObject $deployment = null, $validateVariables = true): Server + public function handle(array $data, ?DeploymentObject $deployment = null, bool $validateVariables = true): Server { if (!isset($data['oom_killer']) && isset($data['oom_disabled'])) { $data['oom_killer'] = !$data['oom_disabled']; } - /** @var Egg $egg */ $egg = Egg::query()->findOrFail($data['egg_id']); // Fill missing fields from egg @@ -62,7 +53,7 @@ class ServerCreationService // // If that connection fails out we will attempt to perform a cleanup by just // deleting the server itself from the system. - /** @var \App\Models\Server $server */ + /** @var Server $server */ $server = $this->connection->transaction(function () use ($data, $eggVariableData) { // Create the server and assign any additional allocations to it. $server = $this->createModel($data); diff --git a/app/Services/Servers/VariableValidatorService.php b/app/Services/Servers/VariableValidatorService.php index bfa986007..f8c59b695 100644 --- a/app/Services/Servers/VariableValidatorService.php +++ b/app/Services/Servers/VariableValidatorService.php @@ -25,7 +25,7 @@ class VariableValidatorService * * @throws \Illuminate\Validation\ValidationException */ - public function handle(int $egg, array $fields = [], $validate = true): Collection + public function handle(int $egg, array $fields = [], bool $validate = true): Collection { $query = EggVariable::query()->where('egg_id', $egg); if (!$this->isUserLevel(User::USER_LEVEL_ADMIN)) {