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 Log;
use Hash;
use Alert;
use Carbon;
use Validator;
@ -107,21 +108,6 @@ class NodesController extends Controller
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.
*
@ -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 {
$node = new NodeRepository;
$node->update($id, $request->only([
'name', 'location_id', 'public',
'fqdn', 'scheme', 'memory',
'memory_overallocate', 'disk',
'disk_overallocate', 'upload_size',
$repo->update($id, $request->intersect([
'name', 'location_id', 'public', 'fqdn', 'scheme', 'memory',
'memory_overallocate', 'disk', 'disk_overallocate', 'upload_size',
'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', [
'id' => $id,
'tab' => 'tab_settings',
]);
} catch (DisplayValidationException $e) {
return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput();
} catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);
Alert::success('Successfully updated this node\'s information. If you changed any daemon settings you will need to restart it now.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.nodes.view.settings', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attempting to edit this node. Please try again.')->flash();
}
return redirect()->route('admin.nodes.view', [
'id' => $id,
'tab' => 'tab_settings',
])->withInput();
return redirect()->route('admin.nodes.view.settings', $id)->withInput();
}
/**
@ -259,7 +244,7 @@ class NodesController extends Controller
* @param Request $request
* @param integer $node
* @param integer $allocation [description]
* @return mixed
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function allocationRemoveSingle(Request $request, $node, $allocation)
{
@ -278,7 +263,7 @@ class NodesController extends Controller
*
* @param Request $request
* @param integer $node
* @return mixed
* @return \Illuminate\Http\RedirectResponse
*/
public function allocationRemoveBlock(Request $request, $node)
{
@ -297,7 +282,8 @@ class NodesController extends Controller
*
* @param Request $request
* @param integer $node
* @return mixed
* @return \Illuminate\Http\Response
* @throws \Exception
*/
public function allocationSetAlias(Request $request, $node)
{
@ -342,51 +328,48 @@ class NodesController extends Controller
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();
$repo = new NodeRepository;
return response()->json($allocations);
}
public function deleteNode(Request $request, $id)
{
try {
$repo = new NodeRepository;
$repo->delete($id);
Alert::success('Successfully deleted the requested node from the panel.')->flash();
return redirect()->route('admin.nodes');
} catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attempting to delete this node. Please try again.')->flash();
}
return redirect()->route('admin.nodes.view', [
'id' => $id,
'tab' => 'tab_delete',
]);
return redirect()->route('admin.nodes.view', $id);
}
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.
Models\Node::findOrFail($id);
$node = Models\Node::findOrFail($id);
// Create a token
$token = new Models\NodeConfigurationToken();
$token->node = $id;
$token->token = str_random(32);
$token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes
$token->save();
$t = Models\NodeConfigurationToken::create([
'node_id' => $id,
'token' => str_random(32),
]);
$token_response = [
'token' => $token->token,
'expires_at' => $token->expires_at->toDateTimeString(),
];
return response()->json($token_response, 200);
return response()->json(['token' => $t->token]);
}
}

View File

@ -105,27 +105,26 @@ class RemoteController extends Controller
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 {
$token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail();
$node = Models\Node::findOrFail($token->node);
$model = Models\NodeConfigurationToken::with('node')->where('token', $token)->firstOrFail();
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response()->json(['error' => 'token_invalid'], 403);
}
// Check if token is expired
if ($token->expires_at->lt(Carbon::now())) {
$token->delete();
if ($model->created_at->lt(Carbon::now())) {
$model->delete();
return response()->json(['error' => 'token_expired'], 403);
}
// Delete the token, it's one-time use
$token->delete();
$model->delete();
// 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',
]);
$router->get('/view/{id}/do/index', [
$router->get('/view/{id}', [
'as' => 'admin.nodes.view',
'uses' => 'Admin\NodesController@viewIndex',
]);
$router->get('/view/{id}/do/settings', [
$router->get('/view/{id}/settings', [
'as' => 'admin.nodes.view.settings',
'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',
'uses' => 'Admin\NodesController@viewConfiguration',
]);
$router->get('/view/{id}/do/allocation', [
$router->get('/view/{id}/allocation', [
'as' => 'admin.nodes.view.allocation',
'uses' => 'Admin\NodesController@viewAllocation',
]);
$router->post('/view/{id}/do/allocation', [
$router->post('/view/{id}/allocation', [
'uses' => 'Admin\NodesController@createAllocation',
]);
$router->get('/view/{id}/do/servers', [
$router->get('/view/{id}/servers', [
'as' => 'admin.nodes.view.servers',
'uses' => 'Admin\NodesController@viewServers',
]);
$router->get('/view/{id}/do/delete', [
$router->delete('/view/{id}/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',
'uses' => 'Admin\NodesController@allocationRemoveSingle',
]);
$router->post('/view/{id}/do/allocation/remove', [
$router->post('/view/{id}/allocation/remove', [
'as' => 'admin.nodes.view.allocation.removeBlock',
'uses' => 'Admin\NodesController@allocationRemoveBlock',
]);
$router->post('/view/{id}/do/allocation/alias', [
$router->post('/view/{id}/allocation/alias', [
'as' => 'admin.nodes.view.allocation.setAlias',
'uses' => 'Admin\NodesController@allocationSetAlias',
]);
$router->get('/view/{id}/allocations.json', [
'as' => 'admin.nodes.view.allocations',
'uses' => 'Admin\NodesController@getAllocationsJson',
]);
$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',
$router->get('/view/{id}/settings/token', [
'as' => 'admin.nodes.view.configuration.token',
'uses' => 'Admin\NodesController@setToken',
]);
});

View File

@ -48,4 +48,14 @@ class NodeConfigurationToken extends Model
* @var array
*/
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
<script>
$('#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({
type: 'success',
title: 'Token created.',