/** * @description MeshCentral IP KVM Management Module * @author Ylian Saint-Hilaire * @copyright Intel Corporation 2021 * @license Apache-2.0 * @version v0.0.1 */ function CreateIPKVMManager(parent) { const obj = {}; const managedGroups = {} // meshid --> Manager // Subscribe for mesh creation events parent.AddEventDispatch(['server-createmesh', 'server-deletemesh'], obj); obj.HandleEvent = function (source, event, ids, id) { if ((event != null) && (event.action == 'createmesh') && (event.mtype == 4)) { // Start managing this new device group startManagement(parent.webserver.meshes[event.meshid]); } else if ((event != null) && (event.action == 'deletemesh') && (event.mtype == 4)) { // Stop managing this device group stopManagement(event.meshid); } } // Run thru the list of device groups that require for (var i in parent.webserver.meshes) { const mesh = parent.webserver.meshes[i]; if ((mesh.mtype == 4) && (mesh.deleted == null)) { startManagement(mesh); } } // Start managing a IP KVM device function startManagement(mesh) { if ((mesh == null) || (mesh.mtype != 4) || (mesh.kvm == null) || (mesh.deleted != null) || (managedGroups[mesh._id] != null)) return; var port = 443, hostSplit = mesh.kvm.host.split(':'), host = hostSplit[0]; if (hostSplit.length == 2) { port = parseInt(hostSplit[1]); } if (mesh.kvm.model == 1) { // Raritan KX III const manager = CreateRaritanKX3Manager(host, port, mesh.kvm.user, mesh.kvm.pass); manager.meshid = mesh._id; managedGroups[mesh._id] = manager; manager.onStateChanged = onStateChanged; manager.onPortsChanged = onPortsChanged; manager.start(); } } // Stop managing a IP KVM device function stopManagement(meshid) { const manager = managedGroups[meshid]; if (manager != null) { delete managedGroups[meshid]; manager.stop(); } } // Called when a KVM device changes state function onStateChanged(sender, state) { console.log('State: ' + ['Disconnected', 'Connecting', 'Connected'][state]); if (state == 2) { console.log('DeviceModel:', sender.deviceModel); console.log('FirmwareVersion:', sender.firmwareVersion); } } // Called when a KVM device changes state function onPortsChanged(sender, updatedPorts) { for (var i = 0; i < updatedPorts.length; i++) { const port = sender.ports[updatedPorts[i]]; if ((port.Status == 1) && (port.Class == 'KVM')) { console.log(port.PortNumber + ', ' + port.PortId + ', ' + port.Name + ', ' + port.Type + ', ' + ((port.StatAvailable == 0) ? 'Idle' : 'Connected')); } } } return obj; } function CreateRaritanKX3Manager(hostname, port, username, password) { const https = require('https'); const obj = {}; var updateTimer = null; var retryTimer = null; obj.authCookie = null; obj.state = 0; // 0 = Disconnected, 1 = Connecting, 2 = Connected obj.ports = []; obj.portCount = 0; obj.portHash = null; obj.deviceCount = 0; obj.deviceHash = null; obj.started = false; // Events obj.onStateChanged = null; obj.onPortsChanged = null; function onCheckServerIdentity(cert) { console.log('TODO: Certificate Check'); } obj.start = function () { if (obj.started) return; obj.started = true; if (obj.state == 0) connect(); } obj.stop = function () { if (!obj.started) return; obj.started = false; if (retryTimer != null) { clearTimeout(retryTimer); retryTimer = null; } setState(0); } function setState(newState) { if (obj.state == newState) return; obj.state = newState; if (obj.onStateChanged != null) { obj.onStateChanged(obj, newState); } if ((newState == 2) && (updateTimer == null)) { updateTimer = setInterval(obj.update, 10000); } if ((newState != 2) && (updateTimer != null)) { clearInterval(updateTimer); updateTimer = null; } if ((newState == 0) && (obj.started == true) && (retryTimer == null)) { retryTimer = setTimeout(connect, 20000); } } function connect() { if (obj.state != 0) return; setState(1); // 1 = Connecting obj.authCookie = null; if (retryTimer != null) { clearTimeout(retryTimer); retryTimer = null; } const data = new TextEncoder().encode('is_dotnet=0&is_javafree=0&is_standalone_client=0&is_javascript_kvm_client=1&is_javascript_rsc_client=1&login=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password) + '&action_login=Login'); const options = { hostname: hostname, port: port, rejectUnauthorized: false, checkServerIdentity: onCheckServerIdentity, path: '/auth.asp?client=javascript', // ?client=standalone method: 'POST', headers: { 'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': data.length } } const req = https.request(options, function (res) { if (obj.state == 0) return; if ((res.statusCode != 302) || (res.headers['set-cookie'] == null) || (res.headers['location'] == null)) { setState(0); return; } 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]; } } if (obj.authCookie == null) { setState(0); return; } res.on('data', function (d) { }) fetchInitialInformation(); }) req.on('error', function (error) { setState(0); }) req.write(data); req.end(); } function checkCookie() { if (obj.state != 2) return; const options = { hostname: hostname, port: port, rejectUnauthorized: false, checkServerIdentity: onCheckServerIdentity, path: '/cookiecheck.asp', method: 'GET', headers: { 'Content-Type': 'text/html; charset=UTF-8', 'Cookie': 'pp_session_id=' + obj.authCookie } } const req = https.request(options, function (res) { if (obj.state == 0) return; if (res.statusCode != 302) { setState(0); return; } 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]; } } } res.on('data', function (d) { }) }); req.on('error', function (error) { setState(0); }) req.end(); } function fetchInitialInformation() { fetch('/webs_cron.asp?_portsstatushash=&_devicesstatushash=&webs_job=sidebarupdates', null, null, function (server, tag, data) { const parsed = parseJsScript(data); for (var i in parsed['updateSidebarPanel']) { if (parsed['updateSidebarPanel'][i][0] == "cron_device") { obj.firmwareVersion = getSubString(parsed['updateSidebarPanel'][i][1], "Firmware: ", "<"); obj.deviceModel = getSubString(parsed['updateSidebarPanel'][i][1], "