Skip to content

Commit 404f948

Browse files
author
Albin Médoc
authored
Make Guacamole Enhanced app (#739)
1 parent 9c8369b commit 404f948

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

Guacamole/Guacamole.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,101 @@
22

33
namespace App\SupportedApps\Guacamole;
44

5-
class Guacamole extends \App\SupportedApps
5+
class Guacamole extends \App\SupportedApps implements \App\EnhancedApps
66
{
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+
}
7102
}

Guacamole/app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"website": "https://guacamole.apache.org",
55
"license": "Apache License 2.0",
66
"description": "Apache Guacamole is a clientless remote desktop gateway. It supports standard protocols like VNC, RDP, and SSH.",
7-
"enhanced": false,
7+
"enhanced": true,
88
"tile_background": "dark",
99
"icon": "guacamole.png"
1010
}

Guacamole/config.blade.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items" style="flex-direction:column">
3+
<div style="display:flex;flex-direction:row;">
4+
<div class="input">
5+
<label>{{ strtoupper(__('app.url')) }}</label>
6+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
7+
</div>
8+
</div>
9+
<div style="display:flex;flex-direction:row;">
10+
<div class="input">
11+
<label>{{ __('app.apps.username') }}</label>
12+
{!! Form::text('config[username]', isset($item) ? $item->getconfig()->username : null, ['placeholder' => __('app.apps.username'), 'data-config' => 'username', 'class' => 'form-control config-item']) !!}
13+
</div>
14+
<div class="input">
15+
<label>{{ __('app.apps.password') }}</label>
16+
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
17+
</div>
18+
</div>
19+
<div style="display:flex;flex-direction:row;">
20+
<div class="input">
21+
<label>Data source</label>
22+
{!! Form::text('config[dataSource]', isset($item) ? $item->getconfig()->dataSource : null, ['placeholder' => __('Default: postgresql'), 'data-config' => 'dataSource', 'class' => 'form-control config-item']) !!}
23+
</div>
24+
<div class="input">
25+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
26+
</div>
27+
</div>
28+
</div>

Guacamole/livestats.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<ul class="livestats">
2+
<li>
3+
<span class="title">Connections</span>
4+
<strong>{!! $connections !!}</strong>
5+
</li>
6+
<li>
7+
<span class="title">Active</span>
8+
<strong>{!! $active_connections !!}</strong>
9+
</li>
10+
</ul>

0 commit comments

Comments
 (0)