Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Allocations/AssignmentService.php
14042 views
1
<?php
2
3
namespace Pterodactyl\Services\Allocations;
4
5
use IPTools\Network;
6
use Pterodactyl\Models\Node;
7
use Illuminate\Database\ConnectionInterface;
8
use Pterodactyl\Exceptions\DisplayException;
9
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
10
use Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException;
11
use Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException;
12
use Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException;
13
use Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException;
14
15
class AssignmentService
16
{
17
public const CIDR_MAX_BITS = 25;
18
public const CIDR_MIN_BITS = 32;
19
public const PORT_FLOOR = 1024;
20
public const PORT_CEIL = 65535;
21
public const PORT_RANGE_LIMIT = 1000;
22
public const PORT_RANGE_REGEX = '/^(\d{4,5})-(\d{4,5})$/';
23
24
/**
25
* AssignmentService constructor.
26
*/
27
public function __construct(protected AllocationRepositoryInterface $repository, protected ConnectionInterface $connection)
28
{
29
}
30
31
/**
32
* Insert allocations into the database and link them to a specific node.
33
*
34
* @throws DisplayException
35
* @throws CidrOutOfRangeException
36
* @throws InvalidPortMappingException
37
* @throws PortOutOfRangeException
38
* @throws TooManyPortsInRangeException
39
*/
40
public function handle(Node $node, array $data): void
41
{
42
$explode = explode('/', $data['allocation_ip']);
43
if (count($explode) !== 1) {
44
if (!ctype_digit($explode[1]) || ($explode[1] > self::CIDR_MIN_BITS || $explode[1] < self::CIDR_MAX_BITS)) {
45
throw new CidrOutOfRangeException();
46
}
47
}
48
49
try {
50
// TODO: how should we approach supporting IPv6 with this?
51
// gethostbyname only supports IPv4, but the alternative (dns_get_record) returns
52
// an array of records, which is not ideal for this use case, we need a SINGLE
53
// IP to use, not multiple.
54
$underlying = gethostbyname($data['allocation_ip']);
55
$parsed = Network::parse($underlying);
56
} catch (\Exception $exception) {
57
// @phpstan-ignore-next-line variable.undefined
58
throw new DisplayException("Could not parse provided allocation IP address ({$underlying}): {$exception->getMessage()}", $exception);
59
}
60
61
$this->connection->beginTransaction();
62
foreach ($parsed as $ip) {
63
foreach ($data['allocation_ports'] as $port) {
64
if (!is_digit($port) && !preg_match(self::PORT_RANGE_REGEX, $port)) {
65
throw new InvalidPortMappingException($port);
66
}
67
68
$insertData = [];
69
if (preg_match(self::PORT_RANGE_REGEX, $port, $matches)) {
70
$block = range($matches[1], $matches[2]);
71
72
if (count($block) > self::PORT_RANGE_LIMIT) {
73
throw new TooManyPortsInRangeException();
74
}
75
76
if ((int) $matches[1] <= self::PORT_FLOOR || (int) $matches[2] > self::PORT_CEIL) {
77
throw new PortOutOfRangeException();
78
}
79
80
foreach ($block as $unit) {
81
$insertData[] = [
82
'node_id' => $node->id,
83
'ip' => $ip->__toString(),
84
'port' => (int) $unit,
85
'ip_alias' => array_get($data, 'allocation_alias'),
86
'server_id' => null,
87
];
88
}
89
} else {
90
if ((int) $port <= self::PORT_FLOOR || (int) $port > self::PORT_CEIL) {
91
throw new PortOutOfRangeException();
92
}
93
94
$insertData[] = [
95
'node_id' => $node->id,
96
'ip' => $ip->__toString(),
97
'port' => (int) $port,
98
'ip_alias' => array_get($data, 'allocation_alias'),
99
'server_id' => null,
100
];
101
}
102
103
$this->repository->insertIgnore($insertData);
104
}
105
}
106
107
$this->connection->commit();
108
}
109
}
110
111