Merge branch 'develop' into feature/api-integration-testing

This commit is contained in:
Dane Everitt 2018-03-21 22:25:16 -05:00
commit bde4d4187f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
61 changed files with 1017 additions and 453 deletions

View file

@ -1,26 +1,24 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Allocations;
use IPTools\Network;
use Pterodactyl\Models\Node;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
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;
class AssignmentService
{
const CIDR_MAX_BITS = 27;
const CIDR_MIN_BITS = 32;
const PORT_FLOOR = 1024;
const PORT_CEIL = 65535;
const PORT_RANGE_LIMIT = 1000;
const PORT_RANGE_REGEX = '/^(\d{1,5})-(\d{1,5})$/';
const PORT_RANGE_REGEX = '/^(\d{4,5})-(\d{4,5})$/';
/**
* @var \Illuminate\Database\ConnectionInterface
@ -38,10 +36,8 @@ class AssignmentService
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
* @param \Illuminate\Database\ConnectionInterface $connection
*/
public function __construct(
AllocationRepositoryInterface $repository,
ConnectionInterface $connection
) {
public function __construct(AllocationRepositoryInterface $repository, ConnectionInterface $connection)
{
$this->connection = $connection;
$this->repository = $repository;
}
@ -49,21 +45,20 @@ class AssignmentService
/**
* Insert allocations into the database and link them to a specific node.
*
* @param int|\Pterodactyl\Models\Node $node
* @param array $data
* @param \Pterodactyl\Models\Node $node
* @param array $data
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
*/
public function handle($node, array $data)
public function handle(Node $node, array $data)
{
if ($node instanceof Node) {
$node = $node->id;
}
$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)) {
throw new DisplayException(trans('exceptions.allocations.cidr_out_of_range'));
throw new CidrOutOfRangeException;
}
}
@ -71,7 +66,7 @@ class AssignmentService
foreach (Network::parse(gethostbyname($data['allocation_ip'])) as $ip) {
foreach ($data['allocation_ports'] as $port) {
if (! is_digit($port) && ! preg_match(self::PORT_RANGE_REGEX, $port)) {
throw new DisplayException(trans('exceptions.allocations.invalid_mapping', ['port' => $port]));
throw new InvalidPortMappingException($port);
}
$insertData = [];
@ -79,12 +74,16 @@ class AssignmentService
$block = range($matches[1], $matches[2]);
if (count($block) > self::PORT_RANGE_LIMIT) {
throw new DisplayException(trans('exceptions.allocations.too_many_ports'));
throw new TooManyPortsInRangeException;
}
if ((int) $matches[1] <= self::PORT_FLOOR || (int) $matches[2] > self::PORT_CEIL) {
throw new PortOutOfRangeException;
}
foreach ($block as $unit) {
$insertData[] = [
'node_id' => $node,
'node_id' => $node->id,
'ip' => $ip->__toString(),
'port' => (int) $unit,
'ip_alias' => array_get($data, 'allocation_alias'),
@ -92,8 +91,12 @@ class AssignmentService
];
}
} else {
if ((int) $port <= self::PORT_FLOOR || (int) $port > self::PORT_CEIL) {
throw new PortOutOfRangeException;
}
$insertData[] = [
'node_id' => $node,
'node_id' => $node->id,
'ip' => $ip->__toString(),
'port' => (int) $port,
'ip_alias' => array_get($data, 'allocation_alias'),

View file

@ -3,24 +3,46 @@
namespace Pterodactyl\Services\Eggs\Variables;
use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Traits\Services\ValidatesValidationRules;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableCreationService
{
use ValidatesValidationRules;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
*/
protected $repository;
private $repository;
/**
* @var \Illuminate\Contracts\Validation\Factory
*/
private $validator;
/**
* VariableCreationService constructor.
*
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(EggVariableRepositoryInterface $repository)
public function __construct(EggVariableRepositoryInterface $repository, Factory $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}
/**
* Return the validation factory instance to be used by rule validation
* checking in the trait.
*
* @return \Illuminate\Contracts\Validation\Factory
*/
protected function getValidator(): Factory
{
return $this->validator;
}
/**
@ -31,6 +53,7 @@ class VariableCreationService
* @return \Pterodactyl\Models\EggVariable
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function handle(int $egg, array $data): EggVariable
@ -42,6 +65,10 @@ class VariableCreationService
));
}
if (! empty($data['rules'] ?? '')) {
$this->validateRules($data['rules']);
}
$options = array_get($data, 'options') ?? [];
return $this->repository->create([

View file

@ -3,25 +3,47 @@
namespace Pterodactyl\Services\Eggs\Variables;
use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Traits\Services\ValidatesValidationRules;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableUpdateService
{
use ValidatesValidationRules;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
*/
protected $repository;
private $repository;
/**
* @var \Illuminate\Contracts\Validation\Factory
*/
private $validator;
/**
* VariableUpdateService constructor.
*
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(EggVariableRepositoryInterface $repository)
public function __construct(EggVariableRepositoryInterface $repository, Factory $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}
/**
* Return the validation factory instance to be used by rule validation
* checking in the trait.
*
* @return \Illuminate\Contracts\Validation\Factory
*/
protected function getValidator(): Factory
{
return $this->validator;
}
/**
@ -58,6 +80,10 @@ class VariableUpdateService
}
}
if (! empty($data['rules'] ?? '')) {
$this->validateRules($data['rules']);
}
$options = array_get($data, 'options') ?? [];
return $this->repository->withoutFreshModel()->update($variable->id, [

View file

@ -105,7 +105,7 @@ class StartupModificationService
'server_id' => $server->id,
'variable_id' => $result->id,
], [
'variable_value' => $result->value,
'variable_value' => $result->value ?? '',
]);
});
}