Skip to content

Commit 1927964

Browse files
committed
Show more user friendly error when allocation fails to parse; closes pterodactyl#3056
1 parent 9a10078 commit 1927964

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

app/Services/Allocations/AssignmentService.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Pterodactyl\Services\Allocations;
44

5+
use Exception;
56
use IPTools\Network;
67
use Pterodactyl\Models\Node;
78
use Illuminate\Database\ConnectionInterface;
9+
use Pterodactyl\Exceptions\DisplayException;
810
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
911
use Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException;
1012
use Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException;
@@ -42,9 +44,10 @@ public function __construct(AllocationRepositoryInterface $repository, Connectio
4244
/**
4345
* Insert allocations into the database and link them to a specific node.
4446
*
47+
* @throws \Pterodactyl\Exceptions\DisplayException
4548
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
46-
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
4749
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
50+
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
4851
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
4952
*/
5053
public function handle(Node $node, array $data)
@@ -56,8 +59,16 @@ public function handle(Node $node, array $data)
5659
}
5760
}
5861

62+
try {
63+
$underlying = gethostbyname($data['allocation_ip']);
64+
$parsed = Network::parse($underlying);
65+
} catch (Exception $exception) {
66+
/* @noinspection PhpUndefinedVariableInspection */
67+
throw new DisplayException("Could not parse provided allocation IP address ({$underlying}): {$exception->getMessage()}", $exception);
68+
}
69+
5970
$this->connection->beginTransaction();
60-
foreach (Network::parse(gethostbyname($data['allocation_ip'])) as $ip) {
71+
foreach ($parsed as $ip) {
6172
foreach ($data['allocation_ports'] as $port) {
6273
if (!is_digit($port) && !preg_match(self::PORT_RANGE_REGEX, $port)) {
6374
throw new InvalidPortMappingException($port);

0 commit comments

Comments
 (0)