Add ServerTransferringException, use is_null

This commit is contained in:
Matthew Penner 2020-12-17 10:34:26 -07:00
parent 5668a780e2
commit fd848985ee
9 changed files with 36 additions and 14 deletions

View file

@ -0,0 +1,14 @@
<?php
namespace Pterodactyl\Exceptions\Http\Server;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
class ServerTransferringException extends HttpException
{
public function __construct()
{
parent::__construct(Response::HTTP_CONFLICT, 'Server is currently being transferred.');
}
}

View file

@ -61,7 +61,7 @@ class WebsocketController extends ClientApiController
$permissions = $this->permissionsService->handle($server, $user); $permissions = $this->permissionsService->handle($server, $user);
$node = null; $node = null;
if ($server->transfer !== null) { if (! is_null($server->transfer)) {
// Check if the user has permissions to receive transfer logs. // Check if the user has permissions to receive transfer logs.
if (! in_array('admin.websocket.transfer', $permissions)) { if (! in_array('admin.websocket.transfer', $permissions)) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'You do not have permission to view transfer logs'); throw new HttpException(Response::HTTP_FORBIDDEN, 'You do not have permission to view transfer logs');

View file

@ -12,6 +12,7 @@ use Pterodactyl\Exceptions\Http\HttpForbiddenException;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\GetUserPermissionsService; use Pterodactyl\Services\Servers\GetUserPermissionsService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Pterodactyl\Exceptions\Http\Server\ServerTransferringException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Pterodactyl\Http\Requests\Api\Remote\SftpAuthenticationFormRequest; use Pterodactyl\Http\Requests\Api\Remote\SftpAuthenticationFormRequest;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
@ -110,9 +111,14 @@ class SftpAuthenticationController extends Controller
} }
} }
// Remeber, for security purposes, only reveal the existence of the server to people that // Prevent SFTP access to servers that are being transferred.
if (! is_null($server->transfer)) {
throw new ServerTransferringException();
}
// Remember, for security purposes, only reveal the existence of the server to people that
// have provided valid credentials, and have permissions to know about it. // have provided valid credentials, and have permissions to know about it.
if ($server->installed !== 1 || $server->suspended || $server->transfer !== null) { if ($server->installed !== 1 || $server->suspended) {
throw new BadRequestHttpException( throw new BadRequestHttpException(
'Server is not installed or is currently suspended.' 'Server is not installed or is currently suspended.'
); );
@ -132,7 +138,7 @@ class SftpAuthenticationController extends Controller
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return string * @return string
*/ */
protected function throttleKey(Request $request) protected function throttleKey(Request $request): string
{ {
$username = explode('.', strrev($request->input('username', ''))); $username = explode('.', strrev($request->input('username', '')));

View file

@ -79,7 +79,7 @@ class AuthenticateServerAccess
} }
} }
if ($server->transfer !== null) { if (! is_null($server->transfer)) {
if (! $user->root_admin || ($user->root_admin && ! $request->routeIs($this->except))) { if (! $user->root_admin || ($user->root_admin && ! $request->routeIs($this->except))) {
throw new ConflictHttpException('Server is currently being transferred.'); throw new ConflictHttpException('Server is currently being transferred.');
} }

View file

@ -9,6 +9,7 @@ use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Pterodactyl\Exceptions\Http\Server\ServerTransferringException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class AccessingValidServer class AccessingValidServer
@ -80,9 +81,9 @@ class AccessingValidServer
return $this->response->view('errors.installing', [], 409); return $this->response->view('errors.installing', [], 409);
} }
if ($server->transfer !== null) { if (! is_null($server->transfer)) {
if ($isApiRequest) { if ($isApiRequest) {
throw new ConflictHttpException('Server is currently being transferred.'); throw new ServerTransferringException();
} }
return $this->response->view('errors.transferring', [], 409); return $this->response->view('errors.transferring', [], 409);

View file

@ -7,6 +7,7 @@ use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Pterodactyl\Exceptions\Http\Server\ServerTransferringException;
class SuspensionService class SuspensionService
{ {
@ -58,8 +59,8 @@ class SuspensionService
} }
// Check if the server is currently being transferred. // Check if the server is currently being transferred.
if ($server->transfer !== null) { if (! is_null($server->transfer)) {
throw new ConflictHttpException('Server is currently being transferred'); throw new ServerTransferringException();
} }
$this->connection->transaction(function () use ($action, $server) { $this->connection->transaction(function () use ($action, $server) {
@ -68,7 +69,7 @@ class SuspensionService
]); ]);
// Only send the suspension request to wings if the server is not currently being transferred. // Only send the suspension request to wings if the server is not currently being transferred.
if ($server->transfer === null) { if (is_null($server->transfer)) {
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND); $this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
} }
}); });

View file

@ -72,7 +72,7 @@ class ServerTransformer extends BaseClientTransformer
], ],
'is_suspended' => $server->suspended, 'is_suspended' => $server->suspended,
'is_installing' => $server->installed !== 1, 'is_installing' => $server->installed !== 1,
'is_transferring' => $server->transfer !== null, 'is_transferring' => ! is_null($server->transfer),
]; ];
} }

View file

@ -66,7 +66,7 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>)
getServer(match.params.id) getServer(match.params.id)
.catch(error => { .catch(error => {
if (error.response?.status === 409) { if (error.response?.status === 409) {
if (error.response.data?.errors[0]?.detail?.includes('transfer')) { if (error.response.data?.errors[0]?.code === 'ServerTransferringException') {
setTransferring(true); setTransferring(true);
} else { } else {
setInstalling(true); setInstalling(true);

View file

@ -72,7 +72,7 @@
<form action="{{ route('admin.servers.view.manage.suspension', $server->id) }}" method="POST"> <form action="{{ route('admin.servers.view.manage.suspension', $server->id) }}" method="POST">
{!! csrf_field() !!} {!! csrf_field() !!}
<input type="hidden" name="action" value="suspend" /> <input type="hidden" name="action" value="suspend" />
<button type="submit" class="btn btn-warning @if($server->transfer !== null) disabled @endif">Suspend Server</button> <button type="submit" class="btn btn-warning @if(! is_null($server->transfer)) disabled @endif">Suspend Server</button>
</form> </form>
</div> </div>
</div> </div>
@ -97,7 +97,7 @@
</div> </div>
@endif @endif
@if($server->transfer === null) @if(is_null($server->transfer))
<div class="col-sm-4"> <div class="col-sm-4">
<div class="box box-success"> <div class="box box-success">
<div class="box-header with-border"> <div class="box-header with-border">