This commit is contained in:
Lance Pioch 2024-07-04 13:11:13 -04:00
parent 233fd50b2b
commit 8b86707150
3 changed files with 41 additions and 42 deletions

View File

@ -319,8 +319,9 @@ class CreateServer extends CreateRecord
->completedIcon('tabler-check') ->completedIcon('tabler-check')
->columns(4) ->columns(4)
->schema([ ->schema([
Forms\Components\TagsInput::make('ports') Forms\Components\TagsInput::make('ports')
->columnSpanFull() ->columnSpan(2)
->hintIcon('tabler-question-mark') ->hintIcon('tabler-question-mark')
->hintIconTooltip('Ports are limited from 1025 to 65535') ->hintIconTooltip('Ports are limited from 1025 to 65535')
->placeholder('Example: 25565, 8080, 1337-1340') ->placeholder('Example: 25565, 8080, 1337-1340')
@ -333,25 +334,6 @@ class CreateServer extends CreateRecord
->afterStateUpdated(self::ports(...)) ->afterStateUpdated(self::ports(...))
->live(), ->live(),
Forms\Components\Repeater::make('ip')
->columnSpan(2)
->defaultItems(5)
->label('IP Assignments')
->live()
->addable(false)
->deletable(false)
->reorderable(false)
->hintIcon('tabler-question-mark')
->hintIconTooltip('These are the IPs available on the selected Node.')
->simple(
Forms\Components\Select::make('port')
->live()
->placeholder('Select an IP')
// ->afterStateUpdated()
->options(fn () => $this->node?->ipAddresses())
->required(),
),
Forms\Components\Repeater::make('assignments') Forms\Components\Repeater::make('assignments')
->columnSpan(2) ->columnSpan(2)
->defaultItems(fn () => count($this->eggDefaultPorts)) ->defaultItems(fn () => count($this->eggDefaultPorts))
@ -386,6 +368,12 @@ class CreateServer extends CreateRecord
->required(), ->required(),
), ),
Forms\Components\Select::make('ip')
->label('IP Address')
->options(fn () => collect($this->node?->ipAddresses())->mapWithKeys(fn ($ip) => [$ip => $ip]))
->placeholder('Any')
->columnSpan(1),
]), ]),
Wizard\Step::make('Environment') Wizard\Step::make('Environment')
@ -705,6 +693,11 @@ class CreateServer extends CreateRecord
protected function handleRecordCreation(array $data): Model protected function handleRecordCreation(array $data): Model
{ {
$ipAddress = $data['ip'] ?? Endpoint::INADDR_ANY;
foreach ($data['ports'] ?? [] as $i => $port) {
$data['ports'][$i] = (string) new Endpoint($port, $ipAddress);
}
foreach (array_keys($this->eggDefaultPorts) as $i => $env) { foreach (array_keys($this->eggDefaultPorts) as $i => $env) {
$data['environment'][$env] = $data['ports'][$data['assignments'][$i]]; $data['environment'][$env] = $data['ports'][$data['assignments'][$i]];
} }

View File

@ -4,6 +4,7 @@ namespace App\Filament\Resources\ServerResource\Pages;
use App\Models\Node; use App\Models\Node;
use App\Models\Objects\Endpoint; use App\Models\Objects\Endpoint;
use Exception;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
use App\Models\Database; use App\Models\Database;
@ -878,34 +879,49 @@ class EditServer extends EditRecord
public function ports($state, Forms\Set $set) public function ports($state, Forms\Set $set)
{ {
$ports = collect(); $ports = collect();
foreach ($state as $portEntry) { foreach ($state as $portEntry) {
if (str_contains($portEntry, '-')) { if (str_contains($portEntry, '-')) {
[$start, $end] = explode('-', $portEntry); [$start, $end] = explode('-', $portEntry);
if (!is_numeric($start) || !is_numeric($end)) {
try {
$startEndpoint = new Endpoint($start);
$endEndpoint = new Endpoint($end);
} catch (Exception) {
continue; continue;
} }
$start = max((int) $start, Endpoint::PORT_FLOOR); if ($startEndpoint->ip !== $endEndpoint->ip) {
$end = min((int) $end, Endpoint::PORT_CEIL); continue;
}
foreach (range($startEndpoint->port, $endEndpoint->port) as $port) {
$ports->push(new Endpoint($port, $startEndpoint->ip));
}
for ($i = $start; $i <= $end; $i++) { for ($i = $start; $i <= $end; $i++) {
$ports->push($i); $ports->push($i);
} }
}
if (!is_numeric($portEntry)) {
continue; continue;
} }
$ports->push((int) $portEntry); try {
$ports->push(new Endpoint($portEntry));
} catch (Exception) {
continue;
} }
}
$ports = $ports->map(fn ($endpoint) => (string) $endpoint);
$uniquePorts = $ports->unique()->values(); $uniquePorts = $ports->unique()->values();
if ($ports->count() > $uniquePorts->count()) { if ($ports->count() > $uniquePorts->count()) {
$ports = $uniquePorts; $ports = $uniquePorts;
} }
$ports = $ports->filter(fn ($port) => $port > Endpoint::PORT_FLOOR && $port < Endpoint::PORT_CEIL)->values();
$set('ports', $ports->all()); $set('ports', $ports->all());
$this->ports = $ports->all(); $this->ports = $ports->all();
} }

View File

@ -200,17 +200,7 @@ class Server extends Model
*/ */
public function getPortMappings(): array public function getPortMappings(): array
{ {
$defaultIp = '0.0.0.0'; return $this->ports->map(fn (Endpoint $port) => (string) $port)->all();
$ports = collect($this->ports)
->map(fn ($port) => str_contains($port, ':') ? $port : "$defaultIp:$port")
->mapToGroups(function ($port) {
[$ip, $port] = explode(':', $port);
return [$ip => (int) $port];
});
return $ports->all();
} }
public function isInstalled(): bool public function isInstalled(): bool