Pterodactyl.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Classes;
  3. use App\Models\Egg;
  4. use App\Models\Nest;
  5. use App\Models\Node;
  6. use App\Models\Server;
  7. use App\Models\Settings;
  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 ' . config("SETTINGS::SYSTEM:PTERODACTYL:TOKEN"),
  26. 'Content-type' => 'application/json',
  27. 'Accept' => 'Application/vnd.pterodactyl.v1+json',
  28. ])->baseUrl(config("SETTINGS::SYSTEM: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. try {
  45. $response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . self::PER_PAGE);
  46. } catch (Exception $e) {
  47. throw self::getException();
  48. }
  49. if ($response->failed()) throw self::getException();
  50. return $response->json()['data'];
  51. }
  52. /**
  53. * @return mixed
  54. * @throws Exception
  55. */
  56. public static function getNodes()
  57. {
  58. try {
  59. $response = self::client()->get('/application/nodes?per_page=' . self::PER_PAGE);
  60. } catch (Exception $e) {
  61. throw self::getException();
  62. }
  63. if ($response->failed()) throw self::getException();
  64. return $response->json()['data'];
  65. }
  66. /**
  67. * @return null
  68. * @throws Exception
  69. */
  70. public static function getNests()
  71. {
  72. try {
  73. $response = self::client()->get('/application/nests?per_page=' . self::PER_PAGE);
  74. } catch (Exception $e) {
  75. throw self::getException();
  76. }
  77. if ($response->failed()) throw self::getException();
  78. return $response->json()['data'];
  79. }
  80. /**
  81. * @return mixed
  82. * @throws Exception
  83. */
  84. public static function getLocations()
  85. {
  86. try {
  87. $response = self::client()->get('/application/locations?per_page=' . self::PER_PAGE);
  88. } catch (Exception $e) {
  89. throw self::getException();
  90. }
  91. if ($response->failed()) throw self::getException();
  92. return $response->json()['data'];
  93. }
  94. /**
  95. * @param Node $node
  96. * @return mixed
  97. * @throws Exception
  98. */
  99. public static function getFreeAllocationId(Node $node)
  100. {
  101. return self::getFreeAllocations($node)[0]['attributes']['id'] ?? null;
  102. }
  103. /**
  104. * @param Node $node
  105. * @return array|mixed|null
  106. * @throws Exception
  107. */
  108. public static function getFreeAllocations(Node $node)
  109. {
  110. $response = self::getAllocations($node);
  111. $freeAllocations = [];
  112. if (isset($response['data'])) {
  113. if (!empty($response['data'])) {
  114. foreach ($response['data'] as $allocation) {
  115. if (!$allocation['attributes']['assigned']) array_push($freeAllocations, $allocation);
  116. }
  117. }
  118. }
  119. return $freeAllocations;
  120. }
  121. /**
  122. * @param Node $node
  123. * @return array|mixed
  124. * @throws Exception
  125. */
  126. public static function getAllocations(Node $node)
  127. {
  128. $per_page = config('SETTINGS::SERVER:ALLOCATION_LIMIT', 200);
  129. try {
  130. $response = self::client()->get("/application/nodes/{$node->id}/allocations?per_page={$per_page}");
  131. } catch (Exception $e) {
  132. throw self::getException();
  133. }
  134. if ($response->failed()) throw self::getException();
  135. return $response->json();
  136. }
  137. /**
  138. * @param String $route
  139. * @return string
  140. */
  141. public static function url(string $route): string
  142. {
  143. return config("SETTINGS::SYSTEM:PTERODACTYL:URL") . $route;
  144. }
  145. /**
  146. * @param Server $server
  147. * @param Egg $egg
  148. * @param int $allocationId
  149. * @return Response
  150. */
  151. public static function createServer(Server $server, Egg $egg, int $allocationId)
  152. {
  153. return self::client()->post("/application/servers", [
  154. "name" => $server->name,
  155. "external_id" => $server->id,
  156. "user" => $server->user->pterodactyl_id,
  157. "egg" => $egg->id,
  158. "docker_image" => $egg->docker_image,
  159. "startup" => $egg->startup,
  160. "environment" => $egg->getEnvironmentVariables(),
  161. "limits" => [
  162. "memory" => $server->product->memory,
  163. "swap" => $server->product->swap,
  164. "disk" => $server->product->disk,
  165. "io" => $server->product->io,
  166. "cpu" => $server->product->cpu
  167. ],
  168. "feature_limits" => [
  169. "databases" => $server->product->databases,
  170. "backups" => $server->product->backups,
  171. "allocations" => $server->product->allocations,
  172. ],
  173. "allocation" => [
  174. "default" => $allocationId
  175. ]
  176. ]);
  177. }
  178. public static function suspendServer(Server $server)
  179. {
  180. try {
  181. $response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
  182. } catch (Exception $e) {
  183. throw self::getException();
  184. }
  185. if ($response->failed()) throw self::getException();
  186. return $response;
  187. }
  188. public static function unSuspendServer(Server $server)
  189. {
  190. try {
  191. $response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
  192. } catch (Exception $e) {
  193. throw self::getException();
  194. }
  195. if ($response->failed()) throw self::getException();
  196. return $response;
  197. }
  198. /**
  199. * Get user by pterodactyl id
  200. * @param int $pterodactylId
  201. * @return mixed
  202. */
  203. public function getUser(int $pterodactylId)
  204. {
  205. try {
  206. $response = self::client()->get("/application/users/{$pterodactylId}");
  207. } catch (Exception $e) {
  208. throw self::getException();
  209. }
  210. if ($response->failed()) throw self::getException();
  211. return $response->json()['attributes'];
  212. }
  213. /**
  214. * Get serverAttributes by pterodactyl id
  215. * @param int $pterodactylId
  216. * @return mixed
  217. */
  218. public static function getServerAttributes(int $pterodactylId)
  219. {
  220. try {
  221. $response = self::client()->get("/application/servers/{$pterodactylId}?include=egg,node,nest,location");
  222. } catch (Exception $e) {
  223. throw self::getException();
  224. }
  225. if ($response->failed()) throw self::getException();
  226. return $response->json()['attributes'];
  227. }
  228. }