Skip to content

Commit d38f89a

Browse files
committed
Cleanup node routes, cleanup remote token
1 parent 2870156 commit d38f89a

File tree

6 files changed

+133
-116
lines changed

6 files changed

+133
-116
lines changed

app/Http/Controllers/Admin/NodesController.php

Lines changed: 56 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use DB;
2828
use Log;
29+
use Hash;
2930
use Alert;
3031
use Carbon;
3132
use Validator;
@@ -107,21 +108,6 @@ public function postNew(Request $request)
107108
return redirect()->route('admin.nodes.new')->withInput();
108109
}
109110

110-
public function getView(Request $request, $id)
111-
{
112-
$node = Models\Node::with(
113-
'servers.user', 'servers.service',
114-
'servers.allocations', 'location'
115-
)->findOrFail($id);
116-
$node->setRelation('allocations', $node->allocations()->with('server')->paginate(40));
117-
118-
return view('admin.nodes.view', [
119-
'node' => $node,
120-
'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first(),
121-
'locations' => Models\Location::all(),
122-
]);
123-
}
124-
125111
/**
126112
* Shows the index overview page for a specific node.
127113
*
@@ -221,36 +207,35 @@ public function viewServers(Request $request, $id)
221207
]);
222208
}
223209

224-
public function postView(Request $request, $id)
210+
/**
211+
* Updates settings for a node.
212+
*
213+
* @param Request $request
214+
* @param integer $node
215+
* @return \Illuminate\Http\RedirectResponse
216+
*/
217+
public function updateSettings(Request $request, $id)
225218
{
219+
$repo = new NodeRepository;
220+
226221
try {
227-
$node = new NodeRepository;
228-
$node->update($id, $request->only([
229-
'name', 'location_id', 'public',
230-
'fqdn', 'scheme', 'memory',
231-
'memory_overallocate', 'disk',
232-
'disk_overallocate', 'upload_size',
222+
$repo->update($id, $request->intersect([
223+
'name', 'location_id', 'public', 'fqdn', 'scheme', 'memory',
224+
'memory_overallocate', 'disk', 'disk_overallocate', 'upload_size',
233225
'daemonSFTP', 'daemonListen', 'reset_secret',
234226
]));
235-
Alert::success('Successfully update this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
236227

237-
return redirect()->route('admin.nodes.view', [
238-
'id' => $id,
239-
'tab' => 'tab_settings',
240-
]);
241-
} catch (DisplayValidationException $e) {
242-
return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput();
243-
} catch (DisplayException $e) {
244-
Alert::danger($e->getMessage())->flash();
245-
} catch (\Exception $e) {
246-
Log::error($e);
228+
Alert::success('Successfully updated this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
229+
} catch (DisplayValidationException $ex) {
230+
return redirect()->route('admin.nodes.view.settings', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
231+
} catch (DisplayException $ex) {
232+
Alert::danger($ex->getMessage())->flash();
233+
} catch (\Exception $ex) {
234+
Log::error($ex);
247235
Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash();
248236
}
249237

250-
return redirect()->route('admin.nodes.view', [
251-
'id' => $id,
252-
'tab' => 'tab_settings',
253-
])->withInput();
238+
return redirect()->route('admin.nodes.view.settings', $id)->withInput();
254239
}
255240

256241
/**
@@ -259,7 +244,7 @@ public function postView(Request $request, $id)
259244
* @param Request $request
260245
* @param integer $node
261246
* @param integer $allocation [description]
262-
* @return mixed
247+
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
263248
*/
264249
public function allocationRemoveSingle(Request $request, $node, $allocation)
265250
{
@@ -278,7 +263,7 @@ public function allocationRemoveSingle(Request $request, $node, $allocation)
278263
*
279264
* @param Request $request
280265
* @param integer $node
281-
* @return mixed
266+
* @return \Illuminate\Http\RedirectResponse
282267
*/
283268
public function allocationRemoveBlock(Request $request, $node)
284269
{
@@ -297,7 +282,8 @@ public function allocationRemoveBlock(Request $request, $node)
297282
*
298283
* @param Request $request
299284
* @param integer $node
300-
* @return mixed
285+
* @return \Illuminate\Http\Response
286+
* @throws \Exception
301287
*/
302288
public function allocationSetAlias(Request $request, $node)
303289
{
@@ -342,51 +328,48 @@ public function createAllocation(Request $request, $node)
342328
return redirect()->route('admin.nodes.view.allocation', $node);
343329
}
344330

345-
public function getAllocationsJson(Request $request, $id)
331+
/**
332+
* Deletes a node from the system.
333+
*
334+
* @param Request $request
335+
* @param integer $id
336+
* @return \Illuminate\Http\RedirectResponse
337+
*/
338+
public function delete(Request $request, $id)
346339
{
347-
$allocations = Models\Allocation::select('ip')->where('node_id', $id)->groupBy('ip')->get();
348-
349-
return response()->json($allocations);
350-
}
340+
$repo = new NodeRepository;
351341

352-
public function deleteNode(Request $request, $id)
353-
{
354342
try {
355-
$repo = new NodeRepository;
356343
$repo->delete($id);
357344
Alert::success('Successfully deleted the requested node from the panel.')->flash();
358345

359346
return redirect()->route('admin.nodes');
360-
} catch (DisplayException $e) {
361-
Alert::danger($e->getMessage())->flash();
362-
} catch (\Exception $e) {
363-
Log::error($e);
347+
} catch (DisplayException $ex) {
348+
Alert::danger($ex->getMessage())->flash();
349+
} catch (\Exception $ex) {
350+
Log::error($ex);
364351
Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash();
365352
}
366353

367-
return redirect()->route('admin.nodes.view', [
368-
'id' => $id,
369-
'tab' => 'tab_delete',
370-
]);
354+
return redirect()->route('admin.nodes.view', $id);
371355
}
372356

373-
public function getConfigurationToken(Request $request, $id)
357+
/**
358+
* Returns the configuration token to auto-deploy a node.
359+
*
360+
* @param Request $request
361+
* @param integer $id
362+
* @return \Illuminate\Http\JsonResponse
363+
*/
364+
public function setToken(Request $request, $id)
374365
{
375-
// Check if Node exists. Will lead to 404 if not.
376-
Models\Node::findOrFail($id);
377-
378-
// Create a token
379-
$token = new Models\NodeConfigurationToken();
380-
$token->node = $id;
381-
$token->token = str_random(32);
382-
$token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes
383-
$token->save();
384-
385-
$token_response = [
386-
'token' => $token->token,
387-
'expires_at' => $token->expires_at->toDateTimeString(),
388-
];
389-
390-
return response()->json($token_response, 200);
366+
$node = Models\Node::findOrFail($id);
367+
368+
$t = Models\NodeConfigurationToken::create([
369+
'node_id' => $id,
370+
'token' => str_random(32),
371+
]);
372+
373+
return response()->json(['token' => $t->token]);
391374
}
392375
}

app/Http/Controllers/Remote/RemoteController.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,26 @@ public function event(Request $request)
105105
return response('', 201);
106106
}
107107

108-
public function getConfiguration(Request $request, $tokenString)
108+
public function getConfiguration(Request $request, $token)
109109
{
110110
// Try to query the token and the node from the database
111111
try {
112-
$token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail();
113-
$node = Models\Node::findOrFail($token->node);
112+
$model = Models\NodeConfigurationToken::with('node')->where('token', $token)->firstOrFail();
114113
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
115114
return response()->json(['error' => 'token_invalid'], 403);
116115
}
117116

118117
// Check if token is expired
119-
if ($token->expires_at->lt(Carbon::now())) {
120-
$token->delete();
118+
if ($model->created_at->lt(Carbon::now())) {
119+
$model->delete();
121120

122121
return response()->json(['error' => 'token_expired'], 403);
123122
}
124123

125124
// Delete the token, it's one-time use
126-
$token->delete();
125+
$model->delete();
127126

128127
// Manually as getConfigurationAsJson() returns it in correct format already
129-
return response($node->getConfigurationAsJson())->header('Content-Type', 'text/json');
128+
return response($model->node->getConfigurationAsJson())->header('Content-Type', 'text/json');
130129
}
131130
}

app/Http/Routes/AdminRoutes.php

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -232,79 +232,62 @@ public function map(Router $router)
232232
'uses' => 'Admin\NodesController@postNew',
233233
]);
234234

235-
$router->get('/view/{id}/do/index', [
235+
$router->get('/view/{id}', [
236236
'as' => 'admin.nodes.view',
237237
'uses' => 'Admin\NodesController@viewIndex',
238238
]);
239239

240-
$router->get('/view/{id}/do/settings', [
240+
$router->get('/view/{id}/settings', [
241241
'as' => 'admin.nodes.view.settings',
242242
'uses' => 'Admin\NodesController@viewSettings',
243243
]);
244244

245-
$router->get('/view/{id}/do/configuration', [
245+
$router->post('/view/{id}/settings', [
246+
'uses' => 'Admin\NodesController@updateSettings',
247+
]);
248+
249+
$router->get('/view/{id}/configuration', [
246250
'as' => 'admin.nodes.view.configuration',
247251
'uses' => 'Admin\NodesController@viewConfiguration',
248252
]);
249253

250-
$router->get('/view/{id}/do/allocation', [
254+
$router->get('/view/{id}/allocation', [
251255
'as' => 'admin.nodes.view.allocation',
252256
'uses' => 'Admin\NodesController@viewAllocation',
253257
]);
254258

255-
$router->post('/view/{id}/do/allocation', [
259+
$router->post('/view/{id}/allocation', [
256260
'uses' => 'Admin\NodesController@createAllocation',
257261
]);
258262

259-
$router->get('/view/{id}/do/servers', [
263+
$router->get('/view/{id}/servers', [
260264
'as' => 'admin.nodes.view.servers',
261265
'uses' => 'Admin\NodesController@viewServers',
262266
]);
263267

264-
$router->get('/view/{id}/do/delete', [
268+
$router->delete('/view/{id}/delete', [
265269
'as' => 'admin.nodes.view.delete',
266-
'uses' => 'Admin\NodesController@viewDelete',
270+
'uses' => 'Admin\NodesController@delete',
267271
]);
268272

269-
$router->delete('/view/{id}/do/allocation/remove/{allocation}', [
273+
$router->delete('/view/{id}/allocation/remove/{allocation}', [
270274
'as' => 'admin.nodes.view.allocation.removeSingle',
271275
'uses' => 'Admin\NodesController@allocationRemoveSingle',
272276
]);
273277

274-
$router->post('/view/{id}/do/allocation/remove', [
278+
$router->post('/view/{id}/allocation/remove', [
275279
'as' => 'admin.nodes.view.allocation.removeBlock',
276280
'uses' => 'Admin\NodesController@allocationRemoveBlock',
277281
]);
278282

279-
$router->post('/view/{id}/do/allocation/alias', [
283+
$router->post('/view/{id}/allocation/alias', [
280284
'as' => 'admin.nodes.view.allocation.setAlias',
281285
'uses' => 'Admin\NodesController@allocationSetAlias',
282286
]);
283287

284-
$router->get('/view/{id}/allocations.json', [
285-
'as' => 'admin.nodes.view.allocations',
286-
'uses' => 'Admin\NodesController@getAllocationsJson',
287-
]);
288-
289-
$router->post('/view/{id}/allocations', [
290-
'as' => 'admin.nodes.post.allocations',
291-
'uses' => 'Admin\NodesController@postAllocations',
292-
]);
293-
294-
// View Deploy
295-
$router->get('/view/{id}/deploy', [
296-
'as' => 'admin.nodes.deply',
297-
'uses' => 'Admin\NodesController@getScript',
298-
]);
299-
300-
$router->delete('/view/{id}', [
301-
'as' => 'admin.nodes.delete',
302-
'uses' => 'Admin\NodesController@deleteNode',
303-
]);
304-
305-
$router->get('/{id}/configurationtoken', [
306-
'as' => 'admin.nodes.configuration-token',
307-
'uses' => 'Admin\NodesController@getConfigurationToken',
288+
$router->get('/view/{id}/settings/token', [
289+
'as' => 'admin.nodes.view.configuration.token',
290+
'uses' => 'Admin\NodesController@setToken',
308291
]);
309292
});
310293

app/Models/NodeConfigurationToken.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ class NodeConfigurationToken extends Model
4848
* @var array
4949
*/
5050
protected $dates = ['created_at', 'updated_at', 'expires_at'];
51+
52+
/**
53+
* Gets the node associated with a configuration token.
54+
*
55+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
56+
*/
57+
public function node()
58+
{
59+
return $this->belongsTo(Node::class);
60+
}
5161
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Hash;
4+
use Illuminate\Support\Facades\Schema;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Database\Migrations\Migration;
7+
use Pterodactyl\Models\NodeConfigurationToken;
8+
9+
class UpdateNodeConfigTokensColumns extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::table('node_configuration_tokens', function (Blueprint $table) {
19+
$table->dropForeign(['node']);
20+
$table->dropColumn('expires_at');
21+
$table->renameColumn('node', 'node_id');
22+
23+
$table->foreign('node_id')->references('id')->on('nodes');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::table('node_configuration_tokens', function (Blueprint $table) {
35+
$table->dropForeign(['node_id']);
36+
$table->renameColumn('node_id', 'node');
37+
$table->timestamp('expires_at')->after('token');
38+
39+
$table->foreign('node')->references('id')->on('nodes');
40+
});
41+
}
42+
}

resources/themes/pterodactyl/admin/nodes/view/configuration.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
@parent
8282
<script>
8383
$('#configTokenBtn').on('click', function (event) {
84-
$.getJSON('{{ route('admin.nodes.configuration-token', $node->id) }}').done(function (data) {
84+
$.getJSON('{{ route('admin.nodes.view.configuration.token', $node->id) }}').done(function (data) {
8585
swal({
8686
type: 'success',
8787
title: 'Token created.',

0 commit comments

Comments
 (0)