Skip to content

Commit 85bdbdc

Browse files
committed
Better handling of file download requests
1 parent 838b9a9 commit 85bdbdc

File tree

6 files changed

+56
-31
lines changed

6 files changed

+56
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1212
### Added
1313
* Adds back client API for sending commands or power toggles to a server though the Panel API: `/api/client/servers/<identifier>`
1414
* Added proper transformer for Packs and re-enabled missing includes on server.
15+
* Added support for using Filesystem as a caching driver, although not recommended.
1516

1617
## v0.7.3 (Derelict Dermodactylus)
1718
### Fixed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Remote;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\JsonResponse;
7+
use Pterodactyl\Http\Controllers\Controller;
8+
use Illuminate\Contracts\Cache\Repository as CacheRepository;
9+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10+
11+
class FileDownloadController extends Controller
12+
{
13+
/**
14+
* @var \Illuminate\Contracts\Cache\Repository
15+
*/
16+
private $cache;
17+
18+
/**
19+
* FileDownloadController constructor.
20+
*
21+
* @param \Illuminate\Contracts\Cache\Repository $cache
22+
*/
23+
public function __construct(CacheRepository $cache)
24+
{
25+
$this->cache = $cache;
26+
}
27+
28+
/**
29+
* Handle a request to authenticate a download using a token and return
30+
* the path of the file to the daemon.
31+
*
32+
* @param \Illuminate\Http\Request $request
33+
* @return \Illuminate\Http\JsonResponse
34+
*
35+
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
36+
*/
37+
public function index(Request $request): JsonResponse
38+
{
39+
$download = $this->cache->pull('Server:Downloads:' . $request->input('token', ''));
40+
41+
if (is_null($download)) {
42+
throw new NotFoundHttpException('No file was found using the token provided.');
43+
}
44+
45+
return response()->json([
46+
'path' => array_get($download, 'path'),
47+
'server' => array_get($download, 'server'),
48+
]);
49+
}
50+
}

app/Http/Controllers/Daemon/ActionController.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@
1010

1111
class ActionController extends Controller
1212
{
13-
/**
14-
* Handles download request from daemon.
15-
*
16-
* @param \Illuminate\Http\Request $request
17-
* @return \Illuminate\Http\JsonResponse
18-
*/
19-
public function authenticateDownload(Request $request)
20-
{
21-
$download = Cache::pull('Server:Downloads:' . $request->input('token'));
22-
23-
if (is_null($download)) {
24-
return response()->json([
25-
'error' => 'An invalid request token was recieved with this request.',
26-
], 403);
27-
}
28-
29-
return response()->json([
30-
'path' => $download['path'],
31-
'server' => $download['server'],
32-
]);
33-
}
34-
3513
/**
3614
* Handles install toggle request from daemon.
3715
*

app/Http/Controllers/Server/Files/DownloadController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Pterodactyl\Http\Controllers\Server\Files;
1111

12+
use Ramsey\Uuid\Uuid;
1213
use Illuminate\Http\Request;
1314
use Illuminate\Cache\Repository;
1415
use Illuminate\Http\RedirectResponse;
@@ -46,8 +47,9 @@ public function index(Request $request, string $uuid, string $file): RedirectRes
4647
$server = $request->attributes->get('server');
4748
$this->authorize('download-files', $server);
4849

49-
$token = str_random(40);
50+
$token = Uuid::uuid4()->toString();
5051
$node = $server->getRelation('node');
52+
5153
$this->cache->put('Server:Downloads:' . $token, ['server' => $server->uuid, 'path' => $file], 5);
5254

5355
return redirect(sprintf('%s://%s:%s/v1/server/file/download/%s', $node->scheme, $node->fqdn, $node->daemonListen, $token));

routes/api-remote.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
2+
93
Route::get('/authenticate/{token}', 'ValidateKeyController@index')->name('api.remote.authenticate');
4+
Route::post('/download-file', 'FileDownloadController@index')->name('api.remote.download_file');
105

116
Route::group(['prefix' => '/eggs'], function () {
127
Route::get('/', 'EggRetrievalController@index')->name('api.remote.eggs');

routes/daemon.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
Route::get('/packs/pull/{uuid}/hash', 'PackController@hash')->name('daemon.pack.hash');
1111
Route::get('/configure/{token}', 'ActionController@configuration')->name('daemon.configuration');
1212

13-
Route::post('/download', 'ActionController@authenticateDownload')->name('daemon.download');
1413
Route::post('/install', 'ActionController@markInstall')->name('daemon.install');

0 commit comments

Comments
 (0)