From 95ec595606da09214e3875620b20bac09ad0f4d1 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Thu, 20 Oct 2022 21:38:17 -0400 Subject: [PATCH] Remove daemon transfer repo --- .../Servers/ServerTransferController.php | 37 ++++++++++++++++--- app/Repositories/Wings/DaemonRepository.php | 3 ++ .../Wings/DaemonTransferRepository.php | 33 ----------------- 3 files changed, 35 insertions(+), 38 deletions(-) delete mode 100644 app/Repositories/Wings/DaemonTransferRepository.php diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php b/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php index 6f49d66e0..501515be5 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php @@ -3,18 +3,21 @@ namespace Pterodactyl\Http\Controllers\Api\Remote\Servers; use Carbon\CarbonImmutable; +use GuzzleHttp\Exception\GuzzleException; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Lcobucci\JWT\Token\Plain; use Pterodactyl\Models\Allocation; use Illuminate\Support\Facades\Log; +use Pterodactyl\Models\Server; use Pterodactyl\Models\ServerTransfer; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Http\Controllers\Controller; +use Pterodactyl\Repositories\Wings\DaemonRepository; use Pterodactyl\Services\Nodes\NodeJWTService; use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository; -use Pterodactyl\Repositories\Wings\DaemonTransferRepository; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; class ServerTransferController extends Controller @@ -26,7 +29,7 @@ class ServerTransferController extends Controller private ConnectionInterface $connection, private ServerRepository $repository, private DaemonServerRepository $daemonServerRepository, - private DaemonTransferRepository $daemonTransferRepository, + private DaemonRepository $daemonRepository, private NodeJWTService $jwtService ) { } @@ -62,15 +65,39 @@ class ServerTransferController extends Controller // On the daemon transfer repository, make sure to set the node after the server // because setServer() tells the repository to use the server's node and not the one // we want to specify. - $this->daemonTransferRepository + $repository = $this->daemonRepository ->setServer($server) - ->setNode($server->transfer->newNode) - ->notify($server, $token); + ->setNode($server->transfer->newNode); + + $this->notify($repository, $server, $token); }); return new JsonResponse([], Response::HTTP_NO_CONTENT); } + + /** + * @throws DaemonConnectionException + */ + private function notify(DaemonRepository $repository, Server $server, Plain $token): void + { + try { + $repository->getHttpClient()->post('/api/transfer', [ + 'json' => [ + 'server_id' => $server->uuid, + 'url' => $server->node->getConnectionAddress() . "/api/servers/$server->uuid/archive", + 'token' => 'Bearer ' . $token->toString(), + 'server' => [ + 'uuid' => $server->uuid, + 'start_on_completion' => false, + ], + ], + ]); + } catch (GuzzleException $exception) { + throw new DaemonConnectionException($exception); + } + } + /** * The daemon notifies us about a transfer failure. * diff --git a/app/Repositories/Wings/DaemonRepository.php b/app/Repositories/Wings/DaemonRepository.php index 512626d73..18f70f146 100644 --- a/app/Repositories/Wings/DaemonRepository.php +++ b/app/Repositories/Wings/DaemonRepository.php @@ -3,6 +3,9 @@ namespace Pterodactyl\Repositories\Wings; use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; +use Lcobucci\JWT\Token\Plain; +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Models\Node; use Webmozart\Assert\Assert; use Pterodactyl\Models\Server; diff --git a/app/Repositories/Wings/DaemonTransferRepository.php b/app/Repositories/Wings/DaemonTransferRepository.php deleted file mode 100644 index 3939a47cd..000000000 --- a/app/Repositories/Wings/DaemonTransferRepository.php +++ /dev/null @@ -1,33 +0,0 @@ -getHttpClient()->post('/api/transfer', [ - 'json' => [ - 'server_id' => $server->uuid, - 'url' => $server->node->getConnectionAddress() . sprintf('/api/servers/%s/archive', $server->uuid), - 'token' => 'Bearer ' . $token->toString(), - 'server' => [ - 'uuid' => $server->uuid, - 'start_on_completion' => false, - ], - ], - ]); - } catch (GuzzleException $exception) { - throw new DaemonConnectionException($exception); - } - } -}