Files
pterodactyl-panel/app/Models/Allocation.php
T

164 lines
4.3 KiB
PHP
Raw Normal View History

2015-12-07 00:47:19 -05:00
<?php
2016-12-07 22:46:38 +00:00
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2015-12-07 00:47:19 -05:00
namespace Pterodactyl\Models;
2026-10-05 18:10:18 -04:00
use Database\Factories\AllocationFactory;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-10-21 19:18:20 -06:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-10-12 00:09:41 -04:00
use Pterodactyl\Actions\Allocations\CreateAllocations;
use Pterodactyl\Exceptions\DisplayException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Traits\HasHashid;
2022-10-14 10:59:20 -06:00
2019-11-16 13:33:01 -08:00
/**
2026-10-11 22:55:07 -04:00
* @property-read Node $node
2019-11-16 13:33:01 -08:00
*/
2026-10-05 18:10:18 -04:00
#[Guarded(['id', 'created_at', 'updated_at'])]
class Allocation extends Model
2015-12-07 00:47:19 -05:00
{
2026-10-05 18:10:18 -04:00
/** @use HasFactory<AllocationFactory> */
2024-10-21 19:18:20 -06:00
use HasFactory;
2026-10-05 18:10:18 -04:00
use HasHashid;
public const string RESOURCE_NAME = 'allocation';
2015-12-07 00:47:19 -05:00
/**
2026-10-05 18:10:18 -04:00
* @return array<string, string>
2015-12-07 00:47:19 -05:00
*/
2026-10-05 18:10:18 -04:00
protected function casts(): array
{
return [
'node_id' => 'integer',
'port' => 'integer',
'server_id' => 'integer',
];
}
2015-12-07 00:47:19 -05:00
2026-10-05 18:10:18 -04:00
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
public function toString(): string
{
return sprintf('%s:%s', $this->ip, $this->port);
}
2017-08-05 21:10:32 -05:00
/**
2026-10-05 18:10:18 -04:00
* @return BelongsTo<Server, $this>
2017-08-05 21:10:32 -05:00
*/
2026-10-05 18:10:18 -04:00
public function server(): BelongsTo
2022-05-22 14:10:01 -04:00
{
2026-10-05 18:10:18 -04:00
return $this->belongsTo(Server::class);
2022-05-22 14:10:01 -04:00
}
/**
2026-10-05 18:10:18 -04:00
* @return BelongsTo<Node, $this>
*/
2026-10-05 18:10:18 -04:00
public function node(): BelongsTo
{
2026-10-05 18:10:18 -04:00
return $this->belongsTo(Node::class);
}
2017-08-05 21:10:32 -05:00
/**
2026-10-05 18:10:18 -04:00
* @return Attribute<string, never>
2017-08-05 21:10:32 -05:00
*/
2026-10-05 18:10:18 -04:00
protected function alias(): Attribute
2017-08-05 21:10:32 -05:00
{
2026-10-05 18:10:18 -04:00
return Attribute::make(get: fn (): string => $this->ip_alias ?? $this->ip);
2017-08-05 21:10:32 -05:00
}
2017-08-05 21:10:32 -05:00
/**
2026-10-05 18:10:18 -04:00
* @return Attribute<bool, never>
2017-08-05 21:10:32 -05:00
*/
2026-10-05 18:10:18 -04:00
protected function hasAlias(): Attribute
2017-08-05 21:10:32 -05:00
{
2026-10-05 18:10:18 -04:00
return Attribute::make(get: fn (): bool => $this->ip_alias !== null);
2017-08-05 21:10:32 -05:00
}
2026-10-05 18:10:18 -04:00
/**
* @param Builder<self> $query
*/
#[Scope]
protected function unassigned(Builder $query): void
{
2026-10-05 18:10:18 -04:00
$query->whereNull('server_id');
}
2017-08-05 21:10:32 -05:00
/**
2026-10-05 18:10:18 -04:00
* Allocations on any of the given nodes; an empty list leaves the query unrestricted.
*
2026-10-05 18:10:18 -04:00
* @param Builder<self> $query
2026-10-12 00:09:41 -04:00
* @param array<int> $nodes
2017-08-05 21:10:32 -05:00
*/
2026-10-05 18:10:18 -04:00
#[Scope]
protected function onNodes(Builder $query, array $nodes): void
2017-08-05 21:10:32 -05:00
{
2026-10-05 18:10:18 -04:00
$query->when($nodes !== [], fn (Builder $q) => $q->whereIn('node_id', $nodes));
2017-08-05 21:10:32 -05:00
}
/**
2026-10-12 00:09:41 -04:00
* 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.
*
2026-10-05 18:10:18 -04:00
* @param Builder<self> $query
2026-10-12 00:09:41 -04:00
* @param list<int|string> $ports
*
* @throws DisplayException
*/
2026-10-05 18:10:18 -04:00
#[Scope]
2026-10-12 00:09:41 -04:00
protected function onPorts(Builder $query, array $ports): void
{
2026-10-12 00:09:41 -04:00
$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 === []) {
2026-10-05 18:10:18 -04:00
return;
}
2026-10-12 00:09:41 -04:00
$query->where(function (Builder $inner) use ($single, $ranges): void {
if ($single !== []) {
$inner->orWhereIn('port', $single);
2026-10-05 18:10:18 -04:00
}
foreach ($ranges as $range) {
$inner->orWhereBetween('port', $range);
}
});
}
/**
* Allocations on node/IP pairs that no server uses yet.
*
* @param Builder<self> $query
2026-10-12 00:09:41 -04:00
* @param array<int> $nodes restrict the occupied-IP lookup to these nodes
2026-10-05 18:10:18 -04:00
*/
#[Scope]
protected function onDedicatedIp(Builder $query, array $nodes = []): void
{
$occupied = self::query()
->selectRaw('CONCAT_WS("-", node_id, ip)')
->whereNotNull('server_id')
->onNodes($nodes);
$query->whereNotIn($query->raw('CONCAT_WS("-", node_id, ip)'), $occupied);
}
2015-12-07 00:47:19 -05:00
}