2017-08-06 02:10:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Allocations;
|
|
|
|
|
|
|
|
use IPTools\Network;
|
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
2018-03-10 19:10:40 +00:00
|
|
|
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-06 02:10:32 +00:00
|
|
|
|
|
|
|
class AssignmentService
|
|
|
|
{
|
|
|
|
const CIDR_MAX_BITS = 27;
|
|
|
|
const CIDR_MIN_BITS = 32;
|
2018-03-10 19:10:40 +00:00
|
|
|
const PORT_FLOOR = 1024;
|
|
|
|
const PORT_CEIL = 65535;
|
2017-08-06 02:10:32 +00:00
|
|
|
const PORT_RANGE_LIMIT = 1000;
|
2018-03-10 19:10:40 +00:00
|
|
|
const PORT_RANGE_REGEX = '/^(\d{4,5})-(\d{4,5})$/';
|
2017-08-06 02:10:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AssignmentService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
2018-03-10 19:10:40 +00:00
|
|
|
public function __construct(AllocationRepositoryInterface $repository, ConnectionInterface $connection)
|
|
|
|
{
|
2017-08-06 02:10:32 +00:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert allocations into the database and link them to a specific node.
|
|
|
|
*
|
2018-03-10 19:10:40 +00:00
|
|
|
* @param \Pterodactyl\Models\Node $node
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
2017-08-06 02:10:32 +00:00
|
|
|
*
|
2018-03-10 19:10:40 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
2018-03-10 19:10:40 +00:00
|
|
|
public function handle(Node $node, array $data)
|
2017-08-06 02:10:32 +00:00
|
|
|
{
|
|
|
|
$explode = explode('/', $data['allocation_ip']);
|
|
|
|
if (count($explode) !== 1) {
|
|
|
|
if (! ctype_digit($explode[1]) || ($explode[1] > self::CIDR_MIN_BITS || $explode[1] < self::CIDR_MAX_BITS)) {
|
2018-03-10 19:10:40 +00:00
|
|
|
throw new CidrOutOfRangeException;
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
foreach (Network::parse(gethostbyname($data['allocation_ip'])) as $ip) {
|
|
|
|
foreach ($data['allocation_ports'] as $port) {
|
2018-01-28 23:14:14 +00:00
|
|
|
if (! is_digit($port) && ! preg_match(self::PORT_RANGE_REGEX, $port)) {
|
2018-03-10 19:10:40 +00:00
|
|
|
throw new InvalidPortMappingException($port);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$insertData = [];
|
|
|
|
if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) {
|
|
|
|
$block = range($matches[1], $matches[2]);
|
|
|
|
|
|
|
|
if (count($block) > self::PORT_RANGE_LIMIT) {
|
2018-03-10 19:10:40 +00:00
|
|
|
throw new TooManyPortsInRangeException;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int) $matches[1] <= self::PORT_FLOOR || (int) $matches[2] > self::PORT_CEIL) {
|
|
|
|
throw new PortOutOfRangeException;
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($block as $unit) {
|
|
|
|
$insertData[] = [
|
2018-03-10 19:10:40 +00:00
|
|
|
'node_id' => $node->id,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip' => $ip->__toString(),
|
|
|
|
'port' => (int) $unit,
|
|
|
|
'ip_alias' => array_get($data, 'allocation_alias'),
|
|
|
|
'server_id' => null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-10 19:10:40 +00:00
|
|
|
if ((int) $port <= self::PORT_FLOOR || (int) $port > self::PORT_CEIL) {
|
|
|
|
throw new PortOutOfRangeException;
|
|
|
|
}
|
|
|
|
|
2017-08-06 02:10:32 +00:00
|
|
|
$insertData[] = [
|
2018-03-10 19:10:40 +00:00
|
|
|
'node_id' => $node->id,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip' => $ip->__toString(),
|
|
|
|
'port' => (int) $port,
|
|
|
|
'ip_alias' => array_get($data, 'allocation_alias'),
|
|
|
|
'server_id' => null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->repository->insertIgnore($insertData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->connection->commit();
|
|
|
|
}
|
|
|
|
}
|