From 3114b7e52a1a078a0d05ec2d8b22fe9073a5bf9b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 2 Feb 2017 19:41:38 -0500 Subject: [PATCH] Complete implementation of new Server model. --- CHANGELOG.md | 5 +- .../Controllers/API/User/ServerController.php | 28 ++--- .../Controllers/Server/AjaxController.php | 24 ++-- .../Controllers/Server/ServerController.php | 103 +++++++----------- .../Controllers/Server/SubuserController.php | 39 +++---- .../Controllers/Server/TaskController.php | 40 +++---- app/Models/Node.php | 16 +++ app/Models/Server.php | 67 +++++++++++- app/Repositories/Daemon/FileRepository.php | 48 ++------ .../server/settings/allocation.blade.php | 2 +- 10 files changed, 170 insertions(+), 202 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b459be5..cc093ab76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,9 +20,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields. * User model now defines mass assignment fields using `$fillable` rather than `$guarded`. * 2FA checkpoint on login is now its own page, and not an AJAX based call. Improves security on that front. +* Updated Server model code to be more efficient, as well as make life easier for backend changes and work. -### Removed +### Deprecated * `Server::getUserDaemonSecret(Server $server)` was removed and replaced with `User::daemonSecret(Server $server)` in order to clean up models. +* `Server::getByUUID()` was replaced with `Server::byUuid()` as well as various other functions through-out the Server model. +* `Server::getHeaders()` was removed and replaced with `Server::getClient()` which returns a Guzzle Client with the correct headers already assigned. ## v0.5.6 (Bodacious Boreopterus) ### Added diff --git a/app/Http/Controllers/API/User/ServerController.php b/app/Http/Controllers/API/User/ServerController.php index 128c5cf68..c6091f0b1 100644 --- a/app/Http/Controllers/API/User/ServerController.php +++ b/app/Http/Controllers/API/User/ServerController.php @@ -34,17 +34,10 @@ class ServerController extends BaseController { public function info(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $node = Models\Node::findOrFail($server->node_id); - $client = Models\Node::guzzleRequest($node->id); + $server = Models\Server::byUuid($uuid)->load('allocations'); try { - $response = $client->request('GET', '/server', [ - 'headers' => [ - 'X-Access-Token' => $server->daemonSecret, - 'X-Access-Server' => $server->uuid, - ], - ]); + $response = $server->guzzleClient()->request('GET', '/server'); $json = json_decode($response->getBody()); $daemon = [ @@ -59,8 +52,7 @@ class ServerController extends BaseController Log::error($ex); } - $allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get(); - foreach ($allocations as &$allocation) { + foreach ($server->allocations as &$allocation) { $allocation->default = ($allocation->id === $server->allocation_id); unset($allocation->id); } @@ -69,7 +61,7 @@ class ServerController extends BaseController 'uuidShort' => $server->uuidShort, 'uuid' => $server->uuid, 'name' => $server->name, - 'node' => $node->name, + 'node' => $server->node->name, 'limits' => [ 'memory' => $server->memory, 'swap' => $server->swap, @@ -78,7 +70,7 @@ class ServerController extends BaseController 'cpu' => $server->cpu, 'oom_disabled' => (bool) $server->oom_disabled, ], - 'allocations' => $allocations, + 'allocations' => $server->allocations, 'sftp' => [ 'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null, ], @@ -91,16 +83,10 @@ class ServerController extends BaseController public function power(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $client = Models\Node::guzzleRequest($server->node_id); - + $server = Models\Server::byUuid($uuid); Auth::user()->can('power-' . $request->input('action'), $server); - $res = $client->request('PUT', '/server/power', [ - 'headers' => [ - 'X-Access-Server' => $server->uuid, - 'X-Access-Token' => $server->daemonSecret, - ], + $res = $server->guzzleClient()->request('PUT', '/server/power', [ 'exceptions' => false, 'json' => [ 'action' => $request->input('action'), diff --git a/app/Http/Controllers/Server/AjaxController.php b/app/Http/Controllers/Server/AjaxController.php index 9ee9f4284..9493bc0f0 100644 --- a/app/Http/Controllers/Server/AjaxController.php +++ b/app/Http/Controllers/Server/AjaxController.php @@ -67,18 +67,14 @@ class AjaxController extends Controller */ public function getStatus(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); if (! $server) { return response()->json([], 404); } - $client = Models\Node::guzzleRequest($server->node_id); - try { - $res = $client->request('GET', '/server', [ - 'headers' => Models\Server::getGuzzleHeaders($uuid), - ]); + $res = $server->guzzleClient()->request('GET', '/server'); if ($res->getStatusCode() === 200) { return response()->json(json_decode($res->getBody())); } @@ -98,10 +94,10 @@ class AjaxController extends Controller */ public function postDirectoryList(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); + $server = Models\Server::byUuid($uuid); $this->authorize('list-files', $server); + $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); $prevDir = [ 'header' => ($this->directory !== '/') ? $this->directory : '', ]; @@ -149,7 +145,7 @@ class AjaxController extends Controller */ public function postSaveFile(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('save-files', $server); $controller = new Repositories\Daemon\FileRepository($uuid); @@ -175,7 +171,7 @@ class AjaxController extends Controller */ public function postSetPrimary(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('allocations'); $this->authorize('set-connection', $server); if ((int) $request->input('allocation') === $server->allocation_id) { @@ -185,7 +181,7 @@ class AjaxController extends Controller } try { - $allocation = Models\Allocation::where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first(); + $allocation = $server->allocations->where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first(); if (! $allocation) { return response()->json([ 'error' => 'No allocation matching your request was found in the system.', @@ -217,10 +213,10 @@ class AjaxController extends Controller public function postResetDatabasePassword(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); - $database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail(); - + $server = Models\Server::byUuid($uuid); $this->authorize('reset-db-password', $server); + + $database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail(); try { $repo = new Repositories\DatabaseRepository; $password = str_random(16); diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index 7f07f977c..0b423cc97 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -59,9 +59,7 @@ class ServerController extends Controller { $server = Models\Server::byUuid($uuid); - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']), - 'node' => collect($server->node_id)->only('fqdn', 'scheme', 'daemonListen'), + $server->js([ 'meta' => [ 'saveFile' => route('server.files.save', $server->uuidShort), 'csrfToken' => csrf_token(), @@ -70,7 +68,7 @@ class ServerController extends Controller return view('server.index', [ 'server' => $server, - 'node' => $server->node_id, + 'node' => $server->node, ]); } @@ -82,14 +80,10 @@ class ServerController extends Controller */ public function getFiles(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('list-files', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), + $server->js([ 'meta' => [ 'directoryList' => route('server.files.directory-list', $server->uuidShort), 'csrftoken' => csrf_token(), @@ -107,7 +101,7 @@ class ServerController extends Controller return view('server.files.index', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } @@ -119,18 +113,14 @@ class ServerController extends Controller */ public function getAddFile(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('add-files', $server); - $node = Models\Node::find($server->node_id); - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.files.add', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/', ]); } @@ -145,9 +135,8 @@ class ServerController extends Controller */ public function getEditFile(Request $request, $uuid, $file) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('edit-files', $server); - $node = Models\Node::find($server->node_id); $fileInfo = (object) pathinfo($file); $controller = new FileRepository($uuid); @@ -165,15 +154,13 @@ class ServerController extends Controller return redirect()->route('server.files.index', $uuid); } - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), + $server->js([ 'stat' => $fileContent['stat'], ]); return view('server.files.edit', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'file' => $file, 'stat' => $fileContent['stat'], 'contents' => $fileContent['file']->content, @@ -191,9 +178,7 @@ class ServerController extends Controller */ public function getDownloadFile(Request $request, $uuid, $file) { - $server = Models\Server::getByUUID($uuid); - $node = Models\Node::find($server->node_id); - + $server = Models\Server::byUuid($uuid); $this->authorize('download-files', $server); $download = new Models\Download; @@ -204,38 +189,31 @@ class ServerController extends Controller $download->save(); - return redirect($node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/file/download/' . $download->token); + return redirect($server->node->scheme . '://' . $server->node->fqdn . ':' . $server->node->daemonListen . '/server/file/download/' . $download->token); } public function getAllocation(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-allocation', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.settings.allocation', [ - 'server' => $server, - 'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), - 'node' => $node, + 'server' => $server->load(['allocations' => function ($query) { + $query->orderBy('ip', 'asc'); + $query->orderBy('port', 'asc'); + }]), + 'node' => $server->node, ]); } public function getStartup(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); + $server->load(['allocations' => function ($query) use ($server) { + $query->where('id', $server->allocation_id); + }]); $this->authorize('view-startup', $server); - $node = Models\Node::find($server->node_id); - $allocation = Models\Allocation::findOrFail($server->allocation_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); $variables = Models\ServiceVariables::select( 'service_variables.*', @@ -252,6 +230,7 @@ class ServerController extends Controller ->where('services.id', $server->service_id) ->first(); + $allocation = $server->allocations->pop(); $serverVariables = [ '{{SERVER_MEMORY}}' => $server->memory, '{{SERVER_IP}}' => $allocation->ip, @@ -260,13 +239,15 @@ class ServerController extends Controller $processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup); foreach ($variables as &$variable) { - $replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '**'; + $replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '[hidden]'; $processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed); } + $server->js(); + return view('server.settings.startup', [ 'server' => $server, - 'node' => Models\Node::find($server->node_id), + 'node' => $server->node, 'variables' => $variables->where('user_viewable', 1), 'service' => $service, 'processedStartup' => $processed, @@ -275,18 +256,13 @@ class ServerController extends Controller public function getDatabases(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-databases', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.settings.databases', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port') ->where('server_id', $server->id) ->join('database_servers', 'database_servers.id', '=', 'databases.db_server') @@ -296,24 +272,19 @@ class ServerController extends Controller public function getSFTP(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-sftp', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.settings.sftp', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } public function postSettingsSFTP(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('reset-sftp', $server); try { @@ -334,7 +305,7 @@ class ServerController extends Controller public function postSettingsStartup(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('edit-startup', $server); try { diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index 1d07b47f7..9badfef45 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -50,18 +50,14 @@ class SubuserController extends Controller public function getIndex(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('list-subusers', $server); - $node = Models\Node::find($server->node_id); - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.users.index', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp') ->join('users', 'users.id', '=', 'subusers.user_id') ->where('server_id', $server->id) @@ -71,14 +67,8 @@ class SubuserController extends Controller public function getView(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('view-subuser', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); $subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail') ->join('users', 'users.id', '=', 'subusers.user_id') @@ -98,9 +88,11 @@ class SubuserController extends Controller $permissions[$perm->permission] = true; } + $server->js(); + return view('server.users.view', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, 'subuser' => $subuser, 'permissions' => $permissions, ]); @@ -108,7 +100,7 @@ class SubuserController extends Controller public function postView(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('edit-subuser', $server); $subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); @@ -148,24 +140,19 @@ class SubuserController extends Controller public function getNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-subuser', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.users.new', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } public function postNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-subuser', $server); try { @@ -193,7 +180,7 @@ class SubuserController extends Controller public function deleteSubuser(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('delete-subuser', $server); try { diff --git a/app/Http/Controllers/Server/TaskController.php b/app/Http/Controllers/Server/TaskController.php index 51243cc8f..4fb492afc 100644 --- a/app/Http/Controllers/Server/TaskController.php +++ b/app/Http/Controllers/Server/TaskController.php @@ -43,19 +43,14 @@ class TaskController extends Controller public function getIndex(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('tasks'); $this->authorize('list-tasks', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.tasks.index', [ 'server' => $server, - 'node' => $node, - 'tasks' => Models\Task::where('server', $server->id)->get(), + 'node' => $server->node, + 'tasks' => $server->tasks, 'actions' => [ 'command' => trans('server.tasks.actions.command'), 'power' => trans('server.tasks.actions.power'), @@ -65,24 +60,19 @@ class TaskController extends Controller public function getNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-task', $server); - $node = Models\Node::find($server->node_id); - - Javascript::put([ - 'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']), - 'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), - ]); + $server->js(); return view('server.tasks.new', [ 'server' => $server, - 'node' => $node, + 'node' => $server->node, ]); } public function postNew(Request $request, $uuid) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid); $this->authorize('create-task', $server); try { @@ -106,12 +96,11 @@ class TaskController extends Controller public function deleteTask(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('tasks'); $this->authorize('delete-task', $server); - $task = Models\Task::findOrFail($id); - - if (! $task || $server->id !== $task->server) { + $task = $server->tasks->where('id', $id)->first(); + if (! $task) { return response()->json([ 'error' => 'No task by that ID was found associated with this server.', ], 404); @@ -133,12 +122,11 @@ class TaskController extends Controller public function toggleTask(Request $request, $uuid, $id) { - $server = Models\Server::getByUUID($uuid); + $server = Models\Server::byUuid($uuid)->load('tasks'); $this->authorize('toggle-task', $server); - $task = Models\Task::findOrFail($id); - - if (! $task || $server->id !== $task->server) { + $task = $server->tasks->where('id', $id)->first(); + if (! $task) { return response()->json([ 'error' => 'No task by that ID was found associated with this server.', ], 404); diff --git a/app/Models/Node.php b/app/Models/Node.php index a5cbce464..8e82ef935 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -96,11 +96,27 @@ class Node extends Model return self::$nodes[$id]; } + /** + * Return an instance of the Guzzle client for this specific node. + * + * @return \GuzzleHttp\Client + */ + public function guzzleClient($headers = []) + { + return new Client([ + 'base_uri' => sprintf('%s://%s:%s/', $this->scheme, $this->fqdn, $this->daemonListen), + 'timeout' => env('GUZZLE_TIMEOUT', 5.0), + 'connect_timeout' => env('GUZZLE_CONNECT_TIMEOUT', 3.0), + 'headers' => $headers, + ]); + } + /** * Returns a Guzzle Client for the node in question. * * @param int $node * @return \GuzzleHttp\Client + * @deprecated */ public static function guzzleRequest($node) { diff --git a/app/Models/Server.php b/app/Models/Server.php index 32240da44..d0d02bc58 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -25,6 +25,7 @@ namespace Pterodactyl\Models; use Auth; +use Javascript; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\SoftDeletes; @@ -67,18 +68,19 @@ class Server extends Model * @var array */ protected $casts = [ - 'node' => 'integer', + 'node_id' => 'integer', 'suspended' => 'integer', - 'owner' => 'integer', + 'owner_id' => 'integer', 'memory' => 'integer', 'swap' => 'integer', 'disk' => 'integer', 'io' => 'integer', 'cpu' => 'integer', 'oom_disabled' => 'integer', - 'port' => 'integer', - 'service' => 'integer', - 'option' => 'integer', + 'allocation_id' => 'integer', + 'service_id' => 'integer', + 'option_id' => 'integer', + 'pack_id' => 'integer', 'installed' => 'integer', ]; @@ -166,7 +168,7 @@ class Server extends Model * @param string $uuid * @return array */ - public function getHeaders() + public function guzzleHeaders() { return [ 'X-Access-Server' => $this->uuid, @@ -174,6 +176,48 @@ class Server extends Model ]; } + /** + * Return an instance of the Guzzle client for this specific server using defined access token. + * + * @return \GuzzleHttp\Client + */ + public function guzzleClient() + { + return $this->node->guzzleClient($this->guzzleHeaders()); + } + + /** + * Returns javascript object to be embedded on server view pages with relevant information. + * + * @return \Laracasts\Utilities\JavaScript\JavaScriptFacade + */ + public function js($additional = null, $overwrite = null) + { + $response = [ + 'server' => collect($this->makeVisible('daemonSecret'))->only([ + 'uuid', + 'uuidShort', + 'daemonSecret', + 'username' + ]), + 'node' => collect($this->node)->only([ + 'fqdn', + 'scheme', + 'daemonListen' + ]), + ]; + + if (is_array($additional)) { + $response = array_merge($response, $additional); + } + + if (is_array($overwrite)) { + $response = $overwrite; + } + + return Javascript::put($response); + } + /** * Gets all allocations associated with this server. * @@ -233,4 +277,15 @@ class Server extends Model { return $this->hasOne(Node::class, 'id', 'node_id'); } + + /** + * Gets information for the tasks associated with this server. + * + * @TODO adjust server column in tasks to be server_id + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function tasks() + { + return $this->hasMany(Task::class, 'server', 'id'); + } } diff --git a/app/Repositories/Daemon/FileRepository.php b/app/Repositories/Daemon/FileRepository.php index d1319cf91..cb5f92a28 100644 --- a/app/Repositories/Daemon/FileRepository.php +++ b/app/Repositories/Daemon/FileRepository.php @@ -40,28 +40,6 @@ class FileRepository */ 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. * @@ -69,10 +47,7 @@ class FileRepository */ 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); + $this->server = Server::byUuid($uuid); } /** @@ -88,12 +63,9 @@ class FileRepository } $file = (object) pathinfo($file); - $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; - $res = $this->client->request('GET', '/server/file/stat/' . rawurlencode($file->dirname . $file->basename), [ - 'headers' => $this->headers, - ]); + $res = $this->server->guzzleClient()->request('GET', '/server/file/stat/' . rawurlencode($file->dirname . $file->basename)); $stat = json_decode($res->getBody()); if ($res->getStatusCode() !== 200 || ! isset($stat->size)) { @@ -108,9 +80,7 @@ class FileRepository throw new DisplayException('That file is too large to open in the browser, consider using a SFTP client.'); } - $res = $this->client->request('GET', '/server/file/f/' . rawurlencode($file->dirname . $file->basename), [ - 'headers' => $this->headers, - ]); + $res = $this->server->guzzleClient()->request('GET', '/server/file/f/' . rawurlencode($file->dirname . $file->basename)); $json = json_decode($res->getBody()); if ($res->getStatusCode() !== 200 || ! isset($json->content)) { @@ -137,11 +107,9 @@ class FileRepository } $file = (object) pathinfo($file); - $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; - $res = $this->client->request('POST', '/server/file/save', [ - 'headers' => $this->headers, + $res = $this->server->guzzleClient()->request('POST', '/server/file/save', [ 'json' => [ 'path' => rawurlencode($file->dirname . $file->basename), 'content' => $content, @@ -167,9 +135,7 @@ class FileRepository throw new Exception('A valid directory must be specified in order to list its contents.'); } - $res = $this->client->request('GET', '/server/directory/' . rawurlencode($directory), [ - 'headers' => $this->headers, - ]); + $res = $this->server->guzzleClient()->request('GET', '/server/directory/' . rawurlencode($directory)); $json = json_decode($res->getBody()); if ($res->getStatusCode() !== 200) { @@ -180,7 +146,7 @@ class FileRepository $files = []; $folders = []; foreach ($json as &$value) { - if ($value->directory === true) { + if ($value->directory) { // @TODO Handle Symlinks $folders[] = [ 'entry' => $value->name, @@ -189,7 +155,7 @@ class FileRepository 'date' => strtotime($value->modified), 'mime' => $value->mime, ]; - } elseif ($value->file === true) { + } elseif ($value->file) { $files[] = [ 'entry' => $value->name, 'directory' => trim($directory, '/'), diff --git a/resources/themes/pterodactyl/server/settings/allocation.blade.php b/resources/themes/pterodactyl/server/settings/allocation.blade.php index 773281e96..a6a0c36ad 100644 --- a/resources/themes/pterodactyl/server/settings/allocation.blade.php +++ b/resources/themes/pterodactyl/server/settings/allocation.blade.php @@ -49,7 +49,7 @@ @lang('strings.port') - @foreach ($allocations as $allocation) + @foreach ($server->allocations as $allocation) {{ $allocation->ip }}