Complete implementation of new Server model.

This commit is contained in:
Dane Everitt 2017-02-02 19:41:38 -05:00
parent 644ee85f59
commit 3114b7e52a
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 170 additions and 202 deletions

View file

@ -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. * 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`. * 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. * 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::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) ## v0.5.6 (Bodacious Boreopterus)
### Added ### Added

View file

@ -34,17 +34,10 @@ class ServerController extends BaseController
{ {
public function info(Request $request, $uuid) public function info(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid)->load('allocations');
$node = Models\Node::findOrFail($server->node_id);
$client = Models\Node::guzzleRequest($node->id);
try { try {
$response = $client->request('GET', '/server', [ $response = $server->guzzleClient()->request('GET', '/server');
'headers' => [
'X-Access-Token' => $server->daemonSecret,
'X-Access-Server' => $server->uuid,
],
]);
$json = json_decode($response->getBody()); $json = json_decode($response->getBody());
$daemon = [ $daemon = [
@ -59,8 +52,7 @@ class ServerController extends BaseController
Log::error($ex); Log::error($ex);
} }
$allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get(); foreach ($server->allocations as &$allocation) {
foreach ($allocations as &$allocation) {
$allocation->default = ($allocation->id === $server->allocation_id); $allocation->default = ($allocation->id === $server->allocation_id);
unset($allocation->id); unset($allocation->id);
} }
@ -69,7 +61,7 @@ class ServerController extends BaseController
'uuidShort' => $server->uuidShort, 'uuidShort' => $server->uuidShort,
'uuid' => $server->uuid, 'uuid' => $server->uuid,
'name' => $server->name, 'name' => $server->name,
'node' => $node->name, 'node' => $server->node->name,
'limits' => [ 'limits' => [
'memory' => $server->memory, 'memory' => $server->memory,
'swap' => $server->swap, 'swap' => $server->swap,
@ -78,7 +70,7 @@ class ServerController extends BaseController
'cpu' => $server->cpu, 'cpu' => $server->cpu,
'oom_disabled' => (bool) $server->oom_disabled, 'oom_disabled' => (bool) $server->oom_disabled,
], ],
'allocations' => $allocations, 'allocations' => $server->allocations,
'sftp' => [ 'sftp' => [
'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null, 'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null,
], ],
@ -91,16 +83,10 @@ class ServerController extends BaseController
public function power(Request $request, $uuid) public function power(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$client = Models\Node::guzzleRequest($server->node_id);
Auth::user()->can('power-' . $request->input('action'), $server); Auth::user()->can('power-' . $request->input('action'), $server);
$res = $client->request('PUT', '/server/power', [ $res = $server->guzzleClient()->request('PUT', '/server/power', [
'headers' => [
'X-Access-Server' => $server->uuid,
'X-Access-Token' => $server->daemonSecret,
],
'exceptions' => false, 'exceptions' => false,
'json' => [ 'json' => [
'action' => $request->input('action'), 'action' => $request->input('action'),

View file

@ -67,18 +67,14 @@ class AjaxController extends Controller
*/ */
public function getStatus(Request $request, $uuid) public function getStatus(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
if (! $server) { if (! $server) {
return response()->json([], 404); return response()->json([], 404);
} }
$client = Models\Node::guzzleRequest($server->node_id);
try { try {
$res = $client->request('GET', '/server', [ $res = $server->guzzleClient()->request('GET', '/server');
'headers' => Models\Server::getGuzzleHeaders($uuid),
]);
if ($res->getStatusCode() === 200) { if ($res->getStatusCode() === 200) {
return response()->json(json_decode($res->getBody())); return response()->json(json_decode($res->getBody()));
} }
@ -98,10 +94,10 @@ class AjaxController extends Controller
*/ */
public function postDirectoryList(Request $request, $uuid) public function postDirectoryList(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
$this->authorize('list-files', $server); $this->authorize('list-files', $server);
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
$prevDir = [ $prevDir = [
'header' => ($this->directory !== '/') ? $this->directory : '', 'header' => ($this->directory !== '/') ? $this->directory : '',
]; ];
@ -149,7 +145,7 @@ class AjaxController extends Controller
*/ */
public function postSaveFile(Request $request, $uuid) public function postSaveFile(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('save-files', $server); $this->authorize('save-files', $server);
$controller = new Repositories\Daemon\FileRepository($uuid); $controller = new Repositories\Daemon\FileRepository($uuid);
@ -175,7 +171,7 @@ class AjaxController extends Controller
*/ */
public function postSetPrimary(Request $request, $uuid) public function postSetPrimary(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid)->load('allocations');
$this->authorize('set-connection', $server); $this->authorize('set-connection', $server);
if ((int) $request->input('allocation') === $server->allocation_id) { if ((int) $request->input('allocation') === $server->allocation_id) {
@ -185,7 +181,7 @@ class AjaxController extends Controller
} }
try { 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) { if (! $allocation) {
return response()->json([ return response()->json([
'error' => 'No allocation matching your request was found in the system.', '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) public function postResetDatabasePassword(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail();
$this->authorize('reset-db-password', $server); $this->authorize('reset-db-password', $server);
$database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail();
try { try {
$repo = new Repositories\DatabaseRepository; $repo = new Repositories\DatabaseRepository;
$password = str_random(16); $password = str_random(16);

View file

@ -59,9 +59,7 @@ class ServerController extends Controller
{ {
$server = Models\Server::byUuid($uuid); $server = Models\Server::byUuid($uuid);
Javascript::put([ $server->js([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']),
'node' => collect($server->node_id)->only('fqdn', 'scheme', 'daemonListen'),
'meta' => [ 'meta' => [
'saveFile' => route('server.files.save', $server->uuidShort), 'saveFile' => route('server.files.save', $server->uuidShort),
'csrfToken' => csrf_token(), 'csrfToken' => csrf_token(),
@ -70,7 +68,7 @@ class ServerController extends Controller
return view('server.index', [ return view('server.index', [
'server' => $server, 'server' => $server,
'node' => $server->node_id, 'node' => $server->node,
]); ]);
} }
@ -82,14 +80,10 @@ class ServerController extends Controller
*/ */
public function getFiles(Request $request, $uuid) public function getFiles(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('list-files', $server); $this->authorize('list-files', $server);
$node = Models\Node::find($server->node_id); $server->js([
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
'meta' => [ 'meta' => [
'directoryList' => route('server.files.directory-list', $server->uuidShort), 'directoryList' => route('server.files.directory-list', $server->uuidShort),
'csrftoken' => csrf_token(), 'csrftoken' => csrf_token(),
@ -107,7 +101,7 @@ class ServerController extends Controller
return view('server.files.index', [ return view('server.files.index', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
]); ]);
} }
@ -119,18 +113,14 @@ class ServerController extends Controller
*/ */
public function getAddFile(Request $request, $uuid) public function getAddFile(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('add-files', $server); $this->authorize('add-files', $server);
$node = Models\Node::find($server->node_id);
Javascript::put([ $server->js();
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.files.add', [ return view('server.files.add', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/', '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) public function getEditFile(Request $request, $uuid, $file)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('edit-files', $server); $this->authorize('edit-files', $server);
$node = Models\Node::find($server->node_id);
$fileInfo = (object) pathinfo($file); $fileInfo = (object) pathinfo($file);
$controller = new FileRepository($uuid); $controller = new FileRepository($uuid);
@ -165,15 +154,13 @@ class ServerController extends Controller
return redirect()->route('server.files.index', $uuid); return redirect()->route('server.files.index', $uuid);
} }
Javascript::put([ $server->js([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
'stat' => $fileContent['stat'], 'stat' => $fileContent['stat'],
]); ]);
return view('server.files.edit', [ return view('server.files.edit', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
'file' => $file, 'file' => $file,
'stat' => $fileContent['stat'], 'stat' => $fileContent['stat'],
'contents' => $fileContent['file']->content, 'contents' => $fileContent['file']->content,
@ -191,9 +178,7 @@ class ServerController extends Controller
*/ */
public function getDownloadFile(Request $request, $uuid, $file) public function getDownloadFile(Request $request, $uuid, $file)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$node = Models\Node::find($server->node_id);
$this->authorize('download-files', $server); $this->authorize('download-files', $server);
$download = new Models\Download; $download = new Models\Download;
@ -204,38 +189,31 @@ class ServerController extends Controller
$download->save(); $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) public function getAllocation(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('view-allocation', $server); $this->authorize('view-allocation', $server);
$node = Models\Node::find($server->node_id); $server->js();
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.settings.allocation', [ return view('server.settings.allocation', [
'server' => $server, 'server' => $server->load(['allocations' => function ($query) {
'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), $query->orderBy('ip', 'asc');
'node' => $node, $query->orderBy('port', 'asc');
}]),
'node' => $server->node,
]); ]);
} }
public function getStartup(Request $request, $uuid) 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); $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( $variables = Models\ServiceVariables::select(
'service_variables.*', 'service_variables.*',
@ -252,6 +230,7 @@ class ServerController extends Controller
->where('services.id', $server->service_id) ->where('services.id', $server->service_id)
->first(); ->first();
$allocation = $server->allocations->pop();
$serverVariables = [ $serverVariables = [
'{{SERVER_MEMORY}}' => $server->memory, '{{SERVER_MEMORY}}' => $server->memory,
'{{SERVER_IP}}' => $allocation->ip, '{{SERVER_IP}}' => $allocation->ip,
@ -260,13 +239,15 @@ class ServerController extends Controller
$processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup); $processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup);
foreach ($variables as &$variable) { 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); $processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed);
} }
$server->js();
return view('server.settings.startup', [ return view('server.settings.startup', [
'server' => $server, 'server' => $server,
'node' => Models\Node::find($server->node_id), 'node' => $server->node,
'variables' => $variables->where('user_viewable', 1), 'variables' => $variables->where('user_viewable', 1),
'service' => $service, 'service' => $service,
'processedStartup' => $processed, 'processedStartup' => $processed,
@ -275,18 +256,13 @@ class ServerController extends Controller
public function getDatabases(Request $request, $uuid) public function getDatabases(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('view-databases', $server); $this->authorize('view-databases', $server);
$node = Models\Node::find($server->node_id); $server->js();
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.settings.databases', [ return view('server.settings.databases', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port') 'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
->where('server_id', $server->id) ->where('server_id', $server->id)
->join('database_servers', 'database_servers.id', '=', 'databases.db_server') ->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
@ -296,24 +272,19 @@ class ServerController extends Controller
public function getSFTP(Request $request, $uuid) public function getSFTP(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('view-sftp', $server); $this->authorize('view-sftp', $server);
$node = Models\Node::find($server->node_id); $server->js();
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.settings.sftp', [ return view('server.settings.sftp', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
]); ]);
} }
public function postSettingsSFTP(Request $request, $uuid) public function postSettingsSFTP(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('reset-sftp', $server); $this->authorize('reset-sftp', $server);
try { try {
@ -334,7 +305,7 @@ class ServerController extends Controller
public function postSettingsStartup(Request $request, $uuid) public function postSettingsStartup(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('edit-startup', $server); $this->authorize('edit-startup', $server);
try { try {

View file

@ -50,18 +50,14 @@ class SubuserController extends Controller
public function getIndex(Request $request, $uuid) public function getIndex(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('list-subusers', $server); $this->authorize('list-subusers', $server);
$node = Models\Node::find($server->node_id);
Javascript::put([ $server->js();
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.users.index', [ return view('server.users.index', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp') 'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp')
->join('users', 'users.id', '=', 'subusers.user_id') ->join('users', 'users.id', '=', 'subusers.user_id')
->where('server_id', $server->id) ->where('server_id', $server->id)
@ -71,14 +67,8 @@ class SubuserController extends Controller
public function getView(Request $request, $uuid, $id) public function getView(Request $request, $uuid, $id)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('view-subuser', $server); $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') $subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail')
->join('users', 'users.id', '=', 'subusers.user_id') ->join('users', 'users.id', '=', 'subusers.user_id')
@ -98,9 +88,11 @@ class SubuserController extends Controller
$permissions[$perm->permission] = true; $permissions[$perm->permission] = true;
} }
$server->js();
return view('server.users.view', [ return view('server.users.view', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
'subuser' => $subuser, 'subuser' => $subuser,
'permissions' => $permissions, 'permissions' => $permissions,
]); ]);
@ -108,7 +100,7 @@ class SubuserController extends Controller
public function postView(Request $request, $uuid, $id) public function postView(Request $request, $uuid, $id)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('edit-subuser', $server); $this->authorize('edit-subuser', $server);
$subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); $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) public function getNew(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('create-subuser', $server); $this->authorize('create-subuser', $server);
$node = Models\Node::find($server->node_id); $server->js();
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.users.new', [ return view('server.users.new', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
]); ]);
} }
public function postNew(Request $request, $uuid) public function postNew(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('create-subuser', $server); $this->authorize('create-subuser', $server);
try { try {
@ -193,7 +180,7 @@ class SubuserController extends Controller
public function deleteSubuser(Request $request, $uuid, $id) public function deleteSubuser(Request $request, $uuid, $id)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('delete-subuser', $server); $this->authorize('delete-subuser', $server);
try { try {

View file

@ -43,19 +43,14 @@ class TaskController extends Controller
public function getIndex(Request $request, $uuid) public function getIndex(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid)->load('tasks');
$this->authorize('list-tasks', $server); $this->authorize('list-tasks', $server);
$node = Models\Node::find($server->node_id); $server->js();
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.tasks.index', [ return view('server.tasks.index', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
'tasks' => Models\Task::where('server', $server->id)->get(), 'tasks' => $server->tasks,
'actions' => [ 'actions' => [
'command' => trans('server.tasks.actions.command'), 'command' => trans('server.tasks.actions.command'),
'power' => trans('server.tasks.actions.power'), 'power' => trans('server.tasks.actions.power'),
@ -65,24 +60,19 @@ class TaskController extends Controller
public function getNew(Request $request, $uuid) public function getNew(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('create-task', $server); $this->authorize('create-task', $server);
$node = Models\Node::find($server->node_id); $server->js();
Javascript::put([
'server' => collect($server->makeVisible('daemonSecret'))->only(['uuid', 'uuidShort', 'daemonSecret', 'username']),
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'),
]);
return view('server.tasks.new', [ return view('server.tasks.new', [
'server' => $server, 'server' => $server,
'node' => $node, 'node' => $server->node,
]); ]);
} }
public function postNew(Request $request, $uuid) public function postNew(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('create-task', $server); $this->authorize('create-task', $server);
try { try {
@ -106,12 +96,11 @@ class TaskController extends Controller
public function deleteTask(Request $request, $uuid, $id) public function deleteTask(Request $request, $uuid, $id)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid)->load('tasks');
$this->authorize('delete-task', $server); $this->authorize('delete-task', $server);
$task = Models\Task::findOrFail($id); $task = $server->tasks->where('id', $id)->first();
if (! $task) {
if (! $task || $server->id !== $task->server) {
return response()->json([ return response()->json([
'error' => 'No task by that ID was found associated with this server.', 'error' => 'No task by that ID was found associated with this server.',
], 404); ], 404);
@ -133,12 +122,11 @@ class TaskController extends Controller
public function toggleTask(Request $request, $uuid, $id) public function toggleTask(Request $request, $uuid, $id)
{ {
$server = Models\Server::getByUUID($uuid); $server = Models\Server::byUuid($uuid)->load('tasks');
$this->authorize('toggle-task', $server); $this->authorize('toggle-task', $server);
$task = Models\Task::findOrFail($id); $task = $server->tasks->where('id', $id)->first();
if (! $task) {
if (! $task || $server->id !== $task->server) {
return response()->json([ return response()->json([
'error' => 'No task by that ID was found associated with this server.', 'error' => 'No task by that ID was found associated with this server.',
], 404); ], 404);

View file

@ -96,11 +96,27 @@ class Node extends Model
return self::$nodes[$id]; 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. * Returns a Guzzle Client for the node in question.
* *
* @param int $node * @param int $node
* @return \GuzzleHttp\Client * @return \GuzzleHttp\Client
* @deprecated
*/ */
public static function guzzleRequest($node) public static function guzzleRequest($node)
{ {

View file

@ -25,6 +25,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Auth; use Auth;
use Javascript;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -67,18 +68,19 @@ class Server extends Model
* @var array * @var array
*/ */
protected $casts = [ protected $casts = [
'node' => 'integer', 'node_id' => 'integer',
'suspended' => 'integer', 'suspended' => 'integer',
'owner' => 'integer', 'owner_id' => 'integer',
'memory' => 'integer', 'memory' => 'integer',
'swap' => 'integer', 'swap' => 'integer',
'disk' => 'integer', 'disk' => 'integer',
'io' => 'integer', 'io' => 'integer',
'cpu' => 'integer', 'cpu' => 'integer',
'oom_disabled' => 'integer', 'oom_disabled' => 'integer',
'port' => 'integer', 'allocation_id' => 'integer',
'service' => 'integer', 'service_id' => 'integer',
'option' => 'integer', 'option_id' => 'integer',
'pack_id' => 'integer',
'installed' => 'integer', 'installed' => 'integer',
]; ];
@ -166,7 +168,7 @@ class Server extends Model
* @param string $uuid * @param string $uuid
* @return array * @return array
*/ */
public function getHeaders() public function guzzleHeaders()
{ {
return [ return [
'X-Access-Server' => $this->uuid, '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. * Gets all allocations associated with this server.
* *
@ -233,4 +277,15 @@ class Server extends Model
{ {
return $this->hasOne(Node::class, 'id', 'node_id'); 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');
}
} }

View file

@ -40,28 +40,6 @@ class FileRepository
*/ */
protected $server; 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. * Constructor.
* *
@ -69,10 +47,7 @@ class FileRepository
*/ */
public function __construct($uuid) public function __construct($uuid)
{ {
$this->server = Server::getByUUID($uuid); $this->server = Server::byUuid($uuid);
$this->node = Node::getByID($this->server->node);
$this->client = Node::guzzleRequest($this->server->node);
$this->headers = Server::getGuzzleHeaders($uuid);
} }
/** /**
@ -88,12 +63,9 @@ class FileRepository
} }
$file = (object) pathinfo($file); $file = (object) pathinfo($file);
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
$res = $this->client->request('GET', '/server/file/stat/' . rawurlencode($file->dirname . $file->basename), [ $res = $this->server->guzzleClient()->request('GET', '/server/file/stat/' . rawurlencode($file->dirname . $file->basename));
'headers' => $this->headers,
]);
$stat = json_decode($res->getBody()); $stat = json_decode($res->getBody());
if ($res->getStatusCode() !== 200 || ! isset($stat->size)) { 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.'); 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), [ $res = $this->server->guzzleClient()->request('GET', '/server/file/f/' . rawurlencode($file->dirname . $file->basename));
'headers' => $this->headers,
]);
$json = json_decode($res->getBody()); $json = json_decode($res->getBody());
if ($res->getStatusCode() !== 200 || ! isset($json->content)) { if ($res->getStatusCode() !== 200 || ! isset($json->content)) {
@ -137,11 +107,9 @@ class FileRepository
} }
$file = (object) pathinfo($file); $file = (object) pathinfo($file);
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
$res = $this->client->request('POST', '/server/file/save', [ $res = $this->server->guzzleClient()->request('POST', '/server/file/save', [
'headers' => $this->headers,
'json' => [ 'json' => [
'path' => rawurlencode($file->dirname . $file->basename), 'path' => rawurlencode($file->dirname . $file->basename),
'content' => $content, 'content' => $content,
@ -167,9 +135,7 @@ class FileRepository
throw new Exception('A valid directory must be specified in order to list its contents.'); throw new Exception('A valid directory must be specified in order to list its contents.');
} }
$res = $this->client->request('GET', '/server/directory/' . rawurlencode($directory), [ $res = $this->server->guzzleClient()->request('GET', '/server/directory/' . rawurlencode($directory));
'headers' => $this->headers,
]);
$json = json_decode($res->getBody()); $json = json_decode($res->getBody());
if ($res->getStatusCode() !== 200) { if ($res->getStatusCode() !== 200) {
@ -180,7 +146,7 @@ class FileRepository
$files = []; $files = [];
$folders = []; $folders = [];
foreach ($json as &$value) { foreach ($json as &$value) {
if ($value->directory === true) { if ($value->directory) {
// @TODO Handle Symlinks // @TODO Handle Symlinks
$folders[] = [ $folders[] = [
'entry' => $value->name, 'entry' => $value->name,
@ -189,7 +155,7 @@ class FileRepository
'date' => strtotime($value->modified), 'date' => strtotime($value->modified),
'mime' => $value->mime, 'mime' => $value->mime,
]; ];
} elseif ($value->file === true) { } elseif ($value->file) {
$files[] = [ $files[] = [
'entry' => $value->name, 'entry' => $value->name,
'directory' => trim($directory, '/'), 'directory' => trim($directory, '/'),

View file

@ -49,7 +49,7 @@
<th>@lang('strings.port')</th> <th>@lang('strings.port')</th>
<th></th> <th></th>
</tr> </tr>
@foreach ($allocations as $allocation) @foreach ($server->allocations as $allocation)
<tr> <tr>
<td> <td>
<code>{{ $allocation->ip }}</code> <code>{{ $allocation->ip }}</code>