Merge pull request #245 from schrej/feature/wings-auto-setup

Configuration retrival for wings via token
This commit is contained in:
Dane Everitt 2017-01-10 17:58:31 -05:00 committed by GitHub
commit c09170a7a7
9 changed files with 232 additions and 42 deletions

View file

@ -27,6 +27,7 @@ namespace Pterodactyl\Http\Controllers\Admin;
use DB; use DB;
use Log; use Log;
use Alert; use Alert;
use Carbon;
use Validator; use Validator;
use Pterodactyl\Models; use Pterodactyl\Models;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -82,6 +83,7 @@ class NodesController extends Controller
'_token', '_token',
])); ]));
Alert::success('Successfully created new node. <strong>Before you can add any servers you need to first assign some IP addresses and ports.</strong>')->flash(); Alert::success('Successfully created new node. <strong>Before you can add any servers you need to first assign some IP addresses and ports.</strong>')->flash();
Alert::info('<strong>To simplify the node setup you can generate a token on the configuration tab.</strong>')->flash();
return redirect()->route('admin.nodes.view', [ return redirect()->route('admin.nodes.view', [
'id' => $new, 'id' => $new,
@ -276,4 +278,24 @@ class NodesController extends Controller
'tab' => 'tab_delete', 'tab' => 'tab_delete',
]); ]);
} }
public function getConfigurationToken(Request $request, $id)
{
// Check if Node exists. Will lead to 404 if not.
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();
$token_response = [
'token' => $token->token,
'expires_at' => $token->expires_at->toDateTimeString(),
];
return response()->json($token_response, 200);
}
} }

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Http\Controllers\Remote; namespace Pterodactyl\Http\Controllers\Remote;
use Carbon\Carbon;
use Pterodactyl\Models; use Pterodactyl\Models;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
@ -107,4 +108,29 @@ class RemoteController extends Controller
return response('', 201); return response('', 201);
} }
public function getConfiguration(Request $request, $tokenString)
{
// 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);
} 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();
return response()->json(['error' => 'token_expired'], 403);
}
// Delete the token, it's one-time use
$token->delete();
// Manually as getConfigurationAsJson() returns it in correct format already
return response($node->getConfigurationAsJson(), 200)
->header('Content-Type', 'application/json');
}
} }

View file

@ -286,6 +286,11 @@ class AdminRoutes
'as' => 'admin.nodes.delete', 'as' => 'admin.nodes.delete',
'uses' => 'Admin\NodesController@deleteNode', 'uses' => 'Admin\NodesController@deleteNode',
]); ]);
$router->get('/{id}/configurationtoken', [
'as' => 'admin.nodes.configuration-token',
'uses' => 'Admin\NodesController@getConfigurationToken',
]);
}); });
// Location Routes // Location Routes

View file

@ -46,6 +46,11 @@ class RemoteRoutes
'as' => 'remote.event', 'as' => 'remote.event',
'uses' => 'Remote\RemoteController@event', 'uses' => 'Remote\RemoteController@event',
]); ]);
$router->get('configuration/{token}', [
'as' => 'remote.configuration',
'uses' => 'Remote\RemoteController@getConfiguration',
]);
}); });
} }
} }

View file

@ -117,4 +117,61 @@ class Node extends Model
return self::$guzzle[$node]; return self::$guzzle[$node];
} }
/**
* Returns the configuration in JSON format.
*
* @param bool $pretty Wether to pretty print the JSON or not
* @return string The configration in JSON format
*/
public function getConfigurationAsJson($pretty = false)
{
$config = [
'web' => [
'host' => '0.0.0.0',
'listen' => $this->daemonListen,
'ssl' => [
'enabled' => $this->scheme === 'https',
'certificate' => '/etc/letsencrypt/live/localhost/fullchain.pem',
'key' => '/etc/letsencrypt/live/localhost/privkey.pem',
],
],
'docker' => [
'socket' => '/var/run/docker.sock',
'autoupdate_images' => true,
],
'sftp' => [
'path' => $this->daemonBase,
'port' => $this->daemonSFTP,
'container' => 'ptdl-sftp',
],
'query' => [
'kill_on_fail' => true,
'fail_limit' => 5,
],
'logger' => [
'path' => 'logs/',
'src' => false,
'level' => 'info',
'period' => '1d',
'count' => 3,
],
'remote' => [
'base' => config('app.url'),
'download' => route('remote.download'),
'installed' => route('remote.install'),
],
'uploads' => [
'size_limit' => $this->upload_size,
],
'keys' => [$this->daemonSecret],
];
$json_options = JSON_UNESCAPED_SLASHES;
if ($pretty) {
$json_options |= JSON_PRETTY_PRINT;
}
return json_encode($config, $json_options);
}
} }

View file

@ -0,0 +1,51 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
*
* 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'];
}

View file

@ -282,6 +282,9 @@ class NodeRepository
// Delete Allocations // Delete Allocations
Models\Allocation::where('node', $node->id)->delete(); Models\Allocation::where('node', $node->id)->delete();
// Delete configure tokens
Models\NodeConfigurationToken::where('node', $node->id)->delete();
// Delete Node // Delete Node
$node->delete(); $node->delete();

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNodeConfigurationTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('node_configuration_tokens', function (Blueprint $table) {
$table->increments('id');
$table->char('token', 32);
$table->timestamp('expires_at');
$table->integer('node')->unsigned();
$table->foreign('node')->references('id')->on('nodes');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('node_configuration_tokens');
}
}

View file

@ -287,48 +287,13 @@
Below is the configuration file for your daemon on this node. We recommend <strong>not</strong> simply copy and pasting the code below unless you know what you are doing. You should run the <code>auto-installer</code> or <code>auto-updater</code> to setup the daemon. Below is the configuration file for your daemon on this node. We recommend <strong>not</strong> simply copy and pasting the code below unless you know what you are doing. You should run the <code>auto-installer</code> or <code>auto-updater</code> to setup the daemon.
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
<pre><code>{ <p>To simplify the configuration of nodes it is possible to fetch the config from the panel. A token is required for this process. The button below will generate a token and provide you with the commands necessary for automatic configuration of the node. Be aware that these tokens are only valid for 5 minutes.</p>
"web": { <p class="text-center">
"host": "0.0.0.0", <button type="button" id="configTokenBtn" class="btn btn-primary">Generate token</button>
"listen": {{ $node->daemonListen }}, </p>
"ssl": { </div>
"enabled": {{ $node->scheme === 'https' ? 'true' : 'false' }}, <div class="col-md-12">
"certificate": "/etc/letsencrypt/live/{{ $node->fqdn }}/fullchain.pem", <pre><code>{{ $node->getConfigurationAsJson(true) }}</code></pre>
"key": "/etc/letsencrypt/live/{{ $node->fqdn }}/privkey.pem"
}
},
"docker": {
"socket": "/var/run/docker.sock",
"autoupdate_images": true
},
"sftp": {
"path": "{{ $node->daemonBase }}",
"port": {{ $node->daemonSFTP }},
"container": "ptdl-sftp"
},
"query": {
"kill_on_fail": true,
"fail_limit": 5
},
"logger": {
"path": "logs/",
"src": false,
"level": "info",
"period": "1d",
"count": 3
},
"remote": {
"base": "{{ config('app.url') }}",
"download": "{{ route('remote.download') }}",
"installed": "{{ route('remote.install') }}"
},
"uploads": {
"size_limit": {{ $node->upload_size }}
},
"keys": [
"{{ $node->daemonSecret }}"
]
}</code></pre>
</div> </div>
</div> </div>
</div> </div>
@ -536,6 +501,27 @@ $(document).ready(function () {
}); });
}); });
$('#configTokenBtn').on('click', function (event) {
$.getJSON('{{ route('admin.nodes.configuration-token', $node->id) }}')
.done(function (data) {
swal({
type: 'success',
title: 'Token created.',
text: 'Here is your token: <code>'+data.token+'</code><br />' +
'It will expire at <i>' + data.expires_at + '</i><br /><br />' +
'<p>To auto-configure your node run<br /><small><code>npm run configure -- --panel-url '+window.location.protocol+'//{{ config('app.url') }} --token '+data.token+'</code></small></p>',
html: true
})
})
.fail(function () {
swal({
title: 'Error',
text: 'Something went wrong creating your token.',
type: 'error'
});
})
})
$('.cloneElement').on('click', function (event) { $('.cloneElement').on('click', function (event) {
event.preventDefault(); event.preventDefault();
var rnd = randomKey(10); var rnd = randomKey(10);