From 605c91a9af927f7dff2c7e02750d770264a7e2bc Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 1 May 2017 14:21:18 -0400 Subject: [PATCH] Use cache helpers rather than database to handle configuration tokens and downloads. --- CHANGELOG.md | 2 + .../Controllers/Admin/NodesController.php | 9 ++- .../Controllers/Daemon/ActionController.php | 33 ++++------ .../Controllers/Server/ServerController.php | 14 ++--- app/Models/Download.php | 37 ----------- app/Models/NodeConfigurationToken.php | 61 ------------------- app/Models/Server.php | 10 --- app/Observers/ServerObserver.php | 1 + app/Repositories/NodeRepository.php | 3 - .../2017_05_01_141528_DeleteDownloadTable.php | 34 +++++++++++ ...01_141559_DeleteNodeConfigurationTable.php | 37 +++++++++++ 11 files changed, 95 insertions(+), 146 deletions(-) delete mode 100644 app/Models/Download.php delete mode 100644 app/Models/NodeConfigurationToken.php create mode 100644 database/migrations/2017_05_01_141528_DeleteDownloadTable.php create mode 100644 database/migrations/2017_05_01_141559_DeleteNodeConfigurationTable.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 48cd36518..4c625fd4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Environment setting commands now attempt to auto-quote strings with spaces in them, as well as comment lines that are edited to avoid manual changes being overwritten. * Version in footer of panel now displays correctly if panel is installed using Git rather than a download from source. * Mobile views are now more... viewable. Fixes `col-xs-6` usage thoughout the Admin CP where it was intended to be `col-md-6`. +* Node Configuration tokens and Download tokens are stored using the cache helpers rather than a database to speed up functions and make use of auto-expiration/deletion functions. +* Old daemon routes using `/remote` have been changed to use `/daemon`, panel changes now reflect this. ## v0.6.0-beta.2.1 (Courageous Carniadactylus) ### Fixed diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 62f8e205c..8471c4bc5 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -27,6 +27,7 @@ namespace Pterodactyl\Http\Controllers\Admin; use DB; use Log; use Alert; +use Cache; use Javascript; use Pterodactyl\Models; use Illuminate\Http\Request; @@ -364,11 +365,9 @@ class NodesController extends Controller { $node = Models\Node::findOrFail($id); - $t = Models\NodeConfigurationToken::create([ - 'node_id' => $id, - 'token' => str_random(32), - ]); + $token = str_random(32); + Cache::put('NodeConfiguration:' . $token, $node->id, 5); - return response()->json(['token' => $t->token]); + return response()->json(['token' => $token]); } } diff --git a/app/Http/Controllers/Daemon/ActionController.php b/app/Http/Controllers/Daemon/ActionController.php index 79c04839d..c2d324b49 100644 --- a/app/Http/Controllers/Daemon/ActionController.php +++ b/app/Http/Controllers/Daemon/ActionController.php @@ -24,11 +24,11 @@ namespace Pterodactyl\Http\Controllers\Daemon; +use Cache; use Illuminate\Http\Request; +use Pterodactyl\Models\Node; use Pterodactyl\Models\Server; -use Pterodactyl\Models\Download; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Models\NodeConfigurationToken; class ActionController extends Controller { @@ -40,18 +40,17 @@ class ActionController extends Controller */ public function authenticateDownload(Request $request) { - $download = Download::where('token', $request->input('token'))->first(); - if (! $download) { + $download = Cache::pull('Download:' . $request->input('token')); + + if (is_null($download)) { return response()->json([ 'error' => 'An invalid request token was recieved with this request.', ], 403); } - $download->delete(); - return response()->json([ - 'path' => $download->path, - 'server' => $download->server, + 'path' => $download['path'], + 'server' => $download['server'], ]); } @@ -94,24 +93,14 @@ class ActionController extends Controller */ public function configuration(Request $request, $token) { - // Try to query the token and the node from the database - try { - $model = NodeConfigurationToken::with('node')->where('token', $token)->firstOrFail(); - } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + $nodeId = Cache::pull('NodeConfiguration:' . $token); + if (is_null($nodeId)) { return response()->json(['error' => 'token_invalid'], 403); } - // Check if token is expired - if ($model->created_at->addMinutes(5)->lt(Carbon::now())) { - $model->delete(); - - return response()->json(['error' => 'token_expired'], 403); - } - - // Delete the token, it's one-time use - $model->delete(); + $node = Node::findOrFail($nodeId); // Manually as getConfigurationAsJson() returns it in correct format already - return response($model->node->getConfigurationAsJson())->header('Content-Type', 'text/json'); + return response($node->getConfigurationAsJson())->header('Content-Type', 'text/json'); } } diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index 4b6c9bcef..0077f2932 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -25,8 +25,8 @@ namespace Pterodactyl\Http\Controllers\Server; use Log; -use Uuid; use Alert; +use Cache; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Exceptions\DisplayException; @@ -201,13 +201,11 @@ class ServerController extends Controller $server = Models\Server::byUuid($uuid); $this->authorize('download-files', $server); - $download = new Models\Download; - - $download->token = (string) Uuid::generate(4); - $download->server = $server->uuid; - $download->path = $file; - - $download->save(); + $token = str_random(40); + Cache::tags(['Downloads', 'Downloads:Server:' . $server->uuid])->put('Download:' . $token, [ + 'server' => $server->uuid, + 'path' => $file, + ], 1); return redirect($server->node->scheme . '://' . $server->node->fqdn . ':' . $server->node->daemonListen . '/server/file/download/' . $download->token); } diff --git a/app/Models/Download.php b/app/Models/Download.php deleted file mode 100644 index d2c3dd885..000000000 --- a/app/Models/Download.php +++ /dev/null @@ -1,37 +0,0 @@ -. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -namespace Pterodactyl\Models; - -use Illuminate\Database\Eloquent\Model; - -class Download extends Model -{ - /** - * The table associated with the model. - * - * @var string - */ - protected $table = 'downloads'; -} diff --git a/app/Models/NodeConfigurationToken.php b/app/Models/NodeConfigurationToken.php deleted file mode 100644 index b09e096bd..000000000 --- a/app/Models/NodeConfigurationToken.php +++ /dev/null @@ -1,61 +0,0 @@ -. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -namespace Pterodactyl\Models; - -use Illuminate\Database\Eloquent\Model; - -class NodeConfigurationToken extends Model -{ - /** - * The table associated with the model. - * - * @var string - */ - protected $table = 'node_configuration_tokens'; - - /** - * Fields that are not mass assignable. - * - * @var array - */ - protected $guarded = ['id', 'created_at', 'updated_at']; - - /** - * The attributes that should be mutated to dates. - * - * @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); - } -} diff --git a/app/Models/Server.php b/app/Models/Server.php index 8efad3845..1a26243e9 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -334,16 +334,6 @@ class Server extends Model return $this->hasMany(Database::class); } - /** - * Gets all downloads associated with a server. - * - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function downloads() - { - return $this->hasMany(Download::class, 'server', 'id'); - } - /** * Gets the location of the server. * diff --git a/app/Observers/ServerObserver.php b/app/Observers/ServerObserver.php index b7720ce52..cd8c2187a 100644 --- a/app/Observers/ServerObserver.php +++ b/app/Observers/ServerObserver.php @@ -138,6 +138,7 @@ class ServerObserver */ Cache::tags('Model:Server:byUuid:' . $server->uuid)->flush(); Cache::tags('Model:Server:byUuid:' . $server->uuidShort)->flush(); + Cache::tags('Downloads:Server:' . $server->uuid)->flush(); event(new Events\Server\Updated($server)); } diff --git a/app/Repositories/NodeRepository.php b/app/Repositories/NodeRepository.php index 80dc0d902..2d6fd3c9c 100644 --- a/app/Repositories/NodeRepository.php +++ b/app/Repositories/NodeRepository.php @@ -284,9 +284,6 @@ class NodeRepository // Delete Allocations Models\Allocation::where('node_id', $node->id)->delete(); - // Delete configure tokens - Models\NodeConfigurationToken::where('node_id', $node->id)->delete(); - // Delete Node $node->delete(); }); diff --git a/database/migrations/2017_05_01_141528_DeleteDownloadTable.php b/database/migrations/2017_05_01_141528_DeleteDownloadTable.php new file mode 100644 index 000000000..90a7f7a6a --- /dev/null +++ b/database/migrations/2017_05_01_141528_DeleteDownloadTable.php @@ -0,0 +1,34 @@ +increments('id'); + $table->char('token', 36)->unique(); + $table->char('server', 36); + $table->text('path'); + $table->timestamps(); + }); + } +} diff --git a/database/migrations/2017_05_01_141559_DeleteNodeConfigurationTable.php b/database/migrations/2017_05_01_141559_DeleteNodeConfigurationTable.php new file mode 100644 index 000000000..369c867be --- /dev/null +++ b/database/migrations/2017_05_01_141559_DeleteNodeConfigurationTable.php @@ -0,0 +1,37 @@ +increments('id'); + $table->char('token', 32); + $table->unsignedInteger('node_id'); + $table->timestamps(); + }); + + Schema::table('node_configuration_tokens', function (Blueprint $table) { + $table->foreign('node_id')->references('id')->on('nodes'); + }); + } +}