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,39 +319,21 @@ class CreateServer extends CreateRecord
->completedIcon('tabler-check')
->columns(4)
->schema([
Forms\Components\TagsInput::make('ports')
->columnSpanFull()
->columnSpan(2)
->hintIcon('tabler-question-mark')
->hintIconTooltip('Ports are limited from 1025 to 65535')
->placeholder('Example: 25565, 8080, 1337-1340')
->splitKeys(['Tab', ' ', ','])
->helperText(new HtmlString('
These are the ports that users can connect to this Server through.
You would typically port forward these on your home network.
'))
These are the ports that users can connect to this Server through.
You would typically port forward these on your home network.
'))
->label('Ports')
->afterStateUpdated(self::ports(...))
->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')
->columnSpan(2)
->defaultItems(fn () => count($this->eggDefaultPorts))
@ -386,6 +368,12 @@ class CreateServer extends CreateRecord
->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')
@ -705,6 +693,11 @@ class CreateServer extends CreateRecord
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) {
$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\Objects\Endpoint;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use App\Models\Database;
@ -878,34 +879,49 @@ class EditServer extends EditRecord
public function ports($state, Forms\Set $set)
{
$ports = collect();
foreach ($state as $portEntry) {
if (str_contains($portEntry, '-')) {
[$start, $end] = explode('-', $portEntry);
if (!is_numeric($start) || !is_numeric($end)) {
try {
$startEndpoint = new Endpoint($start);
$endEndpoint = new Endpoint($end);
} catch (Exception) {
continue;
}
$start = max((int) $start, Endpoint::PORT_FLOOR);
$end = min((int) $end, Endpoint::PORT_CEIL);
if ($startEndpoint->ip !== $endEndpoint->ip) {
continue;
}
foreach (range($startEndpoint->port, $endEndpoint->port) as $port) {
$ports->push(new Endpoint($port, $startEndpoint->ip));
}
for ($i = $start; $i <= $end; $i++) {
$ports->push($i);
}
}
if (!is_numeric($portEntry)) {
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();
if ($ports->count() > $uniquePorts->count()) {
$ports = $uniquePorts;
}
$ports = $ports->filter(fn ($port) => $port > Endpoint::PORT_FLOOR && $port < Endpoint::PORT_CEIL)->values();
$set('ports', $ports->all());
$this->ports = $ports->all();
}

View File

@ -200,17 +200,7 @@ class Server extends Model
*/
public function getPortMappings(): array
{
$defaultIp = '0.0.0.0';
$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();
return $this->ports->map(fn (Endpoint $port) => (string) $port)->all();
}
public function isInstalled(): bool