amt-ider.js 4.4 KB

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