1
0
Fork 0
mirror of https://github.com/Ylianst/MeshCentral.git synced 2025-03-09 15:40:18 +00:00

First version with working Intel AMT IDER from the server for LAN connections only.

This commit is contained in:
Ylian Saint-Hilaire 2019-04-24 18:04:45 -07:00
parent 04f7841b75
commit 26230ae863
2 changed files with 132 additions and 66 deletions

View file

@ -15,6 +15,7 @@
// Construct a MeshAgent object, called upon connection
module.exports.CreateAmtIderSession = function (parent, db, ws, req, args, domain, user) {
const fs = require('fs');
const path = require('path');
const common = parent.common;
const amtMeshRedirModule = require('./amt-redir-mesh.js');
@ -47,8 +48,7 @@ module.exports.CreateAmtIderSession = function (parent, db, ws, req, args, domai
// If the web socket is closed
ws.on('close', function (req) {
// Close the IDER session
obj.ider.Stop();
delete obj.ider;
if (obj.ider) { obj.ider.Stop(); delete obj.ider; }
});
// We are all set, start receiving data
@ -64,42 +64,85 @@ module.exports.CreateAmtIderSession = function (parent, db, ws, req, args, domai
switch (command.action) {
case 'ping': { try { ws.send(JSON.stringify({ action: 'pong' })); } catch (ex) { } break; }
case 'selector': {
var r = { action: 'selector', args: { html: 'Click ok to start IDER session.' }, buttons: 3 };
// TODO: Return a list of disk images for the user to select.
try { ws.send(JSON.stringify(r)); } catch (ex) { }
break;
}
case 'selectorResponse': {
//console.log('selectorResponse', command.args, req.query);
// Start IDER Session
case 'start': {
// Get the list of disk images
var domainx = 'domain' + ((domain.id == '') ? '' : ('-' + domain.id));
var useridx = user._id.split('/')[2];
var userPath = parent.parent.path.join(parent.parent.filespath, domainx, 'user-' + useridx);
command.args = {
floppyPath: 'C:\\Users\\Default.DESKTOP-M9I88C9\\Desktop\\AmtWebApp\\meshcentral-files\\domain\\user-admin\\msdos.img',
cdromPath: 'C:\\Users\\Default.DESKTOP-M9I88C9\\Desktop\\AmtWebApp\\meshcentral-files\\domain\\user-admin\\recovery.iso',
iderStart: 1,
tlsv1only: true
};
// Look for a list of disk images for the user to select.
if (fs.existsSync(userPath)) {
// Do something
readFsRec(userPath, function (err, results) {
var floppyImages = [], cdromImages = [];
for (var i in results) {
if (results[i].toLowerCase().endsWith('.img')) { floppyImages.push(results[i].substring(userPath.length + 1)); }
else if (results[i].toLowerCase().endsWith('.iso')) { cdromImages.push(results[i].substring(userPath.length + 1)); }
}
//console.log(floppyImages, cdromImages);
// Setup the IDER session
obj.ider = amtMeshRedirModule.CreateAmtRedirect(amtMeshIderModule.CreateAmtRemoteIder(parent, parent.parent), domain, user, parent, parent.parent);
obj.ider.onStateChanged = onIderStateChange;
obj.ider.m.iderStart = command.args.iderStart;
obj.ider.m.sectorStats = iderSectorStats;
obj.ider.tlsv1only = req.query.tlsv1only;
var xx, sel = true, html = "<div style='margin:10px 5px 10px 5px'>Select disk images & start type.</div>";
// Setup disk images
if (obj.ider.m.diskSetup(command.args.floppyPath, command.args.cdromPath) != 0) {
// Error with the disk images, unable to start IDER
obj.ider.onStateChanged = null;
obj.ider.m.sectorStats = null;
delete obj.ider;
obj.close();
break;
// Floppy image selection
xx = "<select style=width:240px id=xxFloppyImagesSelect><option value=''>None</option>";
for (var i in floppyImages) { xx += "<option value='" + encodeURIComponent(floppyImages[i]) + "'" + (sel?" selected":"") + ">" + floppyImages[i] + "</option>"; sel = false; }
xx += "</select>";
html += "<div style=margin:5px>" + addHtmlValue("Floppy Image", xx) + "</div>";
// CDROM image selection
sel = true;
xx = "<select style=width:240px id=xxCdromImagesSelect><option value=''>None</option>";
for (var i in cdromImages) { xx += "<option value='" + encodeURIComponent(cdromImages[i]) + "'" + (sel ? " selected" : "") + ">" + cdromImages[i] + "</option>"; sel = false; }
xx += "</select>";
html += "<div style=margin:5px>" + addHtmlValue("CDROM Image", xx) + "</div>";
// Start type
xx = "<select style=width:240px id=xxIderStartType><option value=0>On next boot<option value=1>Graceful<option value=2>Immediate</select>";
html += "<div style=margin:5px>" + addHtmlValue("Session Start", xx) + "</div>";
var js = "function iderServerCall() { return { ider: 1, floppyPath: Q('xxFloppyImagesSelect').value, cdromPath: Q('xxCdromImagesSelect').value, iderStart: Q('xxIderStartType').value }; }";
try { ws.send(JSON.stringify({ action: 'dialog', args: { html: html, js: js }, buttons: 3 })); } catch (ex) { }
});
} else {
// No user folder
try { ws.send(JSON.stringify({ action: 'dialog', args: { html: 'No disk images found on remote server. Upload .img and .iso files to server "My Files" folder to enable this feature.' }, buttons: 2 })); } catch (ex) { }
}
// Start the IDER session
obj.ider.Start(req.query.host, req.query.port, req.query.tls);
break;
}
case 'dialogResponse': {
if (command.args.ider == 1) { // Start IDER Session
// Decode and validate file paths
if ((command.args.floppyPath != null) && (typeof command.args.floppyPath != 'string')) { command.args.floppyPath = null; } else { command.args.floppyPath = decodeURIComponent(command.args.floppyPath); }
if ((command.args.cdromPath != null) && (typeof command.args.cdromPath != 'string')) { command.args.cdromPath = null; } else { command.args.cdromPath = decodeURIComponent(command.args.cdromPath); }
// TODO: Double check that "." or ".." are not used.
if ((command.args.floppyPath != null) && (command.args.floppyPath.indexOf("..") >= 0)) { delete command.args.floppyPath; }
if ((command.args.cdromPath != null) && (command.args.cdromPath.indexOf("..") >= 0)) { delete command.args.cdromPath; }
// Get the disk image paths
var domainx = 'domain' + ((domain.id == '') ? '' : ('-' + domain.id));
var useridx = user._id.split('/')[2];
var floppyPath = null, cdromPath = null;
if (command.args.floppyPath) { floppyPath = parent.parent.path.join(parent.parent.filespath, domainx, 'user-' + useridx, command.args.floppyPath); }
if (command.args.cdromPath) { cdromPath = parent.parent.path.join(parent.parent.filespath, domainx, 'user-' + useridx, command.args.cdromPath); }
// Setup the IDER session
obj.ider = amtMeshRedirModule.CreateAmtRedirect(amtMeshIderModule.CreateAmtRemoteIder(parent, parent.parent), domain, user, parent, parent.parent);
obj.ider.onStateChanged = onIderStateChange;
obj.ider.m.iderStart = command.args.iderStart;
obj.ider.m.sectorStats = iderSectorStats;
obj.ider.tlsv1only = req.query.tlsv1only;
// Setup disk images
var iderError = obj.ider.m.diskSetup(floppyPath, cdromPath);
// Error with the disk images, unable to start IDER
if (iderError != 0) { try { ws.send(JSON.stringify({ action: "error", code: iderError })); } catch (ex) { } break; }
// Start the IDER session
obj.ider.Start(req.query.host, req.query.port, req.query.tls);
}
break;
}
@ -128,5 +171,27 @@ module.exports.CreateAmtIderSession = function (parent, db, ws, req, args, domai
try { ws.send(JSON.stringify({ action: 'stats', mode: mode, dev: dev, total: total, start: start, len: len, toAmt: obj.ider.m.bytesToAmt, fromAmt: obj.ider.m.bytesFromAmt })); } catch (ex) { }
}
// Recursivly read all of the files in a fonder
function readFsRec(dir, func) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) return func(err);
var pending = list.length;
if (!pending) return func(null, results);
list.forEach(function (file) {
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
readFsRec(file, function (err, res) { results = results.concat(res); if (!--pending) func(null, results); });
} else {
results.push(file); if (!--pending) func(null, results);
}
});
});
});
};
function addHtmlValue(t, v) { return '<div style=height:20px><div style=float:right;width:240px;overflow:hidden><b title="' + v + '">' + v + '</b></div><div>' + t + '</div></div>'; }
return obj;
};