Pterodactyl.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Classes;
  3. use App\Models\Configuration;
  4. use App\Models\Egg;
  5. use App\Models\Nest;
  6. use App\Models\Node;
  7. use App\Models\Server;
  8. use Exception;
  9. use Illuminate\Http\Client\PendingRequest;
  10. use Illuminate\Http\Client\Response;
  11. use Illuminate\Support\Facades\Http;
  12. class Pterodactyl
  13. {
  14. /**
  15. * @description per_page option to pull more than the default 50 from pterodactyl
  16. */
  17. public CONST PER_PAGE = 200;
  18. //TODO: Extend error handling (maybe logger for more errors when debugging)
  19. /**
  20. * @return PendingRequest
  21. */
  22. public static function client()
  23. {
  24. return Http::withHeaders([
  25. 'Authorization' => 'Bearer ' . env('PTERODACTYL_TOKEN', false),
  26. 'Content-type' => 'application/json',
  27. 'Accept' => 'Application/vnd.pterodactyl.v1+json',
  28. ])->baseUrl(env('PTERODACTYL_URL') . '/api');
  29. }
  30. /**
  31. * @return Exception
  32. */
  33. private static function getException(): Exception
  34. {
  35. return new Exception('Request Failed, is pterodactyl set-up correctly?');
  36. }
  37. /**
  38. * @param Nest $nest
  39. * @return mixed
  40. * @throws Exception
  41. */
  42. public static function getEggs(Nest $nest)
  43. {
  44. $response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . self::PER_PAGE);
  45. if ($response->failed()) throw self::getException();
  46. return $response->json()['data'];
  47. }
  48. /**
  49. * @return mixed
  50. * @throws Exception
  51. */
  52. public static function getNodes()
  53. {
  54. $response = self::client()->get('/application/nodes?per_page=' . self::PER_PAGE);
  55. if ($response->failed()) throw self::getException();
  56. return $response->json()['data'];
  57. }
  58. /**
  59. * @return null
  60. * @throws Exception
  61. */
  62. public static function getNests()
  63. {
  64. $response = self::client()->get('/application/nests?per_page=' . self::PER_PAGE);
  65. if ($response->failed()) throw self::getException();
  66. return $response->json()['data'];
  67. }
  68. /**
  69. * @return mixed
  70. * @throws Exception
  71. */
  72. public static function getLocations()
  73. {
  74. $response = self::client()->get('/application/locations?per_page=' . self::PER_PAGE);
  75. if ($response->failed()) throw self::getException();
  76. return $response->json()['data'];
  77. }
  78. /**
  79. * @param Node $node
  80. * @return mixed
  81. * @throws Exception
  82. */
  83. public static function getFreeAllocationId(Node $node)
  84. {
  85. return self::getFreeAllocations($node)[0]['attributes']['id'] ?? null;
  86. }
  87. /**
  88. * @param Node $node
  89. * @return array|mixed|null
  90. * @throws Exception
  91. */
  92. public static function getFreeAllocations(Node $node)
  93. {
  94. $response = self::getAllocations($node);
  95. $freeAllocations = [];
  96. if (isset($response['data'])) {
  97. if (!empty($response['data'])) {
  98. foreach ($response['data'] as $allocation) {
  99. if (!$allocation['attributes']['assigned']) array_push($freeAllocations, $allocation);
  100. }
  101. }
  102. }
  103. return $freeAllocations;
  104. }
  105. /**
  106. * @param Node $node
  107. * @return array|mixed
  108. * @throws Exception
  109. */
  110. public static function getAllocations(Node $node)
  111. {
  112. $per_page = Configuration::getValueByKey('ALLOCATION_LIMIT', 200);
  113. $response = self::client()->get("/application/nodes/{$node->id}/allocations?per_page={$per_page}");
  114. if ($response->failed()) throw self::getException();
  115. return $response->json();
  116. }
  117. /**
  118. * @param String $route
  119. * @return string
  120. */
  121. public static function url(string $route): string
  122. {
  123. return env('PTERODACTYL_URL') . $route;
  124. }
  125. /**
  126. * @param Server $server
  127. * @param Egg $egg
  128. * @param int $allocationId
  129. * @return Response
  130. */
  131. public static function createServer(Server $server, Egg $egg, int $allocationId)
  132. {
  133. return self::client()->post("/application/servers", [
  134. "name" => $server->name,
  135. "external_id" => $server->id,
  136. "user" => $server->user->pterodactyl_id,
  137. "egg" => $egg->id,
  138. "docker_image" => $egg->docker_image,
  139. "startup" => $egg->startup,
  140. "environment" => $egg->getEnvironmentVariables(),
  141. "limits" => [
  142. "memory" => $server->product->memory,
  143. "swap" => $server->product->swap,
  144. "disk" => $server->product->disk,
  145. "io" => $server->product->io,
  146. "cpu" => $server->product->cpu
  147. ],
  148. "feature_limits" => [
  149. "databases" => $server->product->databases,
  150. "backups" => $server->product->backups,
  151. "allocations" => $server->product->allocations,
  152. ],
  153. "allocation" => [
  154. "default" => $allocationId
  155. ]
  156. ]);
  157. }
  158. public static function suspendServer(Server $server)
  159. {
  160. $response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
  161. if ($response->failed()) throw self::getException();
  162. return $response;
  163. }
  164. public static function unSuspendServer(Server $server)
  165. {
  166. $response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
  167. if ($response->failed()) throw self::getException();
  168. return $response;
  169. }
  170. /**
  171. * Get user by pterodactyl id
  172. * @param int $pterodactylId
  173. * @return mixed
  174. */
  175. public function getUser(int $pterodactylId)
  176. {
  177. $response = self::client()->get("/application/users/{$pterodactylId}");
  178. if ($response->failed()) return $response->json();
  179. return $response->json()['attributes'];
  180. }
  181. }