Files

110 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2017-08-05 21:10:32 -05:00
<?php
namespace Pterodactyl\Services\Allocations;
use IPTools\Network;
use Pterodactyl\Models\Node;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
2017-08-05 21:10:32 -05:00
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException;
use Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException;
use Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException;
use Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException;
2017-08-05 21:10:32 -05:00
class AssignmentService
{
public const CIDR_MAX_BITS = 25;
2021-01-23 12:33:34 -08:00
public const CIDR_MIN_BITS = 32;
public const PORT_FLOOR = 1024;
public const PORT_CEIL = 65535;
public const PORT_RANGE_LIMIT = 1000;
public const PORT_RANGE_REGEX = '/^(\d{4,5})-(\d{4,5})$/';
2017-08-05 21:10:32 -05:00
/**
* AssignmentService constructor.
*/
2022-10-14 10:59:20 -06:00
public function __construct(protected AllocationRepositoryInterface $repository, protected ConnectionInterface $connection)
{
2017-08-05 21:10:32 -05:00
}
/**
* Insert allocations into the database and link them to a specific node.
*
2024-10-21 19:18:20 -06:00
* @throws DisplayException
* @throws CidrOutOfRangeException
* @throws InvalidPortMappingException
* @throws PortOutOfRangeException
* @throws TooManyPortsInRangeException
2017-08-05 21:10:32 -05:00
*/
2022-10-14 10:59:20 -06:00
public function handle(Node $node, array $data): void
2017-08-05 21:10:32 -05:00
{
$explode = explode('/', $data['allocation_ip']);
if (count($explode) !== 1) {
2021-01-23 12:33:34 -08:00
if (!ctype_digit($explode[1]) || ($explode[1] > self::CIDR_MIN_BITS || $explode[1] < self::CIDR_MAX_BITS)) {
throw new CidrOutOfRangeException();
2017-08-05 21:10:32 -05:00
}
}
try {
// TODO: how should we approach supporting IPv6 with this?
// gethostbyname only supports IPv4, but the alternative (dns_get_record) returns
// an array of records, which is not ideal for this use case, we need a SINGLE
// IP to use, not multiple.
$underlying = gethostbyname($data['allocation_ip']);
$parsed = Network::parse($underlying);
2023-02-23 12:30:16 -07:00
} catch (\Exception $exception) {
// @phpstan-ignore-next-line variable.undefined
throw new DisplayException("Could not parse provided allocation IP address ({$underlying}): {$exception->getMessage()}", $exception);
}
2017-08-05 21:10:32 -05:00
$this->connection->beginTransaction();
foreach ($parsed as $ip) {
2017-08-05 21:10:32 -05:00
foreach ($data['allocation_ports'] as $port) {
2021-01-23 12:33:34 -08:00
if (!is_digit($port) && !preg_match(self::PORT_RANGE_REGEX, $port)) {
throw new InvalidPortMappingException($port);
2017-08-05 21:10:32 -05:00
}
$insertData = [];
if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) {
$block = range($matches[1], $matches[2]);
if (count($block) > self::PORT_RANGE_LIMIT) {
2021-01-23 12:33:34 -08:00
throw new TooManyPortsInRangeException();
}
if ((int) $matches[1] <= self::PORT_FLOOR || (int) $matches[2] > self::PORT_CEIL) {
2021-01-23 12:33:34 -08:00
throw new PortOutOfRangeException();
2017-08-05 21:10:32 -05:00
}
foreach ($block as $unit) {
$insertData[] = [
'node_id' => $node->id,
2017-08-05 21:10:32 -05:00
'ip' => $ip->__toString(),
'port' => (int) $unit,
'ip_alias' => array_get($data, 'allocation_alias'),
'server_id' => null,
];
}
} else {
if ((int) $port <= self::PORT_FLOOR || (int) $port > self::PORT_CEIL) {
2021-01-23 12:33:34 -08:00
throw new PortOutOfRangeException();
}
2017-08-05 21:10:32 -05:00
$insertData[] = [
'node_id' => $node->id,
2017-08-05 21:10:32 -05:00
'ip' => $ip->__toString(),
'port' => (int) $port,
'ip_alias' => array_get($data, 'allocation_alias'),
'server_id' => null,
];
}
$this->repository->insertIgnore($insertData);
}
}
$this->connection->commit();
}
}