Skip to content

Commit e6c4a68

Browse files
committed
Update logic for tracking a server's transfer state
1 parent 5d03c0d commit e6c4a68

File tree

20 files changed

+204
-72
lines changed

20 files changed

+204
-72
lines changed

app/Http/Controllers/Admin/Servers/ServerTransferController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function transfer(Request $request, Server $server)
117117
$this->daemonConfigurationRepository->setNode($node)->getSystemInformation();
118118

119119
// Suspend the server and request an archive to be created.
120-
$this->suspensionService->toggle($server, 'suspend');
120+
//$this->suspensionService->toggle($server, 'suspend');
121121

122122
// Create a new ServerTransfer entry.
123123
$transfer = new ServerTransfer;

app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,12 @@ public function archive(Request $request, string $uuid)
120120

121121
// Unsuspend the server and don't continue the transfer.
122122
if (! $request->input('successful')) {
123-
$this->suspensionService->toggle($server, 'unsuspend');
123+
//$this->suspensionService->toggle($server, 'unsuspend');
124+
$server->transfer->forceFill([
125+
'successful' => false,
126+
])->saveOrFail();
124127

125-
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
128+
return new JsonResponse([], Response::HTTP_NO_CONTENT);
126129
}
127130

128131
$server->node_id = $server->transfer->new_node;
@@ -151,21 +154,23 @@ public function archive(Request $request, string $uuid)
151154
// because setServer() tells the repository to use the server's node and not the one
152155
// we want to specify.
153156
try {
157+
/** @var \Pterodactyl\Models\Node $newNode */
158+
$newNode = $this->nodeRepository->find($server->transfer->new_node);
159+
154160
$this->daemonTransferRepository
155161
->setServer($server)
156-
->setNode($this->nodeRepository->find($server->transfer->new_node))
162+
->setNode($newNode)
157163
->notify($server, $data, $server->node, $token->__toString());
158164
} catch (DaemonConnectionException $exception) {
159165
throw $exception;
160166
}
161167

162-
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
168+
return new JsonResponse([], Response::HTTP_NO_CONTENT);
163169
}
164170

165171
/**
166172
* The daemon notifies us about a transfer failure.
167173
*
168-
* @param \Illuminate\Http\Request $request
169174
* @param string $uuid
170175
* @return \Illuminate\Http\JsonResponse
171176
*
@@ -183,9 +188,9 @@ public function failure(string $uuid)
183188
$this->allocationRepository->updateWhereIn('id', $allocationIds, ['server_id' => null]);
184189

185190
// Unsuspend the server.
186-
$this->suspensionService->toggle($server, 'unsuspend');
191+
//$this->suspensionService->toggle($server, 'unsuspend');
187192

188-
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
193+
return new JsonResponse([], Response::HTTP_NO_CONTENT);
189194
}
190195

191196
/**
@@ -213,11 +218,11 @@ public function success(string $uuid)
213218
// Update the server's allocation_id and node_id.
214219
$server->allocation_id = $transfer->new_allocation;
215220
$server->node_id = $transfer->new_node;
216-
$server->save();
221+
$server->saveOrFail();
217222

218223
// Mark the transfer as successful.
219224
$transfer->successful = true;
220-
$transfer->save();
225+
$transfer->saveOrFail();
221226

222227
// Commit the transaction.
223228
$this->connection->commit();
@@ -231,8 +236,8 @@ public function success(string $uuid)
231236

232237
// Unsuspend the server
233238
$server->load('node');
234-
$this->suspensionService->toggle($server, $this->suspensionService::ACTION_UNSUSPEND);
239+
//$this->suspensionService->toggle($server, $this->suspensionService::ACTION_UNSUSPEND);
235240

236-
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
241+
return new JsonResponse([], Response::HTTP_NO_CONTENT);
237242
}
238243
}

app/Http/Controllers/Api/Remote/SftpAuthenticationController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ public function __invoke(SftpAuthenticationFormRequest $request): JsonResponse
112112

113113
// Remeber, for security purposes, only reveal the existence of the server to people that
114114
// have provided valid credentials, and have permissions to know about it.
115-
if ($server->installed !== 1 || $server->suspended) {
115+
if ($server->installed !== 1 || $server->suspended || $server->transfer !== null) {
116116
throw new BadRequestHttpException(
117117
'Server is not installed or is currently suspended.'
118118
);
119119
}
120120

121-
return JsonResponse::create([
121+
return new JsonResponse([
122122
'server' => $server->uuid,
123123
// Deprecated, but still needed at the moment for Wings.
124124
'token' => '',

app/Http/Middleware/Api/Client/Server/AuthenticateServerAccess.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
1010
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1111
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12-
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1312

1413
class AuthenticateServerAccess
1514
{
@@ -24,7 +23,6 @@ class AuthenticateServerAccess
2423
* @var string[]
2524
*/
2625
protected $except = [
27-
'api:client:server.view',
2826
'api:client:server.ws',
2927
];
3028

@@ -65,17 +63,26 @@ public function handle(Request $request, Closure $next)
6563
}
6664
}
6765

68-
if ($server->suspended && !$request->routeIs('api:client:server.resources')) {
66+
if ($server->suspended && ! $request->routeIs('api:client:server.resources')) {
6967
throw new BadRequestHttpException(
7068
'This server is currently suspended and the functionality requested is unavailable.'
7169
);
7270
}
7371

74-
if (! $server->isInstalled()) {
75-
// Throw an exception for all server routes; however if the user is an admin and requesting the
76-
// server details, don't throw the exception for them.
77-
if (! $user->root_admin || ($user->root_admin && ! $request->routeIs($this->except))) {
78-
throw new ConflictHttpException('Server has not completed the installation process.');
72+
// Still allow users to get information about there server if it is installing or being transferred.
73+
if (! $request->routeIs('api:client:server.view')) {
74+
if (! $server->isInstalled()) {
75+
// Throw an exception for all server routes; however if the user is an admin and requesting the
76+
// server details, don't throw the exception for them.
77+
if (! $user->root_admin || ($user->root_admin && ! $request->routeIs($this->except))) {
78+
throw new ConflictHttpException('Server has not completed the installation process.');
79+
}
80+
}
81+
82+
if ($server->transfer !== null) {
83+
if (! $user->root_admin || ($user->root_admin && ! $request->routeIs($this->except))) {
84+
throw new ConflictHttpException('Server is currently being transferred.');
85+
}
7986
}
8087
}
8188

app/Http/Middleware/Server/AccessingValidServer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public function handle(Request $request, Closure $next)
8080
return $this->response->view('errors.installing', [], 409);
8181
}
8282

83+
if ($server->transfer !== null) {
84+
if ($isApiRequest) {
85+
throw new ConflictHttpException('Server is currently being transferred.');
86+
}
87+
88+
return $this->response->view('errors.transferring', [], 409);
89+
}
90+
8391
// Add server to the request attributes. This will replace sessions
8492
// as files are updated.
8593
$request->attributes->set('server', $server);

app/Models/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public function location()
306306
*/
307307
public function transfer()
308308
{
309-
return $this->hasOne(ServerTransfer::class)->orderByDesc('id');
309+
return $this->hasOne(ServerTransfer::class)->whereNull('successful')->orderByDesc('id');
310310
}
311311

312312
/**

app/Models/ServerTransfer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @property int $new_allocation
1212
* @property string $old_additional_allocations
1313
* @property string $new_additional_allocations
14-
* @property bool $successful
14+
* @property bool|null $successful
1515
* @property \Carbon\Carbon $created_at
1616
* @property \Carbon\Carbon $updated_at
1717
*
@@ -66,7 +66,7 @@ class ServerTransfer extends Model
6666
'new_allocation' => 'required|numeric',
6767
'old_additional_allocations' => 'nullable',
6868
'new_additional_allocations' => 'nullable',
69-
'successful' => 'sometimes|boolean',
69+
'successful' => 'sometimes|nullable|boolean',
7070
];
7171

7272
/**

app/Services/Servers/GetUserPermissionsService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function handle(Server $server, User $user)
2424
if ($user->root_admin) {
2525
$permissions[] = 'admin.websocket.errors';
2626
$permissions[] = 'admin.websocket.install';
27+
$permissions[] = 'admin.websocket.transfer';
2728
}
2829

2930
return $permissions;

app/Services/Servers/SuspensionService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function toggle(Server $server, $action = self::ACTION_SUSPEND)
6161
'suspended' => $action === self::ACTION_SUSPEND,
6262
]);
6363

64-
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
64+
// Only send the suspension request to wings if the server is not currently being transferred.
65+
if ($server->transfer === null) {
66+
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
67+
}
6568
});
6669
}
6770
}

app/Transformers/Api/Application/ServerTransformer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ServerTransformer extends BaseTransformer
2828
'location',
2929
'node',
3030
'databases',
31+
'transfer',
3132
];
3233

3334
/**
@@ -55,8 +56,6 @@ public function getResourceName(): string
5556
*
5657
* @param \Pterodactyl\Models\Server $server
5758
* @return array
58-
*
59-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6059
*/
6160
public function transform(Server $server): array
6261
{

0 commit comments

Comments
 (0)