Clean meshuser merge

This commit is contained in:
Noah Zalev 2021-07-08 10:21:13 -04:00
parent e9e44101f8
commit b05f65d819

View File

@ -1510,15 +1510,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (cmdargs.length == 0) break;
const cmd = cmdargs[0].toLowerCase();
cmdargs = parseArgs(cmdargs);
var cmdData = { result: '', command: command, cmdargs: cmdargs };
// Find the command in the lookup table and run it.
var cmdData = serverUserCommands[cmd], r = '';;
if (cmdData != null) {
try { r = cmdData[0](cmdargs); } catch (ex) { r = '' + ex; }
} else { r = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.'; }
var cmdTableEntry = serverUserCommands[cmd];
if (cmdTableEntry != null) { try { cmdTableEntry[0](cmdData); } catch (ex) { cmdData.result = '' + ex; }
} else { cmdData.result = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.'; }
// Send back the command result
if (r != '') { try { ws.send(JSON.stringify({ action: 'serverconsole', value: r, tag: command.tag })); } catch (ex) { } }
if (cmdData.result != '') { try { ws.send(JSON.stringify({ action: 'serverconsole', value: cmdData.result, tag: command.tag })); } catch (ex) { } }
break;
}
case 'msg':
@ -6179,7 +6179,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
'showpaths': [serverUserCommandShowPaths, ""],
'sms': [serverUserCommandSMS, ""],
'swarmstats': [serverUserCommandSwarmStats, ""],
'tasklimiter': [serverUserCommandTaslkLimiter, "Returns the internal status of the tasklimiter. This is a system used to smooth out work done by the server. It's used by, for example, agent updates so that not all agents are updated at the same time."],
'tasklimiter': [serverUserCommandTaskLimiter, "Returns the internal status of the tasklimiter. This is a system used to smooth out work done by the server. It's used by, for example, agent updates so that not all agents are updated at the same time."],
'trafficdelta': [serverUserCommandTrafficDelta, ""],
'trafficstats': [serverUserCommandTrafficStats, ""],
'updatecheck': [serverUserCommandUpdateCheck, ""],
@ -6190,99 +6190,101 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
'webstats': [serverUserCommandWebStats, ""]
}
function serverUserCommandHelp(cmdargs) {
var r, fin = '', f = '', availcommands = [];
function serverUserCommandHelp(cmdData) {
var fin = '', f = '', availcommands = [];
for (var i in serverUserCommands) { availcommands.push(i); }
availcommands = availcommands.sort();
while (availcommands.length > 0) { if (f.length > 80) { fin += (f + ',\r\n'); f = ''; } f += (((f != '') ? ', ' : ' ') + availcommands.shift()); }
if (f != '') { fin += f; }
if (cmdargs['_'].length == 0) {
r = 'Available commands: \r\n' + fin + '\r\nType help <command> for details.';
if (cmdData.cmdargs['_'].length == 0) {
cmdData.result = 'Available commands: \r\n' + fin + '\r\nType help <command> for details.';
} else {
var cmd2 = cmdargs['_'][0].toLowerCase();
var cmddata = serverUserCommands[cmd2];
if (cmddata) { if (cmddata[1] == '') { r = "No help available for this command."; } else { r = cmddata[1]; } } else { r = "This command does not exist."; }
var cmd2 = cmdData.cmdargs['_'][0].toLowerCase();
var cmdTableEntry = serverUserCommands[cmd2];
if (cmdTableEntry) {
if (cmdTableEntry[1] == '') {
cmdData.result = 'No help available for this command.'
} else {
cmdData.result = cmdTableEntry[1]; }
} else {
cmdData.result = "This command does not exist.";
}
}
return r;
}
function serverUserCommandCertExpire(cmdargs) {
var r = '';
function serverUserCommandCertExpire(cmdData) {
const now = Date.now();
for (var i in parent.webCertificateExpire) {
const domainName = (i == '') ? '[Default]' : i;
r += domainName + ', expires in ' + Math.floor((parent.webCertificateExpire[i] - now) / 86400000) + ' day(s)\r\n';
cmdData.result += domainName + ', expires in ' + Math.floor((parent.webCertificateExpire[i] - now) / 86400000) + ' day(s)\r\n';
}
return r;
}
function serverUserCommandWebPush(cmdargs) {
function serverUserCommandWebPush(cmdData) {
if (parent.parent.webpush == null) {
r = "Web push not supported.";
cmdData.result = "Web push not supported.";
} else {
if (cmdargs['_'].length != 1) {
r = "Usage: WebPush \"Message\"";
if (cmdData.cmdargs['_'].length != 1) {
cmdData.result = "Usage: WebPush \"Message\"";
} else {
const pushSubscription = { "endpoint": "https://updates.push.services.mozilla.com/wpush/v2/gAAAAABgIkO9hjXHWhMPiuk-ppNRw7r_pUZitddwCEK4ykdzeIxOIjFnYhIt_nr-qUca2mpZziwQsSEhYTUCiuYrhWnVDRweMtiUj16yJJq8V5jneaEaUYjEIe5jp3DOMNpoTm1aHgX74gCR8uTXSITcM97bNi-hRxcQ4f6Ie4WSAmoXpd89B_g", "keys": { "auth": "UB2sbLVK7ALnSHw5P1dahg", "p256dh": "BIoRbcNSxBuTjN39CCCUCHo1f4NxBJ1YDdu_k4MbPW_q3NK1_RufnydUzLPDp8ibBVItSI72-s48QJvOjQ_S8Ok" } }
parent.parent.webpush.sendNotification(pushSubscription, cmdargs['_'][0]).then(
function (value) { try { ws.send(JSON.stringify({ action: 'OK', value: r, tag: command.tag })); } catch (ex) { } },
function (error) { try { ws.send(JSON.stringify({ action: 'Error', value: r, tag: command.tag })); } catch (ex) { } }
parent.parent.webpush.sendNotification(pushSubscription, cmdData.cmdargs['_'][0]).then(
function (value) { try { ws.send(JSON.stringify({ action: 'OK', value: cmdData.result, tag: cmdData.command.tag })); } catch (ex) { } },
function (error) { try { ws.send(JSON.stringify({ action: 'Error', value: cmdData.result, tag: cmdData.command.tag })); } catch (ex) { } }
);
}
}
return r;
}
function serverUserCommandAmtManager(cmdargs) {
if (parent.parent.amtManager == null) { return 'Intel AMT Manager not active.'; }
return parent.parent.amtManager.getStatusString();
function serverUserCommandAmtManager(cmdData) {
if (parent.parent.amtManager == null) {
cmdData.result = 'Intel AMT Manager not active.';
} else {
cmdData.result = parent.parent.amtManager.getStatusString();
}
}
function serverUserCommandCertHashes(cmdargs) {
var r = 'AgentCertHash: ' + parent.agentCertificateHashHex;
for (var i in parent.webCertificateHashs) { r += '\r\nwebCertificateHash (' + i + '): ' + common.rstr2hex(parent.webCertificateHashs[i]); }
for (var i in parent.webCertificateFullHashs) { r += '\r\nwebCertificateFullHash (' + i + '): ' + common.rstr2hex(parent.webCertificateFullHashs[i]); }
r += '\r\ndefaultWebCertificateHash: ' + common.rstr2hex(parent.defaultWebCertificateHash);
r += '\r\ndefaultWebCertificateFullHash: ' + common.rstr2hex(parent.defaultWebCertificateFullHash);
return r;
function serverUserCommandCertHashes(cmdData) {
cmdData.result += 'AgentCertHash: ' + parent.agentCertificateHashHex;
for (var i in parent.webCertificateHashs) { cmdData.result += '\r\nwebCertificateHash (' + i + '): ' + common.rstr2hex(parent.webCertificateHashs[i]); }
for (var i in parent.webCertificateFullHashs) { cmdData.result += '\r\nwebCertificateFullHash (' + i + '): ' + common.rstr2hex(parent.webCertificateFullHashs[i]); }
cmdData.result += '\r\ndefaultWebCertificateHash: ' + common.rstr2hex(parent.defaultWebCertificateHash);
cmdData.result += '\r\ndefaultWebCertificateFullHash: ' + common.rstr2hex(parent.defaultWebCertificateFullHash);
}
function serverUserCommandAmtAcm(cmdargs) {
var r = '';
function serverUserCommandAmtAcm(cmdData) {
if ((domain.amtacmactivation == null) || (domain.amtacmactivation.acmmatch == null) || (domain.amtacmactivation.acmmatch.length == 0)) {
r = 'No Intel AMT activation certificates.';
cmdData.result = 'No Intel AMT activation certificates.';
} else {
if (domain.amtacmactivation.log != null) { r += '--- Activation Log ---\r\nFile : ' + domain.amtacmactivation.log + '\r\n'; }
if (domain.amtacmactivation.log != null) { cmdData.result += '--- Activation Log ---\r\nFile : ' + domain.amtacmactivation.log + '\r\n'; }
for (var i in domain.amtacmactivation.acmmatch) {
var acmcert = domain.amtacmactivation.acmmatch[i];
r += '--- Activation Certificate ' + (parseInt(i) + 1) + ' ---\r\nName : ' + acmcert.cn + '\r\nSHA1 : ' + acmcert.sha1 + '\r\nSHA256: ' + acmcert.sha256 + '\r\n';
cmdData.result += '--- Activation Certificate ' + (parseInt(i) + 1) + ' ---\r\nName : ' + acmcert.cn + '\r\nSHA1 : ' + acmcert.sha1 + '\r\nSHA256: ' + acmcert.sha256 + '\r\n';
}
}
return r;
}
function serverUserCommandHeapDump(cmdargs) {
var r = '';
function serverUserCommandHeapDump(cmdData) {
// Heapdump support, see example at:
// https://www.arbazsiddiqui.me/a-practical-guide-to-memory-leaks-in-nodejs/
if (parent.parent.config.settings.heapdump === true) {
var dumpFileName = parent.path.join(parent.parent.datapath, `heapDump-${Date.now()}.heapsnapshot`);
try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Generating dump file at: " + dumpFileName, tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Generating dump file at: " + dumpFileName, tag: cmdData.command.tag })); } catch (ex) { }
require('heapdump').writeSnapshot(dumpFileName, (err, filename) => {
try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Done.", tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Done.", tag: cmdData.command.tag })); } catch (ex) { }
});
} else {
r = "Heapdump not supported, add \"heapdump\":true to settings section of config.json.";
cmdData.result = "Heapdump not supported, add \"heapdump\":true to settings section of config.json.";
}
return r;
}
function serverUserCommandHeapDump2(cmdargs) {
var r = '';
function serverUserCommandHeapDump2(cmdData) {
var heapdump = null;
try { heapdump = require('heapdump'); } catch (ex) { }
if (heapdump == null) {
r = 'Heapdump module not installed, run "npm install heapdump".';
cmdData.result = 'Heapdump module not installed, run "npm install heapdump".';
} else {
heapdump.writeSnapshot(function (err, filename) {
if (err != null) {
@ -6292,224 +6294,212 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
}
});
}
break;
}
function serverUserCommandSMS(cmdargs) {
var r = '';
function serverUserCommandSMS(cmdData) {
if (parent.parent.smsserver == null) {
r = "No SMS gateway in use.";
cmdData.result = "No SMS gateway in use.";
} else {
if (cmdargs['_'].length != 2) {
r = "Usage: SMS \"PhoneNumber\" \"Message\".";
if (cmdData.cmdargs['_'].length != 2) {
cmdData.result = "Usage: SMS \"PhoneNumber\" \"Message\".";
} else {
parent.parent.smsserver.sendSMS(cmdargs['_'][0], cmdargs['_'][1], function (status, msg) {
parent.parent.smsserver.sendSMS(cmdData.cmdargs['_'][0], cmdData.cmdargs['_'][1], function (status, msg) {
if (typeof msg == 'string') {
try { ws.send(JSON.stringify({ action: 'serverconsole', value: status ? ('Success: ' + msg) : ('Failed: ' + msg), tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: status ? ('Success: ' + msg) : ('Failed: ' + msg), tag: cmdData.command.tag })); } catch (ex) { }
} else {
try { ws.send(JSON.stringify({ action: 'serverconsole', value: status ? 'Success' : 'Failed', tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: status ? 'Success' : 'Failed', tag: cmdData.command.tag })); } catch (ex) { }
}
});
}
}
return r;
}
function serverUserCommandEmail(cmdargs) {
var r = '';
function serverUserCommandEmail(cmdData) {
if (domain.mailserver == null) {
r = "No email service enabled.";
cmdData.result = "No email service enabled.";
} else {
if (cmdargs['_'].length != 3) {
r = "Usage: email \"user@sample.com\" \"Subject\" \"Message\".";
if (cmdData.cmdargs['_'].length != 3) {
cmdData.result = "Usage: email \"user@sample.com\" \"Subject\" \"Message\".";
} else {
domain.mailserver.sendMail(cmdargs['_'][0], cmdargs['_'][1], cmdargs['_'][2]);
r = "Done.";
domain.mailserver.sendMail(cmdData.cmdargs['_'][0], cmdData.cmdargs['_'][1], cmdData.cmdargs['_'][2]);
cmdData.result = "Done.";
}
}
return r;
}
function serverUserCommandLe(cmdargs) {
if (parent.parent.letsencrypt == null) { return "Let's Encrypt not in use."; }
return JSON.stringify(parent.parent.letsencrypt.getStats(), null, 4);
}
function serverUserCommandLeCheck(cmdargs) {
if (parent.parent.letsencrypt == null) { return "Let's Encrypt not in use."; }
return ["CertOK", "Request:NoCert", "Request:Expire", "Request:MissingNames"][parent.parent.letsencrypt.checkRenewCertificate()];
}
function serverUserCommandLeEvents(cmdargs) {
if (parent.parent.letsencrypt == null) { return "Let's Encrypt not in use."; }
return parent.parent.letsencrypt.events.join('\r\n');
}
function serverUserCommandBadLogins(cmdargs) {
var r = '';
if (parent.parent.config.settings.maxinvalidlogin == false) {
r = 'Bad login filter is disabled.';
function serverUserCommandLe(cmdData) {
if (parent.parent.letsencrypt == null) {
cmdData.result = "Let's Encrypt not in use.";
} else {
if (cmdargs['_'] == 'reset') {
cmdData.result = JSON.stringify(parent.parent.letsencrypt.getStats(), null, 4);
}
}
function serverUserCommandLeCheck(cmdData) {
if (parent.parent.letsencrypt == null) {
cmdData.result = "Let's Encrypt not in use.";
} else {
cmdData.result = ["CertOK", "Request:NoCert", "Request:Expire", "Request:MissingNames"][parent.parent.letsencrypt.checkRkenewCertificate()];
}
}
function serverUserCommandLeEvents(cmdData) {
if (parent.parent.letsencrypt == null) {
cmdData.result = "Let's Encrypt not in use.";
} else {
cmdData.result = parent.parent.letsencrypt.events.join('\r\n');
}
}
function serverUserCommandBadLogins(cmdData) {
if (parent.parent.config.settings.maxinvalidlogin == false) {
cmdData.result = 'Bad login filter is disabled.';
} else {
if (cmdData.cmdargs['_'] == 'reset') {
// Reset bad login table
parent.badLoginTable = {};
parent.badLoginTableLastClean = 0;
r = 'Done.'
} else if (cmdargs['_'] == '') {
cmdData.result = 'Done.'
} else if (cmdData.cmdargs['_'] == '') {
// Show current bad login table
if (typeof parent.parent.config.settings.maxinvalidlogin.coolofftime == 'number') {
r = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s), " + parent.parent.config.settings.maxinvalidlogin.coolofftime + " minute(s) cooloff.\r\n";
cmdData.result = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s), " + parent.parent.config.settings.maxinvalidlogin.coolofftime + " minute(s) cooloff.\r\n";
} else {
r = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s).\r\n";
cmdData.result = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s).\r\n";
}
var badLoginCount = 0;
parent.cleanBadLoginTable();
for (var i in parent.badLoginTable) {
badLoginCount++;
if (typeof parent.badLoginTable[i] == 'number') {
r += "Cooloff for " + Math.floor((parent.badLoginTable[i] - Date.now()) / 60000) + " minute(s)\r\n";
cmdData.result += "Cooloff for " + Math.floor((parent.badLoginTable[i] - Date.now()) / 60000) + " minute(s)\r\n";
} else {
if (parent.badLoginTable[i].length > 1) {
r += (i + ' - ' + parent.badLoginTable[i].length + " records\r\n");
cmdData.result += (i + ' - ' + parent.badLoginTable[i].length + " records\r\n");
} else {
r += (i + ' - ' + parent.badLoginTable[i].length + " record\r\n");
cmdData.result += (i + ' - ' + parent.badLoginTable[i].length + " record\r\n");
}
}
}
if (badLoginCount == 0) { r += 'No bad logins.'; }
if (badLoginCount == 0) { cmdData.result += 'No bad logins.'; }
} else {
r = 'Usage: badlogin [reset]';
cmdData.result = 'Usage: badlogin [reset]';
}
}
return r;
}
function serverUserCommandDispatchTable(cmdargs) {
var r = '';
for (var i in parent.parent.eventsDispatch) { r += (i + ', ' + parent.parent.eventsDispatch[i].length + '\r\n'); }
return r;
}
function serverUserCommandDupAgents(cmdargs) {
var r = '';
for (var i in parent.duplicateAgentsLog) { r += JSON.stringify(parent.duplicateAgentsLog[i]) + '\r\n'; }
if (r == '') { r = 'No duplicate agents in log.'; }
return r;
}
function serverUserCommandAgentStats(cmdargs) {
var r = '', stats = parent.getAgentStats();
for (var i in stats) {
if (typeof stats[i] == 'object') { r += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { r += (i + ': ' + stats[i] + '\r\n'); }
function serverUserCommandDispatchTable(cmdData) {
for (var i in parent.parent.eventsDispatch) {
cmdData.result += (i + ', ' + parent.parent.eventsDispatch[i].length + '\r\n');
}
return r;
}
function serverUserCommandAgentIssues(cmdargs) {
var r = '', stats = parent.getAgentIssues();
function serverUserCommandDupAgents(cmdData) {
for (var i in parent.duplicateAgentsLog) {
cmdData.result += JSON.stringify(parent.duplicateAgentsLog[i]) + '\r\n';
}
if (cmdData.result == '') { cmdData.result = 'No duplicate agents in log.'; }
}
function serverUserCommandAgentStats(cmdData) {
var stats = parent.getAgentStats();
for (var i in stats) {
if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
}
}
function serverUserCommandAgentIssues(cmdData) {
var stats = parent.getAgentIssues();
if (stats.length == 0) {
r = "No agent issues.";
cmdData.result = "No agent issues.";
} else {
for (var i in stats) { r += stats[i].join(', ') + '\r\n'; }
for (var i in stats) { cmdData.result += stats[i].join(', ') + '\r\n'; }
}
return r;
}
function serverUserCommandWebStats(cmdargs) {
var r = '', stats = parent.getStats();
function serverUserCommandWebStats(cmdData) {
var stats = parent.getStats();
for (var i in stats) {
if (typeof stats[i] == 'object') { r += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { r += (i + ': ' + stats[i] + '\r\n'); }
if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
}
return r;
}
function serverUserCommandTrafficStats(cmdargs) {
var r = '';
function serverUserCommandTrafficStats(cmdData) {
var stats = parent.getTrafficStats();
for (var i in stats) {
if (typeof stats[i] == 'object') { r += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { r += (i + ': ' + stats[i] + '\r\n'); }
if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
}
return r;
}
function serverUserCommandTrafficDelta(cmdargs) {
var r = '';
function serverUserCommandTrafficDelta(cmdData) {
const stats = parent.getTrafficDelta(obj.trafficStats);
obj.trafficStats = stats.current;
for (var i in stats.delta) {
if (typeof stats.delta[i] == 'object') { r += (i + ': ' + JSON.stringify(stats.delta[i]) + '\r\n'); } else { r += (i + ': ' + stats.delta[i] + '\r\n'); }
if (typeof stats.delta[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats.delta[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats.delta[i] + '\r\n'); }
}
return r;
}
function serverUserCommandWatchdog(cmdargs) {
var r = '';
function serverUserCommandWatchdog(cmdData) {
if (parent.parent.watchdog == null) {
r = 'Server watchdog not active.';
cmdData.result = 'Server watchdog not active.';
} else {
r = 'Server watchdog active.\r\n';
if (parent.parent.watchdogmaxtime != null) { r += 'Largest timeout was ' + parent.parent.watchdogmax + 'ms on ' + parent.parent.watchdogmaxtime + '\r\n'; }
for (var i in parent.parent.watchdogtable) { r += parent.parent.watchdogtable[i] + '\r\n'; }
cmdData.result = 'Server watchdog active.\r\n';
if (parent.parent.watchdogmaxtime != null) { cmdData.result += 'Largest timeout was ' + parent.parent.watchdogmax + 'ms on ' + parent.parent.watchdogmaxtime + '\r\n'; }
for (var i in parent.parent.watchdogtable) { cmdData.result += parent.parent.watchdogtable[i] + '\r\n'; }
}
return r;
}
function serverUserCommandAcceleratorsStats(cmdargs) {
var r = '', stats = parent.parent.certificateOperations.getAcceleratorStats();
function serverUserCommandAcceleratorsStats(cmdData) {
var stats = parent.parent.certificateOperations.getAcceleratorStats();
for (var i in stats) {
if (typeof stats[i] == 'object') { r += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { r += (i + ': ' + stats[i] + '\r\n'); }
if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
}
return r;
}
function serverUserCommandMpsStats(cmdargs) {
var r = '';
function serverUserCommandMpsStats(cmdData) {
if (parent.parent.mpsserver == null) {
r = 'MPS not enabled.';
cmdData.result = 'MPS not enabled.';
} else {
var stats = parent.parent.mpsserver.getStats();
for (var i in stats) {
if (typeof stats[i] == 'object') { r += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { r += (i + ': ' + stats[i] + '\r\n'); }
if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
}
}
return r;
}
// List all MPS connections and types.
function serverUserCommandMps(cmdargs) {
var r = '';
function serverUserCommandMps(cmdData) {
if (parent.parent.mpsserver == null) {
r = 'MPS not enabled.';
cmdData.result = 'MPS not enabled.';
} else {
const connectionTypes = ['CIRA', 'Relay', 'LMS'];
for (var nodeid in parent.parent.mpsserver.ciraConnections) {
r += nodeid;
cmdData.result += nodeid;
var connections = parent.parent.mpsserver.ciraConnections[nodeid];
for (var i in connections) { r += ', ' + connectionTypes[connections[i].tag.connType]; }
r += '\r\n';
for (var i in connections) { cmdData.result += ', ' + connectionTypes[connections[i].tag.connType]; }
cmdData.result += '\r\n';
}
if (r == '') { r = 'MPS has not connections.'; }
if (cmdData.result == '') { cmdData.result = 'MPS has not connections.'; }
}
return r;
}
function serverUserCommandDbStats(cmdargs) {
function serverUserCommandDbStats(cmdData) {
parent.parent.db.getStats(function (stats) {
var r2 = '';
for (var i in stats) { r2 += (i + ': ' + stats[i] + '\r\n'); }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: r2, tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: r2, tag: cmdData.command.tag })); } catch (ex) { }
});
}
function serverUserCommandDbCounters(cmdargs) {
try { ws.send(JSON.stringify({ action: 'serverconsole', value: JSON.stringify(parent.parent.db.dbCounters, null, 2), tag: command.tag })); } catch (ex) { }
function serverUserCommandDbCounters(cmdData) {
try { ws.send(JSON.stringify({ action: 'serverconsole', value: JSON.stringify(parent.parent.db.dbCounters, null, 2), tag: cmdData.command.tag })); } catch (ex) { }
}
function serverUserCommandServerUpdate(cmdargs) {
var r = 'Performing server update...';
function serverUserCommandServerUpdate(cmdData) {
cmdData.result = 'Performing server update...';
var version = null;
if (cmdargs['_'].length > 0) {
version = cmdargs['_'][0];
if (cmdData.cmdargs['_'].length > 0) {
version = cmdData.cmdargs['_'][0];
// This call is SLOW. We only want to validate version if we have to
if (version != 'stable' && version != 'latest') {
@ -6517,71 +6507,63 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
var versions = JSON.parse(data);
if (versions.includes(version)) {
if (parent.parent.performServerUpdate(version) == false) {
try {
ws.send(JSON.stringify({
action: 'serverconsole',
value: 'Server self-update not possible.'
}));
if (parent.parent.performServerUpdate(version) == false) {
try {
ws.send(JSON.stringify({ action: 'serverconsole',
value: 'Server self-update not possible.'}));
} catch (ex) { }
}
} else {
try {
ws.send(JSON.stringify({
action: 'serverconsole',
value: 'Invalid version. Aborting update'
}));
try {
ws.send(JSON.stringify({ action: 'serverconsole',
value: 'Invalid version. Aborting update'}));
} catch (ex) { }
}
});
} else {
if (parent.parent.performServerUpdate(version) == false) {
r = 'Server self-update not possible.';
if (parent.parent.performServerUpdate(version) == false) {
cmdData.result = 'Server self-update not possible.';
}
}
}
} else {
if (parent.parent.performServerUpdate(version) == false) {
r = 'Server self-update not possible.';
if (parent.parent.performServerUpdate(version) == false) {
cmdData.result = 'Server self-update not possible.';
}
}
return r;
}
function serverUserCommandPrint(cmdargs) {
console.log(cmdargs['_'][0]);
function serverUserCommandPrint(cmdData) {
console.log(cmdData.cmdargs['_'][0]);
}
function serverUserCommandAmtPasswords(cmdargs) {
var r = '';
function serverUserCommandAmtPasswords(cmdData) {
if (parent.parent.amtPasswords == null) {
r = "No Intel AMT password table."
cmdData.result = "No Intel AMT password table."
} else {
for (var i in parent.parent.amtPasswords) { r += (i + ' - ' + parent.parent.amtPasswords[i].join(', ') + '\r\n'); }
for (var i in parent.parent.amtPasswords) { cmdData.result += (i + ' - ' + parent.parent.amtPasswords[i].join(', ') + '\r\n'); }
}
return r;
}
function serverUserCommandUpdateCheck(cmdargs) {
function serverUserCommandUpdateCheck(cmdData) {
parent.parent.getServerTags(function (tags, error) {
var r2 = '';
if (error != null) { r2 += 'Exception: ' + error + '\r\n'; }
else { for (var i in tags) { r2 += i + ': ' + tags[i] + '\r\n'; } }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: r2, tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: r2, tag: cmdData.command.tag })); } catch (ex) { }
});
return "Checking server update...";
cmdData.result = "Checking server update...";
}
function serverUserCommandMaintenance(cmdargs) {
var arg = null, changed = false, r = '';
if ((cmdargs['_'] != null) && (cmdargs['_'][0] != null)) { arg = cmdargs['_'][0].toLowerCase(); }
function serverUserCommandMaintenance(cmdData) {
var arg = null, changed = false;
if ((cmdData.cmdargs['_'] != null) && (cmdData.cmdargs['_'][0] != null)) { arg = cmdData.cmdargs['_'][0].toLowerCase(); }
if (arg == 'enabled') { parent.parent.config.settings.maintenancemode = 1; changed = true; }
else if (arg == 'disabled') { delete parent.parent.config.settings.maintenancemode; changed = true; }
r = 'Maintenance mode: ' + ((parent.parent.config.settings.maintenancemode == null) ? 'Disabled' : 'Enabled');
if (changed == false) { r += '\r\nTo change type: maintenance [enabled|disabled]'; }
return r;
cmdData.result = 'Maintenance mode: ' + ((parent.parent.config.settings.maintenancemode == null) ? 'Disabled' : 'Enabled');
if (changed == false) { cmdData.result += '\r\nTo change type: maintenance [enabled|disabled]'; }
}
function serverUserCommandInfo(cmdargs) {
function serverUserCommandInfo(cmdData) {
var info = process.memoryUsage();
info.dbType = ['None', 'NeDB', 'MongoJS', 'MongoDB'][parent.db.databaseType];
try { if (parent.parent.multiServer != null) { info.serverId = parent.parent.multiServer.serverid; } } catch (ex) { }
@ -6598,50 +6580,47 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
try { info.database = ["Unknown", "NeDB", "MongoJS", "MongoDB", "MariaDB", "MySQL"][parent.parent.db.databaseType]; } catch (ex) { }
try { info.productionMode = ((process.env.NODE_ENV != null) && (process.env.NODE_ENV == 'production')); } catch (ex) { }
try { info.allDevGroupManagers = parent.parent.config.settings.managealldevicegroups; } catch (ex) { }
return JSON.stringify(info, null, 4);
cmdData.result = JSON.stringify(info, null, 4);
}
function serverUserCommandNodeConfig(cmdargs) {
return JSON.stringify(process.config, null, 4);
function serverUserCommandNodeConfig(cmdData) {
cmdData.result = JSON.stringify(process.config, null, 4);
}
function serverUserCommandVersions(cmdargs) {
return JSON.stringify(process.versions, null, 4);
function serverUserCommandVersions(cmdData) {
cmdData.result = JSON.stringify(process.versions, null, 4);
}
function serverUserCommandArgs(cmdargs) {
return cmd + ': ' + JSON.stringify(cmdargs);
function serverUserCommandArgs(cmdData) {
cmdData.result = 'args: ' + JSON.stringify(cmdData.cmdargs);
}
function serverUserCommandUserSessions(cmdargs) {
var r = '';
function serverUserCommandUserSessions(cmdData) {
var userSessionCount = 0;
var filter = null;
var arg = cmdargs['_'][0];
var arg = cmdData.cmdargs['_'][0];
if (typeof arg == 'string') { if (arg.indexOf('/') >= 0) { filter = arg; } else { filter = ('user/' + domain.id + '/' + arg); } }
for (var i in parent.wssessions) {
if ((filter == null) || (filter == i)) {
userSessionCount++;
r += (i + ', ' + parent.wssessions[i].length + ' session' + ((parent.wssessions[i].length > 1) ? 's' : '') + '.\r\n');
cmdData.result += (i + ', ' + parent.wssessions[i].length + ' session' + ((parent.wssessions[i].length > 1) ? 's' : '') + '.\r\n');
for (var j in parent.wssessions[i]) {
r += ' ' + parent.wssessions[i][j].clientIp + ' --> ' + parent.wssessions[i][j].sessionId + '\r\n';
cmdData.result += ' ' + parent.wssessions[i][j].clientIp + ' --> ' + parent.wssessions[i][j].sessionId + '\r\n';
}
}
}
if (userSessionCount == 0) { r = 'None.'; }
break;
if (userSessionCount == 0) { cmdData.result = 'None.'; }
}
function serverUserCommandCloseUserSessions(cmdargs) {
var r = '';
function serverUserCommandCloseUserSessions(cmdData) {
var userSessionCount = 0;
var filter = null;
var arg = cmdargs['_'][0];
var arg = cmdData.cmdargs['_'][0];
if (typeof arg == 'string') { if (arg.indexOf('/') >= 0) { filter = arg; } else { filter = ('user/' + domain.id + '/' + arg); } }
if (filter == null) {
r += "Usage: closeusersessions <username>";
cmdData.result += "Usage: closeusersessions <username>";
} else {
r += "Closing user sessions for: " + filter + '\r\n';
cmdData.result += "Closing user sessions for: " + filter + '\r\n';
for (var i in parent.wssessions) {
if (filter == i) {
userSessionCount++;
@ -6651,137 +6630,119 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
}
}
}
if (userSessionCount < 2) { r += 'Disconnected ' + userSessionCount + ' session.'; } else { r += 'Disconnected ' + userSessionCount + ' sessions.'; };
if (userSessionCount < 2) { cmdData.result += 'Disconnected ' + userSessionCount + ' session.'; } else { cmdData.result += 'Disconnected ' + userSessionCount + ' sessions.'; };
}
break;
}
function serverUserCommandResetServer(cmdargs) {
var r = '';
function serverUserCommandResetServer(cmdData) {
console.log("Server restart...");
process.exit(0);
break;
}
function serverUserCommandTaslkLimiter(cmdargs) {
var r = '';
function serverUserCommandTaskLimiter(cmdData) {
if (parent.parent.taskLimiter != null) {
//var obj = { maxTasks: maxTasks, maxTaskTime: (maxTaskTime * 1000), nextTaskId: 0, currentCount: 0, current: {}, pending: [[], [], []], timer: null };
const tl = parent.parent.taskLimiter;
r += 'MaxTasks: ' + tl.maxTasks + ', NextTaskId: ' + tl.nextTaskId + '\r\n';
r += 'MaxTaskTime: ' + (tl.maxTaskTime / 1000) + ' seconds, Timer: ' + (tl.timer != null) + '\r\n';
cmdData.result += 'MaxTasks: ' + tl.maxTasks + ', NextTaskId: ' + tl.nextTaskId + '\r\n';
cmdData.result += 'MaxTaskTime: ' + (tl.maxTaskTime / 1000) + ' seconds, Timer: ' + (tl.timer != null) + '\r\n';
var c = [];
for (var i in tl.current) { c.push(i); }
r += 'Current (' + tl.currentCount + '): [' + c.join(', ') + ']\r\n';
r += 'Pending (High/Med/Low): ' + tl.pending[0].length + ', ' + tl.pending[1].length + ', ' + tl.pending[2].length + '\r\n';
cmdData.result += 'Current (' + tl.currentCount + '): [' + c.join(', ') + ']\r\n';
cmdData.result += 'Pending (High/Med/Low): ' + tl.pending[0].length + ', ' + tl.pending[1].length + ', ' + tl.pending[2].length + '\r\n';
}
break;
}
function serverUserCommandSetMaxTasks(cmdargs) {
var r = '';
if ((cmdargs["_"].length != 1) || (parseInt(cmdargs["_"][0]) < 1) || (parseInt(cmdargs["_"][0]) > 1000)) {
r = 'Usage: setmaxtasks [1 to 1000]';
function serverUserCommandSetMaxTasks(cmdData) {
if ((cmdData.cmdargs["_"].length != 1) || (parseInt(cmdData.cmdargs["_"][0]) < 1) || (parseInt(cmdData.cmdargs["_"][0]) > 1000)) {
cmdData.result = 'Usage: setmaxtasks [1 to 1000]';
} else {
parent.parent.taskLimiter.maxTasks = parseInt(cmdargs["_"][0]);
r = 'MaxTasks set to ' + parent.parent.taskLimiter.maxTasks + '.';
parent.parent.taskLimiter.maxTasks = parseInt(cmdData.cmdargs["_"][0]);
cmdData.result = 'MaxTasks set to ' + parent.parent.taskLimiter.maxTasks + '.';
}
break;
}
function serverUserCommandCores(cmdargs) {
var r = '';
if (parent.parent.defaultMeshCores != null) { for (var i in parent.parent.defaultMeshCores) { r += i + ': ' + parent.parent.defaultMeshCores[i].length + ' bytes\r\n'; } }
break;
function serverUserCommandCores(cmdData) {
if (parent.parent.defaultMeshCores != null) {
for (var i in parent.parent.defaultMeshCores) {
cmdData.result += i + ': ' + parent.parent.defaultMeshCores[i].length + ' bytes\r\n';
}
}
}
function serverUserCommandShowPaths(cmdargs) {
var r = '';
r = 'Parent: ' + parent.parent.parentpath + '\r\n';
r += 'Data: ' + parent.parent.datapath + '\r\n';
r += 'Files: ' + parent.parent.filespath + '\r\n';
r += 'Backup: ' + parent.parent.backuppath + '\r\n';
r += 'Record: ' + parent.parent.recordpath + '\r\n';
r += 'WebPublic: ' + parent.parent.webPublicPath + '\r\n';
r += 'WebViews: ' + parent.parent.webViewsPath + '\r\n';
if (parent.parent.webViewsOverridePath) { r += 'XWebPublic: ' + parent.parent.webViewsOverridePath + '\r\n'; }
if (parent.parent.webViewsOverridePath) { r += 'XWebViews: ' + parent.parent.webPublicOverridePath + '\r\n'; }
break;
function serverUserCommandShowPaths(cmdData) {
cmdData.result = 'Parent: ' + parent.parent.parentpath + '\r\n';
cmdData.result += 'Data: ' + parent.parent.datapath + '\r\n';
cmdData.result += 'Files: ' + parent.parent.filespath + '\r\n';
cmdData.result += 'Backup: ' + parent.parent.backuppath + '\r\n';
cmdData.result += 'Record: ' + parent.parent.recordpath + '\r\n';
cmdData.result += 'WebPublic: ' + parent.parent.webPublicPath + '\r\n';
cmdData.result += 'WebViews: ' + parent.parent.webViewsPath + '\r\n';
if (parent.parent.webViewsOverridePath) { cmdData.result += 'XWebPublic: ' + parent.parent.webViewsOverridePath + '\r\n'; }
if (parent.parent.webViewsOverridePath) { cmdData.result += 'XWebViews: ' + parent.parent.webPublicOverridePath + '\r\n'; }
}
function serverUserCommandMigrationAgents(cmdargs) {
var r = '';
function serverUserCommandMigrationAgents(cmdData) {
if (parent.parent.swarmserver == null) {
r = 'Swarm server not running.';
cmdData.result = 'Swarm server not running.';
} else {
for (var i in parent.parent.swarmserver.migrationAgents) {
var arch = parent.parent.swarmserver.migrationAgents[i];
for (var j in arch) { var agent = arch[j]; r += 'Arch ' + agent.arch + ', Ver ' + agent.ver + ', Size ' + ((agent.binary == null) ? 0 : agent.binary.length) + '<br />'; }
for (var j in arch) { var agent = arch[j]; cmdData.result += 'Arch ' + agent.arch + ', Ver ' + agent.ver + ', Size ' + ((agent.binary == null) ? 0 : agent.binary.length) + '<br />'; }
}
}
break;
}
function serverUserCommandSwarmStats(cmdargs) {
var r = '';
function serverUserCommandSwarmStats(cmdData) {
if (parent.parent.swarmserver == null) {
r = 'Swarm server not running.';
cmdData.result = 'Swarm server not running.';
} else {
for (var i in parent.parent.swarmserver.stats) {
if (typeof parent.parent.swarmserver.stats[i] == 'object') {
r += i + ': ' + JSON.stringify(parent.parent.swarmserver.stats[i]) + '\r\n';
cmdData.result += i + ': ' + JSON.stringify(parent.parent.swarmserver.stats[i]) + '\r\n';
} else {
r += i + ': ' + parent.parent.swarmserver.stats[i] + '\r\n';
cmdData.result += i + ': ' + parent.parent.swarmserver.stats[i] + '\r\n';
}
}
}
break;
}
function serverUserCommandRelays(cmdargs) {
var r = '';
function serverUserCommandRelays(cmdData) {
for (var i in parent.wsrelays) {
r += 'id: ' + i + ', ' + ((parent.wsrelays[i].state == 2) ? 'connected' : 'pending');
cmdData.result += 'id: ' + i + ', ' + ((parent.wsrelays[i].state == 2) ? 'connected' : 'pending');
if (parent.wsrelays[i].peer1 != null) {
r += ', ' + cleanRemoteAddr(parent.wsrelays[i].peer1.req.clientIp);
if (parent.wsrelays[i].peer1.user) { r += ' (User:' + parent.wsrelays[i].peer1.user.name + ')' }
cmdData.result += ', ' + cleanRemoteAddr(parent.wsrelays[i].peer1.req.clientIp);
if (parent.wsrelays[i].peer1.user) { cmdData.result += ' (User:' + parent.wsrelays[i].peer1.user.name + ')' }
}
if (parent.wsrelays[i].peer2 != null) {
r += ' to ' + cleanRemoteAddr(parent.wsrelays[i].peer2.req.clientIp);
if (parent.wsrelays[i].peer2.user) { r += ' (User:' + parent.wsrelays[i].peer2.user.name + ')' }
cmdData.result += ' to ' + cleanRemoteAddr(parent.wsrelays[i].peer2.req.clientIp);
if (parent.wsrelays[i].peer2.user) { cmdData.result += ' (User:' + parent.wsrelays[i].peer2.user.name + ')' }
}
r += '\r\n';
cmdData.result += '\r\n';
}
if (r == '') { r = 'No relays.'; }
break;
if (cmdData.result == '') { cmdData.result = 'No relays.'; }
}
function serverUserCommandAutoBackup(cmdargs) {
var r = '';
function serverUserCommandAutoBackup(cmdData) {
var backupResult = parent.db.performBackup(function (msg) {
try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: command.tag })); } catch (ex) { }
try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { }
});
if (backupResult == 0) { r = 'Starting auto-backup...'; } else { r = 'Backup alreay in progress.'; }
break;
if (backupResult == 0) { cmdData.result = 'Starting auto-backup...'; } else { cmdData.result = 'Backup alreay in progress.'; }
}
function serverUserCommandBackupConfig(cmdargs) {
var r = '';
r = parent.db.getBackupConfig();
break;
function serverUserCommandBackupConfig(cmdData) {
cmdData.result = parent.db.getBackupConfig();
}
function serverUserCommandFirebase(cmdargs) {
var r = '';
function serverUserCommandFirebase(cmdData) {
if (parent.parent.firebase == null) {
r = "Firebase push messaging not supported";
cmdData.result = "Firebase push messaging not supported";
} else {
r = JSON.stringify(parent.parent.firebase.stats, null, 2);
cmdData.result = JSON.stringify(parent.parent.firebase.stats, null, 2);
}
break;
}
function csvClean(s) { return '\"' + s.split('\"').join('').split(',').join('').split('\r').join('').split('\n').join('') + '\"'; }
// Return detailed information about an array of nodeid's