Skip to content

Commit e1e159b

Browse files
committed
add ability to generate a token to retrieve the config for a specific node
1 parent 24bab6d commit e1e159b

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

app/Http/Controllers/Admin/NodesController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Log;
2929
use Alert;
3030
use Validator;
31+
use Carbon\Carbon;
3132
use Pterodactyl\Models;
3233
use Illuminate\Http\Request;
3334
use Pterodactyl\Exceptions\DisplayException;
@@ -276,4 +277,24 @@ public function deleteNode(Request $request, $id)
276277
'tab' => 'tab_delete',
277278
]);
278279
}
280+
281+
public function getConfigurationToken(Request $request, $id) {
282+
// Check if Node exists. Will lead to 404 if not.
283+
Models\Node::findOrFail($id);
284+
285+
// Create a token
286+
$token = new Models\NodeConfigurationToken();
287+
$token->node = $id;
288+
$token->token = str_random(32);
289+
$token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes
290+
$token->save();
291+
292+
$token_response = array(
293+
'token' => $token->token,
294+
'expires_at' => $token->expires_at->toDateTimeString()
295+
);
296+
297+
return response(json_encode($token_response), 200)
298+
->header('Content-Type', 'application/json');
299+
}
279300
}

app/Http/Controllers/Remote/RemoteController.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Remote;
2626

27+
use Carbon\Carbon;
2728
use Pterodactyl\Models;
2829
use Illuminate\Http\Request;
2930
use Pterodactyl\Http\Controllers\Controller;
3031
use Pterodactyl\Services\NotificationService;
32+
use Pterodactyl\Models\NodeConfigurationToken;
3133

3234
class RemoteController extends Controller
3335
{
@@ -107,4 +109,28 @@ public function event(Request $request)
107109

108110
return response('', 201);
109111
}
112+
113+
public function getConfiguration(Request $request, $tokenString) {
114+
// Try to query the token and the node from the database
115+
try {
116+
$token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail();
117+
$node = Models\Node::findOrFail($token->node);
118+
} catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
119+
return response(json_encode(array('error' => 'token_invalid')), 403)
120+
->header('Content-Type', 'application/json');
121+
}
122+
123+
// Check if token is expired
124+
if ($token->expires_at->lt(Carbon::now())) {
125+
$token->delete();
126+
return response(json_encode(array('error' => 'token_expired')), 403)
127+
->header('Content-Type', 'application/json');
128+
}
129+
130+
// Delete the token, it's one-time use
131+
$token->delete();
132+
133+
return response($node->getConfigurationAsJson(), 200)
134+
->header('Content-Type', 'application/json');
135+
}
110136
}

app/Http/Routes/AdminRoutes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ public function map(Router $router)
286286
'as' => 'admin.nodes.delete',
287287
'uses' => 'Admin\NodesController@deleteNode',
288288
]);
289+
290+
$router->get('/{id}/configurationtoken', [
291+
'as' => 'admin.nodes.configurationtoken',
292+
'uses' => 'Admin\NodesController@getConfigurationToken',
293+
]);
289294
});
290295

291296
// Location Routes

app/Http/Routes/RemoteRoutes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function map(Router $router)
4646
'as' => 'remote.event',
4747
'uses' => 'Remote\RemoteController@event',
4848
]);
49+
50+
$router->get('configuration/{token}', [
51+
'as' => 'remote.configuration',
52+
'uses' => 'Remote\RemoteController@getConfiguration',
53+
]);
4954
});
5055
}
5156
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Models;
26+
27+
use Illuminate\Database\Eloquent\Model;
28+
29+
class NodeConfigurationToken extends Model
30+
{
31+
/**
32+
* The table associated with the model.
33+
*
34+
* @var string
35+
*/
36+
protected $table = 'node_configuration_tokens';
37+
38+
/**
39+
* Fields that are not mass assignable.
40+
*
41+
* @var array
42+
*/
43+
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
45+
/**
46+
* The attributes that should be mutated to dates.
47+
*
48+
* @var array
49+
*/
50+
protected $dates = ['created_at', 'updated_at', 'expires_at'];
51+
}

app/Repositories/NodeRepository.php

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

285+
// Delete configure tokens
286+
Models\NodeConfigureToken::where('node', $node->id)->delete();
287+
285288
// Delete Node
286289
$node->delete();
287290

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateNodeConfigurationTokensTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('node_configuration_tokens', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->char('token', 32);
19+
$table->timestamp('expires_at');
20+
$table->integer('node')->unsigned();
21+
$table->foreign('node')
22+
->references('id')->on('nodes');
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('node_configuration_tokens');
35+
}
36+
}

0 commit comments

Comments
 (0)