From e72b364d214a25dbb320774db029d91c8a11ab80 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Mon, 12 Oct 2026 01:07:41 -0400 Subject: [PATCH] port scope --- app/Actions/Servers/CreateServer.php | 33 +++++++++++++++++++++++++- app/Models/Allocation.php | 35 +++++++--------------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/app/Actions/Servers/CreateServer.php b/app/Actions/Servers/CreateServer.php index d2ded7ccd..d723a0481 100644 --- a/app/Actions/Servers/CreateServer.php +++ b/app/Actions/Servers/CreateServer.php @@ -16,6 +16,7 @@ use Pterodactyl\Contracts\Servers\CreatesServers; use Pterodactyl\Contracts\Servers\DeletesServers; use Pterodactyl\Data\ValidatedEggVariable; use Pterodactyl\Events\Server\OperationCompleted; +use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException; use Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException; @@ -142,15 +143,45 @@ final readonly class CreateServer implements CreatesServers throw_if($nodes === [], NoViableNodeException::class, trans('exceptions.deployment.no_viable_nodes')); + [$ports, $ranges] = $this->parsePorts($deployment->getPorts()); + return Allocation::query() ->unassigned() ->onNodes($nodes) - ->onPorts($deployment->getPorts()) + ->onPorts($ports, $ranges) ->when($deployment->isDedicated(), fn (Builder $query) => $query->onDedicatedIp($nodes)) ->inRandomOrder() ->first() ?? throw new NoViableAllocationException(trans('exceptions.deployment.no_viable_allocations')); } + /** + * Splits deployment ports into single ports and [start, end] ranges. Entries that are + * neither are ignored. + * + * @param list $ports + * @return array{list, list} + * + * @throws DisplayException + */ + private function parsePorts(array $ports): array + { + $single = []; + $ranges = []; + foreach ($ports as $port) { + if (ctype_digit((string) $port)) { + $single[] = (int) $port; + } + + if (preg_match(CreateAllocations::PORT_RANGE_REGEX, (string) $port, $matches)) { + throw_if(abs($matches[2] - $matches[1]) > CreateAllocations::PORT_RANGE_LIMIT, DisplayException::class, trans('exceptions.allocations.too_many_ports')); + + $ranges[] = [(int) $matches[1], (int) $matches[2]]; + } + } + + return [$single, $ranges]; + } + /** * A server given no description gets an empty one, and a feature limit given as null or * left out is 0. A server given no OOM flag, or null, has the OOM killer disabled. diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index bc4eba242..6265f658a 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -11,8 +11,6 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Pterodactyl\Actions\Allocations\CreateAllocations; -use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Models\Traits\HasHashid; /** @@ -104,38 +102,23 @@ class Allocation extends Model } /** - * Allocations on any of the given ports or "start-end" port ranges. Entries that are - * neither are ignored, and with none left the query is unrestricted. + * Allocations whose port is one of the given ports or falls inside one of the + * given [start, end] ranges; with neither given the query is unrestricted. * * @param Builder $query - * @param list $ports - * - * @throws DisplayException + * @param list $ports + * @param list $ranges */ #[Scope] - protected function onPorts(Builder $query, array $ports): void + protected function onPorts(Builder $query, array $ports, array $ranges = []): void { - $single = []; - $ranges = []; - foreach ($ports as $port) { - if (ctype_digit((string) $port)) { - $single[] = (int) $port; - } - - if (preg_match(CreateAllocations::PORT_RANGE_REGEX, (string) $port, $matches)) { - throw_if(abs($matches[2] - $matches[1]) > CreateAllocations::PORT_RANGE_LIMIT, DisplayException::class, trans('exceptions.allocations.too_many_ports')); - - $ranges[] = [(int) $matches[1], (int) $matches[2]]; - } - } - - if ($single === [] && $ranges === []) { + if ($ports === [] && $ranges === []) { return; } - $query->where(function (Builder $inner) use ($single, $ranges): void { - if ($single !== []) { - $inner->orWhereIn('port', $single); + $query->where(function (Builder $inner) use ($ports, $ranges): void { + if ($ports !== []) { + $inner->orWhereIn('port', $ports); } foreach ($ranges as $range) {