Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/NodesController.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin;
4
5
use Illuminate\View\View;
6
use Illuminate\Http\Request;
7
use Pterodactyl\Models\Node;
8
use Illuminate\Http\Response;
9
use Pterodactyl\Models\Allocation;
10
use Illuminate\Http\RedirectResponse;
11
use Prologue\Alerts\AlertsMessageBag;
12
use Illuminate\View\Factory as ViewFactory;
13
use Pterodactyl\Http\Controllers\Controller;
14
use Pterodactyl\Services\Nodes\NodeUpdateService;
15
use Illuminate\Cache\Repository as CacheRepository;
16
use Pterodactyl\Services\Nodes\NodeCreationService;
17
use Pterodactyl\Services\Nodes\NodeDeletionService;
18
use Pterodactyl\Services\Allocations\AssignmentService;
19
use Pterodactyl\Services\Helpers\SoftwareVersionService;
20
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
21
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
22
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
23
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
24
use Pterodactyl\Services\Allocations\AllocationDeletionService;
25
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
26
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
27
use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;
28
29
class NodesController extends Controller
30
{
31
/**
32
* NodesController constructor.
33
*/
34
public function __construct(
35
protected AlertsMessageBag $alert,
36
protected AllocationDeletionService $allocationDeletionService,
37
protected AllocationRepositoryInterface $allocationRepository,
38
protected AssignmentService $assignmentService,
39
protected CacheRepository $cache,
40
protected NodeCreationService $creationService,
41
protected NodeDeletionService $deletionService,
42
protected LocationRepositoryInterface $locationRepository,
43
protected NodeRepositoryInterface $repository,
44
protected ServerRepositoryInterface $serverRepository,
45
protected NodeUpdateService $updateService,
46
protected SoftwareVersionService $versionService,
47
protected ViewFactory $view,
48
) {
49
}
50
51
/**
52
* Displays create new node page.
53
*/
54
public function create(): View|RedirectResponse
55
{
56
$locations = $this->locationRepository->all();
57
if (count($locations) < 1) {
58
$this->alert->warning(trans('admin/node.notices.location_required'))->flash();
59
60
return redirect()->route('admin.locations');
61
}
62
63
return view('admin.nodes.new', ['locations' => $locations]);
64
}
65
66
/**
67
* Post controller to create a new node on the system.
68
*
69
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
70
*/
71
public function store(NodeFormRequest $request): RedirectResponse
72
{
73
$node = $this->creationService->handle($request->normalize());
74
$this->alert->info(trans('admin/node.notices.node_created'))->flash();
75
76
return redirect()->route('admin.nodes.view.allocation', $node->id);
77
}
78
79
/**
80
* Updates settings for a node.
81
*
82
* @throws \Pterodactyl\Exceptions\DisplayException
83
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
84
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
85
*/
86
public function updateSettings(NodeFormRequest $request, Node $node): RedirectResponse
87
{
88
$this->updateService->handle($node, $request->normalize(), $request->input('reset_secret') === 'on');
89
$this->alert->success(trans('admin/node.notices.node_updated'))->flash();
90
91
return redirect()->route('admin.nodes.view.settings', $node->id)->withInput();
92
}
93
94
/**
95
* Removes a single allocation from a node.
96
*
97
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
98
*/
99
public function allocationRemoveSingle(int $node, Allocation $allocation): Response
100
{
101
$this->allocationDeletionService->handle($allocation);
102
103
return response('', 204);
104
}
105
106
/**
107
* Removes multiple individual allocations from a node.
108
*
109
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
110
*/
111
public function allocationRemoveMultiple(Request $request, int $node): Response
112
{
113
$allocations = $request->input('allocations');
114
foreach ($allocations as $rawAllocation) {
115
$allocation = new Allocation();
116
$allocation->id = $rawAllocation['id'];
117
$this->allocationRemoveSingle($node, $allocation);
118
}
119
120
return response('', 204);
121
}
122
123
/**
124
* Remove all allocations for a specific IP at once on a node.
125
*/
126
public function allocationRemoveBlock(Request $request, int $node): RedirectResponse
127
{
128
$this->allocationRepository->deleteWhere([
129
['node_id', '=', $node],
130
['server_id', '=', null],
131
['ip', '=', $request->input('ip')],
132
]);
133
134
$this->alert->success(trans('admin/node.notices.unallocated_deleted', ['ip' => htmlspecialchars($request->input('ip'))]))
135
->flash();
136
137
return redirect()->route('admin.nodes.view.allocation', $node);
138
}
139
140
/**
141
* Sets an alias for a specific allocation on a node.
142
*
143
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
144
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
145
*/
146
public function allocationSetAlias(AllocationAliasFormRequest $request): \Symfony\Component\HttpFoundation\Response
147
{
148
$this->allocationRepository->update($request->input('allocation_id'), [
149
'ip_alias' => (empty($request->input('alias'))) ? null : $request->input('alias'),
150
]);
151
152
return response('', 204);
153
}
154
155
/**
156
* Creates new allocations on a node.
157
*
158
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
159
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
160
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
161
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
162
*/
163
public function createAllocation(AllocationFormRequest $request, Node $node): RedirectResponse
164
{
165
$this->assignmentService->handle($node, $request->normalize());
166
$this->alert->success(trans('admin/node.notices.allocations_added'))->flash();
167
168
return redirect()->route('admin.nodes.view.allocation', $node->id);
169
}
170
171
/**
172
* Deletes a node from the system.
173
*
174
* @throws \Pterodactyl\Exceptions\DisplayException
175
*/
176
public function delete(int|Node $node): RedirectResponse
177
{
178
$this->deletionService->handle($node);
179
$this->alert->success(trans('admin/node.notices.node_deleted'))->flash();
180
181
return redirect()->route('admin.nodes');
182
}
183
}
184
185