Pterodactyl.php 5.6 KB

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