2017-07-23 19:51:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2019-11-30 23:37:13 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-23 19:51:18 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2019-11-25 04:19:31 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2021-01-30 21:28:31 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
2017-07-23 19:51:18 +00:00
|
|
|
|
|
|
|
class SuspensionService
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
public const ACTION_SUSPEND = 'suspend';
|
|
|
|
public const ACTION_UNSUSPEND = 'unsuspend';
|
2018-01-20 19:48:02 +00:00
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2019-11-30 23:37:13 +00:00
|
|
|
private $connection;
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2019-11-25 04:19:31 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
|
|
|
*/
|
|
|
|
private $daemonServerRepository;
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
/**
|
|
|
|
* SuspensionService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2019-11-30 23:37:13 +00:00
|
|
|
ConnectionInterface $connection,
|
2020-10-08 04:56:44 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository
|
2017-07-23 19:51:18 +00:00
|
|
|
) {
|
2019-11-30 23:37:13 +00:00
|
|
|
$this->connection = $connection;
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suspends a server on the system.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param string $action
|
2017-07-23 19:51:18 +00:00
|
|
|
*
|
2019-11-30 23:37:13 +00:00
|
|
|
* @throws \Throwable
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
2019-11-30 23:37:13 +00:00
|
|
|
public function toggle(Server $server, $action = self::ACTION_SUSPEND)
|
2017-07-23 19:51:18 +00:00
|
|
|
{
|
2019-11-30 23:37:13 +00:00
|
|
|
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2020-10-08 04:56:44 +00:00
|
|
|
$isSuspending = $action === self::ACTION_SUSPEND;
|
|
|
|
// Nothing needs to happen if we're suspending the server and it is already
|
|
|
|
// suspended in the database. Additionally, nothing needs to happen if the server
|
|
|
|
// is not suspended and we try to un-suspend the instance.
|
2021-01-17 23:51:56 +00:00
|
|
|
if ($isSuspending === $server->isSuspended()) {
|
2019-11-30 23:37:13 +00:00
|
|
|
return;
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 23:55:44 +00:00
|
|
|
// Check if the server is currently being transferred.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_null($server->transfer)) {
|
2021-01-30 21:28:31 +00:00
|
|
|
throw new ConflictHttpException('Cannot toggle suspension status on a server that is currently being transferred.');
|
2020-12-16 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 23:51:56 +00:00
|
|
|
$this->connection->transaction(function () use ($action, $server, $isSuspending) {
|
2020-10-08 04:56:44 +00:00
|
|
|
$server->update([
|
2021-01-17 23:51:56 +00:00
|
|
|
'status' => $isSuspending ? Server::STATUS_SUSPENDED : null,
|
2019-11-30 23:37:13 +00:00
|
|
|
]);
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
// Only send the suspension request to wings if the server is not currently being transferred.
|
2020-12-17 17:34:26 +00:00
|
|
|
if (is_null($server->transfer)) {
|
2020-12-16 16:34:47 +00:00
|
|
|
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
|
|
|
|
}
|
2019-11-30 23:37:13 +00:00
|
|
|
});
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
}
|