Skip to content

Commit 52229d5

Browse files
committed
Add SFTP management to server front-end
1 parent b6e83b8 commit 52229d5

File tree

6 files changed

+196
-8
lines changed

6 files changed

+196
-8
lines changed

app/Http/Controllers/Server/ServerController.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@
2424
namespace Pterodactyl\Http\Controllers\Server;
2525

2626
use Auth;
27+
use Debugbar;
28+
use Uuid;
29+
use Alert;
30+
use Log;
31+
2732
use Pterodactyl\Models\Server;
2833
use Pterodactyl\Models\Node;
2934
use Pterodactyl\Models\Download;
3035
use Pterodactyl\Models\Allocation;
31-
use Debugbar;
32-
use Uuid;
33-
use Alert;
3436

3537
use Pterodactyl\Exceptions\DisplayException;
36-
use Pterodactyl\Repositories;
38+
use Pterodactyl\Exceptions\DisplayValidationException;
39+
use Pterodactyl\Repositories\Daemon\FileRepository;
40+
use Pterodactyl\Repositories\ServerRepository;
41+
3742
use Pterodactyl\Http\Controllers\Controller;
3843
use Illuminate\Http\Request;
3944

@@ -127,7 +132,7 @@ public function getEditFile(Request $request, $uuid, $file)
127132
$this->authorize('edit-files', $server);
128133

129134
$fileInfo = (object) pathinfo($file);
130-
$controller = new Repositories\Daemon\FileRepository($uuid);
135+
$controller = new FileRepository($uuid);
131136

132137
try {
133138
$fileContent = $controller->returnFileContents($file);
@@ -184,4 +189,39 @@ public function getDownloadFile(Request $request, $uuid, $file)
184189

185190
}
186191

192+
/**
193+
* Renders server settings page.
194+
*
195+
* @param \Illuminate\Http\Request $request
196+
* @return \Illuminate\Contracts\View\View
197+
*/
198+
public function getSettings(Request $request, $uuid)
199+
{
200+
$server = Server::getByUUID($uuid);
201+
return view('server.settings', [
202+
'server' => $server,
203+
'node' => Node::find($server->node)
204+
]);
205+
}
206+
207+
public function postSettingsSFTP(Request $request, $uuid)
208+
{
209+
$server = Server::getByUUID($uuid);
210+
$this->authorize('reset-sftp', $server);
211+
212+
try {
213+
$repo = new ServerRepository;
214+
$repo->updateSFTPPassword($server->id, $request->input('sftp_pass'));
215+
Alert::success('Successfully updated this servers SFTP password.')->flash();
216+
} catch (DisplayValidationException $ex) {
217+
return redirect()->route('server.settings', $uuid)->withErrors(json_decode($ex->getMessage()));
218+
} catch (DisplayException $ex) {
219+
Alert::danger($ex->getMessage())->flash();
220+
} catch (\Exception $ex) {
221+
Log::error($ex);
222+
Alert::danger('An unknown error occured while attempting to update this server\'s SFTP settings.')->flash();
223+
}
224+
return redirect()->route('server.settings', $uuid);
225+
}
226+
187227
}

app/Http/Routes/ServerRoutes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public function map(Router $router) {
4242
'uses' => 'Server\ServerController@getIndex'
4343
]);
4444

45+
// Settings
46+
$router->get('/settings', [
47+
'as' => 'server.settings',
48+
'uses' => 'Server\ServerController@getSettings'
49+
]);
50+
51+
$router->post('/settings/sftp', [
52+
'as' => 'server.settings.sftp',
53+
'uses' => 'Server\ServerController@postSettingsSFTP'
54+
]);
55+
4556
// File Manager Routes
4657
$router->get('/files', [
4758
'as' => 'files.index',

app/Models/Node.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public static function guzzleRequest($node)
9898
// @TODO: Better solution to disabling verification. Security risk.
9999
self::$guzzle[$node] = new Client([
100100
'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen),
101-
'timeout' => 10.0,
102-
'connect_timeout' => 5.0,
101+
'timeout' => 5.0,
102+
'connect_timeout' => 3.0,
103103
]);
104104

105105
return self::$guzzle[$node];

app/Repositories/ServerRepository.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,4 +724,39 @@ public function unsuspend($id)
724724
return true;
725725
}
726726

727+
public function updateSFTPPassword($id, $password)
728+
{
729+
$server = Models\Server::findOrFail($id);
730+
$node = Models\Node::findOrFail($server->node);
731+
732+
$validator = Validator::make([
733+
'password' => $password,
734+
], [
735+
'password' => 'required|regex:/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/'
736+
]);
737+
738+
if ($validator->fails()) {
739+
throw new DisplayValidationException(json_encode($validator->errors()));
740+
}
741+
742+
try {
743+
$client = Models\Node::guzzleRequest($server->node);
744+
$client->request('POST', '/server/password', [
745+
'headers' => [
746+
'X-Access-Token' => $node->daemonSecret,
747+
'X-Access-Server' => $server->uuid
748+
],
749+
'json' => [
750+
'password' => $password,
751+
],
752+
]);
753+
return true;
754+
} catch (\GuzzleHttp\Exception\TransferException $ex) {
755+
throw new DisplayException('There was an error while attmping to contact the remote service to change the password.');
756+
} catch (\Exception $ex) {
757+
throw $ex;
758+
}
759+
760+
}
761+
727762
}

resources/views/layouts/master.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@
218218
<a href="/server/{{ $server->uuidShort }}/" class="list-group-item server-index">{{ trans('pagination.sidebar.overview') }}</a>
219219
@can('list-files', $server)<a href="/server/{{ $server->uuidShort }}/files" class="list-group-item server-files">{{ trans('pagination.sidebar.files') }}</a>@endcan
220220
@can('list-subusers', $server)<a href="/server/{{ $server->uuidShort }}/users" class="list-group-item server-users">{{ trans('pagination.sidebar.subusers') }}</a>@endcan
221-
@can('view-manage', $server)<a href="/server/{{ $server->uuidShort }}/settings" class="list-group-item server-settings">{{ trans('pagination.sidebar.manage') }}</a>@endcan
221+
@can('view-sftp', $server)
222+
<a href="/server/{{ $server->uuidShort }}/settings" class="list-group-item server-settings">{{ trans('pagination.sidebar.manage') }}</a>
223+
@else
224+
@can('view-startup', $server)
225+
<a href="/server/{{ $server->uuidShort }}/settings" class="list-group-item server-settings">{{ trans('pagination.sidebar.manage') }}</a>
226+
@endcan
227+
@endcan
222228
</div>
223229
@endif
224230
@show
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{{--
2+
Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
--}}
22+
@extends('layouts.master')
23+
24+
@section('title')
25+
Server Settings
26+
@endsection
27+
28+
@section('content')
29+
<div class="col-md-12">
30+
<h3 class="nopad">Server Settings</h3><hr />
31+
<ul class="nav nav-tabs tabs_with_panel" id="config_tabs">
32+
@can('view-sftp', $server)<li class="active"><a href="#tab_sftp" data-toggle="tab">SFTP Settings</a></li>@endcan
33+
@can('view-startup', $server)<li><a href="#tab_startup" data-toggle="tab">Startup Configuration</a></li>@endcan
34+
</ul>
35+
<div class="tab-content">
36+
@can('view-sftp', $server)
37+
<div class="tab-pane active" id="tab_sftp">
38+
<div class="panel panel-default">
39+
<div class="panel-heading"></div>
40+
<div class="panel-body">
41+
<div class="row">
42+
<div class="form-group col-md-6">
43+
<label class="control-label">SFTP Connection Address:</label>
44+
<div>
45+
<input type="text" readonly="readonly" class="form-control" value="{{ $node->fqdn }}:{{ $node->daemonSFTP }}" />
46+
</div>
47+
</div>
48+
<div class="form-group col-md-6">
49+
<label class="control-label">SFTP Username:</label>
50+
<div>
51+
<input type="text" readonly="readonly" class="form-control" value="{{ $server->username }}" />
52+
</div>
53+
</div>
54+
</div>
55+
@can('reset-sftp', $server)
56+
<form action="{{ route('server.settings.sftp', $server->uuidShort) }}" method="POST">
57+
<div class="row">
58+
<div class="form-group col-md-6">
59+
<label class="control-label">New SFTP Password:</label>
60+
<div>
61+
<input type="password" name="sftp_pass" class="form-control" />
62+
<p class="text-muted"><small>Passwords must meet the following requirements: at least one uppercase character, one lowercase character, one digit, and be at least 8 characters in length. <a href="#" data-action="generate-password">Click here</a> to generate one to use.</small></p>
63+
</div>
64+
</div>
65+
<div class="form-group col-md-6">
66+
<label class="control-label">&nbsp;</label>
67+
<div>
68+
{!! csrf_field() !!}
69+
<input type="submit" class="btn btn-sm btn-primary" value="Update Password" />
70+
</div>
71+
</div>
72+
</div>
73+
</form>
74+
@endcan
75+
</div>
76+
</div>
77+
</div>
78+
@endcan
79+
@can('view-startup', $server)
80+
<div class="tab-pane" id="tab_startup">
81+
<div class="panel panel-default">
82+
<div class="panel-heading"></div>
83+
<div class="panel-body">
84+
Startup
85+
</div>
86+
</div>
87+
</div>
88+
@endcan
89+
</div>
90+
</div>
91+
<script>
92+
$(document).ready(function () {
93+
$('.server-settings').addClass('active');
94+
});
95+
</script>
96+
@endsection

0 commit comments

Comments
 (0)