diff --git a/CraftyController/CraftyController.php b/CraftyController/CraftyController.php new file mode 100644 index 0000000000..b44a6fd0fd --- /dev/null +++ b/CraftyController/CraftyController.php @@ -0,0 +1,135 @@ +jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set + } + + private function getToken() + { + $res = parent::execute($this->url('api/v2/auth/login'), $this->authAttrs()); + $data = json_decode($res->getBody()); + if ($data->status == 'ok') { + $this->token = $data->data->token; + } + } + + public function test() + { + try { + $this->getToken(); + echo "Successfully communicated with the API"; + } catch (Exception $err) { + echo $err->getMessage(); + } + } + + public function livestats() + { + $status = "inactive"; + $this->getToken(); + + $res = parent::execute( + $this->url('api/v2/servers'), + attrs: $this->attrs(), + overridemethod: 'GET' + ); + $details = json_decode($res->getBody()); + + $vars['servers_total'] = count($details->data); + + $servers_online = 0; + $players_online = 0; + $mem = 0; + + foreach ($details->data as $server) { + $server_res = parent::execute( + $this->url('api/v2/servers/' . $server->server_id . '/stats'), + attrs: $this->attrs(), + overridemethod: 'GET' + ); + $server_details = json_decode($server_res->getBody()); + $players_online += $server_details->data->online; + $mem += $this->decodeSizeToGB($server_details->data->mem); + if ($server_details->data->running == true) { + $servers_online++; + } + } + + $vars['servers_online'] = $servers_online; + $vars['players_online'] = $players_online; + $vars['mem'] = $mem; + + return parent::getLiveStats($status, $vars); + } + + private function authAttrs() + { + $ignoreTls = $this->getConfigValue("ignore_tls", false); + return [ + "body" => json_encode([ + "username" => $this->getConfigValue("username", null), + "password" => $this->getConfigValue("password", null) + ]), + "verify" => ($ignoreTls ? false : true) + ]; + } + + + public function attrs() + { + $ignoreTls = $this->getConfigValue("ignore_tls", false); + return [ + "headers" => [ + "content-type" => "application/json", + "Authorization" => "Bearer " . $this->token, + ], + "verify" => ($ignoreTls ? false : true) + ]; + } + + public function url($endpoint) + { + $api_url = parent::normaliseurl($this->config->url) . $endpoint; + return $api_url; + } + + private function getConfigValue($key, $default = null) + { + return isset($this->config) && isset($this->config->$key) + ? $this->config->$key + : $default; + } + + private function decodeSizeToGB($size) + { + $units = [ + 'B' => 1 / (1024 * 1024 * 1024), // Convert bytes to GB + 'KB' => 1 / (1024 * 1024), // Convert KB to GB + 'MB' => 1 / 1024, // Convert MB to GB + 'GB' => 1, // GB is the base unit + 'TB' => 1024, // Convert TB to GB + 'PB' => 1024 * 1024, // Convert PB to GB + ]; + + if (preg_match('/^([\d\.]+)\s*([KMGTP]?B)$/i', strtoupper($size), $matches)) { + $value = (float)$matches[1]; + $unit = $matches[2]; + + return round($value * ($units[$unit] ?? 1), 1); + } + + return false; // Invalid format + } +} diff --git a/CraftyController/app.json b/CraftyController/app.json new file mode 100644 index 0000000000..edd052843e --- /dev/null +++ b/CraftyController/app.json @@ -0,0 +1,10 @@ +{ + "appid": "200fe80f6b54e75f36a0ae671663f47b417de384", + "name": "Crafty Controller", + "website": "https://craftycontrol.com", + "license": "GNU General Public License v3.0 or later", + "description": "Crafty Controller is a free and open-source Minecraft launcher and manager that allows users to start and administer Minecraft servers from a user-friendly interface.", + "enhanced": true, + "tile_background": "dark", + "icon": "craftycontroller.png" +} diff --git a/CraftyController/config.blade.php b/CraftyController/config.blade.php new file mode 100644 index 0000000000..93635e82e0 --- /dev/null +++ b/CraftyController/config.blade.php @@ -0,0 +1,36 @@ +