Improved IP-KVM relay to support remote desktop.
Ylian Saint-Hilaire committed
Dec 6, 2021 at 21:01 UTC
e66649b95b76384fa66bff353ff80b8ea73b4e9a
1 file changed
+82
-8
meshipkvm.js
+82
-8
@@ -8,6 +8,7 @@
8
9
function CreateIPKVMManager(parent) {
10
const obj = {};
11
+ obj.parent = parent;
12
obj.managedGroups = {} // meshid --> Manager
13
obj.managedPorts = {} // nodeid --> PortInfo
14
@@ -35,7 +36,7 @@ function CreateIPKVMManager(parent) {
36
var port = 443, hostSplit = mesh.kvm.host.split(':'), host = hostSplit[0];
37
if (hostSplit.length == 2) { port = parseInt(hostSplit[1]); }
38
if (mesh.kvm.model == 1) { // Raritan KX III
38
- const manager = CreateRaritanKX3Manager(host, port, mesh.kvm.user, mesh.kvm.pass);
39
+ const manager = CreateRaritanKX3Manager(obj, host, port, mesh.kvm.user, mesh.kvm.pass);
40
manager.meshid = mesh._id;
41
manager.domainid = mesh._id.split('/')[1];
42
obj.managedGroups[mesh._id] = manager;
@@ -107,7 +108,7 @@ function CreateIPKVMManager(parent) {
108
// Set the connectivity state if needed
109
if (obj.managedPorts[nodeid] == null) {
110
parent.SetConnectivityState(sender.meshid, nodeid, Date.now(), 1, 1, null, null);
110
- obj.managedPorts[nodeid] = { name: port.Name, meshid: sender.meshid, portid: port.PortId };
111
+ obj.managedPorts[nodeid] = { name: port.Name, meshid: sender.meshid, portid: port.PortId, portType: port.PortType, portNo: port.PortIndex };
112
}
113
114
// Update busy state
@@ -197,7 +198,7 @@ function CreateIPKVMManager(parent) {
198
return obj;
199
}
200
200
-function CreateRaritanKX3Manager(hostname, port, username, password) {
201
+function CreateRaritanKX3Manager(parent, hostname, port, username, password) {
202
const https = require('https');
203
const obj = {};
204
var updateTimer = null;
@@ -431,6 +432,7 @@ function CreateRaritanKX3Manager(hostname, port, username, password) {
432
433
obj.fetch = function(url, postdata, tag, func) {
434
if (obj.state == 0) return;
435
+
436
var data = '';
437
const options = {
438
hostname: hostname,
@@ -449,7 +451,11 @@ function CreateRaritanKX3Manager(hostname, port, username, password) {
451
if (res.statusCode != 200) { setState(0); return; }
452
if (res.headers['set-cookie'] != null) { for (var i in res.headers['set-cookie']) { if (res.headers['set-cookie'][i].startsWith('pp_session_id=')) { obj.authCookie = res.headers['set-cookie'][i].substring(14).split(';')[0]; } } }
453
res.on('data', function (d) { data += d; });
452
- res.on('end', function () { func(obj, tag, data, res); });
454
+ res.on('end', function () {
455
+ // This line is used for debugging only, used to swap a file.
456
+ //if (url.endsWith('js_kvm_client.1604062083669.min.js')) { data = parent.parent.fs.readFileSync('c:\\tmp\\js_kvm_client.1604062083669.min.js').toString(); }
457
+ func(obj, tag, data, res);
458
+ });
459
});
460
req.on('error', function (error) { console.log(error); setState(0); })
461
req.end();
@@ -457,16 +463,19 @@ function CreateRaritanKX3Manager(hostname, port, username, password) {
463
464
// Handle a IP-KVM HTTP get request
465
obj.handleIpKvmGet = function (domain, reqinfo, req, res, next) {
460
- if (reqinfo.relurl == '/') { res.redirect(reqinfo.preurl + '/jsclient/Client.asp#portId=' + reqinfo.kvmport.portid); return; }
466
+ if (reqinfo.relurl == '/') { res.redirect(reqinfo.preurl + '/jsclient/Client.asp'); return; }
467
468
// Example: /jsclient/Client.asp#portId=P_000d5d20f64c_1
469
obj.fetch(reqinfo.relurl, null, [res, reqinfo], function (server, args, data, rres) {
470
const resx = args[0], xreqinfo = args[1];
471
if (rres.headers['content-type']) { resx.set('content-type', rres.headers['content-type']); }
472
467
- // We need to replace the WebSocket code in one of the files to make it work with our server.
468
- // Replace "b=new WebSocket(e+"//"+c+"/"+g);" in file "/js/js_kvm_client.1604062083669.min.js"
473
if (xreqinfo.relurl.startsWith('/js/js_kvm_client.')) {
474
+ // Since our cookies can't be read from the html page for security, we embed the cookie right into the page.
475
+ data = data.replace('module$js$helper$Extensions.Utils.getCookieValue("pp_session_id")', '"' + obj.authCookie + '"');
476
+ // Add the connection information directly into the file.
477
+ data = data.replace('\'use strict\';', '\'use strict\';sessionStorage.setItem("portPermission","CCC");sessionStorage.setItem("appId","1638838693725_3965868704642470");sessionStorage.setItem("portId","' + xreqinfo.kvmport.portid + '");sessionStorage.setItem("channelName","' + xreqinfo.kvmport.name + '");sessionStorage.setItem("portType","' + xreqinfo.kvmport.portType + '");sessionStorage.setItem("portNo","' + xreqinfo.kvmport.portNo + '");');
478
+ // Replace the WebSocket code in one of the files to make it work with our server.
479
data = data.replace('b=new WebSocket(e+"//"+c+"/"+g);', 'b=new WebSocket(e+"//"+c+"/ipkvm.ashx/' + xreqinfo.nid + '/"+g);');
480
}
481
@@ -476,8 +485,73 @@ function CreateRaritanKX3Manager(hostname, port, username, password) {
485
486
// Handle a IP-KVM HTTP websocket request
487
obj.handleIpKvmWebSocket = function (domain, reqinfo, ws, req) {
488
+ ws._socket.pause();
489
//console.log('handleIpKvmWebSocket', reqinfo.preurl);
480
- // TODO
490
+
491
+ if (reqinfo.kvmport.wsClient != null) {
492
+ // Relay already open
493
+ console.log('IPKVM Relay already present');
494
+ try { ws.close(); } catch (ex) { }
495
+ } else {
496
+ // Setup a websocket-to-websocket relay
497
+ try {
498
+ const options = {
499
+ rejectUnauthorized: false,
500
+ servername: 'Raritan',
501
+ headers: { Cookie: 'pp_session_id=' + obj.authCookie + '; view_length=32' }
502
+ };
503
+ parent.parent.debug('relay', 'IPKVM: Relay connecting to: wss://' + hostname + ':' + port + '/rfb');
504
+ const WebSocket = require('ws');
505
+ reqinfo.kvmport.wsClient = new WebSocket('wss://' + hostname + ':' + port + '/rfb', options);
506
+ reqinfo.kvmport.wsClient.wsBrowser = ws;
507
+ ws.wsClient = reqinfo.kvmport.wsClient;
508
+ reqinfo.kvmport.wsClient.kvmport = reqinfo.kvmport;
509
+
510
+ reqinfo.kvmport.wsClient.on('open', function () {
511
+ parent.parent.debug('relay', 'IPKVM: Relay websocket open');
512
+ this.wsBrowser.on('message', function (data) {
513
+ //console.log('KVM browser data', data, data.toString());
514
+ this._socket.pause();
515
+ this.wsClient.send(data);
516
+ this._socket.resume();
517
+ });
518
+ this.wsBrowser.on('close', function () {
519
+ parent.parent.debug('relay', 'IPKVM: Relay browser websocket closed');
520
+
521
+ // Clean up
522
+ if (this.wsClient) {
523
+ try { this.wsClient.close(); } catch (ex) { }
524
+ if (this.wsClient.kvmport) { delete this.wsClient.kvmport.wsClient; delete this.wsClient.kvmport; }
525
+ delete this.wsClient.wsBrowser; delete this.wsClient;
526
+ }
527
+ });
528
+ this.wsBrowser.on('error', function (err) {
529
+ parent.parent.debug('relay', 'IPKVM: Relay browser websocket error: ' + err);
530
+ });
531
+ this.wsBrowser._socket.resume();
532
+ });
533
+ reqinfo.kvmport.wsClient.on('message', function (data) { // Make sure to handle flow control.
534
+ //console.log('KVM switch data', data, data.toString());
535
+ this._socket.pause();
536
+ this.wsBrowser.send(data);
537
+ this._socket.resume();
538
+ });
539
+ reqinfo.kvmport.wsClient.on('close', function () {
540
+ parent.parent.debug('relay', 'IPKVM: Relay websocket closed');
541
+
542
+ // Clean up
543
+ if (this.wsBrowser) {
544
+ try { this.wsBrowser.close(); } catch (ex) { }
545
+ delete this.wsBrowser.wsClient; delete this.wsBrowser;
546
+ }
547
+ if (this.kvmport) { delete this.kvmport.wsClient; delete this.kvmport; }
548
+ });
549
+ reqinfo.kvmport.wsClient.on('error', function (err) {
550
+ parent.parent.debug('relay', 'IPKVM: Relay websocket error: ' + err);
551
+
552
+ });
553
+ } catch (ex) { console.log(ex); }
554
+ }
555
}
556
557
return obj;