| 1 | /** |
| 2 | * @description MeshCentral Server IDER handler |
| 3 | * @author Ylian Saint-Hilaire & Bryan Roe |
| 4 | * @copyright Intel Corporation 2018-2022 |
| 5 | * @license Apache-2.0 |
| 6 | * @version v0.0.1 |
| 7 | */ |
| 8 | |
| 9 | /*jslint node: true */ |
| 10 | /*jshint node: true */ |
| 11 | /*jshint strict:false */ |
| 12 | /*jshint -W097 */ |
| 13 | /*jshint esversion: 6 */ |
| 14 | "use strict"; |
| 15 | |
| 16 | // Construct a MeshAgent object, called upon connection |
| 17 | module.exports.CreateAmtIderSession = function (parent, db, ws, req, args, domain, user) { |
| 18 | const fs = require('fs'); |
| 19 | const path = require('path'); |
| 20 | const common = parent.common; |
| 21 | const amtMeshRedirModule = require('./amt/amt-redir-mesh.js'); |
| 22 | const amtMeshIderModule = require('./amt/amt-ider-module.js'); |
| 23 | |
| 24 | console.log('New Server IDER session from ' + user.name); |
| 25 | |
| 26 | var obj = {}; |
| 27 | obj.user = user; |
| 28 | obj.domain = domain; |
| 29 | obj.ider = null; |
| 30 | |
| 31 | // Send a message to the user |
| 32 | //obj.send = function (data) { try { if (typeof data == 'string') { ws.send(Buffer.from(data, 'binary')); } else { ws.send(data); } } catch (e) { } } |
| 33 | |
| 34 | // Disconnect this user |
| 35 | obj.close = function (arg) { |
| 36 | if ((arg == 1) || (arg == null)) { try { ws.close(); parent.parent.debug(1, 'Soft disconnect'); } catch (e) { console.log(e); } } // Soft close, close the websocket |
| 37 | if (arg == 2) { try { ws._socket._parent.end(); parent.parent.debug(1, 'Hard disconnect'); } catch (e) { console.log(e); } } // Hard close, close the TCP socket |
| 38 | }; |
| 39 | |
| 40 | try { |
| 41 | |
| 42 | // Check if the user is logged in |
| 43 | if (user == null) { try { ws.close(); } catch (e) { } return; } |
| 44 | |
| 45 | // When data is received from the web socket |
| 46 | ws.on('message', processWebSocketData); |
| 47 | |
| 48 | // If error, do nothing |
| 49 | ws.on('error', function (err) { console.log(err); obj.close(0); }); |
| 50 | |
| 51 | // If the web socket is closed |
| 52 | ws.on('close', function (req) { obj.close(0); }); |
| 53 | |
| 54 | // We are all set, start receiving data |
| 55 | ws._socket.resume(); |
| 56 | |
| 57 | } catch (e) { console.log(e); } |
| 58 | |
| 59 | // Process incoming web socket data from the browser |
| 60 | function processWebSocketData(msg) { |
| 61 | var command, i = 0, mesh = null, meshid = null, nodeid = null, meshlinks = null, change = 0; |
| 62 | try { command = JSON.parse(msg.toString('utf8')); } catch (e) { return; } |
| 63 | if (common.validateString(command.action, 3, 32) == false) return; // Action must be a string between 3 and 32 chars |
| 64 | |
| 65 | switch (command.action) { |
| 66 | case 'ping': { try { ws.send(JSON.stringify({ action: 'pong' })); } catch (ex) { } break; } |
| 67 | case 'selector': { |
| 68 | var r = { action: 'selector', args: { html: 'Click ok to start IDER session.' }, buttons: 3 }; |
| 69 | // TODO: Return a list of disk images for the user to select. |
| 70 | try { ws.send(JSON.stringify(r)); } catch (ex) { } |
| 71 | break; |
| 72 | } |
| 73 | case 'selectorResponse': { |
| 74 | console.log('selectorResponse', command.args, req.query); |
| 75 | |
| 76 | // TODO: Start IDER Session |
| 77 | // req.query = { host: 'node//KV6AZh3KoEzr71IaM40KqpBXQCn0qysZrMYlCOcvivNkV2$zfP2MXBE4IizBn1Bw', port: '16994', tls: '0', serverauth: '1', tls1only: '1' } |
| 78 | |
| 79 | command.args = { |
| 80 | floppyPath: '', |
| 81 | cdromPath: '', |
| 82 | iderStart: 1, |
| 83 | tlsv1only: true |
| 84 | }; |
| 85 | |
| 86 | obj.ider = amtMeshRedirModule.CreateAmtRedirect(amtMeshIderModule.CreateAmtRemoteIder(), domain, user, parent, parent.parent); |
| 87 | obj.ider.onStateChanged = onIderStateChange; |
| 88 | obj.ider.m.debug = true; |
| 89 | obj.ider.m.floppy = command.args.floppyPath; |
| 90 | obj.ider.m.cdrom = command.args.cdromPath; |
| 91 | obj.ider.m.iderStart = command.args.iderStart; |
| 92 | obj.ider.m.sectorStats = iderSectorStats; |
| 93 | obj.ider.tlsv1only = req.query.tlsv1only; |
| 94 | obj.ider.Start(req.query.host, req.query.port, req.query.tls); |
| 95 | |
| 96 | break; |
| 97 | } |
| 98 | default: { |
| 99 | // Unknown user action |
| 100 | console.log('Unknown IDER action from user ' + user.name + ': ' + command.action + '.'); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | function onIderStateChange(sender, state) { |
| 107 | console.log('onIderStateChange', state); |
| 108 | } |
| 109 | |
| 110 | function iderSectorStats(mode, dev, total, start, len) { |
| 111 | console.log('iderSectorStats', mode, dev, total, start, len); |
| 112 | } |
| 113 | |
| 114 | return obj; |
| 115 | }; |