|
2 | 2 |
|
3 | 3 | namespace App\SupportedApps\Guacamole; |
4 | 4 |
|
5 | | -class Guacamole extends \App\SupportedApps |
| 5 | +class Guacamole extends \App\SupportedApps implements \App\EnhancedApps |
6 | 6 | { |
| 7 | + public $config; |
| 8 | + |
| 9 | + //protected $login_first = true; // Uncomment if api requests need to be authed first |
| 10 | + //protected $method = 'POST'; // Uncomment if requests to the API should be set by POST |
| 11 | + |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set |
| 15 | + } |
| 16 | + |
| 17 | + private function getConfigValue($key, $default = null) |
| 18 | + { |
| 19 | + return isset($this->config) && isset($this->config->$key) |
| 20 | + ? $this->config->$key |
| 21 | + : $default; |
| 22 | + } |
| 23 | + |
| 24 | + private function getToken() |
| 25 | + { |
| 26 | + $username = urlencode($this->config->username); |
| 27 | + $password = urlencode($this->config->password); |
| 28 | + |
| 29 | + $attrs = [ |
| 30 | + "body" => "username=" . $username . "&password=" . $password, |
| 31 | + "headers" => [ |
| 32 | + "Content-Type" => "application/x-www-form-urlencoded" |
| 33 | + ], |
| 34 | + ]; |
| 35 | + |
| 36 | + $res = parent::execute($this->url("api/tokens"), $attrs, null, "POST"); |
| 37 | + |
| 38 | + switch ($res->getStatusCode()) { |
| 39 | + case 200: |
| 40 | + $details = json_decode($res->getBody()); |
| 41 | + return $details->authToken; |
| 42 | + case 400: |
| 43 | + throw new \Exception("Invalid username format"); |
| 44 | + case 401: |
| 45 | + case 403: |
| 46 | + throw new \Exception("Invalid username/password"); |
| 47 | + default: |
| 48 | + throw new \Exception("Could not connect to Guacamole"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private function getConnections($token) |
| 53 | + { |
| 54 | + $dataSource = $this->getConfigValue('dataSource', 'postgresql'); |
| 55 | + |
| 56 | + $url = $this->url("api/session/data/" . $dataSource . "/connections?token=" . $token); |
| 57 | + $res = parent::execute($url); |
| 58 | + if ($res->getStatusCode() == 404) { |
| 59 | + throw new \Exception("Invalid data source " . $token); |
| 60 | + } |
| 61 | + |
| 62 | + return json_decode($res->getBody()); |
| 63 | + } |
| 64 | + |
| 65 | + public function test() |
| 66 | + { |
| 67 | + try { |
| 68 | + $token = $this->getToken(); |
| 69 | + $this->getConnections($token); |
| 70 | + echo "Successfully communicated with the API"; |
| 71 | + } catch (Exception $err) { |
| 72 | + echo $err->getMessage(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public function livestats() |
| 77 | + { |
| 78 | + $status = "inactive"; |
| 79 | + |
| 80 | + $token = $this->getToken(); |
| 81 | + $details = $this->getConnections($token); |
| 82 | + |
| 83 | + $data = []; |
| 84 | + if ($details != null) { |
| 85 | + $activeConnections = 0; |
| 86 | + foreach ($details as $item) { |
| 87 | + $activeConnections += $item->activeConnections; |
| 88 | + } |
| 89 | + |
| 90 | + $data["connections"] = count((array)$details); |
| 91 | + $data["active_connections"] = $activeConnections; |
| 92 | + } |
| 93 | + return parent::getLiveStats($status, $data); |
| 94 | + } |
| 95 | + |
| 96 | + public function url($endpoint) |
| 97 | + { |
| 98 | + $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
| 99 | + |
| 100 | + return $api_url; |
| 101 | + } |
7 | 102 | } |
0 commit comments