Skip to content

Commit

Permalink
Make Guacamole Enhanced app (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
albinmedoc authored Jun 4, 2024
1 parent 9c8369b commit 404f948
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
97 changes: 96 additions & 1 deletion Guacamole/Guacamole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,101 @@

namespace App\SupportedApps\Guacamole;

class Guacamole extends \App\SupportedApps
class Guacamole extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;

//protected $login_first = true; // Uncomment if api requests need to be authed first
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST

public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}

private function getConfigValue($key, $default = null)
{
return isset($this->config) && isset($this->config->$key)
? $this->config->$key
: $default;
}

private function getToken()
{
$username = urlencode($this->config->username);
$password = urlencode($this->config->password);

$attrs = [
"body" => "username=" . $username . "&password=" . $password,
"headers" => [
"Content-Type" => "application/x-www-form-urlencoded"
],
];

$res = parent::execute($this->url("api/tokens"), $attrs, null, "POST");

switch ($res->getStatusCode()) {
case 200:
$details = json_decode($res->getBody());
return $details->authToken;
case 400:
throw new \Exception("Invalid username format");
case 401:
case 403:
throw new \Exception("Invalid username/password");
default:
throw new \Exception("Could not connect to Guacamole");
}
}

private function getConnections($token)
{
$dataSource = $this->getConfigValue('dataSource', 'postgresql');

$url = $this->url("api/session/data/" . $dataSource . "/connections?token=" . $token);
$res = parent::execute($url);
if ($res->getStatusCode() == 404) {
throw new \Exception("Invalid data source " . $token);
}

return json_decode($res->getBody());
}

public function test()
{
try {
$token = $this->getToken();
$this->getConnections($token);
echo "Successfully communicated with the API";
} catch (Exception $err) {
echo $err->getMessage();
}
}

public function livestats()
{
$status = "inactive";

$token = $this->getToken();
$details = $this->getConnections($token);

$data = [];
if ($details != null) {
$activeConnections = 0;
foreach ($details as $item) {
$activeConnections += $item->activeConnections;
}

$data["connections"] = count((array)$details);
$data["active_connections"] = $activeConnections;
}
return parent::getLiveStats($status, $data);
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;

return $api_url;
}
}
2 changes: 1 addition & 1 deletion Guacamole/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"website": "https://guacamole.apache.org",
"license": "Apache License 2.0",
"description": "Apache Guacamole is a clientless remote desktop gateway. It supports standard protocols like VNC, RDP, and SSH.",
"enhanced": false,
"enhanced": true,
"tile_background": "dark",
"icon": "guacamole.png"
}
28 changes: 28 additions & 0 deletions Guacamole/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items" style="flex-direction:column">
<div style="display:flex;flex-direction:row;">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
</div>
<div style="display:flex;flex-direction:row;">
<div class="input">
<label>{{ __('app.apps.username') }}</label>
{!! Form::text('config[username]', isset($item) ? $item->getconfig()->username : null, ['placeholder' => __('app.apps.username'), 'data-config' => 'username', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.password') }}</label>
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
</div>
</div>
<div style="display:flex;flex-direction:row;">
<div class="input">
<label>Data source</label>
{!! Form::text('config[dataSource]', isset($item) ? $item->getconfig()->dataSource : null, ['placeholder' => __('Default: postgresql'), 'data-config' => 'dataSource', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
</div>
10 changes: 10 additions & 0 deletions Guacamole/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul class="livestats">
<li>
<span class="title">Connections</span>
<strong>{!! $connections !!}</strong>
</li>
<li>
<span class="title">Active</span>
<strong>{!! $active_connections !!}</strong>
</li>
</ul>

0 comments on commit 404f948

Please sign in to comment.