Code cleanup and fixes filemanager

This commit is contained in:
Dane Everitt 2016-01-01 18:08:15 -05:00
parent a391a2d854
commit 46117afc77
9 changed files with 246 additions and 23 deletions

View file

@ -1,6 +1,6 @@
<?php
namespace Pterodactyl\Http\Controllers\Scales;
namespace Pterodactyl\Http\Controllers\Daemon;
use \Exception;
use Log;
@ -9,7 +9,7 @@ use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Helpers;
use Pterodactyl\Repositories\HelperRepository;
use Pterodactyl\Http\Controllers\Controller;
use GuzzleHttp\Client;
@ -76,7 +76,7 @@ class FileController extends Controller
}
$file = (object) pathinfo($file);
if (!in_array($file->extension, Helpers::editableFiles())) {
if (!in_array($file->extension, HelperRepository::editableFiles())) {
throw new DisplayException('You do not have permission to edit this type of file.');
}
@ -111,7 +111,7 @@ class FileController extends Controller
$file = (object) pathinfo($file);
if(!in_array($file->extension, Helpers::editableFiles())) {
if(!in_array($file->extension, HelperRepository::editableFiles())) {
throw new DisplayException('You do not have permission to edit this type of file.');
}
@ -175,7 +175,7 @@ class FileController extends Controller
'entry' => $value->name,
'directory' => trim($directory, '/'),
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
'size' => Helpers::bytesToHuman($value->size),
'size' => HelperRepository::bytesToHuman($value->size),
'date' => strtotime($value->modified)
]]);

View file

@ -6,10 +6,9 @@ use Log;
use Debugbar;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Http\Helpers;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Scales\FileController;
use Pterodactyl\Repositories;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
@ -119,7 +118,7 @@ class AjaxController extends Controller
$prevDir['link_show'] = trim($prevDir['link'], '/');
}
$controller = new FileController($uuid);
$controller = new Repositories\Daemon\FileRepository($uuid);
try {
$directoryContents = $controller->returnDirectoryListing($this->directory);
@ -140,7 +139,7 @@ class AjaxController extends Controller
'server' => $server,
'files' => $directoryContents->files,
'folders' => $directoryContents->folders,
'extensions' => Helpers::editableFiles(),
'extensions' => Repositories\HelperRepository::editableFiles(),
'directory' => $prevDir
]);
@ -159,7 +158,7 @@ class AjaxController extends Controller
$server = Server::getByUUID($uuid);
$this->authorize('save-files', $server);
$controller = new FileController($uuid);
$controller = new Repositories\Daemon\FileRepository($uuid);
try {
$controller->saveFileContents($request->input('file'), $request->input('contents'));

View file

@ -11,7 +11,7 @@ use Uuid;
use Alert;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Scales\FileController;
use Pterodactyl\Repositories;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
@ -102,7 +102,7 @@ class ServerController extends Controller
$this->authorize('edit-files', $server);
$fileInfo = (object) pathinfo($file);
$controller = new FileController($uuid);
$controller = new Repositories\Daemon\FileRepository($uuid);
try {
$fileContent = $controller->returnFileContents($file);
@ -124,7 +124,7 @@ class ServerController extends Controller
'server' => $server,
'node' => Node::find($server->node),
'file' => $file,
'contents' => $fileContent->contents,
'contents' => $fileContent->content,
'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/',
'extension' => $fileInfo->extension
]);
@ -155,7 +155,7 @@ class ServerController extends Controller
$download->save();
return redirect('https://' . $node->fqdn . ':' . $node->daemonListen . '/server/download/' . $download->token);
return redirect( $node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/download/' . $download->token);
}

View file

@ -69,7 +69,7 @@ class Node extends Model
// @TODO: Better solution to disabling verification. Security risk.
self::$guzzle[$node] = new Client([
'base_uri' => sprintf('http%s://%s:%s/', ($nodeData->https === true) ? 's' : '', $nodeData->fqdn, $nodeData->daemonListen),
'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen),
'timeout' => 10.0,
'connect_timeout' => 5.0,
'verify' => false,

View file

@ -0,0 +1,191 @@
<?php
namespace Pterodactyl\Repositories\Daemon;
use \Exception;
use Log;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Repositories\HelperRepository;
use Pterodactyl\Exceptions\DisplayException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class FileRepository
{
/**
* The Eloquent Model associated with the requested server.
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $server;
/**
* The Eloquent Model for the node corresponding with the requested server.
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $node;
/**
* The Guzzle Client associated with the requested server and node.
*
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* The Guzzle Client headers associated with the requested server and node.
* (non-administrative headers)
*
* @var array
*/
protected $headers;
/**
* Constructor
*
* @param string $server The server Short UUID
*/
public function __construct($uuid)
{
$this->server = Server::getByUUID($uuid);
$this->node = Node::getByID($this->server->node);
$this->client = Node::guzzleRequest($this->server->node);
$this->headers = Server::getGuzzleHeaders($uuid);
}
/**
* Get the contents of a requested file for the server.
*
* @param string $file
* @return string
*/
public function returnFileContents($file)
{
if (empty($file)) {
throw new Exception('Not all parameters were properly passed to the function.');
}
$file = (object) pathinfo($file);
if (!in_array($file->extension, HelperRepository::editableFiles())) {
throw new DisplayException('You do not have permission to edit this type of file.');
}
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
$res = $this->client->request('GET', '/server/file/' . rawurlencode($file->dirname.$file->basename), [
'headers' => $this->headers
]);
$json = json_decode($res->getBody());
if($res->getStatusCode() !== 200 || !isset($json->content)) {
throw new DisplayException('Scales provided a non-200 error code: HTTP\\' . $res->getStatusCode());
}
return $json;
}
/**
* Save the contents of a requested file on the Scales instance.
*
* @param string $file
* @param string $content
* @return boolean
*/
public function saveFileContents($file, $content)
{
if (empty($file)) {
throw new Exception('A valid file and path must be specified to save a file.');
}
$file = (object) pathinfo($file);
if(!in_array($file->extension, HelperRepository::editableFiles())) {
throw new DisplayException('You do not have permission to edit this type of file.');
}
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
$res = $this->client->request('POST', '/server/file/' . rawurlencode($file->dirname.$file->basename), [
'headers' => $this->headers,
'json' => [
'content' => $content
]
]);
if ($res->getStatusCode() !== 204) {
throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody());
}
return true;
}
/**
* Returns a listing of all files and folders within a specified Scales directory.
*
* @param string $directory
* @return object
*/
public function returnDirectoryListing($directory)
{
if (empty($directory)) {
throw new Exception('A valid directory must be specified in order to list its contents.');
}
$res = $this->client->request('GET', '/server/directory/' . $directory, [
'headers' => $this->headers
]);
$json = json_decode($res->getBody());
if($res->getStatusCode() !== 200) {
throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody());
}
// Iterate through results
$files = [];
$folders = [];
foreach($json as &$value) {
if ($value->directory === true) {
// @TODO Handle Symlinks
$folders = array_merge($folders, [[
'entry' => $value->name,
'directory' => trim($directory, '/'),
'size' => null,
'date' => strtotime($value->modified)
]]);
} else if ($value->file === true) {
$files = array_merge($files, [[
'entry' => $value->name,
'directory' => trim($directory, '/'),
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
'size' => HelperRepository::bytesToHuman($value->size),
'date' => strtotime($value->modified)
]]);
}
}
return (object) [
'files' => $files,
'folders' => $folders,
];
}
}

View file

@ -1,8 +1,8 @@
<?php
namespace Pterodactyl\Http;
namespace Pterodactyl\Repositories;
class Helpers {
class HelperRepository {
/**
* Listing of editable files in the control panel.

View file

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class NodeHttpsToScheme extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('nodes', function (Blueprint $table) {
$table->string('https', 5)->default('http')->change();
$table->renameColumn('https', 'scheme');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('nodes', function (Blueprint $table) {
$table->boolean('scheme')->default(false)->change();
$table->renameColumn('scheme', 'https');
});
}
}

View file

@ -6,8 +6,8 @@
@section('content')
<div class="col-md-9">
<div class="row" id="internal_alert">
<div class="col-md-12">
<div class="row">
<div class="col-md-12" id="internal_alert">
<div class="alert alert-info">
<i class="fa fa-spinner fa-spin"></i> {{ trans('server.files.loading') }}
</div>
@ -98,7 +98,7 @@
$.ajax({
type: 'DELETE',
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/file/' + deleteItemPath,
url: '{{ $node->scheme }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/file/' + deleteItemPath,
headers: {
'X-Access-Token': '{{ $server->daemonSecret }}',
'X-Access-Server': '{{ $server->uuid }}'

View file

@ -178,7 +178,7 @@ $(window).load(function () {
'X-Access-Token': '{{ $server->daemonSecret }}',
'X-Access-Server': '{{ $server->uuid }}'
},
url: 'http{{ $node->https ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/log',
url: '{{ $node->scheme }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/log',
timeout: 10000
}).done(function(data) {
$('#live_console').val(data);
@ -250,7 +250,7 @@ $(window).load(function () {
'X-Access-Server': '{{ $server->uuid }}'
},
contentType: 'application/json; charset=utf-8',
url: 'http{{ $node->https ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/command',
url: '{{ $node->scheme }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/command',
timeout: 10000,
data: JSON.stringify({ command: ccmd })
}).fail(function (jqXHR) {
@ -307,7 +307,7 @@ $(window).load(function () {
data: JSON.stringify({
action: action
}),
url: 'http{{ $node->https ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power',
url: '{{ $node->scheme }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power',
timeout: 10000
}).done(function(data) {
$('#pw_resp').attr('class', 'alert alert-success').html('Server has been ' + action + 'ed successfully.').fadeIn().delay(5000).fadeOut();