2019-03-24 00:47:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-07-10 03:36:08 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Models\Allocation;
|
2020-07-10 02:56:46 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
|
2019-03-24 00:47:20 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
|
2020-07-10 03:36:08 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
|
2020-09-28 15:50:34 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;
|
2020-07-10 02:56:46 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
|
2020-09-28 15:50:34 +00:00
|
|
|
use Pterodactyl\Services\Allocations\AssignmentService;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-03-24 00:47:20 +00:00
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
class NetworkAllocationController extends ClientApiController
|
2019-03-24 00:47:20 +00:00
|
|
|
{
|
|
|
|
/**
|
2019-09-06 04:41:20 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
|
2019-03-24 00:47:20 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $serverRepository;
|
|
|
|
|
2020-09-28 15:50:34 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Allocations\AssignmentService
|
|
|
|
*/
|
|
|
|
protected $assignmentService;
|
|
|
|
|
2019-03-24 00:47:20 +00:00
|
|
|
/**
|
|
|
|
* NetworkController constructor.
|
|
|
|
*
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
|
2020-07-10 02:56:46 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
|
2020-09-28 15:50:34 +00:00
|
|
|
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
|
2019-03-24 00:47:20 +00:00
|
|
|
*/
|
2020-07-10 02:56:46 +00:00
|
|
|
public function __construct(
|
|
|
|
AllocationRepository $repository,
|
2020-09-28 15:50:34 +00:00
|
|
|
ServerRepository $serverRepository,
|
|
|
|
AssignmentService $assignmentService
|
2020-07-10 02:56:46 +00:00
|
|
|
) {
|
2019-03-24 00:47:20 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
2020-07-10 02:56:46 +00:00
|
|
|
$this->serverRepository = $serverRepository;
|
2020-09-28 15:50:34 +00:00
|
|
|
$this->assignmentService = $assignmentService;
|
2019-03-24 00:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all of the allocations available to a server and wether or
|
|
|
|
* not they are currently assigned as the primary for this server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-03-24 00:47:20 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function index(GetNetworkRequest $request, Server $server): array
|
2019-03-24 00:47:20 +00:00
|
|
|
{
|
2020-07-10 02:56:46 +00:00
|
|
|
return $this->fractal->collection($server->allocations)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
/**
|
|
|
|
* Set the primary allocation for a server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Allocation $allocation
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function update(UpdateAllocationRequest $request, Server $server, Allocation $allocation): array
|
|
|
|
{
|
|
|
|
$allocation = $this->repository->update($allocation->id, [
|
|
|
|
'notes' => $request->input('notes'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->fractal->item($allocation)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
/**
|
|
|
|
* Set the primary allocation for a server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2020-07-10 03:36:08 +00:00
|
|
|
* @param \Pterodactyl\Models\Allocation $allocation
|
2020-07-10 02:56:46 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2020-07-10 03:36:08 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2020-07-10 02:56:46 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2020-07-10 03:36:08 +00:00
|
|
|
*/
|
|
|
|
public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array
|
|
|
|
{
|
|
|
|
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
|
|
|
|
|
2020-09-28 15:50:34 +00:00
|
|
|
return $this->fractal->item($allocation)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->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]);
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
return $this->fractal->item($allocation)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an allocation from a server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Allocation $allocation
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2020-07-10 02:56:46 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2020-07-10 03:36:08 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2020-07-10 02:56:46 +00:00
|
|
|
*/
|
2020-07-10 03:36:08 +00:00
|
|
|
public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation)
|
2020-07-10 02:56:46 +00:00
|
|
|
{
|
2020-07-10 03:36:08 +00:00
|
|
|
if ($allocation->id === $server->allocation_id) {
|
2020-07-10 02:56:46 +00:00
|
|
|
throw new DisplayException(
|
2020-07-10 03:36:08 +00:00
|
|
|
'Cannot delete the primary allocation for a server.'
|
2020-07-10 02:56:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
$this->repository->update($allocation->id, ['server_id' => null, 'notes' => null]);
|
2019-03-24 00:47:20 +00:00
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
2019-03-24 00:47:20 +00:00
|
|
|
}
|
|
|
|
}
|