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

272 lines
10 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Admin;
2020-05-21 21:16:16 +00:00
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Illuminate\Http\Response;
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\Database;
use Pterodactyl\Models\MountServer;
use Illuminate\Http\RedirectResponse;
2017-08-05 22:26:30 +00:00
use Prologue\Alerts\AlertsMessageBag;
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\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\Services\Servers\ServerConfigurationStructureService;
use Pterodactyl\Http\Requests\Admin\Servers\Databases\StoreServerDatabaseRequest;
class ServersController extends Controller
{
/**
* ServersController constructor.
*/
2017-07-15 16:52:34 +00:00
public function __construct(
protected AlertsMessageBag $alert,
protected AllocationRepositoryInterface $allocationRepository,
protected BuildModificationService $buildModificationService,
protected ConfigRepository $config,
protected DaemonServerRepository $daemonServerRepository,
protected DatabaseManagementService $databaseManagementService,
protected DatabasePasswordService $databasePasswordService,
protected DatabaseRepositoryInterface $databaseRepository,
protected DatabaseHostRepository $databaseHostRepository,
protected ServerDeletionService $deletionService,
protected DetailsModificationService $detailsModificationService,
protected ReinstallServerService $reinstallService,
protected ServerRepositoryInterface $repository,
protected MountRepository $mountRepository,
protected ServerConfigurationStructureService $serverConfigurationStructureService,
protected StartupModificationService $startupModificationService,
protected SuspensionService $suspensionService
2017-07-15 16:52:34 +00:00
) {
}
2017-03-05 00:03:49 +00:00
/**
* Update the details for a server.
*
* @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): RedirectResponse
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 installation status for a server.
*
* @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): RedirectResponse
2017-03-05 00:03:49 +00:00
{
if ($server->status === Server::STATUS_INSTALL_FAILED) {
throw new DisplayException(trans('admin/server.exceptions.marked_as_failed'));
2017-03-05 00:03:49 +00:00
}
$this->repository->update($server->id, [
'status' => $server->isInstalled() ? Server::STATUS_INSTALLING : null,
], 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.
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function reinstallServer(Server $server): RedirectResponse
{
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.
*
* @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): RedirectResponse
{
$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.
*
* @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): RedirectResponse
{
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->getValidator());
}
$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.
*
* @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): RedirectResponse
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.
*
2018-05-13 16:19:35 +00:00
* @throws \Illuminate\Validation\ValidationException
*/
public function saveStartup(Request $request, Server $server): RedirectResponse
{
$data = $request->except('_token');
if (!empty($data['custom_docker_image'])) {
$data['docker_image'] = $data['custom_docker_image'];
unset($data['custom_docker_image']);
}
try {
$this->startupModificationService
->setUserLevel(User::USER_LEVEL_ADMIN)
->handle($server, $data);
} catch (DataValidationException $exception) {
throw new ValidationException($exception->getValidator());
}
$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
*
* @throws \Throwable
*/
public function newDatabase(StoreServerDatabaseRequest $request, Server $server): RedirectResponse
{
$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
*
* @throws \Throwable
*/
public function resetDatabasePassword(Request $request, Server $server): Response
{
/** @var \Pterodactyl\Models\Database $database */
$database = $server->databases()->findOrFail($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-07-22 19:07:51 +00:00
* @throws \Exception
*/
public function deleteDatabase(Server $server, Database $database): Response
{
$this->databaseManagementService->delete($database);
2017-07-22 19:07:51 +00:00
return response('', 204);
}
/**
* Add a mount to a server.
*
* @throws \Throwable
*/
public function addMount(Request $request, Server $server): RedirectResponse
{
2021-01-23 20:33:34 +00:00
$mountServer = (new MountServer())->forceFill([
'mount_id' => $request->input('mount_id'),
'server_id' => $server->id,
]);
$mountServer->saveOrFail();
$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.
*/
public function deleteMount(Server $server, Mount $mount): RedirectResponse
{
MountServer::where('mount_id', $mount->id)->where('server_id', $server->id)->delete();
$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);
}
}