Skip to content

Commit 605c91a

Browse files
committed
Use cache helpers rather than database to handle configuration tokens and downloads.
1 parent 2330c25 commit 605c91a

File tree

11 files changed

+95
-146
lines changed

11 files changed

+95
-146
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
2020
* 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.
2121
* Version in footer of panel now displays correctly if panel is installed using Git rather than a download from source.
2222
* Mobile views are now more... viewable. Fixes `col-xs-6` usage thoughout the Admin CP where it was intended to be `col-md-6`.
23+
* 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.
24+
* Old daemon routes using `/remote` have been changed to use `/daemon`, panel changes now reflect this.
2325

2426
## v0.6.0-beta.2.1 (Courageous Carniadactylus)
2527
### Fixed

app/Http/Controllers/Admin/NodesController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use DB;
2828
use Log;
2929
use Alert;
30+
use Cache;
3031
use Javascript;
3132
use Pterodactyl\Models;
3233
use Illuminate\Http\Request;
@@ -364,11 +365,9 @@ public function setToken(Request $request, $id)
364365
{
365366
$node = Models\Node::findOrFail($id);
366367

367-
$t = Models\NodeConfigurationToken::create([
368-
'node_id' => $id,
369-
'token' => str_random(32),
370-
]);
368+
$token = str_random(32);
369+
Cache::put('NodeConfiguration:' . $token, $node->id, 5);
371370

372-
return response()->json(['token' => $t->token]);
371+
return response()->json(['token' => $token]);
373372
}
374373
}

app/Http/Controllers/Daemon/ActionController.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Daemon;
2626

27+
use Cache;
2728
use Illuminate\Http\Request;
29+
use Pterodactyl\Models\Node;
2830
use Pterodactyl\Models\Server;
29-
use Pterodactyl\Models\Download;
3031
use Pterodactyl\Http\Controllers\Controller;
31-
use Pterodactyl\Models\NodeConfigurationToken;
3232

3333
class ActionController extends Controller
3434
{
@@ -40,18 +40,17 @@ class ActionController extends Controller
4040
*/
4141
public function authenticateDownload(Request $request)
4242
{
43-
$download = Download::where('token', $request->input('token'))->first();
44-
if (! $download) {
43+
$download = Cache::pull('Download:' . $request->input('token'));
44+
45+
if (is_null($download)) {
4546
return response()->json([
4647
'error' => 'An invalid request token was recieved with this request.',
4748
], 403);
4849
}
4950

50-
$download->delete();
51-
5251
return response()->json([
53-
'path' => $download->path,
54-
'server' => $download->server,
52+
'path' => $download['path'],
53+
'server' => $download['server'],
5554
]);
5655
}
5756

@@ -94,24 +93,14 @@ public function markInstall(Request $request)
9493
*/
9594
public function configuration(Request $request, $token)
9695
{
97-
// Try to query the token and the node from the database
98-
try {
99-
$model = NodeConfigurationToken::with('node')->where('token', $token)->firstOrFail();
100-
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
96+
$nodeId = Cache::pull('NodeConfiguration:' . $token);
97+
if (is_null($nodeId)) {
10198
return response()->json(['error' => 'token_invalid'], 403);
10299
}
103100

104-
// Check if token is expired
105-
if ($model->created_at->addMinutes(5)->lt(Carbon::now())) {
106-
$model->delete();
107-
108-
return response()->json(['error' => 'token_expired'], 403);
109-
}
110-
111-
// Delete the token, it's one-time use
112-
$model->delete();
101+
$node = Node::findOrFail($nodeId);
113102

114103
// Manually as getConfigurationAsJson() returns it in correct format already
115-
return response($model->node->getConfigurationAsJson())->header('Content-Type', 'text/json');
104+
return response($node->getConfigurationAsJson())->header('Content-Type', 'text/json');
116105
}
117106
}

app/Http/Controllers/Server/ServerController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
namespace Pterodactyl\Http\Controllers\Server;
2626

2727
use Log;
28-
use Uuid;
2928
use Alert;
29+
use Cache;
3030
use Pterodactyl\Models;
3131
use Illuminate\Http\Request;
3232
use Pterodactyl\Exceptions\DisplayException;
@@ -201,13 +201,11 @@ public function getDownloadFile(Request $request, $uuid, $file)
201201
$server = Models\Server::byUuid($uuid);
202202
$this->authorize('download-files', $server);
203203

204-
$download = new Models\Download;
205-
206-
$download->token = (string) Uuid::generate(4);
207-
$download->server = $server->uuid;
208-
$download->path = $file;
209-
210-
$download->save();
204+
$token = str_random(40);
205+
Cache::tags(['Downloads', 'Downloads:Server:' . $server->uuid])->put('Download:' . $token, [
206+
'server' => $server->uuid,
207+
'path' => $file,
208+
], 1);
211209

212210
return redirect($server->node->scheme . '://' . $server->node->fqdn . ':' . $server->node->daemonListen . '/server/file/download/' . $download->token);
213211
}

app/Models/Download.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

app/Models/NodeConfigurationToken.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

app/Models/Server.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,6 @@ public function databases()
334334
return $this->hasMany(Database::class);
335335
}
336336

337-
/**
338-
* Gets all downloads associated with a server.
339-
*
340-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
341-
*/
342-
public function downloads()
343-
{
344-
return $this->hasMany(Download::class, 'server', 'id');
345-
}
346-
347337
/**
348338
* Gets the location of the server.
349339
*

app/Observers/ServerObserver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function updated(Server $server)
138138
*/
139139
Cache::tags('Model:Server:byUuid:' . $server->uuid)->flush();
140140
Cache::tags('Model:Server:byUuid:' . $server->uuidShort)->flush();
141+
Cache::tags('Downloads:Server:' . $server->uuid)->flush();
141142

142143
event(new Events\Server\Updated($server));
143144
}

app/Repositories/NodeRepository.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ public function delete($id)
284284
// Delete Allocations
285285
Models\Allocation::where('node_id', $node->id)->delete();
286286

287-
// Delete configure tokens
288-
Models\NodeConfigurationToken::where('node_id', $node->id)->delete();
289-
290287
// Delete Node
291288
$node->delete();
292289
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class DeleteDownloadTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::dropIfExists('downloads');
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*
22+
* @return void
23+
*/
24+
public function down()
25+
{
26+
Schema::create('downloads', function (Blueprint $table) {
27+
$table->increments('id');
28+
$table->char('token', 36)->unique();
29+
$table->char('server', 36);
30+
$table->text('path');
31+
$table->timestamps();
32+
});
33+
}
34+
}

0 commit comments

Comments
 (0)