Improved MeshCmd support
Ylian Saint-Hilaire committed
Dec 15, 2017 at 13:43 UTC
1aa0e80f53e8e48e0081b6d39e7a0c54fa6539aa
5 files changed
+72
-33
agents/meshcmd.js
+17
-13
@@ -68,14 +68,17 @@ function run(argv) {
68
//console.log('addedModules = ' + JSON.stringify(addedModules));
69
var actionpath = 'meshaction.txt';
70
if (args.actionfile != null) { actionpath = args.actionfile; }
71
+ var actions = ['ROUTE', 'AMTLMS', 'AMTLOADWEBAPP', 'AMTLOADSMALLWEBAPP', 'AMTLOADLARGEWEBAPP', 'AMTCLEARWEBAPP', 'AMTSTORAGESTATE', 'MEINFO', 'MEVERSIONS', 'MEHASHES'];
72
73
// Load the action file
74
var actionfile = null;
75
try { actionfile = fs.readFileSync(actionpath); } catch (e) { }
76
if ((actionpath != 'meshaction.txt') && (actionfile == null)) { console.log('Unable to load \"' + actionpath + '\". Create this file or specify the location using --actionfile [filename].'); exit(1); return; }
77
if (actionfile != null) { try { settings = JSON.parse(actionfile); } catch (e) { console.log(actionpath, e); exit(1); return; } } else { if (argv.length >= 2) { settings = { action: argv[1] } } }
78
+ if (settings == null) { settings = {}; }
79
80
// Set the arguments
81
+ if ((typeof args.action) == 'string') { settings.action = args.action; }
82
if ((typeof args.localport) == 'string') { settings.localport = parseInt(args.localport); }
83
if ((typeof args.remotenodeid) == 'string') { settings.remoteNodeId = args.remotenodeid; }
84
if ((typeof args.username) == 'string') { settings.username = args.username; }
@@ -88,9 +91,10 @@ function run(argv) {
91
if ((typeof args.serverhttpshash) == 'string') { settings.serverHttpsHash = args.serverhttpshash; }
92
if ((typeof args.remoteport) == 'string') { settings.remotePort = parseInt(args.remoteport); }
93
if ((typeof args.debug) == 'string') { settings.debugLevel = parseInt(args.debug); }
94
+ if ((argv.length > 1) && (actions.indexOf(argv[1].toUpperCase()) >= 0)) { settings.action = argv[1]; }
95
96
// Validate meshaction.txt
93
- if ((settings == null) || (settings.action == null)) { console.log('No action specified, valid actions are ROUTE, LOADWEBAPP, CLEARWEBAPP, STORAGESTATE, MEINFO, MEVERSIONS, MEHASHES, LMS.'); exit(1); return; }
97
+ if (settings.action == null) { console.log('No action specified, valid actions are ' + actions.join(', ') + '.'); exit(1); return; }
98
settings.action = settings.action.toLowerCase();
99
debug(1, "Settings: " + JSON.stringify(settings));
100
if (settings.action == 'route') { // MeshCentral Router
@@ -103,7 +107,7 @@ function run(argv) {
107
if ((settings.remotePort == null) || (typeof settings.remotePort != 'number') || (settings.remotePort < 0) || (settings.remotePort > 65535)) { console.log('No or invalid \"remotePort\" specified, use --remoteport [remoteport].'); exit(1); return; }
108
if (settings.serverUrl != null) { startRouter(); } else { discoverMeshServer(); } // Start MeshCentral Router
109
}
106
- else if ((settings.action == 'loadwebapp') || (settings.action == 'loadsmallwebapp') || (settings.action == 'loadlargewebapp') || (settings.action == 'clearwebapp') || (settings.action == 'storagestate')) { // Intel AMT Web Application Actions
110
+ else if ((settings.action == 'amtloadwebapp') || (settings.action == 'amtloadsmallwebapp') || (settings.action == 'amtloadlargewebapp') || (settings.action == 'amtclearwebapp') || (settings.action == 'amtstoragestate')) { // Intel AMT Web Application Actions
111
if ((settings.password == null) || (typeof settings.password != 'string') || (settings.password == '')) { console.log('No or invalid \"password\" specified, use --password [password].'); exit(1); return; }
112
if ((settings.hostname == null) || (typeof settings.hostname != 'string') || (settings.hostname == '')) { settings.hostname = '127.0.0.1'; }
113
if ((settings.username == null) || (typeof settings.username != 'string') || (settings.username == '')) { settings.username = 'admin'; }
@@ -112,13 +116,13 @@ function run(argv) {
116
debug(1, "Settings: " + JSON.stringify(settings));
117
digest = require('http-digest').create(settings.username, settings.password);
118
digest.http = require('http');
115
- if (settings.action == 'storagestate') {
119
+ if (settings.action == 'amtstoragestate') {
120
getAmtStorage(function (statusCode, data) { if (statusCode == 200) { console.log("Storage State: " + JSON.stringify(data, null, 2)); exit(); return; } else { console.log("Unable to read storage state."); exit(); return; } });
121
} else {
118
- if (settings.action == 'loadwebapp') { settings.webapp = Medium_IntelAmtWebApp; }
119
- else if (settings.action == 'loadsmallwebapp') { settings.webapp = Small_IntelAmtWebApp; }
120
- else if (settings.action == 'loadlargewebapp') { settings.webapp = Large_IntelAmtWebApp; }
121
- else if (settings.action == 'clearwebapp') { settings.webapp = null; }
122
+ if (settings.action == 'amtloadwebapp') { settings.webapp = Medium_IntelAmtWebApp; }
123
+ else if (settings.action == 'amtloadsmallwebapp') { settings.webapp = Small_IntelAmtWebApp; }
124
+ else if (settings.action == 'amtloadlargewebapp') { settings.webapp = Large_IntelAmtWebApp; }
125
+ else if (settings.action == 'amtclearwebapp') { settings.webapp = null; }
126
nextStepStorageUpload();
127
}
128
} else if ((settings.action == 'meversion') || (settings.action == 'meversions') || (settings.action == 'mever')) {
@@ -141,7 +145,7 @@ function run(argv) {
145
exitOnCount = handles.length;
146
for (var i = 0; i < handles.length; ++i) {
147
this.getCertHashEntry(handles[i], function (result) {
144
- console.log('----------\r\n' + result.name + ', (' + (result.isDefault ? 'Default' : '') + (result.isActive ? ', Active' : ', Disabled') + ')\r\n' + result.hashAlgorithm + ': ' + result.certificateHash);
148
+ console.log('----------\r\n' + result.name + ', (' + (result.isDefault ? 'Default' : '') + (result.isActive ? ', Active' : ', Disabled') + ')\r\n' + result.hashAlgorithmStr + ': ' + result.certificateHash);
149
if (--exitOnCount == 0) { console.log('----------'); exit(1); }
150
});
151
}
@@ -162,15 +166,15 @@ function run(argv) {
166
this.getDnsSuffix(function (result) {
167
mestate.dns = result;
168
var str = 'Intel AMT v' + mestate.ver;
165
- if (mestate.ProvisioningState == 'PRE') { str += ', pre-provisioning state'; }
166
- else if (mestate.ProvisioningState == 'IN') { str += ', in-provisioning state'; }
167
- else if (mestate.ProvisioningState == 'POST') { if (mestate.ProvisioningMode == 'ENTERPRISE') { str += ', activated in ' + mestate.controlmode.consoleMode; } else { str += ', activated in ' + mestate.ProvisioningMode; } }
168
- if (mestate.ehbc == true) { str += ', EHBC enabled'; }
169
+ if (mestate.ProvisioningState.stateStr == 'PRE') { str += ', pre-provisioning state'; }
170
+ else if (mestate.ProvisioningState.stateStr == 'IN') { str += ', in-provisioning state'; }
171
+ else if (mestate.ProvisioningState.stateStr == 'POST') { if (mestate.ProvisioningMode.modeStr == 'ENTERPRISE') { str += ', activated in ' + ["none", "client control mode", "admin control mode", "remote assistance mode"][mestate.controlmode.controlMode]; } else { str += ', activated in ' + mestate.ProvisioningMode.modeStr; } }
172
+ if (mestate.ehbc.EHBC == true) { str += ', EHBC enabled'; }
173
console.log(str + '.');
174
exit(1);
175
});
176
});
173
- } else if (settings.action == 'lms') {
177
+ } else if (settings.action == 'amtlms') {
178
startLms();
179
} else {
180
console.log('Invalid \"action\" specified.'); exit(1); return;
agents/meshcore.js
+1
-1
@@ -684,7 +684,7 @@ function createMeshCore(agent) {
684
var response = null;
685
switch (cmd) {
686
case 'help': { // Displays available commands
687
- response = 'xxxxxxAvailable commands: help, info, args, print, type, dbget, dbset, dbcompact, parseuri, httpget, wslist, wsconnect, wssend, wsclose, notify, ls, amt, netinfo, location, power, wakeonlan, scanwifi.';
687
+ response = 'Available commands: help, info, args, print, type, dbget, dbset, dbcompact, parseuri, httpget, wslist, wsconnect, wssend, wsclose, notify, ls, amt, netinfo, location, power, wakeonlan, scanwifi.';
688
break;
689
}
690
case 'notify': { // Send a notification message to the mesh
package.json
+1
-1
@@ -1,6 +1,6 @@
1
{
2
"name": "meshcentral",
3
- "version": "0.1.1-c",
3
+ "version": "0.1.1-f",
4
"keywords": [
5
"Remote Management",
6
"Intel AMT",
views/default.handlebars
+25
-7
@@ -754,6 +754,7 @@
754
hideContextMenu(); // Hide the context menu if present
755
QV('verifyEmailId2', false);
756
QV('logoutControl', false);
757
+ serverPoll();
758
} else if (state == 2) {
759
// Fetch list of meshes, nodes, files
760
meshserver.Send({ action: 'meshes' });
@@ -762,6 +763,18 @@
763
}
764
}
765
766
+ // Poll the server, if it responds, refresh the page.
767
+ function serverPoll() {
768
+ xdr = null;
769
+ try { xdr = new XDomainRequest(); } catch (e) { }
770
+ if (!xdr) xdr = new XMLHttpRequest();
771
+ xdr.open("HEAD", window.location.href);
772
+ xdr.timeout = 15000;
773
+ xdr.onload = function () { reload(); };
774
+ xdr.onerror = xdr.ontimeout = function () { console.log('error'); setTimeout(serverPoll, 10000); };
775
+ xdr.send();
776
+ }
777
+
778
// Return true if this browser supports clickonce
779
function detectClickOnce() {
780
for (var i in window.navigator.mimeTypes) { if (window.navigator.mimeTypes[i].type == "application/x-ms-application") { return true; } }
@@ -1390,7 +1403,10 @@
1403
r += '</tr></table><div style=height:1px></div>'; // This height of 1 div fixes a problem in Linux firefox browsers
1404
1405
// Add a "Add Mesh" option
1393
- if ((view < 3) && (sort == 0) && (meshcount > 0)) { r += '<div style=border-top-style:solid;border-top-width:1px;border-top-color:#DDDDDD;cursor:pointer;font-size:10px title="Create a new group of computers."><a onclick=account_createMesh() style=cursor:pointer>Add Mesh</a></div>'; }
1406
+ r += '<div style=border-top-style:solid;border-top-width:1px;border-top-color:#DDDDDD;cursor:pointer;font-size:10px>';
1407
+ if ((view < 3) && (sort == 0) && (meshcount > 0)) { r += '<a onclick=account_createMesh() title="Create a new group of computers." style=cursor:pointer>Add Mesh</a> '; }
1408
+ r += '<a onclick=p10showMeshCmdDialog(0) style=cursor:pointer title="Download MeshCmd, a command line tool that performs many functions.">MeshCmd</a></div>';
1409
+ r += '</div>';
1410
1411
QH('xdevices', r);
1412
deviceHeaderSet();
@@ -2481,7 +2497,7 @@
2497
if (mesh.mtype == 2) x += '<a style=cursor:pointer onclick=p10showNodeNetInfoDialog("' + node._id + '") title="Show device network interface information">Interfaces</a> ';
2498
if (xxmap != null) x += '<a style=cursor:pointer onclick=p10showNodeLocationDialog("' + node._id + '") title="Show device locations information">Location</a> ';
2499
2484
- if (mesh.mtype == 2) x += '<a style=cursor:pointer onclick=p10showRouterDialog("' + node._id + '") title="Traffic router used to connect to a device thru this server.">Router</a> ';
2500
+ if (mesh.mtype == 2) x += '<a style=cursor:pointer onclick=p10showMeshCmdDialog(1,"' + node._id + '") title="Traffic router used to connect to a device thru this server.">Router</a> ';
2501
2502
// RDP link, show this link only of the remote machine is Windows.
2503
if (((connectivity & 1) != 0) && (clickOnce == true) && (mesh.mtype == 2) && ((meshrights & 8) != 0)) {
@@ -2758,7 +2774,7 @@
2774
}
2775
2776
// Show router dialog
2761
- function p10showRouterDialog() {
2777
+ function p10showMeshCmdDialog(mode, nodeid) {
2778
if (xxdialogMode) return;
2779
var y = "<select id=aginsSelect onclick=meshCmdOsClick() style=width:236px>";
2780
y += "<option value=3>Windows (32bit)</option>";
@@ -2769,13 +2785,15 @@
2785
y += "</select>";
2786
2787
var x = "";
2772
- x += "<div>Download \"meshcmd\" with an action file to route traffic thru this server to this device. Make sure to edit meshaction.txt and add your account password or make any changes needed.<br /><br />";
2788
+ if (mode == 0) { x += '<div>MeshCmd is a command line tool that performs lots of different operations. The action file can optionally be downloaded and edited to provide server information and credentials.<br /><br />'; }
2789
+ if (mode == 1) { x += '<div>Download "meshcmd" with an action file to route traffic thru this server to this device. Make sure to edit meshaction.txt and add your account password or make any changes needed.<br /><br />'; }
2790
x += addHtmlValue('Operating System', y);
2774
- x += addHtmlValue('Mesh Command', '<a id="meshcmddownloadid" href="meshagents?meshcmd=3" target="_blank"></a>');
2775
- x += addHtmlValue('Action File', '<a href="meshagents?meshaction=route&nodeid=' + currentNode._id + '" target="_blank">MeshAction (.txt)</a>');
2791
+ x += addHtmlValue('MeshCmd', '<a id=meshcmddownloadid href="meshagents?meshcmd=3" target="_blank"></a>');
2792
+ if (mode == 0) { x += addHtmlValue('Action File', '<a href="meshagents?meshaction=generic" target="_blank">MeshAction (.txt)</a>'); }
2793
+ if (mode == 1) { x += addHtmlValue('Action File', '<a href="meshagents?meshaction=route&nodeid=' + nodeid + '" target="_blank">MeshAction (.txt)</a>'); }
2794
x += "</div>";
2795
2778
- setDialogMode(2, "Network Router", 1, null, x, currentNode._id);
2796
+ setDialogMode(2, ["Download MeshCmd","Network Router"][mode], 9, null, x);
2797
meshCmdOsClick();
2798
}
2799
webserver.js
+28
-11
@@ -1354,18 +1354,33 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
1354
});
1355
} else if (req.query.meshaction != null) {
1356
var domain = checkUserIpAddress(req, res);
1357
+ if (domain == null) { res.sendStatus(404); return; }
1358
var user = obj.users[req.session.userid];
1358
- if (domain == null || req.query.nodeid == null) { res.sendStatus(404); return; }
1359
- obj.db.Get(req.query.nodeid, function (err, nodes) {
1360
- if (nodes.length != 1) { res.sendStatus(401); return; }
1361
- var node = nodes[0];
1362
- // Create the meshaction.txt file for meshcmd.exe
1359
+ if ((req.query.meshaction == 'route') && (req.query.nodeid != null)) {
1360
+ obj.db.Get(req.query.nodeid, function (err, nodes) {
1361
+ if (nodes.length != 1) { res.sendStatus(401); return; }
1362
+ var node = nodes[0];
1363
+ // Create the meshaction.txt file for meshcmd.exe
1364
+ var meshaction = {
1365
+ action: req.query.meshaction,
1366
+ localPort: 1234,
1367
+ remoteName: node.name,
1368
+ remoteNodeId: node._id,
1369
+ remotePort: 3389,
1370
+ username: '',
1371
+ password: '',
1372
+ serverId: obj.agentCertificateHashHex.toUpperCase(), // SHA384 of server HTTPS public key
1373
+ serverHttpsHash: new Buffer(obj.webCertificateHash, 'binary').toString('hex').toUpperCase(), // SHA384 of server HTTPS certificate
1374
+ debugLevel: 0
1375
+ }
1376
+ if (user != null) { meshaction.username = user.name; }
1377
+ if (obj.args.lanonly != true) { meshaction.serverUrl = ((obj.args.notls == true) ? 'ws://' : 'wss://') + obj.certificates.CommonName + ':' + obj.args.port + '/' + ((domain.id == '') ? '' : ('/' + domain.id)) + 'meshrelay.ashx'; }
1378
+ res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'text/plain', 'Content-Disposition': 'attachment; filename=meshaction.txt' });
1379
+ res.send(JSON.stringify(meshaction, null, ' '));
1380
+ });
1381
+ }
1382
+ else if (req.query.meshaction == 'generic') {
1383
var meshaction = {
1364
- action: req.query.meshaction,
1365
- localPort: 1234,
1366
- remoteName: node.name,
1367
- remoteNodeId: node._id,
1368
- remotePort: 3389,
1384
username: '',
1385
password: '',
1386
serverId: obj.agentCertificateHashHex.toUpperCase(), // SHA384 of server HTTPS public key
@@ -1376,7 +1391,9 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
1391
if (obj.args.lanonly != true) { meshaction.serverUrl = ((obj.args.notls == true) ? 'ws://' : 'wss://') + obj.certificates.CommonName + ':' + obj.args.port + '/' + ((domain.id == '') ? '' : ('/' + domain.id)) + 'meshrelay.ashx'; }
1392
res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'text/plain', 'Content-Disposition': 'attachment; filename=meshaction.txt' });
1393
res.send(JSON.stringify(meshaction, null, ' '));
1379
- });
1394
+ } else {
1395
+ res.sendStatus(401);
1396
+ }
1397
} else {
1398
// Send a list of available mesh agents
1399
var response = '<html><head><title>Mesh Agents</title><style>table,th,td { border:1px solid black;border-collapse:collapse;padding:3px; }</style></head><body><table>';