diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 88975c57..fb2c568e 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -424,4 +424,42 @@ class ServersController extends Controller ])->withInput(); } + public function postSuspendServer(Request $request, $id) + { + try { + $repo = new ServerRepository; + $repo->suspend($id); + Alert::success('Server has been suspended on the system. All running processes have been stopped and will not be startable until it is un-suspended.'); + } catch (\Pterodactyl\Exceptions\DisplayException $e) { + Alert::danger($e->getMessage())->flash(); + } catch(\Exception $e) { + Log::error($e); + Alert::danger('An unhandled exception occured while attemping to suspend this server. Please try again.')->flash(); + } finally { + return redirect()->route('admin.servers.view', [ + 'id' => $id, + 'tab' => 'tab_manage' + ]); + } + } + + public function postUnsuspendServer(Request $request, $id) + { + try { + $repo = new ServerRepository; + $repo->unsuspend($id); + Alert::success('Server has been unsuspended on the system. Access has been re-enabled.'); + } catch (\Pterodactyl\Exceptions\DisplayException $e) { + Alert::danger($e->getMessage())->flash(); + } catch(\Exception $e) { + Log::error($e); + Alert::danger('An unhandled exception occured while attemping to unsuspend this server. Please try again.')->flash(); + } finally { + return redirect()->route('admin.servers.view', [ + 'id' => $id, + 'tab' => 'tab_manage' + ]); + } + } + } diff --git a/app/Http/Middleware/CheckServer.php b/app/Http/Middleware/CheckServer.php index c4b06da4..7ae0c115 100644 --- a/app/Http/Middleware/CheckServer.php +++ b/app/Http/Middleware/CheckServer.php @@ -46,11 +46,15 @@ class CheckServer $server = Server::getByUUID($request->route()->server); if (!$server) { - return response()->view('errors.403', [], 403); + return response()->view('errors.404', [], 404); + } + + if ($server->suspended === 1) { + return response()->view('errors.suspended', [], 403); } if ($server->installed !== 1) { - return response()->view('errors.installing', [], 503); + return response()->view('errors.installing', [], 403); } return $next($request); diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 53f1a23a..e4c2a09f 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -129,21 +129,21 @@ class AdminRoutes { ]); // Assorted Page Helpers - $router->post('/new/get-nodes', [ - 'uses' => 'Admin\ServersController@postNewServerGetNodes' - ]); + $router->post('/new/get-nodes', [ + 'uses' => 'Admin\ServersController@postNewServerGetNodes' + ]); - $router->post('/new/get-ips', [ - 'uses' => 'Admin\ServersController@postNewServerGetIps' - ]); + $router->post('/new/get-ips', [ + 'uses' => 'Admin\ServersController@postNewServerGetIps' + ]); - $router->post('/new/service-options', [ - 'uses' => 'Admin\ServersController@postNewServerServiceOptions' - ]); + $router->post('/new/service-options', [ + 'uses' => 'Admin\ServersController@postNewServerServiceOptions' + ]); - $router->post('/new/service-variables', [ - 'uses' => 'Admin\ServersController@postNewServerServiceVariables' - ]); + $router->post('/new/service-variables', [ + 'uses' => 'Admin\ServersController@postNewServerServiceVariables' + ]); // End Assorted Page Helpers // View Specific Server @@ -179,6 +179,16 @@ class AdminRoutes { 'uses' => 'Admin\ServersController@postUpdateServerUpdateBuild' ]); + // Suspend Server + $router->post('/view/{id}/suspend', [ + 'uses' => 'Admin\ServersController@postSuspendServer' + ]); + + // Unsuspend Server + $router->post('/view/{id}/unsuspend', [ + 'uses' => 'Admin\ServersController@postUnsuspendServer' + ]); + // Change Install Status $router->post('/view/{id}/installed', [ 'uses' => 'Admin\ServersController@postToggleInstall' diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 21be6597..c5d0c6bc 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -728,9 +728,31 @@ class ServerRepository */ public function suspend($id) { - // @TODO: Implement logic; not doing it now since that is outside of the - // scope of this API brance. - return true; + $server = Models\Server::findOrFail($id); + $node = Models\Node::findOrFail($server->node); + + DB::beginTransaction(); + + try { + $server->suspended = 1; + $server->save(); + + $client = Models\Node::guzzleRequest($server->node); + $client->request('POST', '/server/suspend', [ + 'headers' => [ + 'X-Access-Token' => $node->daemonSecret, + 'X-Access-Server' => $server->uuid + ] + ]); + + return DB::commit(); + } catch (\GuzzleHttp\Exception\TransferException $ex) { + DB::rollBack(); + throw new DisplayException('An error occured while attempting to suspend this server.', $ex); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } } /** @@ -740,9 +762,31 @@ class ServerRepository */ public function unsuspend($id) { - // @TODO: Implement logic; not doing it now since that is outside of the - // scope of this API brance. - return true; + $server = Models\Server::findOrFail($id); + $node = Models\Node::findOrFail($server->node); + + DB::beginTransaction(); + + try { + $server->suspended = 0; + $server->save(); + + $client = Models\Node::guzzleRequest($server->node); + $client->request('POST', '/server/unsuspend', [ + 'headers' => [ + 'X-Access-Token' => $node->daemonSecret, + 'X-Access-Server' => $server->uuid + ] + ]); + + return DB::commit(); + } catch (\GuzzleHttp\Exception\TransferException $ex) { + DB::rollBack(); + throw new DisplayException('An error occured while attempting to un-suspend this server.', $ex); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } } public function updateSFTPPassword($id, $password) diff --git a/database/migrations/2016_09_01_193520_add_suspension_for_servers.php b/database/migrations/2016_09_01_193520_add_suspension_for_servers.php new file mode 100644 index 00000000..39717253 --- /dev/null +++ b/database/migrations/2016_09_01_193520_add_suspension_for_servers.php @@ -0,0 +1,31 @@ +tinyInteger('suspended')->unsigned()->default(0)->after('active'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('suspended'); + }); + } +} diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index 23dd5dcb..54eda116 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -42,8 +42,8 @@
@foreach ($servers as $server) -{{ $server->ip_alias }}:{{ $server->port }}
@if($server->ip !== $server->ip_alias)alias@endifThis will suspend the server, stop any running processes, and immediately block the user from being able to access their files or otherwise manage the server through the panel or API.
+This will unsuspend the server and restore normal user access.
+This server has been suspended and cannot be accessed.
+Take me back or go home.
+