1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2016 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Http\Controllers\Admin; |
26
|
|
|
|
27
|
|
|
use DB; |
28
|
|
|
use Log; |
29
|
|
|
use Alert; |
30
|
|
|
use Carbon; |
31
|
|
|
use Validator; |
32
|
|
|
use Pterodactyl\Models; |
33
|
|
|
use Illuminate\Http\Request; |
34
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
35
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
36
|
|
|
use Pterodactyl\Repositories\NodeRepository; |
37
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
38
|
|
|
|
39
|
|
|
class NodesController extends Controller |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Controller Constructor. |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
// |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getScript(Request $request, $id) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return response()->view('admin.nodes.remote.deploy', ['node' => Models\Node::findOrFail($id)])->header('Content-Type', 'text/plain'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getIndex(Request $request) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return view('admin.nodes.index', [ |
57
|
|
|
'nodes' => Models\Node::select( |
58
|
|
|
'nodes.*', |
59
|
|
|
'locations.long as a_locationName', |
60
|
|
|
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.node = nodes.id) as a_serverCount') |
61
|
|
|
)->join('locations', 'nodes.location', '=', 'locations.id')->paginate(20), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getNew(Request $request) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
if (! Models\Location::all()->count()) { |
68
|
|
|
Alert::warning('You must add a location before you can add a new node.')->flash(); |
69
|
|
|
|
70
|
|
|
return redirect()->route('admin.locations'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return view('admin.nodes.new', [ |
74
|
|
|
'locations' => Models\Location::all(), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function postNew(Request $request) |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
$node = new NodeRepository; |
82
|
|
|
$new = $node->create($request->except([ |
83
|
|
|
'_token', |
84
|
|
|
])); |
85
|
|
|
Alert::success('Successfully created new node. <strong>Before you can add any servers you need to first assign some IP addresses and ports.</strong>')->flash(); |
86
|
|
|
Alert::info('<strong>To simplify the node setup you can generate a token on the configuration tab.</strong>')->flash(); |
87
|
|
|
|
88
|
|
|
return redirect()->route('admin.nodes.view', [ |
89
|
|
|
'id' => $new, |
90
|
|
|
'tab' => 'tab_allocation', |
91
|
|
|
]); |
92
|
|
|
} catch (DisplayValidationException $e) { |
93
|
|
|
return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput(); |
94
|
|
|
} catch (DisplayException $e) { |
95
|
|
|
Alert::danger($e->getMessage())->flash(); |
96
|
|
|
} catch (\Exception $e) { |
97
|
|
|
Log::error($e); |
98
|
|
|
Alert::danger('An unhandled exception occured while attempting to add this node. Please try again.')->flash(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return redirect()->route('admin.nodes.new')->withInput(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getView(Request $request, $id) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$node = Models\Node::findOrFail($id); |
107
|
|
|
|
108
|
|
|
return view('admin.nodes.view', [ |
109
|
|
|
'node' => $node, |
110
|
|
|
'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName') |
111
|
|
|
->join('users', 'users.id', '=', 'servers.owner') |
112
|
|
|
->join('services', 'services.id', '=', 'servers.service') |
113
|
|
|
->where('node', $id)->paginate(10, ['*'], 'servers'), |
114
|
|
|
'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first(), |
115
|
|
|
'locations' => Models\Location::all(), |
116
|
|
|
'allocations' => Models\Allocation::select('allocations.*', 'servers.name as assigned_to_name') |
117
|
|
|
->where('allocations.node', $node->id) |
118
|
|
|
->leftJoin('servers', 'servers.id', '=', 'allocations.assigned_to') |
119
|
|
|
->orderBy('allocations.ip', 'asc') |
120
|
|
|
->orderBy('allocations.port', 'asc') |
121
|
|
|
->paginate(20, ['*'], 'allocations'), |
122
|
|
|
'allocation_ips' => Models\Allocation::select('id', 'ip') |
123
|
|
|
->where('node', $node->id) |
124
|
|
|
->groupBy('ip') |
125
|
|
|
->get(), |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function postView(Request $request, $id) |
130
|
|
|
{ |
131
|
|
|
try { |
132
|
|
|
$node = new NodeRepository; |
133
|
|
|
$node->update($id, $request->except([ |
134
|
|
|
'_token', |
135
|
|
|
])); |
136
|
|
|
Alert::success('Successfully update this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash(); |
137
|
|
|
|
138
|
|
|
return redirect()->route('admin.nodes.view', [ |
139
|
|
|
'id' => $id, |
140
|
|
|
'tab' => 'tab_settings', |
141
|
|
|
]); |
142
|
|
|
} catch (DisplayValidationException $e) { |
143
|
|
|
return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput(); |
144
|
|
|
} catch (DisplayException $e) { |
145
|
|
|
Alert::danger($e->getMessage())->flash(); |
146
|
|
|
} catch (\Exception $e) { |
147
|
|
|
Log::error($e); |
148
|
|
|
Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return redirect()->route('admin.nodes.view', [ |
152
|
|
|
'id' => $id, |
153
|
|
|
'tab' => 'tab_settings', |
154
|
|
|
])->withInput(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function deallocateSingle(Request $request, $node, $allocation) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$query = Models\Allocation::where('node', $node)->whereNull('assigned_to')->where('id', $allocation)->delete(); |
160
|
|
|
if ((int) $query === 0) { |
161
|
|
|
return response()->json([ |
162
|
|
|
'error' => 'Unable to find an allocation matching those details to delete.', |
163
|
|
|
], 400); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return response('', 204); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function deallocateBlock(Request $request, $node) |
170
|
|
|
{ |
171
|
|
|
$query = Models\Allocation::where('node', $node)->whereNull('assigned_to')->where('ip', $request->input('ip'))->delete(); |
172
|
|
|
if ((int) $query === 0) { |
173
|
|
|
Alert::danger('There was an error while attempting to delete allocations on that IP.')->flash(); |
174
|
|
|
|
175
|
|
|
return redirect()->route('admin.nodes.view', [ |
176
|
|
|
'id' => $node, |
177
|
|
|
'tab' => 'tab_allocations', |
178
|
|
|
]); |
179
|
|
|
} |
180
|
|
|
Alert::success('Deleted all unallocated ports for <code>' . $request->input('ip') . '</code>.')->flash(); |
181
|
|
|
|
182
|
|
|
return redirect()->route('admin.nodes.view', [ |
183
|
|
|
'id' => $node, |
184
|
|
|
'tab' => 'tab_allocation', |
185
|
|
|
]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setAlias(Request $request, $node) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
if (! $request->input('allocation')) { |
191
|
|
|
return response('Missing required parameters.', 422); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
try { |
195
|
|
|
$update = Models\Allocation::findOrFail($request->input('allocation')); |
196
|
|
|
$update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias'); |
197
|
|
|
$update->save(); |
198
|
|
|
|
199
|
|
|
return response('', 204); |
200
|
|
|
} catch (\Exception $ex) { |
201
|
|
|
throw $ex; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getAllocationsJson(Request $request, $id) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$allocations = Models\Allocation::select('ip')->where('node', $id)->groupBy('ip')->get(); |
208
|
|
|
|
209
|
|
|
return response()->json($allocations); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function postAllocations(Request $request, $id) |
213
|
|
|
{ |
214
|
|
|
$validator = Validator::make($request->all(), [ |
215
|
|
|
'allocate_ip.*' => 'required|string', |
216
|
|
|
'allocate_port.*' => 'required', |
217
|
|
|
]); |
218
|
|
|
|
219
|
|
|
if ($validator->fails()) { |
220
|
|
|
return redirect()->route('admin.nodes.view', [ |
221
|
|
|
'id' => $id, |
222
|
|
|
'tab' => 'tab_allocation', |
223
|
|
|
])->withErrors($validator->errors())->withInput(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$processedData = []; |
227
|
|
|
foreach ($request->input('allocate_ip') as $ip) { |
|
|
|
|
228
|
|
|
if (! array_key_exists($ip, $processedData)) { |
229
|
|
|
$processedData[$ip] = []; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
foreach ($request->input('allocate_port') as $portid => $ports) { |
|
|
|
|
234
|
|
|
if (array_key_exists($portid, $request->input('allocate_ip'))) { |
235
|
|
|
$json = json_decode($ports); |
236
|
|
|
if (json_last_error() === 0 && ! empty($json)) { |
237
|
|
|
foreach ($json as &$parsed) { |
238
|
|
|
array_push($processedData[$request->input('allocate_ip')[$portid]], $parsed->value); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
try { |
245
|
|
|
$node = new NodeRepository; |
246
|
|
|
$node->addAllocations($id, $processedData); |
247
|
|
|
Alert::success('Successfully added new allocations to this node.')->flash(); |
248
|
|
|
} catch (DisplayException $e) { |
249
|
|
|
Alert::danger($e->getMessage())->flash(); |
250
|
|
|
} catch (\Exception $e) { |
251
|
|
|
Log::error($e); |
252
|
|
|
Alert::danger('An unhandled exception occured while attempting to add allocations this node. Please try again.')->flash(); |
253
|
|
|
} finally { |
254
|
|
|
return redirect()->route('admin.nodes.view', [ |
255
|
|
|
'id' => $id, |
256
|
|
|
'tab' => 'tab_allocation', |
257
|
|
|
]); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
public function deleteNode(Request $request, $id) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
try { |
264
|
|
|
$repo = new NodeRepository; |
265
|
|
|
$repo->delete($id); |
266
|
|
|
Alert::success('Successfully deleted the requested node from the panel.')->flash(); |
267
|
|
|
|
268
|
|
|
return redirect()->route('admin.nodes'); |
269
|
|
|
} catch (DisplayException $e) { |
270
|
|
|
Alert::danger($e->getMessage())->flash(); |
271
|
|
|
} catch (\Exception $e) { |
272
|
|
|
Log::error($e); |
273
|
|
|
Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return redirect()->route('admin.nodes.view', [ |
277
|
|
|
'id' => $id, |
278
|
|
|
'tab' => 'tab_delete', |
279
|
|
|
]); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function getConfigurationToken(Request $request, $id) |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
// Check if Node exists. Will lead to 404 if not. |
285
|
|
|
Models\Node::findOrFail($id); |
286
|
|
|
|
287
|
|
|
// Create a token |
288
|
|
|
$token = new Models\NodeConfigurationToken(); |
289
|
|
|
$token->node = $id; |
|
|
|
|
290
|
|
|
$token->token = str_random(32); |
|
|
|
|
291
|
|
|
$token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes |
|
|
|
|
292
|
|
|
$token->save(); |
293
|
|
|
|
294
|
|
|
$token_response = [ |
295
|
|
|
'token' => $token->token, |
|
|
|
|
296
|
|
|
'expires_at' => $token->expires_at->toDateTimeString(), |
|
|
|
|
297
|
|
|
]; |
298
|
|
|
|
299
|
|
|
return response()->json($token_response, 200); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.