Added IP-KVM session eventing.

Ylian Saint-Hilaire committed Dec 7, 2021 at 14:14 UTC de39ebfcf305c283838682a31e46bc6deb2c0c69
1 file changed +34 -9
meshipkvm.js
+34 -9
@@ -202,7 +202,7 @@ function CreateIPKVMManager(parent) {
202 urlargs.shift();
203 var relurl = '/' + urlargs.join('/')
204 if (relurl.endsWith('/.websocket')) { relurl = relurl.substring(0, relurl.length - 11); }
205 - return { relurl: relurl, preurl: q.path.substring(0, i + 76), nodeid: nodeid, nid: nid, kvmmanager: kvmmanager, kvmport: kvmport };
205 + return { domain: domain.id, relurl: relurl, preurl: q.path.substring(0, i + 76), nodeid: nodeid, nid: nid, kvmmanager: kvmmanager, kvmport: kvmport };
206 }
207
208 // Handle a IP-KVM HTTP get request
@@ -211,14 +211,12 @@ function CreateIPKVMManager(parent) {
211 const reqinfo = parseIpKvmUrl(domain, req.url);
212 if (reqinfo == null) { next(); return; }
213
214 - /*
214 // Check node rights
215 if ((req.session == null) || (req.session.userid == null)) { next(); return; }
216 const user = parent.webserver.users[req.session.userid];
217 if (user == null) { next(); return; }
218 const rights = parent.webserver.GetNodeRights(user, reqinfo.kvmmanager.meshid, reqinfo.nodeid);
219 if ((rights & MESHRIGHT_REMOTECONTROL) == 0) { next(); return; }
221 - */
220
221 // Process the request
222 reqinfo.kvmmanager.handleIpKvmGet(domain, reqinfo, req, res, next);
@@ -230,14 +228,17 @@ function CreateIPKVMManager(parent) {
228 const reqinfo = parseIpKvmUrl(domain, req.url);
229 if (reqinfo == null) { try { ws.close(); } catch (ex) { } return; }
230
233 - /*
231 // Check node rights
232 if ((req.session == null) || (req.session.userid == null)) { try { ws.close(); } catch (ex) { } return; }
233 const user = parent.webserver.users[req.session.userid];
234 if (user == null) { try { ws.close(); } catch (ex) { } return; }
235 const rights = parent.webserver.GetNodeRights(user, reqinfo.kvmmanager.meshid, reqinfo.nodeid);
236 if ((rights & MESHRIGHT_REMOTECONTROL) == 0) { try { ws.close(); } catch (ex) { } return; }
240 - */
237 +
238 + // Add more logging data to the request information
239 + reqinfo.clientIp = req.clientIp;
240 + reqinfo.userid = req.session.userid;
241 + reqinfo.username = user.name;
242
243 // Process the request
244 reqinfo.kvmmanager.handleIpKvmWebSocket(domain, reqinfo, ws, req);
@@ -532,10 +533,25 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
533 });
534 }
535
535 - // TODO:
536 - // We need to hide the cookie from the web page.
537 - // Find message with "{SHA256}", do a.substring(1, 65)
538 - // Do SHA256(Nonce + Cookie) and return that to KVM device.
536 + function logConnection(wsClient) {
537 + const kvmport = wsClient.kvmport
538 + const reqinfo = wsClient.reqinfo;
539 + var event = { etype: 'relay', action: 'relaylog', domain: reqinfo.domain, userid: reqinfo.userid, username: reqinfo.username, msgid: 15, msgArgs: [kvmport.portid, reqinfo.clientIp, kvmport.portNo], msg: 'Started desktop session' + ' \"' + kvmport.portid + '\" from ' + reqinfo.clientIp + ' to ' + kvmport.portNo, protocol: 2, nodeid: reqinfo.nodeid };
540 + parent.parent.DispatchEvent(['*', reqinfo.userid, reqinfo.nodeid, kvmport.meshid], obj, event);
541 + }
542 +
543 + function logDisconnection(wsClient) {
544 + const kvmport = wsClient.kvmport
545 + const reqinfo = wsClient.reqinfo;
546 + var event = { etype: 'relay', action: 'relaylog', domain: reqinfo.domain, userid: reqinfo.userid, username: reqinfo.username, msgid: 11, msgArgs: [kvmport.portid, reqinfo.clientIp, kvmport.portNo, Math.floor((Date.now() - kvmport.connectionStart) / 1000)], msg: 'Ended desktop session' + ' \"' + kvmport.portid + '\" from ' + reqinfo.clientIp + ' to ' + kvmport.portNo + ', ' + Math.floor((Date.now() - kvmport.connectionStart) / 1000) + ' second(s)', protocol: 2, nodeid: reqinfo.nodeid, bytesin: kvmport.bytesIn, bytesout: kvmport.bytesOut };
547 + parent.parent.DispatchEvent(['*', reqinfo.userid, reqinfo.nodeid, kvmport.meshid], obj, event);
548 +
549 + delete kvmport.bytesIn;
550 + delete kvmport.bytesOut;
551 + delete kvmport.connectionStart;
552 + delete wsClient.kvmport;
553 + delete wsClient.reqinfo;
554 + }
555
556 // Handle a IP-KVM HTTP websocket request
557 obj.handleIpKvmWebSocket = function (domain, reqinfo, ws, req) {
@@ -560,6 +576,11 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
576 reqinfo.kvmport.wsClient.wsBrowser = ws;
577 ws.wsClient = reqinfo.kvmport.wsClient;
578 reqinfo.kvmport.wsClient.kvmport = reqinfo.kvmport;
579 + reqinfo.kvmport.wsClient.reqinfo = reqinfo;
580 + reqinfo.kvmport.connectionStart = Date.now();
581 + reqinfo.kvmport.bytesIn = 0;
582 + reqinfo.kvmport.bytesOut = 0;
583 + logConnection(reqinfo.kvmport.wsClient);
584
585 reqinfo.kvmport.wsClient.on('open', function () {
586 parent.parent.debug('relay', 'IPKVM: Relay websocket open');
@@ -576,6 +597,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
597 this.xAuthNonce = 1;
598 }
599
600 + try { this.wsClient.kvmport.bytesOut += data.length; } catch (ex) { }
601 this._socket.pause();
602 try { this.wsClient.send(data); } catch (ex) { }
603 this._socket.resume();
@@ -585,6 +607,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
607
608 // Clean up
609 if (this.wsClient) {
610 + logDisconnection(this.wsClient);
611 try { this.wsClient.close(); } catch (ex) { }
612 if (this.wsClient.kvmport) { delete this.wsClient.kvmport.wsClient; delete this.wsClient.kvmport; }
613 delete this.wsClient.wsBrowser; delete this.wsClient;
@@ -603,6 +626,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
626 this.wsBrowser.xAuthNonce = data.slice(2).toString().substring(0, 64);
627 }
628
629 + try { this.wsBrowser.wsClient.kvmport.bytesIn += data.length; } catch (ex) { }
630 this._socket.pause();
631 try { this.wsBrowser.send(data); } catch (ex) { }
632 this._socket.resume();
@@ -612,6 +636,7 @@ function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
636
637 // Clean up
638 if (this.wsBrowser) {
639 + logDisconnection(this.wsBrowser.wsClient);
640 try { this.wsBrowser.close(); } catch (ex) { }
641 delete this.wsBrowser.wsClient; delete this.wsBrowser;
642 }