Path: blob/1.0-develop/app/Http/Controllers/Admin/NodesController.php
10279 views
<?php12namespace Pterodactyl\Http\Controllers\Admin;34use Illuminate\View\View;5use Illuminate\Http\Request;6use Pterodactyl\Models\Node;7use Illuminate\Http\Response;8use Pterodactyl\Models\Allocation;9use Illuminate\Http\RedirectResponse;10use Prologue\Alerts\AlertsMessageBag;11use Illuminate\View\Factory as ViewFactory;12use Pterodactyl\Http\Controllers\Controller;13use Pterodactyl\Services\Nodes\NodeUpdateService;14use Illuminate\Cache\Repository as CacheRepository;15use Pterodactyl\Services\Nodes\NodeCreationService;16use Pterodactyl\Services\Nodes\NodeDeletionService;17use Pterodactyl\Services\Allocations\AssignmentService;18use Pterodactyl\Services\Helpers\SoftwareVersionService;19use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;20use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;21use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;22use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;23use Pterodactyl\Services\Allocations\AllocationDeletionService;24use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;25use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;26use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;2728class NodesController extends Controller29{30/**31* NodesController constructor.32*/33public function __construct(34protected AlertsMessageBag $alert,35protected AllocationDeletionService $allocationDeletionService,36protected AllocationRepositoryInterface $allocationRepository,37protected AssignmentService $assignmentService,38protected CacheRepository $cache,39protected NodeCreationService $creationService,40protected NodeDeletionService $deletionService,41protected LocationRepositoryInterface $locationRepository,42protected NodeRepositoryInterface $repository,43protected ServerRepositoryInterface $serverRepository,44protected NodeUpdateService $updateService,45protected SoftwareVersionService $versionService,46protected ViewFactory $view,47) {48}4950/**51* Displays create new node page.52*/53public function create(): View|RedirectResponse54{55$locations = $this->locationRepository->all();56if (count($locations) < 1) {57$this->alert->warning(trans('admin/node.notices.location_required'))->flash();5859return redirect()->route('admin.locations');60}6162return view('admin.nodes.new', ['locations' => $locations]);63}6465/**66* Post controller to create a new node on the system.67*68* @throws \Pterodactyl\Exceptions\Model\DataValidationException69*/70public function store(NodeFormRequest $request): RedirectResponse71{72$node = $this->creationService->handle($request->normalize());73$this->alert->info(trans('admin/node.notices.node_created'))->flash();7475return redirect()->route('admin.nodes.view.allocation', $node->id);76}7778/**79* Updates settings for a node.80*81* @throws \Pterodactyl\Exceptions\DisplayException82* @throws \Pterodactyl\Exceptions\Model\DataValidationException83* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException84*/85public function updateSettings(NodeFormRequest $request, Node $node): RedirectResponse86{87$this->updateService->handle($node, $request->normalize(), $request->input('reset_secret') === 'on');88$this->alert->success(trans('admin/node.notices.node_updated'))->flash();8990return redirect()->route('admin.nodes.view.settings', $node->id)->withInput();91}9293/**94* Removes a single allocation from a node.95*96* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException97*/98public function allocationRemoveSingle(int $node, Allocation $allocation): Response99{100$this->allocationDeletionService->handle($allocation);101102return response('', 204);103}104105/**106* Removes multiple individual allocations from a node.107*108* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException109*/110public function allocationRemoveMultiple(Request $request, int $node): Response111{112$allocations = $request->input('allocations');113foreach ($allocations as $rawAllocation) {114$allocation = new Allocation();115$allocation->id = $rawAllocation['id'];116$this->allocationRemoveSingle($node, $allocation);117}118119return response('', 204);120}121122/**123* Remove all allocations for a specific IP at once on a node.124*/125public function allocationRemoveBlock(Request $request, int $node): RedirectResponse126{127$this->allocationRepository->deleteWhere([128['node_id', '=', $node],129['server_id', '=', null],130['ip', '=', $request->input('ip')],131]);132133$this->alert->success(trans('admin/node.notices.unallocated_deleted', ['ip' => htmlspecialchars($request->input('ip'))]))134->flash();135136return redirect()->route('admin.nodes.view.allocation', $node);137}138139/**140* Sets an alias for a specific allocation on a node.141*142* @throws \Pterodactyl\Exceptions\Model\DataValidationException143* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException144*/145public function allocationSetAlias(AllocationAliasFormRequest $request): \Symfony\Component\HttpFoundation\Response146{147$this->allocationRepository->update($request->input('allocation_id'), [148'ip_alias' => (empty($request->input('alias'))) ? null : $request->input('alias'),149]);150151return response('', 204);152}153154/**155* Creates new allocations on a node.156*157* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException158* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException159* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException160* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException161*/162public function createAllocation(AllocationFormRequest $request, Node $node): RedirectResponse163{164$this->assignmentService->handle($node, $request->normalize());165$this->alert->success(trans('admin/node.notices.allocations_added'))->flash();166167return redirect()->route('admin.nodes.view.allocation', $node->id);168}169170/**171* Deletes a node from the system.172*173* @throws \Pterodactyl\Exceptions\DisplayException174*/175public function delete(int|Node $node): RedirectResponse176{177$this->deletionService->handle($node);178$this->alert->success(trans('admin/node.notices.node_deleted'))->flash();179180return redirect()->route('admin.nodes');181}182}183184185