From ef1fa4c4e6fd8fb49ed4cc8aecec681b3ee99e24 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:06:09 +0100 Subject: [PATCH 01/10] add method to get config as json to node model --- app/Models/Node.php | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/app/Models/Node.php b/app/Models/Node.php index 396f2849d..eceb48fab 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -117,4 +117,58 @@ class Node extends Model return self::$guzzle[$node]; } + + /** + * Returns the configuration in JSON format + * + * @param boolean $pretty Wether to pretty print the JSON or not + * @return string The configration in JSON format + */ + public function getConfigurationAsJson($pretty = false) { + $config = array( + 'web' => array( + 'host' => '0.0.0.0', + 'listen' => $this->daemonListen, + 'ssl' => array( + 'enabled' => $this->scheme === 'https', + 'certificate' => '/etc/letsencrypt/live/localhost/fullchain.pem', + 'key' => '/etc/letsencrypt/live/localhost/privkey.pem' + ) + ), + 'docker' => array( + 'socket' => '/var/run/docker.sock', + 'autoupdate_images' => true + ), + 'sftp' => array( + 'path' => $this->daemonBase, + 'port' => $this->daemonSFTP, + 'container' => 'ptdl-sftp' + ), + 'query' => array( + 'kill_on_fail' => true, + 'fail_limit' => 5 + ), + 'logger' => array( + 'path' => 'logs/', + 'src' => false, + 'level' => 'info', + 'period' => '1d', + 'count' => 3 + ), + 'remote' => array( + 'base' => config('app.url'), + 'download' => route('remote.download'), + 'installed' => route('remote.install') + ), + 'uploads' => array( + 'size_limit' => $this->upload_size + ), + 'keys' => array($this->daemonSecret) + ); + + $json_options = JSON_UNESCAPED_SLASHES; + if ($pretty) $json_options |= JSON_PRETTY_PRINT; + + return json_encode($config, $json_options); + } } From 24bab6de170018b4f2d63acf92e5eb6edcc30243 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:09:04 +0100 Subject: [PATCH 02/10] use getConfigurationAsJson in the admin/nodes/view view --- resources/views/admin/nodes/view.blade.php | 43 +--------------------- 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index 3d5d9c7d7..bc7523217 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -287,48 +287,7 @@ Below is the configuration file for your daemon on this node. We recommend not simply copy and pasting the code below unless you know what you are doing. You should run the auto-installer or auto-updater to setup the daemon.
-
{
-    "web": {
-        "host": "0.0.0.0",
-        "listen": {{ $node->daemonListen }},
-        "ssl": {
-            "enabled": {{ $node->scheme === 'https' ? 'true' : 'false' }},
-            "certificate": "/etc/letsencrypt/live/{{ $node->fqdn }}/fullchain.pem",
-            "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 }}"
-    ]
-}
+
{{ $node->getConfigurationAsJson(true) }}
From e1e159b7de87053d02de6009aa49302e25f02082 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:10:11 +0100 Subject: [PATCH 03/10] add ability to generate a token to retrieve the config for a specific node --- .../Controllers/Admin/NodesController.php | 21 ++++++++ .../Controllers/Remote/RemoteController.php | 26 ++++++++++ app/Http/Routes/AdminRoutes.php | 5 ++ app/Http/Routes/RemoteRoutes.php | 5 ++ app/Models/NodeConfigurationToken.php | 51 +++++++++++++++++++ app/Repositories/NodeRepository.php | 3 ++ ...create_node_configuration_tokens_table.php | 36 +++++++++++++ 7 files changed, 147 insertions(+) create mode 100644 app/Models/NodeConfigurationToken.php create mode 100644 database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index e2b617b63..0fad31d3f 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -28,6 +28,7 @@ use DB; use Log; use Alert; use Validator; +use Carbon\Carbon; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Exceptions\DisplayException; @@ -276,4 +277,24 @@ class NodesController extends Controller '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 = array( + 'token' => $token->token, + 'expires_at' => $token->expires_at->toDateTimeString() + ); + + return response(json_encode($token_response), 200) + ->header('Content-Type', 'application/json'); + } } diff --git a/app/Http/Controllers/Remote/RemoteController.php b/app/Http/Controllers/Remote/RemoteController.php index e6dab6984..8a2e84383 100644 --- a/app/Http/Controllers/Remote/RemoteController.php +++ b/app/Http/Controllers/Remote/RemoteController.php @@ -24,10 +24,12 @@ namespace Pterodactyl\Http\Controllers\Remote; +use Carbon\Carbon; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\NotificationService; +use Pterodactyl\Models\NodeConfigurationToken; class RemoteController extends Controller { @@ -107,4 +109,28 @@ class RemoteController extends Controller 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_encode(array('error' => 'token_invalid')), 403) + ->header('Content-Type', 'application/json'); + } + + // Check if token is expired + if ($token->expires_at->lt(Carbon::now())) { + $token->delete(); + return response(json_encode(array('error' => 'token_expired')), 403) + ->header('Content-Type', 'application/json'); + } + + // Delete the token, it's one-time use + $token->delete(); + + return response($node->getConfigurationAsJson(), 200) + ->header('Content-Type', 'application/json'); + } } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 916517171..6a1cc8400 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -286,6 +286,11 @@ class AdminRoutes 'as' => 'admin.nodes.delete', 'uses' => 'Admin\NodesController@deleteNode', ]); + + $router->get('/{id}/configurationtoken', [ + 'as' => 'admin.nodes.configurationtoken', + 'uses' => 'Admin\NodesController@getConfigurationToken', + ]); }); // Location Routes diff --git a/app/Http/Routes/RemoteRoutes.php b/app/Http/Routes/RemoteRoutes.php index 2e2201c45..a42a611e9 100644 --- a/app/Http/Routes/RemoteRoutes.php +++ b/app/Http/Routes/RemoteRoutes.php @@ -46,6 +46,11 @@ class RemoteRoutes 'as' => 'remote.event', 'uses' => 'Remote\RemoteController@event', ]); + + $router->get('configuration/{token}', [ + 'as' => 'remote.configuration', + 'uses' => 'Remote\RemoteController@getConfiguration', + ]); }); } } diff --git a/app/Models/NodeConfigurationToken.php b/app/Models/NodeConfigurationToken.php new file mode 100644 index 000000000..d7a309adc --- /dev/null +++ b/app/Models/NodeConfigurationToken.php @@ -0,0 +1,51 @@ +. + * + * 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']; +} diff --git a/app/Repositories/NodeRepository.php b/app/Repositories/NodeRepository.php index 73a4588e5..7a396af3a 100644 --- a/app/Repositories/NodeRepository.php +++ b/app/Repositories/NodeRepository.php @@ -282,6 +282,9 @@ class NodeRepository // Delete Allocations Models\Allocation::where('node', $node->id)->delete(); + // Delete configure tokens + Models\NodeConfigureToken::where('node', $node->id)->delete(); + // Delete Node $node->delete(); diff --git a/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php b/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php new file mode 100644 index 000000000..e86a87979 --- /dev/null +++ b/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php @@ -0,0 +1,36 @@ +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'); + } +} From 52a395ac9aafc9e3a74ba683ba8d7681dc6a0c42 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:26:45 +0100 Subject: [PATCH 04/10] fix forgotten rename of NodeConfigurationToken --- app/Repositories/NodeRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Repositories/NodeRepository.php b/app/Repositories/NodeRepository.php index 7a396af3a..021626e97 100644 --- a/app/Repositories/NodeRepository.php +++ b/app/Repositories/NodeRepository.php @@ -283,7 +283,7 @@ class NodeRepository Models\Allocation::where('node', $node->id)->delete(); // Delete configure tokens - Models\NodeConfigureToken::where('node', $node->id)->delete(); + Models\NodeConfigurationToken::where('node', $node->id)->delete(); // Delete Node $node->delete(); From a1568e5acb987dc83ed3c4bcf90aa8f9021288ea Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:27:19 +0100 Subject: [PATCH 05/10] add button to generate token to node configuration tab add info message after node creation about token generation --- .../Controllers/Admin/NodesController.php | 1 + resources/views/admin/nodes/view.blade.php | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 0fad31d3f..ad9a1b31d 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -83,6 +83,7 @@ class NodesController extends Controller '_token', ])); Alert::success('Successfully created new node. Before you can add any servers you need to first assign some IP addresses and ports.')->flash(); + Alert::info('To simplify the node setup you can generate a token on the configuration tab.')->flash(); return redirect()->route('admin.nodes.view', [ 'id' => $new, diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index bc7523217..3ecb859ef 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -286,6 +286,12 @@
Below is the configuration file for your daemon on this node. We recommend not simply copy and pasting the code below unless you know what you are doing. You should run the auto-installer or auto-updater to setup the daemon.
+
+

You can generate a token for automatic setups. Pressing the button below will generate a token. It can only be used once and is valid for 5 minutes. It will also provide the necessary commands to auto-configure the node.

+

+ +

+
{{ $node->getConfigurationAsJson(true) }}
@@ -495,6 +501,27 @@ $(document).ready(function () { }); }); + $('#configTokenBtn').on('click', function (event) { + $.getJSON('{{ route('admin.nodes.configurationtoken', $node->id) }}') + .done(function (data) { + swal({ + type: 'success', + title: 'Token created.', + text: 'Here is your token: '+data.token+'
' + + 'It will expire at ' + data.expires_at + '

' + + '

To auto-configure your node run
npm run configure -- --panel-url {{ config('app.url') }} --token '+data.token+'

', + html: true + }) + }) + .fail(function () { + swal({ + title: 'Error', + text: 'Something went wrong creating your token.', + type: 'error' + }); + }) + }) + $('.cloneElement').on('click', function (event) { event.preventDefault(); var rnd = randomKey(10); From cc0d54e906c38b4ea3a0449fc5426c21dc5e0f93 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:33:37 +0100 Subject: [PATCH 06/10] improve wording on token explanation on node configuration tab --- resources/views/admin/nodes/view.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index 3ecb859ef..97dfc0306 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -287,7 +287,7 @@ Below is the configuration file for your daemon on this node. We recommend not simply copy and pasting the code below unless you know what you are doing. You should run the auto-installer or auto-updater to setup the daemon.
-

You can generate a token for automatic setups. Pressing the button below will generate a token. It can only be used once and is valid for 5 minutes. It will also provide the necessary commands to auto-configure the node.

+

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.

From a661f71974db45e5e070b5442fea34202e9e86c4 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:39:41 +0100 Subject: [PATCH 07/10] fix styleci issues --- .../Controllers/Admin/NodesController.php | 9 +-- .../Controllers/Remote/RemoteController.php | 10 +-- app/Models/Node.php | 63 ++++++++++--------- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index ad9a1b31d..a5bcae5b2 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -279,7 +279,8 @@ class NodesController extends Controller ]); } - public function getConfigurationToken(Request $request, $id) { + public function getConfigurationToken(Request $request, $id) + { // Check if Node exists. Will lead to 404 if not. Models\Node::findOrFail($id); @@ -290,10 +291,10 @@ class NodesController extends Controller $token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes $token->save(); - $token_response = array( + $token_response = [ 'token' => $token->token, - 'expires_at' => $token->expires_at->toDateTimeString() - ); + 'expires_at' => $token->expires_at->toDateTimeString(), + ]; return response(json_encode($token_response), 200) ->header('Content-Type', 'application/json'); diff --git a/app/Http/Controllers/Remote/RemoteController.php b/app/Http/Controllers/Remote/RemoteController.php index 8a2e84383..d387899b6 100644 --- a/app/Http/Controllers/Remote/RemoteController.php +++ b/app/Http/Controllers/Remote/RemoteController.php @@ -29,7 +29,6 @@ use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\NotificationService; -use Pterodactyl\Models\NodeConfigurationToken; class RemoteController extends Controller { @@ -110,20 +109,21 @@ class RemoteController extends Controller return response('', 201); } - public function getConfiguration(Request $request, $tokenString) { + 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_encode(array('error' => 'token_invalid')), 403) + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + return response(json_encode(['error' => 'token_invalid']), 403) ->header('Content-Type', 'application/json'); } // Check if token is expired if ($token->expires_at->lt(Carbon::now())) { $token->delete(); - return response(json_encode(array('error' => 'token_expired')), 403) + return response(json_encode(['error' => 'token_expired']), 403) ->header('Content-Type', 'application/json'); } diff --git a/app/Models/Node.php b/app/Models/Node.php index eceb48fab..7c113b2f0 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -119,55 +119,58 @@ class Node extends Model } /** - * Returns the configuration in JSON format + * Returns the configuration in JSON format. * - * @param boolean $pretty Wether to pretty print the JSON or not + * @param bool $pretty Wether to pretty print the JSON or not * @return string The configration in JSON format */ - public function getConfigurationAsJson($pretty = false) { - $config = array( - 'web' => array( + public function getConfigurationAsJson($pretty = false) + { + $config = [ + 'web' => [ 'host' => '0.0.0.0', 'listen' => $this->daemonListen, - 'ssl' => array( + 'ssl' => [ 'enabled' => $this->scheme === 'https', 'certificate' => '/etc/letsencrypt/live/localhost/fullchain.pem', - 'key' => '/etc/letsencrypt/live/localhost/privkey.pem' - ) - ), - 'docker' => array( + 'key' => '/etc/letsencrypt/live/localhost/privkey.pem', + ], + ], + 'docker' => [ 'socket' => '/var/run/docker.sock', - 'autoupdate_images' => true - ), - 'sftp' => array( + 'autoupdate_images' => true, + ], + 'sftp' => [ 'path' => $this->daemonBase, 'port' => $this->daemonSFTP, - 'container' => 'ptdl-sftp' - ), - 'query' => array( + 'container' => 'ptdl-sftp', + ], + 'query' => [ 'kill_on_fail' => true, - 'fail_limit' => 5 - ), - 'logger' => array( + 'fail_limit' => 5, + ], + 'logger' => [ 'path' => 'logs/', 'src' => false, 'level' => 'info', 'period' => '1d', - 'count' => 3 - ), - 'remote' => array( + 'count' => 3, + ], + 'remote' => [ 'base' => config('app.url'), 'download' => route('remote.download'), - 'installed' => route('remote.install') - ), - 'uploads' => array( - 'size_limit' => $this->upload_size - ), - 'keys' => array($this->daemonSecret) - ); + '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; + if ($pretty) { + $json_options |= JSON_PRETTY_PRINT; + } return json_encode($config, $json_options); } From f70b33d69ce2ac8cb7d4fc878d503e3a1d371b5f Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 18:40:55 +0100 Subject: [PATCH 08/10] =?UTF-8?q?one=20more=20styleci=20fix.=20don?= =?UTF-8?q?=E2=80=99t=20be=20that=20picky!=20=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Remote/RemoteController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/Remote/RemoteController.php b/app/Http/Controllers/Remote/RemoteController.php index d387899b6..b762a38b2 100644 --- a/app/Http/Controllers/Remote/RemoteController.php +++ b/app/Http/Controllers/Remote/RemoteController.php @@ -123,6 +123,7 @@ class RemoteController extends Controller // Check if token is expired if ($token->expires_at->lt(Carbon::now())) { $token->delete(); + return response(json_encode(['error' => 'token_expired']), 403) ->header('Content-Type', 'application/json'); } From 67bb8fe230b94038151d798b6d238c66db251610 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sat, 7 Jan 2017 22:16:23 +0100 Subject: [PATCH 09/10] add protocol to the generated node configuration command --- resources/views/admin/nodes/view.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index 97dfc0306..f4f6b6ba8 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -509,7 +509,7 @@ $(document).ready(function () { title: 'Token created.', text: 'Here is your token: '+data.token+'
' + 'It will expire at ' + data.expires_at + '

' + - '

To auto-configure your node run
npm run configure -- --panel-url {{ config('app.url') }} --token '+data.token+'

', + '

To auto-configure your node run
npm run configure -- --panel-url '+window.location.protocol+'//{{ config('app.url') }} --token '+data.token+'

', html: true }) }) From 9f2ca17ea451154d3a3e04dbb2921d4b7d2810e0 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Sun, 8 Jan 2017 15:21:02 +0100 Subject: [PATCH 10/10] replace manual json headers with laravel response()->json() better Carbon dependency rename admin.nodes.configuration-token route style fixes --- app/Http/Controllers/Admin/NodesController.php | 5 ++--- app/Http/Controllers/Remote/RemoteController.php | 7 +++---- app/Http/Routes/AdminRoutes.php | 2 +- ...01_07_154228_create_node_configuration_tokens_table.php | 3 +-- resources/views/admin/nodes/view.blade.php | 2 +- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index a5bcae5b2..e7f53fa7f 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -27,8 +27,8 @@ namespace Pterodactyl\Http\Controllers\Admin; use DB; use Log; use Alert; +use Carbon; use Validator; -use Carbon\Carbon; use Pterodactyl\Models; use Illuminate\Http\Request; use Pterodactyl\Exceptions\DisplayException; @@ -296,7 +296,6 @@ class NodesController extends Controller 'expires_at' => $token->expires_at->toDateTimeString(), ]; - return response(json_encode($token_response), 200) - ->header('Content-Type', 'application/json'); + return response()->json($token_response, 200); } } diff --git a/app/Http/Controllers/Remote/RemoteController.php b/app/Http/Controllers/Remote/RemoteController.php index b762a38b2..600e3d0d0 100644 --- a/app/Http/Controllers/Remote/RemoteController.php +++ b/app/Http/Controllers/Remote/RemoteController.php @@ -116,21 +116,20 @@ class RemoteController extends Controller $token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail(); $node = Models\Node::findOrFail($token->node); } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { - return response(json_encode(['error' => 'token_invalid']), 403) - ->header('Content-Type', 'application/json'); + return response()->json(['error' => 'token_invalid'], 403); } // Check if token is expired if ($token->expires_at->lt(Carbon::now())) { $token->delete(); - return response(json_encode(['error' => 'token_expired']), 403) - ->header('Content-Type', 'application/json'); + 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'); } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 6a1cc8400..3b94b2aeb 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -288,7 +288,7 @@ class AdminRoutes ]); $router->get('/{id}/configurationtoken', [ - 'as' => 'admin.nodes.configurationtoken', + 'as' => 'admin.nodes.configuration-token', 'uses' => 'Admin\NodesController@getConfigurationToken', ]); }); diff --git a/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php b/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php index e86a87979..905d28a46 100644 --- a/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php +++ b/database/migrations/2017_01_07_154228_create_node_configuration_tokens_table.php @@ -18,8 +18,7 @@ class CreateNodeConfigurationTokensTable extends Migration $table->char('token', 32); $table->timestamp('expires_at'); $table->integer('node')->unsigned(); - $table->foreign('node') - ->references('id')->on('nodes'); + $table->foreign('node')->references('id')->on('nodes'); $table->timestamps(); }); } diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index f4f6b6ba8..c6befed5b 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -502,7 +502,7 @@ $(document).ready(function () { }); $('#configTokenBtn').on('click', function (event) { - $.getJSON('{{ route('admin.nodes.configurationtoken', $node->id) }}') + $.getJSON('{{ route('admin.nodes.configuration-token', $node->id) }}') .done(function (data) { swal({ type: 'success',