From 3d966d8826ed93e81e295ad9a1f2c0ad69f72e39 Mon Sep 17 00:00:00 2001 From: notCharles Date: Thu, 24 Oct 2024 16:07:13 -0400 Subject: [PATCH] Port check --- .../AllocationsRelationManager.php | 37 ++++++++++++++----- .../ServerResource/Pages/CreateServer.php | 27 +++++++++++--- .../AllocationsRelationManager.php | 35 ++++++++++++++---- 3 files changed, 77 insertions(+), 22 deletions(-) diff --git a/app/Filament/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php b/app/Filament/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php index 5cdf2b1721..0b3d28d95d 100644 --- a/app/Filament/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php +++ b/app/Filament/Resources/NodeResource/RelationManagers/AllocationsRelationManager.php @@ -8,7 +8,9 @@ use Filament\Forms\Components\TagsInput; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Forms\Set; +use Filament\Notifications\Notification; use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Actions\BulkActionGroup; @@ -97,18 +99,26 @@ public function table(Table $table): Table ->label('Ports') ->inlineLabel() ->live() - ->afterStateUpdated(function ($state, Set $set) { + ->afterStateUpdated(function ($state, Set $set, Get $get) { $ports = collect(); $update = false; foreach ($state as $portEntry) { if (!str_contains($portEntry, '-')) { if (is_numeric($portEntry)) { - $ports->push((int) $portEntry); - - continue; + if (Allocation::query()->where('ip', $get('allocation_ip'))->where('port', $portEntry)->exists()) { + Notification::make() + ->title('Port Already Exists') + ->danger() + ->body('Port ' . $portEntry . ' already exists.') + ->send(); + } else { + $ports->push((int) $portEntry); + + continue; + } } - // Do not add non numerical ports + // Do not add non-numerical ports $update = true; continue; @@ -122,8 +132,19 @@ public function table(Table $table): Table $start = max((int) $start, 0); $end = min((int) $end, 2 ** 16 - 1); - foreach (range($start, $end) as $i) { - $ports->push($i); + $range = $start <= $end ? range($start, $end) : range($end, $start); + foreach ($range as $i) { + if ($i > 1024 && $i <= 65535) { + if (Allocation::query()->where('ip', $get('allocation_ip'))->where('port', $portEntry)->exists()) { + Notification::make() + ->title('Port Already Exists') + ->danger() + ->body('Port ' . $portEntry . ' already exists.') + ->send(); + } else { + $ports->push($i); + } + } } } @@ -139,8 +160,6 @@ public function table(Table $table): Table $ports = $sortedPorts; } - $ports = $ports->filter(fn ($port) => $port > 1024 && $port < 65535)->values(); - if ($update) { $set('allocation_ports', $ports->all()); } diff --git a/app/Filament/Resources/ServerResource/Pages/CreateServer.php b/app/Filament/Resources/ServerResource/Pages/CreateServer.php index c5a5997177..2a8fdbdd2b 100644 --- a/app/Filament/Resources/ServerResource/Pages/CreateServer.php +++ b/app/Filament/Resources/ServerResource/Pages/CreateServer.php @@ -34,6 +34,7 @@ use Filament\Forms\Form; use Filament\Forms\Get; use Filament\Forms\Set; +use Filament\Notifications\Notification; use Filament\Resources\Pages\CreateRecord; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -219,15 +220,23 @@ public function form(Form $form): Form ->label('Ports') ->inlineLabel() ->live() - ->afterStateUpdated(function ($state, Set $set) { + ->afterStateUpdated(function ($state, Set $set, Get $get) { $ports = collect(); $update = false; foreach ($state as $portEntry) { if (!str_contains($portEntry, '-')) { if (is_numeric($portEntry)) { - $ports->push((int) $portEntry); - - continue; + if (Allocation::query()->where('ip', $get('allocation_ip'))->where('port', $portEntry)->exists()) { + Notification::make() + ->title('Port Already Exists') + ->danger() + ->body('Port ' . $portEntry . ' already exists.') + ->send(); + } else { + $ports->push((int) $portEntry); + + continue; + } } // Do not add non-numerical ports @@ -247,7 +256,15 @@ public function form(Form $form): Form $range = $start <= $end ? range($start, $end) : range($end, $start); foreach ($range as $i) { if ($i > 1024 && $i <= 65535) { - $ports->push($i); + if (Allocation::query()->where('ip', $get('allocation_ip'))->where('port', $portEntry)->exists()) { + Notification::make() + ->title('Port Already Exists') + ->danger() + ->body('Port ' . $portEntry . ' already exists.') + ->send(); + } else { + $ports->push($i); + } } } } diff --git a/app/Filament/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php b/app/Filament/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php index c43132b585..8f4bf71806 100644 --- a/app/Filament/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php +++ b/app/Filament/Resources/ServerResource/RelationManagers/AllocationsRelationManager.php @@ -8,7 +8,9 @@ use Filament\Forms\Components\TagsInput; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Forms\Set; +use Filament\Notifications\Notification; use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Actions\Action; @@ -93,18 +95,26 @@ public function table(Table $table): Table ->label('Ports') ->inlineLabel() ->live() - ->afterStateUpdated(function ($state, Set $set) { + ->afterStateUpdated(function ($state, Set $set, Get $get) { $ports = collect(); $update = false; foreach ($state as $portEntry) { if (!str_contains($portEntry, '-')) { if (is_numeric($portEntry)) { - $ports->push((int) $portEntry); + if (Allocation::query()->where('ip', $get('allocation_ip'))->where('port', $portEntry)->exists()) { + Notification::make() + ->title('Port Already Exists') + ->danger() + ->body('Port ' . $portEntry . ' already exists.') + ->send(); + } else { + $ports->push((int) $portEntry); - continue; + continue; + } } - // Do not add non numerical ports + // Do not add non-numerical ports $update = true; continue; @@ -118,8 +128,19 @@ public function table(Table $table): Table $start = max((int) $start, 0); $end = min((int) $end, 2 ** 16 - 1); - foreach (range($start, $end) as $i) { - $ports->push($i); + $range = $start <= $end ? range($start, $end) : range($end, $start); + foreach ($range as $i) { + if ($i > 1024 && $i <= 65535) { + if (Allocation::query()->where('ip', $get('allocation_ip'))->where('port', $portEntry)->exists()) { + Notification::make() + ->title('Port Already Exists') + ->danger() + ->body('Port ' . $portEntry . ' already exists.') + ->send(); + } else { + $ports->push($i); + } + } } } @@ -135,8 +156,6 @@ public function table(Table $table): Table $ports = $sortedPorts; } - $ports = $ports->filter(fn ($port) => $port > 1024 && $port < 65535)->values(); - if ($update) { $set('allocation_ports', $ports->all()); }