2015-12-06 18:58:49 +00:00
|
|
|
<?php
|
2016-01-20 00:10:39 +00:00
|
|
|
/**
|
2016-01-20 21:05:16 +00:00
|
|
|
* Pterodactyl - Panel
|
2016-12-07 22:46:38 +00:00
|
|
|
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* 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:
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* 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.
|
2016-01-20 00:10:39 +00:00
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Server;
|
|
|
|
|
2016-02-13 22:29:52 +00:00
|
|
|
use DB;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Log;
|
2016-01-22 04:53:48 +00:00
|
|
|
use Uuid;
|
|
|
|
use Alert;
|
2017-01-03 21:47:33 +00:00
|
|
|
use Javascript;
|
2016-01-23 00:40:48 +00:00
|
|
|
use Pterodactyl\Models;
|
2015-12-06 18:58:49 +00:00
|
|
|
use Illuminate\Http\Request;
|
2016-10-06 21:27:30 +00:00
|
|
|
use InvalidArgumentException;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Repositories\ServerRepository;
|
|
|
|
use Pterodactyl\Repositories\Daemon\FileRepository;
|
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
2016-10-06 21:27:30 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
class ServerController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2016-12-07 22:46:38 +00:00
|
|
|
* Controller Constructor.
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-01-04 21:09:39 +00:00
|
|
|
//
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders server index page for specified server.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function getIndex(Request $request)
|
|
|
|
{
|
2016-01-23 00:40:48 +00:00
|
|
|
$server = Models\Server::getByUUID($request->route()->server);
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2017-01-03 21:47:33 +00:00
|
|
|
Javascript::put([
|
|
|
|
'meta' => [
|
|
|
|
'saveFile' => route('server.files.save', $server->uuidShort),
|
|
|
|
'csrfToken' => csrf_token(),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
return view('server.index', [
|
|
|
|
'server' => $server,
|
2016-01-23 00:40:48 +00:00
|
|
|
'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
|
2016-12-07 22:46:38 +00:00
|
|
|
'node' => Models\Node::find($server->node),
|
2015-12-06 18:58:49 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders file overview page.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2017-01-03 21:47:33 +00:00
|
|
|
public function getFiles(Request $request, $uuid)
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2017-01-03 21:47:33 +00:00
|
|
|
$server = Models\Server::getByUUID($uuid);
|
2015-12-06 18:58:49 +00:00
|
|
|
$this->authorize('list-files', $server);
|
|
|
|
|
2017-01-03 21:47:33 +00:00
|
|
|
$node = Models\Node::find($server->node);
|
|
|
|
|
|
|
|
Javascript::put([
|
|
|
|
'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'),
|
|
|
|
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
|
|
|
|
'meta' => [
|
|
|
|
'directoryList' => route('server.files.directory-list', $server->uuidShort),
|
|
|
|
'csrftoken' => csrf_token(),
|
|
|
|
],
|
|
|
|
'permissions' => [
|
|
|
|
'moveFiles' => $request->user()->can('move-files', $server),
|
|
|
|
'copyFiles' => $request->user()->can('copy-files', $server),
|
|
|
|
'compressFiles' => $request->user()->can('compress-files', $server),
|
|
|
|
'decompressFiles' => $request->user()->can('decompress-files', $server),
|
|
|
|
'createFiles' => $request->user()->can('create-files', $server),
|
|
|
|
'downloadFiles' => $request->user()->can('download-files', $server),
|
|
|
|
'deleteFiles' => $request->user()->can('delete-files', $server),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
return view('server.files.index', [
|
|
|
|
'server' => $server,
|
2017-01-03 21:47:33 +00:00
|
|
|
'node' => $node,
|
2015-12-06 18:58:49 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders add file page.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2017-01-03 21:47:33 +00:00
|
|
|
public function getAddFile(Request $request, $uuid)
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2017-01-03 21:47:33 +00:00
|
|
|
$server = Models\Server::getByUUID($uuid);
|
2015-12-06 18:58:49 +00:00
|
|
|
$this->authorize('add-files', $server);
|
|
|
|
|
|
|
|
return view('server.files.add', [
|
|
|
|
'server' => $server,
|
2016-01-23 00:40:48 +00:00
|
|
|
'node' => Models\Node::find($server->node),
|
2016-12-07 22:46:38 +00:00
|
|
|
'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/',
|
2015-12-06 18:58:49 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders edit file page for a given file.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param string $uuid
|
|
|
|
* @param string $file
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function getEditFile(Request $request, $uuid, $file)
|
|
|
|
{
|
2016-01-23 00:40:48 +00:00
|
|
|
$server = Models\Server::getByUUID($uuid);
|
2015-12-06 18:58:49 +00:00
|
|
|
$this->authorize('edit-files', $server);
|
|
|
|
|
|
|
|
$fileInfo = (object) pathinfo($file);
|
2016-01-22 04:53:48 +00:00
|
|
|
$controller = new FileRepository($uuid);
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$fileContent = $controller->returnFileContents($file);
|
2016-09-07 20:12:06 +00:00
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-09-07 20:12:06 +00:00
|
|
|
return redirect()->route('server.files.index', $uuid);
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occured while attempting to load the requested file for editing, please try again.')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-09-07 19:28:57 +00:00
|
|
|
return redirect()->route('server.files.index', $uuid);
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('server.files.edit', [
|
|
|
|
'server' => $server,
|
2016-01-23 00:40:48 +00:00
|
|
|
'node' => Models\Node::find($server->node),
|
2015-12-06 18:58:49 +00:00
|
|
|
'file' => $file,
|
2016-10-02 03:09:55 +00:00
|
|
|
'stat' => $fileContent['stat'],
|
|
|
|
'contents' => $fileContent['file']->content,
|
2016-12-07 22:46:38 +00:00
|
|
|
'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/',
|
2015-12-06 18:58:49 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles downloading a file for the user.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param string $uuid
|
|
|
|
* @param string $file
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function getDownloadFile(Request $request, $uuid, $file)
|
|
|
|
{
|
2016-01-23 00:40:48 +00:00
|
|
|
$server = Models\Server::getByUUID($uuid);
|
|
|
|
$node = Models\Node::find($server->node);
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
$this->authorize('download-files', $server);
|
|
|
|
|
2016-01-25 23:39:34 +00:00
|
|
|
$download = new Models\Download;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2016-09-05 16:00:56 +00:00
|
|
|
$download->token = (string) Uuid::generate(4);
|
2016-01-02 00:27:44 +00:00
|
|
|
$download->server = $server->uuid;
|
2016-10-05 01:38:32 +00:00
|
|
|
$download->path = $file;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
$download->save();
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
return redirect($node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/file/download/' . $download->token);
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 04:53:48 +00:00
|
|
|
/**
|
|
|
|
* Renders server settings page.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function getSettings(Request $request, $uuid)
|
|
|
|
{
|
2016-01-23 00:40:48 +00:00
|
|
|
$server = Models\Server::getByUUID($uuid);
|
2016-08-31 20:03:37 +00:00
|
|
|
$allocation = Models\Allocation::findOrFail($server->allocation);
|
|
|
|
|
2016-02-13 22:29:52 +00:00
|
|
|
$variables = Models\ServiceVariables::select(
|
|
|
|
'service_variables.*',
|
|
|
|
DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue')
|
|
|
|
)->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
|
2016-08-16 23:06:03 +00:00
|
|
|
->where('service_variables.option_id', $server->option)
|
|
|
|
->where('server_variables.server_id', $server->id)
|
2016-01-23 00:40:48 +00:00
|
|
|
->get();
|
2016-02-13 22:29:52 +00:00
|
|
|
|
|
|
|
$service = Models\Service::select(
|
2016-09-16 22:44:12 +00:00
|
|
|
DB::raw('IFNULL(service_options.executable, services.executable) as executable')
|
2016-02-13 22:29:52 +00:00
|
|
|
)->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id')
|
2016-09-16 22:44:12 +00:00
|
|
|
->where('service_options.id', $server->option)
|
2016-02-13 22:29:52 +00:00
|
|
|
->where('services.id', $server->service)
|
|
|
|
->first();
|
2016-01-23 00:40:48 +00:00
|
|
|
|
|
|
|
$serverVariables = [
|
|
|
|
'{{SERVER_MEMORY}}' => $server->memory,
|
2016-08-31 20:03:37 +00:00
|
|
|
'{{SERVER_IP}}' => $allocation->ip,
|
|
|
|
'{{SERVER_PORT}}' => $allocation->port,
|
2016-01-23 00:40:48 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup);
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach ($variables as &$variable) {
|
2016-01-23 00:40:48 +00:00
|
|
|
$replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '**';
|
|
|
|
$processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed);
|
|
|
|
}
|
|
|
|
|
2016-01-22 04:53:48 +00:00
|
|
|
return view('server.settings', [
|
|
|
|
'server' => $server,
|
2016-02-15 02:43:20 +00:00
|
|
|
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
|
2016-08-16 04:07:10 +00:00
|
|
|
->where('server_id', $server->id)
|
2016-02-15 02:43:20 +00:00
|
|
|
->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
|
|
|
|
->get(),
|
2016-01-23 00:40:48 +00:00
|
|
|
'node' => Models\Node::find($server->node),
|
2016-02-13 22:29:52 +00:00
|
|
|
'variables' => $variables->where('user_viewable', 1),
|
2016-01-23 00:40:48 +00:00
|
|
|
'service' => $service,
|
|
|
|
'processedStartup' => $processed,
|
2016-01-22 04:53:48 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postSettingsSFTP(Request $request, $uuid)
|
|
|
|
{
|
2016-01-23 00:40:48 +00:00
|
|
|
$server = Models\Server::getByUUID($uuid);
|
2016-01-22 04:53:48 +00:00
|
|
|
$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();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-22 04:53:48 +00:00
|
|
|
return redirect()->route('server.settings', $uuid);
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:40:48 +00:00
|
|
|
public function postSettingsStartup(Request $request, $uuid)
|
|
|
|
{
|
|
|
|
$server = Models\Server::getByUUID($uuid);
|
|
|
|
$this->authorize('edit-startup', $server);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$repo = new ServerRepository;
|
|
|
|
$repo->updateStartup($server->id, $request->except([
|
2016-12-07 22:46:38 +00:00
|
|
|
'_token',
|
2016-01-23 00:40:48 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Server startup variables were successfully updated.')->flash();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
} catch (\Exception $ex) {
|
2016-01-23 00:40:48 +00:00
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-13 22:29:52 +00:00
|
|
|
return redirect()->route('server.settings', [
|
|
|
|
'uuid' => $uuid,
|
2016-12-07 22:46:38 +00:00
|
|
|
'tab' => 'tab_startup',
|
2016-02-13 22:29:52 +00:00
|
|
|
]);
|
2016-01-23 00:40:48 +00:00
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|