|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Services\Allocations; |
| 4 | + |
| 5 | +use Pterodactyl\Models\Server; |
| 6 | +use Pterodactyl\Models\Allocation; |
| 7 | +use GuzzleHttp\Exception\RequestException; |
| 8 | +use Illuminate\Database\ConnectionInterface; |
| 9 | +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
| 10 | +use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; |
| 11 | +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; |
| 12 | +use Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException; |
| 13 | +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonRepositoryInterface; |
| 14 | + |
| 15 | +class SetDefaultAllocationService |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var \Illuminate\Database\ConnectionInterface |
| 19 | + */ |
| 20 | + private $connection; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface |
| 24 | + */ |
| 25 | + private $daemonRepository; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface |
| 29 | + */ |
| 30 | + private $repository; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface |
| 34 | + */ |
| 35 | + private $serverRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * SetDefaultAllocationService constructor. |
| 39 | + * |
| 40 | + * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository |
| 41 | + * @param \Illuminate\Database\ConnectionInterface $connection |
| 42 | + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonRepository |
| 43 | + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + AllocationRepositoryInterface $repository, |
| 47 | + ConnectionInterface $connection, |
| 48 | + DaemonRepositoryInterface $daemonRepository, |
| 49 | + ServerRepositoryInterface $serverRepository |
| 50 | + ) { |
| 51 | + $this->connection = $connection; |
| 52 | + $this->daemonRepository = $daemonRepository; |
| 53 | + $this->repository = $repository; |
| 54 | + $this->serverRepository = $serverRepository; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Update the default allocation for a server only if that allocation is currently |
| 59 | + * assigned to the specified server. |
| 60 | + * |
| 61 | + * @param int|\Pterodactyl\Models\Server $server |
| 62 | + * @param int $allocation |
| 63 | + * @return \Pterodactyl\Models\Allocation |
| 64 | + * |
| 65 | + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException |
| 66 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 67 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 68 | + * @throws \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException |
| 69 | + */ |
| 70 | + public function handle($server, int $allocation): Allocation |
| 71 | + { |
| 72 | + if (! $server instanceof Server) { |
| 73 | + $server = $this->serverRepository->find($server); |
| 74 | + } |
| 75 | + |
| 76 | + $allocations = $this->repository->findWhere([['server_id', '=', $server->id]]); |
| 77 | + $model = $allocations->filter(function ($model) use ($allocation) { |
| 78 | + return $model->id === $allocation; |
| 79 | + })->first(); |
| 80 | + |
| 81 | + if (! $model instanceof Allocation) { |
| 82 | + throw new AllocationDoesNotBelongToServerException; |
| 83 | + } |
| 84 | + |
| 85 | + $this->connection->beginTransaction(); |
| 86 | + $this->serverRepository->withoutFresh()->update($server->id, ['allocation_id' => $model->id]); |
| 87 | + |
| 88 | + // Update on the daemon. |
| 89 | + try { |
| 90 | + $this->daemonRepository->setAccessServer($server->uuid)->setNode($server->node_id)->update([ |
| 91 | + 'build' => [ |
| 92 | + 'default' => [ |
| 93 | + 'ip' => $model->ip, |
| 94 | + 'port' => $model->port, |
| 95 | + ], |
| 96 | + 'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) { |
| 97 | + return $item->pluck('port'); |
| 98 | + })->toArray(), |
| 99 | + ], |
| 100 | + ]); |
| 101 | + |
| 102 | + $this->connection->commit(); |
| 103 | + } catch (RequestException $exception) { |
| 104 | + $this->connection->rollBack(); |
| 105 | + throw new DaemonConnectionException($exception); |
| 106 | + } |
| 107 | + |
| 108 | + return $model; |
| 109 | + } |
| 110 | +} |
0 commit comments