Cleanup node routes, cleanup remote token

This commit is contained in:
Dane Everitt 2017-03-03 23:14:23 -05:00
parent 287015669a
commit d38f89a468
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 131 additions and 114 deletions

View file

@ -26,6 +26,7 @@ namespace Pterodactyl\Http\Controllers\Admin;
use DB; use DB;
use Log; use Log;
use Hash;
use Alert; use Alert;
use Carbon; use Carbon;
use Validator; use Validator;
@ -107,21 +108,6 @@ class NodesController extends Controller
return redirect()->route('admin.nodes.new')->withInput(); return redirect()->route('admin.nodes.new')->withInput();
} }
public function getView(Request $request, $id)
{
$node = Models\Node::with(
'servers.user', 'servers.service',
'servers.allocations', 'location'
)->findOrFail($id);
$node->setRelation('allocations', $node->allocations()->with('server')->paginate(40));
return view('admin.nodes.view', [
'node' => $node,
'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first(),
'locations' => Models\Location::all(),
]);
}
/** /**
* Shows the index overview page for a specific node. * Shows the index overview page for a specific node.
* *
@ -221,36 +207,35 @@ class NodesController extends Controller
]); ]);
} }
public function postView(Request $request, $id) /**
* Updates settings for a node.
*
* @param Request $request
* @param integer $node
* @return \Illuminate\Http\RedirectResponse
*/
public function updateSettings(Request $request, $id)
{ {
$repo = new NodeRepository;
try { try {
$node = new NodeRepository; $repo->update($id, $request->intersect([
$node->update($id, $request->only([ 'name', 'location_id', 'public', 'fqdn', 'scheme', 'memory',
'name', 'location_id', 'public', 'memory_overallocate', 'disk', 'disk_overallocate', 'upload_size',
'fqdn', 'scheme', 'memory',
'memory_overallocate', 'disk',
'disk_overallocate', 'upload_size',
'daemonSFTP', 'daemonListen', 'reset_secret', 'daemonSFTP', 'daemonListen', 'reset_secret',
])); ]));
Alert::success('Successfully update this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
return redirect()->route('admin.nodes.view', [ Alert::success('Successfully updated this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
'id' => $id, } catch (DisplayValidationException $ex) {
'tab' => 'tab_settings', return redirect()->route('admin.nodes.view.settings', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
]); } catch (DisplayException $ex) {
} catch (DisplayValidationException $e) { Alert::danger($ex->getMessage())->flash();
return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput(); } catch (\Exception $ex) {
} catch (DisplayException $e) { Log::error($ex);
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);
Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash(); Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash();
} }
return redirect()->route('admin.nodes.view', [ return redirect()->route('admin.nodes.view.settings', $id)->withInput();
'id' => $id,
'tab' => 'tab_settings',
])->withInput();
} }
/** /**
@ -259,7 +244,7 @@ class NodesController extends Controller
* @param Request $request * @param Request $request
* @param integer $node * @param integer $node
* @param integer $allocation [description] * @param integer $allocation [description]
* @return mixed * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/ */
public function allocationRemoveSingle(Request $request, $node, $allocation) public function allocationRemoveSingle(Request $request, $node, $allocation)
{ {
@ -278,7 +263,7 @@ class NodesController extends Controller
* *
* @param Request $request * @param Request $request
* @param integer $node * @param integer $node
* @return mixed * @return \Illuminate\Http\RedirectResponse
*/ */
public function allocationRemoveBlock(Request $request, $node) public function allocationRemoveBlock(Request $request, $node)
{ {
@ -297,7 +282,8 @@ class NodesController extends Controller
* *
* @param Request $request * @param Request $request
* @param integer $node * @param integer $node
* @return mixed * @return \Illuminate\Http\Response
* @throws \Exception
*/ */
public function allocationSetAlias(Request $request, $node) public function allocationSetAlias(Request $request, $node)
{ {
@ -342,51 +328,48 @@ class NodesController extends Controller
return redirect()->route('admin.nodes.view.allocation', $node); return redirect()->route('admin.nodes.view.allocation', $node);
} }
public function getAllocationsJson(Request $request, $id) /**
* Deletes a node from the system.
*
* @param Request $request
* @param integer $id
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(Request $request, $id)
{ {
$allocations = Models\Allocation::select('ip')->where('node_id', $id)->groupBy('ip')->get();
return response()->json($allocations);
}
public function deleteNode(Request $request, $id)
{
try {
$repo = new NodeRepository; $repo = new NodeRepository;
try {
$repo->delete($id); $repo->delete($id);
Alert::success('Successfully deleted the requested node from the panel.')->flash(); Alert::success('Successfully deleted the requested node from the panel.')->flash();
return redirect()->route('admin.nodes'); return redirect()->route('admin.nodes');
} catch (DisplayException $e) { } catch (DisplayException $ex) {
Alert::danger($e->getMessage())->flash(); Alert::danger($ex->getMessage())->flash();
} catch (\Exception $e) { } catch (\Exception $ex) {
Log::error($e); Log::error($ex);
Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash(); Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash();
} }
return redirect()->route('admin.nodes.view', [ return redirect()->route('admin.nodes.view', $id);
'id' => $id,
'tab' => 'tab_delete',
]);
} }
public function getConfigurationToken(Request $request, $id) /**
* Returns the configuration token to auto-deploy a node.
*
* @param Request $request
* @param integer $id
* @return \Illuminate\Http\JsonResponse
*/
public function setToken(Request $request, $id)
{ {
// Check if Node exists. Will lead to 404 if not. $node = Models\Node::findOrFail($id);
Models\Node::findOrFail($id);
// Create a token $t = Models\NodeConfigurationToken::create([
$token = new Models\NodeConfigurationToken(); 'node_id' => $id,
$token->node = $id; 'token' => str_random(32),
$token->token = str_random(32); ]);
$token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes
$token->save();
$token_response = [ return response()->json(['token' => $t->token]);
'token' => $token->token,
'expires_at' => $token->expires_at->toDateTimeString(),
];
return response()->json($token_response, 200);
} }
} }

View file

@ -105,27 +105,26 @@ class RemoteController extends Controller
return response('', 201); return response('', 201);
} }
public function getConfiguration(Request $request, $tokenString) public function getConfiguration(Request $request, $token)
{ {
// Try to query the token and the node from the database // Try to query the token and the node from the database
try { try {
$token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail(); $model = Models\NodeConfigurationToken::with('node')->where('token', $token)->firstOrFail();
$node = Models\Node::findOrFail($token->node);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response()->json(['error' => 'token_invalid'], 403); return response()->json(['error' => 'token_invalid'], 403);
} }
// Check if token is expired // Check if token is expired
if ($token->expires_at->lt(Carbon::now())) { if ($model->created_at->lt(Carbon::now())) {
$token->delete(); $model->delete();
return response()->json(['error' => 'token_expired'], 403); return response()->json(['error' => 'token_expired'], 403);
} }
// Delete the token, it's one-time use // Delete the token, it's one-time use
$token->delete(); $model->delete();
// Manually as getConfigurationAsJson() returns it in correct format already // Manually as getConfigurationAsJson() returns it in correct format already
return response($node->getConfigurationAsJson())->header('Content-Type', 'text/json'); return response($model->node->getConfigurationAsJson())->header('Content-Type', 'text/json');
} }
} }

View file

@ -232,79 +232,62 @@ class AdminRoutes
'uses' => 'Admin\NodesController@postNew', 'uses' => 'Admin\NodesController@postNew',
]); ]);
$router->get('/view/{id}/do/index', [ $router->get('/view/{id}', [
'as' => 'admin.nodes.view', 'as' => 'admin.nodes.view',
'uses' => 'Admin\NodesController@viewIndex', 'uses' => 'Admin\NodesController@viewIndex',
]); ]);
$router->get('/view/{id}/do/settings', [ $router->get('/view/{id}/settings', [
'as' => 'admin.nodes.view.settings', 'as' => 'admin.nodes.view.settings',
'uses' => 'Admin\NodesController@viewSettings', 'uses' => 'Admin\NodesController@viewSettings',
]); ]);
$router->get('/view/{id}/do/configuration', [ $router->post('/view/{id}/settings', [
'uses' => 'Admin\NodesController@updateSettings',
]);
$router->get('/view/{id}/configuration', [
'as' => 'admin.nodes.view.configuration', 'as' => 'admin.nodes.view.configuration',
'uses' => 'Admin\NodesController@viewConfiguration', 'uses' => 'Admin\NodesController@viewConfiguration',
]); ]);
$router->get('/view/{id}/do/allocation', [ $router->get('/view/{id}/allocation', [
'as' => 'admin.nodes.view.allocation', 'as' => 'admin.nodes.view.allocation',
'uses' => 'Admin\NodesController@viewAllocation', 'uses' => 'Admin\NodesController@viewAllocation',
]); ]);
$router->post('/view/{id}/do/allocation', [ $router->post('/view/{id}/allocation', [
'uses' => 'Admin\NodesController@createAllocation', 'uses' => 'Admin\NodesController@createAllocation',
]); ]);
$router->get('/view/{id}/do/servers', [ $router->get('/view/{id}/servers', [
'as' => 'admin.nodes.view.servers', 'as' => 'admin.nodes.view.servers',
'uses' => 'Admin\NodesController@viewServers', 'uses' => 'Admin\NodesController@viewServers',
]); ]);
$router->get('/view/{id}/do/delete', [ $router->delete('/view/{id}/delete', [
'as' => 'admin.nodes.view.delete', 'as' => 'admin.nodes.view.delete',
'uses' => 'Admin\NodesController@viewDelete', 'uses' => 'Admin\NodesController@delete',
]); ]);
$router->delete('/view/{id}/do/allocation/remove/{allocation}', [ $router->delete('/view/{id}/allocation/remove/{allocation}', [
'as' => 'admin.nodes.view.allocation.removeSingle', 'as' => 'admin.nodes.view.allocation.removeSingle',
'uses' => 'Admin\NodesController@allocationRemoveSingle', 'uses' => 'Admin\NodesController@allocationRemoveSingle',
]); ]);
$router->post('/view/{id}/do/allocation/remove', [ $router->post('/view/{id}/allocation/remove', [
'as' => 'admin.nodes.view.allocation.removeBlock', 'as' => 'admin.nodes.view.allocation.removeBlock',
'uses' => 'Admin\NodesController@allocationRemoveBlock', 'uses' => 'Admin\NodesController@allocationRemoveBlock',
]); ]);
$router->post('/view/{id}/do/allocation/alias', [ $router->post('/view/{id}/allocation/alias', [
'as' => 'admin.nodes.view.allocation.setAlias', 'as' => 'admin.nodes.view.allocation.setAlias',
'uses' => 'Admin\NodesController@allocationSetAlias', 'uses' => 'Admin\NodesController@allocationSetAlias',
]); ]);
$router->get('/view/{id}/allocations.json', [ $router->get('/view/{id}/settings/token', [
'as' => 'admin.nodes.view.allocations', 'as' => 'admin.nodes.view.configuration.token',
'uses' => 'Admin\NodesController@getAllocationsJson', 'uses' => 'Admin\NodesController@setToken',
]);
$router->post('/view/{id}/allocations', [
'as' => 'admin.nodes.post.allocations',
'uses' => 'Admin\NodesController@postAllocations',
]);
// View Deploy
$router->get('/view/{id}/deploy', [
'as' => 'admin.nodes.deply',
'uses' => 'Admin\NodesController@getScript',
]);
$router->delete('/view/{id}', [
'as' => 'admin.nodes.delete',
'uses' => 'Admin\NodesController@deleteNode',
]);
$router->get('/{id}/configurationtoken', [
'as' => 'admin.nodes.configuration-token',
'uses' => 'Admin\NodesController@getConfigurationToken',
]); ]);
}); });

View file

@ -48,4 +48,14 @@ class NodeConfigurationToken extends Model
* @var array * @var array
*/ */
protected $dates = ['created_at', 'updated_at', 'expires_at']; protected $dates = ['created_at', 'updated_at', 'expires_at'];
/**
* Gets the node associated with a configuration token.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function node()
{
return $this->belongsTo(Node::class);
}
} }

View file

@ -0,0 +1,42 @@
<?php
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Pterodactyl\Models\NodeConfigurationToken;
class UpdateNodeConfigTokensColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('node_configuration_tokens', function (Blueprint $table) {
$table->dropForeign(['node']);
$table->dropColumn('expires_at');
$table->renameColumn('node', 'node_id');
$table->foreign('node_id')->references('id')->on('nodes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('node_configuration_tokens', function (Blueprint $table) {
$table->dropForeign(['node_id']);
$table->renameColumn('node_id', 'node');
$table->timestamp('expires_at')->after('token');
$table->foreign('node')->references('id')->on('nodes');
});
}
}

View file

@ -81,7 +81,7 @@
@parent @parent
<script> <script>
$('#configTokenBtn').on('click', function (event) { $('#configTokenBtn').on('click', function (event) {
$.getJSON('{{ route('admin.nodes.configuration-token', $node->id) }}').done(function (data) { $.getJSON('{{ route('admin.nodes.view.configuration.token', $node->id) }}').done(function (data) {
swal({ swal({
type: 'success', type: 'success',
title: 'Token created.', title: 'Token created.',