From 52229d5d2e9618ba12a58458f58887542ad4adaa Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 21 Jan 2016 23:53:48 -0500 Subject: [PATCH] Add SFTP management to server front-end --- .../Controllers/Server/ServerController.php | 50 +++++++++- app/Http/Routes/ServerRoutes.php | 11 +++ app/Models/Node.php | 4 +- app/Repositories/ServerRepository.php | 35 +++++++ resources/views/layouts/master.blade.php | 8 +- resources/views/server/settings.blade.php | 96 +++++++++++++++++++ 6 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 resources/views/server/settings.blade.php diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index b39377b2f..3b436d095 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -24,16 +24,21 @@ namespace Pterodactyl\Http\Controllers\Server; use Auth; +use Debugbar; +use Uuid; +use Alert; +use Log; + use Pterodactyl\Models\Server; use Pterodactyl\Models\Node; use Pterodactyl\Models\Download; use Pterodactyl\Models\Allocation; -use Debugbar; -use Uuid; -use Alert; use Pterodactyl\Exceptions\DisplayException; -use Pterodactyl\Repositories; +use Pterodactyl\Exceptions\DisplayValidationException; +use Pterodactyl\Repositories\Daemon\FileRepository; +use Pterodactyl\Repositories\ServerRepository; + use Pterodactyl\Http\Controllers\Controller; use Illuminate\Http\Request; @@ -127,7 +132,7 @@ class ServerController extends Controller $this->authorize('edit-files', $server); $fileInfo = (object) pathinfo($file); - $controller = new Repositories\Daemon\FileRepository($uuid); + $controller = new FileRepository($uuid); try { $fileContent = $controller->returnFileContents($file); @@ -184,4 +189,39 @@ class ServerController extends Controller } + /** + * Renders server settings page. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Contracts\View\View + */ + public function getSettings(Request $request, $uuid) + { + $server = Server::getByUUID($uuid); + return view('server.settings', [ + 'server' => $server, + 'node' => Node::find($server->node) + ]); + } + + public function postSettingsSFTP(Request $request, $uuid) + { + $server = Server::getByUUID($uuid); + $this->authorize('reset-sftp', $server); + + try { + $repo = new ServerRepository; + $repo->updateSFTPPassword($server->id, $request->input('sftp_pass')); + Alert::success('Successfully updated this servers SFTP password.')->flash(); + } catch (DisplayValidationException $ex) { + return redirect()->route('server.settings', $uuid)->withErrors(json_decode($ex->getMessage())); + } catch (DisplayException $ex) { + Alert::danger($ex->getMessage())->flash(); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An unknown error occured while attempting to update this server\'s SFTP settings.')->flash(); + } + return redirect()->route('server.settings', $uuid); + } + } diff --git a/app/Http/Routes/ServerRoutes.php b/app/Http/Routes/ServerRoutes.php index 7392a9ec2..e022da365 100644 --- a/app/Http/Routes/ServerRoutes.php +++ b/app/Http/Routes/ServerRoutes.php @@ -42,6 +42,17 @@ class ServerRoutes { 'uses' => 'Server\ServerController@getIndex' ]); + // Settings + $router->get('/settings', [ + 'as' => 'server.settings', + 'uses' => 'Server\ServerController@getSettings' + ]); + + $router->post('/settings/sftp', [ + 'as' => 'server.settings.sftp', + 'uses' => 'Server\ServerController@postSettingsSFTP' + ]); + // File Manager Routes $router->get('/files', [ 'as' => 'files.index', diff --git a/app/Models/Node.php b/app/Models/Node.php index 02cb09364..28e30662a 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -98,8 +98,8 @@ class Node extends Model // @TODO: Better solution to disabling verification. Security risk. self::$guzzle[$node] = new Client([ 'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen), - 'timeout' => 10.0, - 'connect_timeout' => 5.0, + 'timeout' => 5.0, + 'connect_timeout' => 3.0, ]); return self::$guzzle[$node]; diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index ba6a6be92..faa22f1dc 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -724,4 +724,39 @@ class ServerRepository return true; } + public function updateSFTPPassword($id, $password) + { + $server = Models\Server::findOrFail($id); + $node = Models\Node::findOrFail($server->node); + + $validator = Validator::make([ + 'password' => $password, + ], [ + 'password' => 'required|regex:/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/' + ]); + + if ($validator->fails()) { + throw new DisplayValidationException(json_encode($validator->errors())); + } + + try { + $client = Models\Node::guzzleRequest($server->node); + $client->request('POST', '/server/password', [ + 'headers' => [ + 'X-Access-Token' => $node->daemonSecret, + 'X-Access-Server' => $server->uuid + ], + 'json' => [ + 'password' => $password, + ], + ]); + return true; + } catch (\GuzzleHttp\Exception\TransferException $ex) { + throw new DisplayException('There was an error while attmping to contact the remote service to change the password.'); + } catch (\Exception $ex) { + throw $ex; + } + + } + } diff --git a/resources/views/layouts/master.blade.php b/resources/views/layouts/master.blade.php index b24a8d987..6fe4942c0 100644 --- a/resources/views/layouts/master.blade.php +++ b/resources/views/layouts/master.blade.php @@ -218,7 +218,13 @@ {{ trans('pagination.sidebar.overview') }} @can('list-files', $server){{ trans('pagination.sidebar.files') }}@endcan @can('list-subusers', $server){{ trans('pagination.sidebar.subusers') }}@endcan - @can('view-manage', $server){{ trans('pagination.sidebar.manage') }}@endcan + @can('view-sftp', $server) + {{ trans('pagination.sidebar.manage') }} + @else + @can('view-startup', $server) + {{ trans('pagination.sidebar.manage') }} + @endcan + @endcan @endif @show diff --git a/resources/views/server/settings.blade.php b/resources/views/server/settings.blade.php new file mode 100644 index 000000000..0f155c3bf --- /dev/null +++ b/resources/views/server/settings.blade.php @@ -0,0 +1,96 @@ +{{-- + Copyright (c) 2015 - 2016 Dane Everitt + + 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. +--}} +@extends('layouts.master') + +@section('title') + Server Settings +@endsection + +@section('content') +
+

Server Settings


+ +
+ @can('view-sftp', $server) +
+
+
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ @can('reset-sftp', $server) +
+
+
+ +
+ +

Passwords must meet the following requirements: at least one uppercase character, one lowercase character, one digit, and be at least 8 characters in length. Click here to generate one to use.

+
+
+
+ +
+ {!! csrf_field() !!} + +
+
+
+
+ @endcan +
+
+
+ @endcan + @can('view-startup', $server) +
+
+
+
+ Startup +
+
+
+ @endcan +
+
+ +@endsection