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

192 lines
5.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;
use Illuminate\Support\Carbon;
use Pterodactyl\Models\Traits\HasHashid;
2022-10-14 10:59:20 -06:00
2019-11-16 13:33:01 -08:00
/**
* Pterodactyl\Models\Allocation.
*
* @property int $id
* @property int $node_id
* @property string $ip
* @property string|null $ip_alias
* @property int $port
* @property int|null $server_id
* @property string|null $notes
2026-10-05 18:10:18 -04:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string $alias
* @property bool $has_alias
2024-10-21 19:18:20 -06:00
* @property Server|null $server
* @property Node $node
*
2026-10-05 18:10:18 -04:00
* @method static AllocationFactory factory(...$parameters)
* @method static Builder|Allocation newModelQuery()
* @method static Builder|Allocation newQuery()
* @method static Builder|Allocation query()
* @method static Builder|Allocation whereCreatedAt($value)
* @method static Builder|Allocation whereId($value)
* @method static Builder|Allocation whereIp($value)
* @method static Builder|Allocation whereIpAlias($value)
* @method static Builder|Allocation whereNodeId($value)
* @method static Builder|Allocation whereNotes($value)
* @method static Builder|Allocation wherePort($value)
* @method static Builder|Allocation whereServerId($value)
* @method static Builder|Allocation whereUpdatedAt($value)
2022-10-14 10:59:20 -06:00
*
2026-10-05 18:10:18 -04:00
* @mixin \Illuminate\Database\Eloquent\Model
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;
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
2026-10-05 18:10:18 -04:00
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
* Gets information for the server associated with this allocation.
*
* @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 the Node model associated with this allocation.
*
* @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
/**
* Accessor to automatically provide the IP alias if defined.
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
/**
* Accessor to quickly determine if this allocation has an alias.
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
/**
* Allocations not yet assigned to a server.
*
* @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
* @param list<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-05 18:10:18 -04:00
* 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.
*
2026-10-05 18:10:18 -04:00
* @param Builder<self> $query
* @param list<int> $ports
* @param list<array{int, int}> $ranges
*/
2026-10-05 18:10:18 -04:00
#[Scope]
protected function onPorts(Builder $query, array $ports, array $ranges = []): void
{
2026-10-05 18:10:18 -04:00
if ($ports === [] && $ranges === []) {
return;
}
$query->where(function (Builder $inner) use ($ports, $ranges): void {
if ($ports !== []) {
$inner->orWhereIn('port', $ports);
}
foreach ($ranges as $range) {
$inner->orWhereBetween('port', $range);
}
});
}
/**
* Allocations on node/IP pairs that no server uses yet.
*
* @param Builder<self> $query
* @param list<int> $nodes restrict the occupied-IP lookup to these nodes
*/
#[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
}