From 125856d92f02f7cc2182d058fe3173b488111d31 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 20 Oct 2016 16:42:54 -0400 Subject: [PATCH] Support for server info and minor changes to API setup --- CHANGELOG.md | 2 + .../Controllers/API/User/ServerController.php | 54 ++++++++++++++++++- app/Models/Server.php | 11 +++- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3be6bf7b..d670a3ab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,11 +9,13 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Return node configuration from remote API by using `/api/nodes/{id}/config` endpoint. Only accepts SSL connections. * Support for filtering servers within Admin CP to narrow down results by name, email, allocation, or defined fields. * Setup scripts (user, mail, env) now support argument flags for use in containers and other non-terminal environments. +* New API endpoints for individual users to control their servers with at `/api/me/*`. ### Changed * Creating a user, server, or node now returns `HTTP/1.1 200` and a JSON element with the user/server/node's ID. * Environment setting script is much more user friendly and does not require an excessive amount of clicking and typing. * File upload method switched from BinaryJS to Socket.io implementation to fix bugs as well as be a little speedier and allow upload throttling. +* `Server::getbyUUID()` now accepts either the `uuidShort` or full-length `uuid` for server identification. ## v0.5.0-pre.2 (Bodacious Boreopterus) diff --git a/app/Http/Controllers/API/User/ServerController.php b/app/Http/Controllers/API/User/ServerController.php index 805ca0c99..b29b4c9f8 100644 --- a/app/Http/Controllers/API/User/ServerController.php +++ b/app/Http/Controllers/API/User/ServerController.php @@ -23,6 +23,7 @@ */ namespace Pterodactyl\Http\Controllers\API\User; +use Log; use Pterodactyl\Models; use Illuminate\Http\Request; @@ -33,7 +34,58 @@ class ServerController extends BaseController public function info(Request $request, $uuid) { - // Will return server info including latest query and stats from daemon. + $server = Models\Server::getByUUID($uuid); + $node = Models\Node::findOrFail($server->node); + $client = Models\Node::guzzleRequest($node->id); + + try { + $response = $client->request('GET', '/server', [ + 'headers' => [ + 'X-Access-Token' => $server->daemonSecret, + 'X-Access-Server' => $server->uuid + ] + ]); + + $json = json_decode($response->getBody()); + $daemon = [ + 'status' => $json->status, + 'stats' => $json->proc, + 'query' => $json->query + ]; + } catch (\Exception $ex) { + $daemon = [ + 'error' => 'An error was encountered while trying to connect to the daemon to collece information. It might be offline.' + ]; + Log::error($ex); + } + + $allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get(); + foreach($allocations as &$allocation) { + $allocation->default = ($allocation->id === $server->allocation); + unset($allocation->id); + } + return [ + 'uuidShort' => $server->uuidShort, + 'uuid' => $server->uuid, + 'name' => $server->name, + 'node' => $node->name, + 'limits' => [ + 'memory' => $server->memory, + 'swap' => $server->swap, + 'disk' => $server->disk, + 'io' => $server->io, + 'cpu' => $server->cpu, + 'oom_disabled' => (bool) $server->oom_disabled + ], + 'allocations' => $allocations, + 'sftp' => [ + 'username' => $server->username + ], + 'daemon' => [ + 'token' => ($request->secure()) ? $server->daemonSecret : false, + 'response' => $daemon + ] + ]; } public function power(Request $request, $uuid) diff --git a/app/Models/Server.php b/app/Models/Server.php index af160da7b..af62a528b 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -27,6 +27,8 @@ use Auth; use Pterodactyl\Models\Subuser; use Illuminate\Database\Eloquent\Model; +use Pterodactyl\Exception\DisplayException; + class Server extends Model { @@ -104,7 +106,7 @@ class Server extends Model * @param Illuminate\Database\Eloquent\Model\Server $server * @return string */ - protected static function getUserDaemonSecret(Server $server) + public static function getUserDaemonSecret(Server $server) { if (self::$user->id === $server->owner || self::$user->root_admin === 1) { @@ -174,7 +176,8 @@ class Server extends Model $query = self::select('servers.*', 'services.file as a_serviceFile') ->join('services', 'services.id', '=', 'servers.service') - ->where('uuidShort', $uuid); + ->where('uuidShort', $uuid) + ->orWhere('uuid', $uuid); if (self::$user->root_admin !== 1) { $query->whereIn('servers.id', Subuser::accessServers()); @@ -182,6 +185,10 @@ class Server extends Model $result = $query->first(); + if (!$result) { + throw new DisplayException('No server was found belonging to this user.'); + } + if(!is_null($result)) { $result->daemonSecret = self::getUserDaemonSecret($result); }