Added Automatic Allocations

Known issues:
- Port range to auto create is hard coded
- React interface is still a WIP.
This commit is contained in:
Caleb 2020-09-28 11:50:34 -04:00
parent bcbd2c4996
commit 8f8bd0be83
8 changed files with 186 additions and 4 deletions

View file

@ -13,7 +13,10 @@ use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
use Pterodactyl\Services\Allocations\AssignmentService;
use Illuminate\Support\Facades\Log;
class NetworkAllocationController extends ClientApiController
{
@ -27,20 +30,28 @@ class NetworkAllocationController extends ClientApiController
*/
private $serverRepository;
/**
* @var \Pterodactyl\Services\Allocations\AssignmentService
*/
protected $assignmentService;
/**
* NetworkController constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
*/
public function __construct(
AllocationRepository $repository,
ServerRepository $serverRepository
ServerRepository $serverRepository,
AssignmentService $assignmentService
) {
parent::__construct();
$this->repository = $repository;
$this->serverRepository = $serverRepository;
$this->assignmentService = $assignmentService;
}
/**
@ -100,6 +111,66 @@ class NetworkAllocationController extends ClientApiController
->toArray();
}
/**
* Set the notes for the allocation for a server.
*s
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest $request
* @param \Pterodactyl\Models\Server $server
* @param \Pterodactyl\Models\Allocation $allocation
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function addNew(NewAllocationRequest $request, Server $server): array
{
Log::info('addNew()');
$topRange = 25700;
$bottomRange = 25565;
if($server->allocation_limit <= $server->allocations->count()) {
Log::error('You have created the maximum number of allocations!');
throw new DisplayException(
'You have created the maximum number of allocations!'
);
}
$allocation = $server->node->allocations()->where('ip',$server->allocation->ip)->whereNull('server_id')->first();
if(!$allocation) {
if($server->node->allocations()->where('ip',$server->allocation->ip)->count() >= $topRange-$bottomRange) {
Log::error('No allocations available!');
throw new DisplayException(
'No more allocations available!'
);
}
Log::info('Creating new allocation...');
$allPorts = $server->node->allocations()->select(['port'])->where('ip',$server->allocation->ip)->get()->pluck('port')->toArray();
do {
$port = rand($bottomRange, $topRange);
Log::info('Picking port....');
// TODO ADD ITERATOR THAT TIMES OUT AFTER SEARCHING FOR SO MUCH TIME?
} while(array_search($port, $allPorts));
$this->assignmentService->handle($server->node,[
'allocation_ip'=>$server->allocation->ip,
'allocation_ports'=>[$port],
'server_id'=>$server->id
]);
$allocation = $server->node->allocations()->where('ip',$server->allocation->ip)->where('port', $port)->first();
}
$allocation->update(['server_id' => $server->id]);
return $this->fractal->item($allocation)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toArray();
}
/**
* Delete an allocation from a server.
*

View file

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Permission;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
class NewAllocationRequest extends ClientApiRequest
{
/**
* @return string
*/
public function permission(): string
{
return Permission::ACTION_ALLOCATION_CREATE;
}
}