misc_pterodactyl-panel/app/Http/Controllers/Admin/ServersController.php

478 lines
17 KiB
PHP
Raw Normal View History

<?php
2016-01-20 00:10:39 +00:00
/**
2016-01-20 21:05:16 +00:00
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
2016-01-20 00:10:39 +00:00
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2016-01-20 00:10:39 +00:00
*/
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Admin;
use Illuminate\Support\Arr;
2020-05-21 21:16:16 +00:00
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
2020-10-03 19:42:27 +00:00
use Pterodactyl\Models\Mount;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Models\Server;
use Pterodactyl\Models\MountServer;
2017-08-05 22:26:30 +00:00
use Prologue\Alerts\AlertsMessageBag;
2020-05-21 21:16:16 +00:00
use GuzzleHttp\Exception\RequestException;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Validation\ValidationException;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Services\Servers\SuspensionService;
2020-05-21 19:19:59 +00:00
use Pterodactyl\Repositories\Eloquent\MountRepository;
use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Exceptions\Model\DataValidationException;
2020-05-21 21:16:16 +00:00
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Services\Servers\BuildModificationService;
use Pterodactyl\Services\Databases\DatabasePasswordService;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Services\Servers\StartupModificationService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Services\Databases\DatabaseManagementService;
2017-08-05 22:26:30 +00:00
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
2020-05-21 21:16:16 +00:00
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
use Pterodactyl\Http\Requests\Admin\Servers\Databases\StoreServerDatabaseRequest;
class ServersController extends Controller
{
/**
* @var \Prologue\Alerts\AlertsMessageBag
*/
protected $alert;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
*/
protected $allocationRepository;
/**
* @var \Pterodactyl\Services\Servers\BuildModificationService
*/
protected $buildModificationService;
2017-07-15 16:52:34 +00:00
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
2017-07-15 16:52:34 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
protected $databaseRepository;
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
protected $databaseManagementService;
/**
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
*/
protected $databasePasswordService;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
protected $databaseHostRepository;
/**
* @var \Pterodactyl\Services\Servers\ServerDeletionService
*/
protected $deletionService;
/**
* @var \Pterodactyl\Services\Servers\DetailsModificationService
*/
protected $detailsModificationService;
2020-05-21 19:19:59 +00:00
/**
* @var \Pterodactyl\Repositories\Eloquent\MountRepository
*/
protected $mountRepository;
2017-10-07 22:21:41 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
protected $nestRepository;
/**
* @var \Pterodactyl\Services\Servers\ReinstallServerService
*/
protected $reinstallService;
2017-07-15 16:52:34 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
private $serverConfigurationStructureService;
/**
* @var \Pterodactyl\Services\Servers\StartupModificationService
*/
private $startupModificationService;
/**
* @var \Pterodactyl\Services\Servers\SuspensionService
*/
protected $suspensionService;
/**
* ServersController constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Servers\BuildModificationService $buildModificationService
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
* @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository
* @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService
* @param \Pterodactyl\Services\Servers\DetailsModificationService $detailsModificationService
* @param \Pterodactyl\Services\Servers\ReinstallServerService $reinstallService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
2020-05-21 19:19:59 +00:00
* @param \Pterodactyl\Repositories\Eloquent\MountRepository $mountRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $serverConfigurationStructureService
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Servers\StartupModificationService $startupModificationService
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
*/
2017-07-15 16:52:34 +00:00
public function __construct(
AlertsMessageBag $alert,
AllocationRepositoryInterface $allocationRepository,
BuildModificationService $buildModificationService,
2017-07-15 16:52:34 +00:00
ConfigRepository $config,
DaemonServerRepository $daemonServerRepository,
DatabaseManagementService $databaseManagementService,
DatabasePasswordService $databasePasswordService,
2017-07-15 16:52:34 +00:00
DatabaseRepositoryInterface $databaseRepository,
DatabaseHostRepository $databaseHostRepository,
ServerDeletionService $deletionService,
DetailsModificationService $detailsModificationService,
ReinstallServerService $reinstallService,
2017-07-15 16:52:34 +00:00
ServerRepositoryInterface $repository,
2020-05-21 19:19:59 +00:00
MountRepository $mountRepository,
2017-10-07 22:21:41 +00:00
NestRepositoryInterface $nestRepository,
ServerConfigurationStructureService $serverConfigurationStructureService,
StartupModificationService $startupModificationService,
SuspensionService $suspensionService
2017-07-15 16:52:34 +00:00
) {
$this->alert = $alert;
$this->allocationRepository = $allocationRepository;
$this->buildModificationService = $buildModificationService;
2017-07-15 16:52:34 +00:00
$this->config = $config;
$this->daemonServerRepository = $daemonServerRepository;
$this->databaseHostRepository = $databaseHostRepository;
$this->databaseManagementService = $databaseManagementService;
$this->databasePasswordService = $databasePasswordService;
2017-07-15 16:52:34 +00:00
$this->databaseRepository = $databaseRepository;
$this->detailsModificationService = $detailsModificationService;
$this->deletionService = $deletionService;
2017-10-07 22:21:41 +00:00
$this->nestRepository = $nestRepository;
$this->reinstallService = $reinstallService;
2017-07-15 16:52:34 +00:00
$this->repository = $repository;
2020-05-21 19:19:59 +00:00
$this->mountRepository = $mountRepository;
$this->serverConfigurationStructureService = $serverConfigurationStructureService;
$this->startupModificationService = $startupModificationService;
$this->suspensionService = $suspensionService;
2017-07-15 16:52:34 +00:00
}
2017-03-05 00:03:49 +00:00
/**
* Update the details for a server.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Http\Request $request
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-05 00:03:49 +00:00
*/
public function setDetails(Request $request, Server $server)
2017-03-05 00:03:49 +00:00
{
$this->detailsModificationService->handle($server, $request->only([
'owner_id', 'external_id', 'name', 'description',
]));
$this->alert->success(trans('admin/server.alerts.details_updated'))->flash();
return redirect()->route('admin.servers.view.details', $server->id);
}
2017-03-05 00:03:49 +00:00
/**
* Toggles the install status for a server.
*
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-08-27 00:58:24 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-05 00:03:49 +00:00
*/
public function toggleInstall(Server $server)
2017-03-05 00:03:49 +00:00
{
if ($server->installed > 1) {
throw new DisplayException(trans('admin/server.exceptions.marked_as_failed'));
2017-03-05 00:03:49 +00:00
}
$this->repository->update($server->id, [
'installed' => ! $server->installed,
], true, true);
$this->alert->success(trans('admin/server.alerts.install_toggled'))->flash();
return redirect()->route('admin.servers.view.manage', $server->id);
}
/**
* Reinstalls the server with the currently assigned service.
*
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
2017-07-23 20:09:25 +00:00
public function reinstallServer(Server $server)
{
2020-10-06 04:54:29 +00:00
$this->reinstallService->handle($server);
$this->alert->success(trans('admin/server.alerts.server_reinstalled'))->flash();
2017-07-23 20:09:25 +00:00
return redirect()->route('admin.servers.view.manage', $server->id);
}
2017-03-05 00:03:49 +00:00
/**
* Manage the suspension status for a server.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Http\Request $request
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-08-27 00:58:24 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-03-05 00:03:49 +00:00
*/
public function manageSuspension(Request $request, Server $server)
{
$this->suspensionService->toggle($server, $request->input('action'));
$this->alert->success(trans('admin/server.alerts.suspension_toggled', [
'status' => $request->input('action') . 'ed',
]))->flash();
2017-03-05 00:03:49 +00:00
return redirect()->route('admin.servers.view.manage', $server->id);
}
2017-03-05 00:03:49 +00:00
/**
* Update the build configuration for a server.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2018-03-03 01:49:09 +00:00
*
* @throws \Pterodactyl\Exceptions\DisplayException
2018-03-03 01:49:09 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Illuminate\Validation\ValidationException
2017-03-05 00:03:49 +00:00
*/
public function updateBuild(Request $request, Server $server)
{
try {
$this->buildModificationService->handle($server, $request->only([
'allocation_id', 'add_allocations', 'remove_allocations',
'memory', 'swap', 'io', 'cpu', 'threads', 'disk',
'database_limit', 'allocation_limit', 'backup_limit', 'oom_disabled',
]));
} catch (DataValidationException $exception) {
throw new ValidationException($exception->validator);
}
$this->alert->success(trans('admin/server.alerts.build_updated'))->flash();
2016-12-07 22:46:38 +00:00
return redirect()->route('admin.servers.view.build', $server->id);
}
2017-03-05 00:03:49 +00:00
/**
* Start the server deletion process.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Http\Request $request
2017-08-22 03:10:48 +00:00
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
2019-12-22 21:45:40 +00:00
* @throws \Throwable
2017-03-05 00:03:49 +00:00
*/
public function delete(Request $request, Server $server)
2016-01-04 21:09:22 +00:00
{
2017-12-17 19:07:38 +00:00
$this->deletionService->withForce($request->filled('force_delete'))->handle($server);
$this->alert->success(trans('admin/server.alerts.server_deleted'))->flash();
2017-03-05 00:03:49 +00:00
return redirect()->route('admin.servers');
}
2017-03-05 00:03:49 +00:00
/**
* Update the startup command as well as variables.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*
2018-05-13 16:19:35 +00:00
* @throws \Illuminate\Validation\ValidationException
2017-08-27 00:58:24 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function saveStartup(Request $request, Server $server)
{
try {
$this->startupModificationService
->setUserLevel(User::USER_LEVEL_ADMIN)
->handle($server, $request->except('_token'));
} catch (DataValidationException $exception) {
throw new ValidationException($exception->validator);
}
$this->alert->success(trans('admin/server.alerts.startup_changed'))->flash();
return redirect()->route('admin.servers.view.startup', $server->id);
}
/**
* Creates a new database assigned to a specific server.
2017-03-19 23:36:50 +00:00
*
* @param \Pterodactyl\Http\Requests\Admin\Servers\Databases\StoreServerDatabaseRequest $request
* @param \Pterodactyl\Models\Server $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2017-07-22 19:07:51 +00:00
*
* @throws \Throwable
*/
public function newDatabase(StoreServerDatabaseRequest $request, Server $server)
{
$this->databaseManagementService->create($server, [
'database' => DatabaseManagementService::generateUniqueDatabaseName($request->input('database'), $server->id),
'remote' => $request->input('remote'),
'database_host_id' => $request->input('database_host_id'),
2020-05-19 08:14:36 +00:00
'max_connections' => $request->input('max_connections'),
]);
return redirect()->route('admin.servers.view.database', $server->id)->withInput();
}
/**
* Resets the database password for a specific database on this server.
2017-03-19 23:36:50 +00:00
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
2019-09-06 04:32:57 +00:00
* @param int $server
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2017-07-22 19:07:51 +00:00
*
* @throws \Throwable
*/
2017-07-23 20:09:25 +00:00
public function resetDatabasePassword(Request $request, $server)
{
2017-07-22 19:07:51 +00:00
$database = $this->databaseRepository->findFirstWhere([
2017-07-23 20:09:25 +00:00
['server_id', '=', $server],
2017-07-22 19:07:51 +00:00
['id', '=', $request->input('database')],
]);
$this->databasePasswordService->handle($database);
2017-07-22 19:07:51 +00:00
return response('', 204);
}
/**
* Deletes a database from a server.
2017-03-19 23:36:50 +00:00
*
2017-08-22 03:10:48 +00:00
* @param int $server
* @param int $database
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2017-07-22 19:07:51 +00:00
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
2017-07-23 20:09:25 +00:00
public function deleteDatabase($server, $database)
{
2017-07-22 19:07:51 +00:00
$database = $this->databaseRepository->findFirstWhere([
2017-07-23 20:09:25 +00:00
['server_id', '=', $server],
2017-07-22 19:07:51 +00:00
['id', '=', $database],
]);
$this->databaseManagementService->delete($database);
2017-07-22 19:07:51 +00:00
return response('', 204);
}
/**
* Add a mount to a server.
*
* @param Server $server
2020-10-03 19:42:27 +00:00
* @param \Pterodactyl\Models\Mount $mount
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException|\Throwable
*/
2020-10-03 19:42:27 +00:00
public function addMount(Server $server, Mount $mount)
{
$mountServer = new MountServer;
$mountServer->mount_id = $mount->id;
$mountServer->server_id = $server->id;
$mountServer->saveOrFail();
$data = $this->serverConfigurationStructureService->handle($server);
try {
$this->daemonServerRepository
->setServer($server)
->update(Arr::only($data, ['mounts']));
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);
}
$this->alert->success('Mount was added successfully.')->flash();
2020-05-21 20:27:23 +00:00
return redirect()->route('admin.servers.view.mounts', $server->id);
}
/**
* Remove a mount from a server.
*
* @param Server $server
2020-10-03 19:42:27 +00:00
* @param \Pterodactyl\Models\Mount $mount
* @return \Illuminate\Http\RedirectResponse
*
2020-10-03 19:42:27 +00:00
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
2020-10-03 19:42:27 +00:00
public function deleteMount(Server $server, Mount $mount)
{
MountServer::where('mount_id', $mount->id)->where('server_id', $server->id)->delete();
$data = $this->serverConfigurationStructureService->handle($server);
try {
$this->daemonServerRepository
->setServer($server)
->update(Arr::only($data, ['mounts']));
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);
}
$this->alert->success('Mount was removed successfully.')->flash();
2020-05-21 20:27:23 +00:00
return redirect()->route('admin.servers.view.mounts', $server->id);
}
}