forked from UB-Mannheim/PalMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSVNCDaemon.php
318 lines (249 loc) · 9.24 KB
/
SSVNCDaemon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php namespace palma;
// Copyright (C) 2014 Universitätsbibliothek Mannheim
// See file LICENSE for license details.
require_once("DBConnector.class.php");
require_once("globals.php");
class SSVNCDaemon
{
// all during session connected VNC Clients
private $VNC_CLIENTS;
// count of all connected clients
// private $_VNC_CLIENT_COUNT;
// active client count
private $CONNECTIONS = 0;
// ignored VNC Clients
// TODO: check if no more longer helpful, has to be cleaned
private $IGNORE_LIST = array();
public $db;
public function __construct()
{
global $db;
$this->db = $db;
// Start Vncviewer
$handle = $this->startVNCViewer();
// Read log in loop
$this->readLog($handle);
}
protected function startVNCViewer()
{
// Startup SSVNC-Viewer in Multilisten-Mode
$cmd = "export DISPLAY=" . CONFIG_DISPLAY .
"; killall -q ssvncviewer; ssvncviewer -viewonly -multilisten 0 2>&1";
$handle = popen($cmd, 'r');
print("[Daemon]: vnc_viewer started");
return $handle;
}
protected function readLog($handle)
{
print("\n[Daemon]: +++ readLog() +++");
// local SSVNC-Client info
$client = array(
"ip" => "",
"hostname" => "",
"active" => 1,
"exit" => 0
);
// Read File continuously
while (!feof($handle)) {
$buffer = fgets($handle);
print($buffer);
if ($this->CONNECTIONS == 0) {
//print("\n --- WAITING FOR NEW CONNECTIONS --- \n");
}
$ip = $this->parseIP($buffer);
$hostname = $this->parseHostname($buffer);
$exit = $this->parseExit($buffer);
if ($ip!="") {
$client["ip"] = $ip;
}
if ($hostname!="") {
$client["hostname"] = $hostname;
}
if ($exit!=0) {
$client["exit"] = $exit;
}
if (strstr($buffer, 'create_image') && $client["ip"] != "" &&
$client["hostname"]!="" ) {
// add client
$this->addClient($client["ip"], $client["hostname"]);
// reset local Client information after adding it
$client["ip"] = "";
$client["hostname"] = "";
}
if ($exit == 1) {
// decrease active client count
if ($this->CONNECTIONS > 0) {
$this->CONNECTIONS--;
$this->deleteInactiveVncWindow();
}
}
$halt=$this->CONNECTIONS;
if ($halt == -1) {
exit(0);
}
flush();
}
pclose($handle);
}
protected function parseIP($buffer)
{
$ip = "";
$line = $buffer;
if (strpos($line, "Reverse VNC connection from IP")) {
$line=trim($line);
$item=explode(":", $line);
$ip=trim($item[1]);
$ip=preg_replace('/\W\W\d{4}\/\d{2}\/\d{2} \d{2}/', '', $ip);
}
if ($ip!="") {
print("\nFOUND IP: " . $ip . "\n");
}
return $ip;
}
protected function parseHostname($buffer)
{
$hostname = "";
$line = $buffer;
if (strpos($line, "Hostname")) {
$line=trim($line);
$item=explode(":", $line);
$hostname=trim($item[1]);
$hostname=preg_replace('/\.uni\-mannheim\.de/', '', $hostname);
}
if ($hostname!="") {
print("\nFOUND HOSTNAME: " . $hostname . "\n");
}
return $hostname;
}
protected function parseExit($line)
{
$exit = 0;
if (strpos($line, "VNC Viewer exiting")) {
$exit = 1;
}
if ($exit!=0) {
print("\nCLIENT HAS DISCONNECTED " . $exit . "\n");
}
return $exit;
}
protected function addClient($ip, $hostname)
{
$vncclient = array(
"ip" => $ip,
"hostname" => $hostname,
"active" => 1,
"exit" => 0
);
if (count($this->VNC_CLIENTS) == 0) {
$id=1;
} else {
$id=count($this->VNC_CLIENTS) + 1;
}
$this->VNC_CLIENTS[$id] = $vncclient;
print("\nCLIENT OBJECT with ID " . $id . " CREATED : "
. $vncclient["ip"] . " | " . $vncclient["hostname"] . " | "
. $vncclient["active"] . " | " . $vncclient["exit"] . "\n");
print($this->CONNECTIONS+1 . " CLIENT(S) CONNECTED ...");
$this->sendVncWindowToNuc($id, $vncclient);
$this->CONNECTIONS++;
print("\n active connections: ".$this->CONNECTIONS+1);
print("\n all saved clients: " . serialize($this->VNC_CLIENTS));
}
protected function sendVncWindowToNuc($id, $vncclient)
{
print("\n[Daemon]: +++sendVncWindowToNuc() +++ ");
$vnc_id = $vncclient["hostname"] . "-" . $id;
$db = new DBConnector();
// already existing in db?
$clients_in_db = array();
$client_info = $db->getVNCClientInfo();
foreach ($client_info as $info) {
$clients_in_db[] = $info["file"];
}
$ip = $vncclient["ip"];
$name = $db->querySingle('SELECT user.name FROM address, user
WHERE user.userid = (
SELECT address.userid
FROM address
WHERE address.address ="' . $ip . '"
)');
// print("\n[Daemon]: USERNAME : " . $name);
// $vncClientCount = $db->querySingle('SELECT count(id) FROM window WHERE handler = "vnc"');
// $vncClientsAll = $db->query("SELECT user.name FROM address");
// print("\n[Daemon]: clients in db = " . $vncClientCount);
// print("\n[Daemon]: clients ignored = " . serialize($this->IGNORE_LIST));
// if vnc_id not in database create window and send to nuc
if (!(in_array($vnc_id, $clients_in_db))) {
// print("\n[Daemon]: insert $vnc_id into db");
$dt = new \DateTime();
$date = $dt->format('Y-m-d H:i:s');
$window = array(
"id" => "",
"win_id" => "",
"name" => "",
"state" => "",
"file" => $name . "@" . $vnc_id,
"handler" => "vnc",
"userid" => $name,
"date" => $date
);
$serializedWindow = serialize($window);
$sw = urlencode($serializedWindow);
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => CONFIG_CONTROL_FILE . '?newVncWindow=' . $sw,
CURLOPT_USERAGENT => 'PalMA cURL Request'
));
// Send the request
curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
}
// add unique id to ignore list after sending to nuc
array_push($this->IGNORE_LIST, $vnc_id);
}
protected function deleteInactiveVncWindow()
{
// print("\n[Daemon]: +++ TODO: deleteInactiveWindow() +++");
$db = new DBConnector();
// window_ids in db
$vnc_windows_in_db = array();
$client_info = $db->getVNCClientInfo();
foreach ($client_info as $info) {
$vnc_windows_in_db[] = $info["win_id"];
}
// window_ids on screen
$windows_on_screen = array();
$windows = explode("\n", shell_exec('wmctrl -l'));
foreach ($windows as $w) {
$field = explode(' ', $w);
$id = $field[0];
if ($id != '') {
$windows_on_screen[] = $id;
}
}
// print("[Daemon]: clients in db = " . serialize($vnc_windows_in_db));
// print("[Daemon]: client on screen = " . serialize($windows_on_screen));
// window_ids that are in the db, but not on the screen (window already closed)
$inactive_vnc_window_ids = array_diff($vnc_windows_in_db, $windows_on_screen);
foreach ($inactive_vnc_window_ids as $inactive_win_id) {
// define vnc-id
$inactive_vnc_id = $db->querySingle("SELECT file FROM window WHERE win_id='".$inactive_win_id."'");
// delete from database (send to control.php)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => CONFIG_CONTROL_FILE .
'?window=vncwin&delete=VNC&vncid=' . $inactive_win_id,
CURLOPT_USERAGENT => 'PalMA cURL Request'
));
curl_exec($curl);
curl_close($curl);
// print("[Daemon]: inactive vnc_id = $inactive_vnc_id >> add to list: " .serialize($this->IGNORE_LIST));
}
}
}
$vnc = new SSVNCDaemon();