|
5 | 5 | use Cache; |
6 | 6 | use Illuminate\Http\Request; |
7 | 7 | use Pterodactyl\Models\Node; |
| 8 | +use Illuminate\Http\Response; |
8 | 9 | use Pterodactyl\Models\Server; |
| 10 | +use Illuminate\Http\JsonResponse; |
9 | 11 | use Pterodactyl\Http\Controllers\Controller; |
| 12 | +use Pterodactyl\Repositories\Eloquent\ServerRepository; |
10 | 13 | use Pterodactyl\Events\Server\Installed as ServerInstalled; |
11 | 14 | use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; |
| 15 | +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; |
12 | 16 |
|
13 | 17 | class ActionController extends Controller |
14 | 18 | { |
15 | 19 | /** |
16 | 20 | * @var \Illuminate\Contracts\Events\Dispatcher |
17 | 21 | */ |
18 | 22 | private $eventDispatcher; |
| 23 | + /** |
| 24 | + * @var \Pterodactyl\Repositories\Eloquent\ServerRepository |
| 25 | + */ |
| 26 | + private $repository; |
19 | 27 |
|
20 | 28 | /** |
21 | 29 | * ActionController constructor. |
22 | 30 | * |
23 | | - * @param \Illuminate\Contracts\Events\Dispatcher $eventDispatcher |
| 31 | + * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository |
| 32 | + * @param \Illuminate\Contracts\Events\Dispatcher $eventDispatcher |
24 | 33 | */ |
25 | | - public function __construct(EventDispatcher $eventDispatcher) |
| 34 | + public function __construct(ServerRepository $repository, EventDispatcher $eventDispatcher) |
26 | 35 | { |
27 | 36 | $this->eventDispatcher = $eventDispatcher; |
| 37 | + $this->repository = $repository; |
28 | 38 | } |
29 | 39 |
|
30 | 40 | /** |
31 | 41 | * Handles install toggle request from daemon. |
32 | 42 | * |
33 | 43 | * @param \Illuminate\Http\Request $request |
34 | 44 | * @return \Illuminate\Http\JsonResponse |
| 45 | + * |
| 46 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 47 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
35 | 48 | */ |
36 | | - public function markInstall(Request $request) |
| 49 | + public function markInstall(Request $request): JsonResponse |
37 | 50 | { |
38 | | - $server = Server::where('uuid', $request->input('server'))->with('node')->first(); |
39 | | - if (! $server) { |
40 | | - return response()->json([ |
| 51 | + try { |
| 52 | + /** @var \Pterodactyl\Models\Server $server */ |
| 53 | + $server = $this->repository->findFirstWhere([ |
| 54 | + 'uuid' => $request->input('server'), |
| 55 | + ]); |
| 56 | + } catch (RecordNotFoundException $exception) { |
| 57 | + return JsonResponse::create([ |
41 | 58 | 'error' => 'No server by that ID was found on the system.', |
42 | | - ], 422); |
| 59 | + ], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 60 | + } |
| 61 | + |
| 62 | + if (! $server->relationLoaded('node')) { |
| 63 | + $server->load('node'); |
43 | 64 | } |
44 | 65 |
|
45 | 66 | $hmac = $request->input('signed'); |
46 | 67 | $status = $request->input('installed'); |
47 | 68 |
|
48 | | - if (! hash_equals(base64_decode($hmac), hash_hmac('sha256', $server->uuid, $server->node->daemonSecret, true))) { |
49 | | - return response()->json([ |
| 69 | + if (! hash_equals(base64_decode($hmac), hash_hmac('sha256', $server->uuid, $server->getRelation('node')->daemonSecret, true))) { |
| 70 | + return JsonResponse::create([ |
50 | 71 | 'error' => 'Signed HMAC was invalid.', |
51 | | - ], 403); |
| 72 | + ], Response::HTTP_FORBIDDEN); |
52 | 73 | } |
53 | 74 |
|
54 | | - $server->installed = ($status === 'installed') ? 1 : 2; |
55 | | - $server->save(); |
| 75 | + $this->repository->update($server->id, [ |
| 76 | + 'installed' => ($status === 'installed') ? 1 : 2, |
| 77 | + ], true, true); |
56 | 78 |
|
57 | 79 | // Only fire event if server installed successfully. |
58 | | - if ($server->installed === 1) { |
| 80 | + if ($status === 'installed') { |
59 | 81 | $this->eventDispatcher->dispatch(new ServerInstalled($server)); |
60 | 82 | } |
61 | 83 |
|
62 | | - return response()->json([]); |
| 84 | + // Don't use a 204 here, the daemon is hard-checking for a 200 code. |
| 85 | + return JsonResponse::create([]); |
63 | 86 | } |
64 | 87 |
|
65 | 88 | /** |
|
0 commit comments