1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 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 Log; |
28
|
|
|
use Alert; |
29
|
|
|
use Pterodactyl\Models; |
30
|
|
|
use Illuminate\Http\Request; |
31
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
32
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
33
|
|
|
use Pterodactyl\Repositories\ServerRepository; |
34
|
|
|
use Pterodactyl\Repositories\DatabaseRepository; |
35
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
36
|
|
|
|
37
|
|
|
class ServersController extends Controller |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Controller Constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
// |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getIndex(Request $request) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return view('admin.servers.index', [ |
50
|
|
|
'servers' => Models\Server::withTrashed()->with('node', 'user')->paginate(25), |
|
|
|
|
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getNew(Request $request) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return view('admin.servers.new', [ |
57
|
|
|
'locations' => Models\Location::all(), |
58
|
|
|
'services' => Models\Service::all(), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getView(Request $request, $id) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$server = Models\Server::withTrashed()->with( |
|
|
|
|
65
|
|
|
'user', 'option.variables', 'variables', |
66
|
|
|
'node.allocations', 'databases.host' |
67
|
|
|
)->findOrFail($id); |
68
|
|
|
|
69
|
|
|
$server->option->variables->transform(function ($item, $key) use ($server) { |
|
|
|
|
70
|
|
|
$item->server_value = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first(); |
71
|
|
|
|
72
|
|
|
return $item; |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
return view('admin.servers.view', [ |
76
|
|
|
'server' => $server, |
77
|
|
|
'assigned' => $server->node->allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'), |
78
|
|
|
'unassigned' => $server->node->allocations->where('server_id', null)->sortBy('port')->sortBy('ip'), |
79
|
|
|
'db_servers' => Models\DatabaseServer::all(), |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function postNewServer(Request $request) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$server = new ServerRepository; |
87
|
|
|
$response = $server->create($request->except('_token')); |
88
|
|
|
|
89
|
|
|
return redirect()->route('admin.servers.view', ['id' => $response->id]); |
|
|
|
|
90
|
|
|
} catch (DisplayValidationException $ex) { |
91
|
|
|
return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
92
|
|
|
} catch (DisplayException $ex) { |
93
|
|
|
Alert::danger($ex->getMessage())->flash(); |
94
|
|
|
|
95
|
|
|
return redirect()->route('admin.servers.new')->withInput(); |
96
|
|
|
} catch (\Exception $ex) { |
97
|
|
|
Log::error($ex); |
98
|
|
|
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash(); |
99
|
|
|
|
100
|
|
|
return redirect()->route('admin.servers.new')->withInput(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns a JSON tree of all avaliable nodes in a given location. |
106
|
|
|
* |
107
|
|
|
* @param \Illuminate\Http\Request $request |
108
|
|
|
* @return \Illuminate\Contracts\View\View |
109
|
|
|
*/ |
110
|
|
|
public function postNewServerGetNodes(Request $request) |
111
|
|
|
{ |
112
|
|
|
if (! $request->input('location')) { |
113
|
|
|
return response()->json([ |
114
|
|
|
'error' => 'Missing location in request.', |
115
|
|
|
], 500); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return response()->json(Models\Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns a JSON tree of all avaliable IPs and Ports on a given node. |
123
|
|
|
* |
124
|
|
|
* @param \Illuminate\Http\Request $request |
125
|
|
|
* @return \Illuminate\Contracts\View\View |
126
|
|
|
*/ |
127
|
|
|
public function postNewServerGetIps(Request $request) |
128
|
|
|
{ |
129
|
|
|
if (! $request->input('node')) { |
130
|
|
|
return response()->json([ |
131
|
|
|
'error' => 'Missing node in request.', |
132
|
|
|
], 500); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$ips = Models\Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get(); |
136
|
|
|
$listing = []; |
137
|
|
|
|
138
|
|
|
foreach ($ips as &$ip) { |
139
|
|
|
if (array_key_exists($ip->ip, $listing)) { |
140
|
|
|
$listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]); |
141
|
|
|
} else { |
142
|
|
|
$listing[$ip->ip] = [$ip->port]; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return response()->json($listing); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns a JSON tree of all avaliable options for a given service. |
151
|
|
|
* |
152
|
|
|
* @param \Illuminate\Http\Request $request |
153
|
|
|
* @return \Illuminate\Contracts\View\View |
154
|
|
|
*/ |
155
|
|
|
public function postNewServerServiceOption(Request $request) |
156
|
|
|
{ |
157
|
|
|
if (! $request->input('service')) { |
158
|
|
|
return response()->json([ |
159
|
|
|
'error' => 'Missing service in request.', |
160
|
|
|
], 500); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$service = Models\Service::select('executable', 'startup')->where('id', $request->input('service'))->first(); |
|
|
|
|
164
|
|
|
|
165
|
|
|
return response()->json(Models\ServiceOption::select('id', 'name', 'docker_image')->where('service_id', $request->input('service'))->orderBy('name', 'asc')->get()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns a JSON tree of all avaliable variables for a given service option. |
170
|
|
|
* |
171
|
|
|
* @param \Illuminate\Http\Request $request |
172
|
|
|
* @return \Illuminate\Contracts\View\View |
173
|
|
|
*/ |
174
|
|
|
public function postNewServerOptionDetails(Request $request) |
175
|
|
|
{ |
176
|
|
|
if (! $request->input('option')) { |
177
|
|
|
return response()->json([ |
178
|
|
|
'error' => 'Missing option in request.', |
179
|
|
|
], 500); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$option = Models\ServiceOption::with('variables')->with(['packs' => function ($query) { |
183
|
|
|
$query->where('selectable', true); |
184
|
|
|
}])->findOrFail($request->input('option')); |
185
|
|
|
|
186
|
|
|
return response()->json([ |
187
|
|
|
'packs' => $option->packs, |
188
|
|
|
'variables' => $option->variables, |
189
|
|
|
'exec' => $option->display_executable, |
190
|
|
|
'startup' => $option->display_startup, |
191
|
|
|
]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function postUpdateServerDetails(Request $request, $id) |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
|
|
$server = new ServerRepository; |
198
|
|
|
$server->updateDetails($id, [ |
199
|
|
|
'owner' => $request->input('owner'), |
200
|
|
|
'name' => $request->input('name'), |
201
|
|
|
'reset_token' => ($request->input('reset_token', false) === 'on') ? true : false, |
|
|
|
|
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
Alert::success('Server details were successfully updated.')->flash(); |
205
|
|
|
} catch (DisplayValidationException $ex) { |
206
|
|
|
return redirect()->route('admin.servers.view', [ |
207
|
|
|
'id' => $id, |
208
|
|
|
'tab' => 'tab_details', |
209
|
|
|
])->withErrors(json_decode($ex->getMessage()))->withInput(); |
210
|
|
|
} catch (DisplayException $ex) { |
211
|
|
|
Alert::danger($ex->getMessage())->flash(); |
212
|
|
|
} catch (\Exception $ex) { |
213
|
|
|
Log::error($ex); |
214
|
|
|
Alert::danger('An unhandled exception occured while attemping to update this server. Please try again.')->flash(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return redirect()->route('admin.servers.view', [ |
218
|
|
|
'id' => $id, |
219
|
|
|
'tab' => 'tab_details', |
220
|
|
|
])->withInput(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function postUpdateContainerDetails(Request $request, $id) |
224
|
|
|
{ |
225
|
|
|
try { |
226
|
|
|
$server = new ServerRepository; |
227
|
|
|
$server->updateContainer($id, ['image' => $request->input('docker_image')]); |
228
|
|
|
Alert::success('Successfully updated this server\'s docker image.')->flash(); |
229
|
|
|
} catch (DisplayValidationException $ex) { |
230
|
|
|
return redirect()->route('admin.servers.view', [ |
231
|
|
|
'id' => $id, |
232
|
|
|
'tab' => 'tab_details', |
233
|
|
|
])->withErrors(json_decode($ex->getMessage()))->withInput(); |
234
|
|
|
} catch (DisplayException $ex) { |
235
|
|
|
Alert::danger($ex->getMessage())->flash(); |
236
|
|
|
} catch (\Exception $ex) { |
237
|
|
|
Log::error($ex); |
238
|
|
|
Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. Please try again.')->flash(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return redirect()->route('admin.servers.view', [ |
242
|
|
|
'id' => $id, |
243
|
|
|
'tab' => 'tab_details', |
244
|
|
|
]); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function postUpdateServerToggleBuild(Request $request, $id) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
$server = Models\Server::with('node')->findOrFail($id); |
|
|
|
|
250
|
|
|
|
251
|
|
|
try { |
252
|
|
|
$res = $server->node->guzzleClient([ |
|
|
|
|
253
|
|
|
'X-Access-Server' => $server->uuid, |
254
|
|
|
'X-Access-Token' => $node->daemonSecret, |
|
|
|
|
255
|
|
|
])->request('POST', '/server/rebuild'); |
256
|
|
|
Alert::success('A rebuild has been queued successfully. It will run the next time this server is booted.')->flash(); |
257
|
|
|
} catch (\GuzzleHttp\Exception\TransferException $ex) { |
258
|
|
|
Log::warning($ex); |
259
|
|
|
Alert::danger('An error occured while attempting to toggle a rebuild.')->flash(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return redirect()->route('admin.servers.view', [ |
263
|
|
|
'id' => $id, |
264
|
|
|
'tab' => 'tab_manage', |
265
|
|
|
]); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function postUpdateServerUpdateBuild(Request $request, $id) |
269
|
|
|
{ |
270
|
|
|
try { |
271
|
|
|
$server = new ServerRepository; |
272
|
|
|
$server->changeBuild($id, $request->only([ |
273
|
|
|
'default', 'add_additional', |
274
|
|
|
'remove_additional', 'memory', |
275
|
|
|
'swap', 'io', 'cpu', |
276
|
|
|
])); |
277
|
|
|
Alert::success('Server details were successfully updated.')->flash(); |
278
|
|
|
} catch (DisplayValidationException $ex) { |
279
|
|
|
return redirect()->route('admin.servers.view', [ |
280
|
|
|
'id' => $id, |
281
|
|
|
'tab' => 'tab_build', |
282
|
|
|
])->withErrors(json_decode($ex->getMessage()))->withInput(); |
283
|
|
|
} catch (DisplayException $ex) { |
284
|
|
|
Alert::danger($ex->getMessage())->flash(); |
285
|
|
|
|
286
|
|
|
return redirect()->route('admin.servers.view', [ |
287
|
|
|
'id' => $id, |
288
|
|
|
'tab' => 'tab_build', |
289
|
|
|
]); |
290
|
|
|
} catch (\Exception $ex) { |
291
|
|
|
Log::error($ex); |
292
|
|
|
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return redirect()->route('admin.servers.view', [ |
296
|
|
|
'id' => $id, |
297
|
|
|
'tab' => 'tab_build', |
298
|
|
|
]); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
View Code Duplication |
public function deleteServer(Request $request, $id, $force = null) |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
try { |
304
|
|
|
$server = new ServerRepository; |
305
|
|
|
$server->deleteServer($id, $force); |
306
|
|
|
Alert::success('Server has been marked for deletion on the system.')->flash(); |
307
|
|
|
|
308
|
|
|
return redirect()->route('admin.servers'); |
309
|
|
|
} catch (DisplayException $ex) { |
310
|
|
|
Alert::danger($ex->getMessage())->flash(); |
311
|
|
|
} catch (\Exception $ex) { |
312
|
|
|
Log::error($ex); |
313
|
|
|
Alert::danger('An unhandled exception occured while attemping to delete this server. Please try again.')->flash(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return redirect()->route('admin.servers.view', [ |
317
|
|
|
'id' => $id, |
318
|
|
|
'tab' => 'tab_delete', |
319
|
|
|
]); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
View Code Duplication |
public function postToggleInstall(Request $request, $id) |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
try { |
325
|
|
|
$server = new ServerRepository; |
326
|
|
|
$server->toggleInstall($id); |
327
|
|
|
Alert::success('Server status was successfully toggled.')->flash(); |
328
|
|
|
} catch (DisplayException $ex) { |
329
|
|
|
Alert::danger($ex->getMessage())->flash(); |
330
|
|
|
} catch (\Exception $ex) { |
331
|
|
|
Log::error($ex); |
332
|
|
|
Alert::danger('An unhandled exception occured while attemping to toggle this servers status.')->flash(); |
333
|
|
|
} finally { |
334
|
|
|
return redirect()->route('admin.servers.view', [ |
335
|
|
|
'id' => $id, |
336
|
|
|
'tab' => 'tab_manage', |
337
|
|
|
]); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
View Code Duplication |
public function postUpdateServerStartup(Request $request, $id) |
|
|
|
|
342
|
|
|
{ |
343
|
|
|
try { |
344
|
|
|
$server = new ServerRepository; |
345
|
|
|
$server->updateStartup($id, $request->except([ |
346
|
|
|
'_token', |
347
|
|
|
]), true); |
348
|
|
|
Alert::success('Server startup variables were successfully updated.')->flash(); |
349
|
|
|
} catch (\Pterodactyl\Exceptions\DisplayException $e) { |
350
|
|
|
Alert::danger($e->getMessage())->flash(); |
351
|
|
|
} catch (\Exception $e) { |
352
|
|
|
Log::error($e); |
353
|
|
|
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash(); |
354
|
|
|
} finally { |
355
|
|
|
return redirect()->route('admin.servers.view', [ |
356
|
|
|
'id' => $id, |
357
|
|
|
'tab' => 'tab_startup', |
358
|
|
|
])->withInput(); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function postDatabase(Request $request, $id) |
363
|
|
|
{ |
364
|
|
|
try { |
365
|
|
|
$repo = new DatabaseRepository; |
366
|
|
|
$repo->create($id, $request->only([ |
367
|
|
|
'db_server', 'database', 'remote', |
368
|
|
|
])); |
369
|
|
|
Alert::success('Added new database to this server.')->flash(); |
370
|
|
|
} catch (DisplayValidationException $ex) { |
371
|
|
|
return redirect()->route('admin.servers.view', [ |
372
|
|
|
'id' => $id, |
373
|
|
|
'tab' => 'tab_database', |
374
|
|
|
])->withInput()->withErrors(json_decode($ex->getMessage()))->withInput(); |
375
|
|
|
} catch (\Exception $ex) { |
376
|
|
|
Log::error($ex); |
377
|
|
|
Alert::danger('An exception occured while attempting to add a new database for this server.')->flash(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
return redirect()->route('admin.servers.view', [ |
381
|
|
|
'id' => $id, |
382
|
|
|
'tab' => 'tab_database', |
383
|
|
|
])->withInput(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
View Code Duplication |
public function postSuspendServer(Request $request, $id) |
|
|
|
|
387
|
|
|
{ |
388
|
|
|
try { |
389
|
|
|
$repo = new ServerRepository; |
390
|
|
|
$repo->suspend($id); |
391
|
|
|
Alert::success('Server has been suspended on the system. All running processes have been stopped and will not be startable until it is un-suspended.'); |
392
|
|
|
} catch (DisplayException $e) { |
393
|
|
|
Alert::danger($e->getMessage())->flash(); |
394
|
|
|
} catch (\Exception $e) { |
395
|
|
|
Log::error($e); |
396
|
|
|
Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash(); |
397
|
|
|
} finally { |
398
|
|
|
return redirect()->route('admin.servers.view', [ |
399
|
|
|
'id' => $id, |
400
|
|
|
'tab' => 'tab_manage', |
401
|
|
|
]); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
View Code Duplication |
public function postUnsuspendServer(Request $request, $id) |
|
|
|
|
406
|
|
|
{ |
407
|
|
|
try { |
408
|
|
|
$repo = new ServerRepository; |
409
|
|
|
$repo->unsuspend($id); |
410
|
|
|
Alert::success('Server has been unsuspended on the system. Access has been re-enabled.'); |
411
|
|
|
} catch (DisplayException $e) { |
412
|
|
|
Alert::danger($e->getMessage())->flash(); |
413
|
|
|
} catch (\Exception $e) { |
414
|
|
|
Log::error($e); |
415
|
|
|
Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash(); |
416
|
|
|
} finally { |
417
|
|
|
return redirect()->route('admin.servers.view', [ |
418
|
|
|
'id' => $id, |
419
|
|
|
'tab' => 'tab_manage', |
420
|
|
|
]); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
public function postQueuedDeletionHandler(Request $request, $id) |
425
|
|
|
{ |
426
|
|
|
try { |
427
|
|
|
$repo = new ServerRepository; |
428
|
|
|
if (! is_null($request->input('cancel'))) { |
429
|
|
|
$repo->cancelDeletion($id); |
430
|
|
|
Alert::success('Server deletion has been cancelled. This server will remain suspended until you unsuspend it.')->flash(); |
431
|
|
|
|
432
|
|
|
return redirect()->route('admin.servers.view', $id); |
433
|
|
|
} elseif (! is_null($request->input('delete'))) { |
434
|
|
|
$repo->deleteNow($id); |
435
|
|
|
Alert::success('Server was successfully deleted from the system.')->flash(); |
436
|
|
|
|
437
|
|
|
return redirect()->route('admin.servers'); |
438
|
|
|
} elseif (! is_null($request->input('force_delete'))) { |
439
|
|
|
$repo->deleteNow($id, true); |
440
|
|
|
Alert::success('Server was successfully force deleted from the system.')->flash(); |
441
|
|
|
|
442
|
|
|
return redirect()->route('admin.servers'); |
443
|
|
|
} |
444
|
|
|
} catch (DisplayException $ex) { |
445
|
|
|
Alert::danger($ex->getMessage())->flash(); |
446
|
|
|
|
447
|
|
|
return redirect()->route('admin.servers.view', $id); |
448
|
|
|
} catch (\Exception $ex) { |
449
|
|
|
Log::error($ex); |
450
|
|
|
Alert::danger('An unhandled error occured while attempting to perform this action.')->flash(); |
451
|
|
|
|
452
|
|
|
return redirect()->route('admin.servers.view', $id); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.