Skip to content

Commit 125856d

Browse files
committed
Support for server info and minor changes to API setup
1 parent 5a03ce7 commit 125856d

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
99
* Return node configuration from remote API by using `/api/nodes/{id}/config` endpoint. Only accepts SSL connections.
1010
* Support for filtering servers within Admin CP to narrow down results by name, email, allocation, or defined fields.
1111
* Setup scripts (user, mail, env) now support argument flags for use in containers and other non-terminal environments.
12+
* New API endpoints for individual users to control their servers with at `/api/me/*`.
1213

1314
### Changed
1415
* Creating a user, server, or node now returns `HTTP/1.1 200` and a JSON element with the user/server/node's ID.
1516
* Environment setting script is much more user friendly and does not require an excessive amount of clicking and typing.
1617
* File upload method switched from BinaryJS to Socket.io implementation to fix bugs as well as be a little speedier and allow upload throttling.
18+
* `Server::getbyUUID()` now accepts either the `uuidShort` or full-length `uuid` for server identification.
1719

1820
## v0.5.0-pre.2 (Bodacious Boreopterus)
1921

app/Http/Controllers/API/User/ServerController.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
namespace Pterodactyl\Http\Controllers\API\User;
2525

26+
use Log;
2627
use Pterodactyl\Models;
2728
use Illuminate\Http\Request;
2829

@@ -33,7 +34,58 @@ class ServerController extends BaseController
3334

3435
public function info(Request $request, $uuid)
3536
{
36-
// Will return server info including latest query and stats from daemon.
37+
$server = Models\Server::getByUUID($uuid);
38+
$node = Models\Node::findOrFail($server->node);
39+
$client = Models\Node::guzzleRequest($node->id);
40+
41+
try {
42+
$response = $client->request('GET', '/server', [
43+
'headers' => [
44+
'X-Access-Token' => $server->daemonSecret,
45+
'X-Access-Server' => $server->uuid
46+
]
47+
]);
48+
49+
$json = json_decode($response->getBody());
50+
$daemon = [
51+
'status' => $json->status,
52+
'stats' => $json->proc,
53+
'query' => $json->query
54+
];
55+
} catch (\Exception $ex) {
56+
$daemon = [
57+
'error' => 'An error was encountered while trying to connect to the daemon to collece information. It might be offline.'
58+
];
59+
Log::error($ex);
60+
}
61+
62+
$allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get();
63+
foreach($allocations as &$allocation) {
64+
$allocation->default = ($allocation->id === $server->allocation);
65+
unset($allocation->id);
66+
}
67+
return [
68+
'uuidShort' => $server->uuidShort,
69+
'uuid' => $server->uuid,
70+
'name' => $server->name,
71+
'node' => $node->name,
72+
'limits' => [
73+
'memory' => $server->memory,
74+
'swap' => $server->swap,
75+
'disk' => $server->disk,
76+
'io' => $server->io,
77+
'cpu' => $server->cpu,
78+
'oom_disabled' => (bool) $server->oom_disabled
79+
],
80+
'allocations' => $allocations,
81+
'sftp' => [
82+
'username' => $server->username
83+
],
84+
'daemon' => [
85+
'token' => ($request->secure()) ? $server->daemonSecret : false,
86+
'response' => $daemon
87+
]
88+
];
3789
}
3890

3991
public function power(Request $request, $uuid)

app/Models/Server.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Pterodactyl\Models\Subuser;
2828
use Illuminate\Database\Eloquent\Model;
2929

30+
use Pterodactyl\Exception\DisplayException;
31+
3032
class Server extends Model
3133
{
3234

@@ -104,7 +106,7 @@ public function __construct()
104106
* @param Illuminate\Database\Eloquent\Model\Server $server
105107
* @return string
106108
*/
107-
protected static function getUserDaemonSecret(Server $server)
109+
public static function getUserDaemonSecret(Server $server)
108110
{
109111

110112
if (self::$user->id === $server->owner || self::$user->root_admin === 1) {
@@ -174,14 +176,19 @@ public static function getByUUID($uuid)
174176

175177
$query = self::select('servers.*', 'services.file as a_serviceFile')
176178
->join('services', 'services.id', '=', 'servers.service')
177-
->where('uuidShort', $uuid);
179+
->where('uuidShort', $uuid)
180+
->orWhere('uuid', $uuid);
178181

179182
if (self::$user->root_admin !== 1) {
180183
$query->whereIn('servers.id', Subuser::accessServers());
181184
}
182185

183186
$result = $query->first();
184187

188+
if (!$result) {
189+
throw new DisplayException('No server was found belonging to this user.');
190+
}
191+
185192
if(!is_null($result)) {
186193
$result->daemonSecret = self::getUserDaemonSecret($result);
187194
}

0 commit comments

Comments
 (0)