Improved web socket error handling, Selfupdate can now be set to specific version.

This commit is contained in:
Ylian Saint-Hilaire 2019-01-27 12:23:38 -08:00
parent 25ccec1d81
commit e6b05f2b4a
6 changed files with 44 additions and 27 deletions

2
db.js
View File

@ -48,7 +48,7 @@ module.exports.CreateDB = function (parent) {
var datastoreOptions = { filename: obj.parent.getConfigFilePath('meshcentral.db'), autoload: true };
// If a DB encryption key is provided, perform database encryption
if (typeof obj.parent.args.dbencryptkey == 'string') {
if ((typeof obj.parent.args.dbencryptkey == 'string') && (obj.parent.args.dbencryptkey.length != 0)) {
// Hash the database password into a AES256 key and setup encryption and decryption.
obj.dbKey = obj.parent.crypto.createHash('sha384').update(obj.parent.args.dbencryptkey).digest("raw").slice(0, 32);
datastoreOptions.afterSerialization = function (plaintext) {

View File

@ -94,7 +94,7 @@ function CreateMeshCentralServer(config, args) {
try { require('./pass').hash('test', function () { }); } catch (e) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not.
// Check for invalid arguments
var validArguments = ['_', 'notls', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'shownodes', 'showmeshes', 'showevents', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpsdebug', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'selfupdate', 'tlsoffload', 'userallowedip', 'swarmallowedip', 'fastcert', 'swarmport', 'swarmdebug', 'logintoken', 'logintokenkey', 'logintokengen', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore'];
var validArguments = ['_', 'notls', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'shownodes', 'showmeshes', 'showevents', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpsdebug', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'swarmallowedip', 'fastcert', 'swarmport', 'swarmdebug', 'logintoken', 'logintokenkey', 'logintokengen', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore'];
for (var arg in obj.args) { obj.args[arg.toLocaleLowerCase()] = obj.args[arg]; if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
if (obj.args.mongodb == true) { console.log('Must specify: --mongodb [connectionstring] \r\nSee https://docs.mongodb.com/manual/reference/connection-string/ for MongoDB connection string.'); return; }
for (i in obj.config.settings) { obj.args[i] = obj.config.settings[i]; } // Place all settings into arguments, arguments have already been placed into settings so arguments take precedence.
@ -168,12 +168,15 @@ function CreateMeshCentralServer(config, args) {
process.exit(); // User CTRL-C exit.
} else if (xprocess.xrestart == 3) {
// Server self-update exit
var version = '';
if (typeof obj.args.selfupdate == 'string') { version = '@' + obj.args.selfupdate; }
var child_process = require('child_process');
var xxprocess = child_process.exec('npm install meshcentral', { maxBuffer: Infinity, cwd: obj.path.join(__dirname, '../..') }, function (error, stdout, stderr) { });
xxprocess.data = '';
xxprocess.stdout.on('data', function (data) { xxprocess.data += data; });
xxprocess.stderr.on('data', function (data) { xxprocess.data += data; });
xxprocess.on('close', function (code) { console.log('Update completed...'); setTimeout(function () { obj.launchChildServer(startLine); }, 1000); });
console.log('npm install meshcentral' + version);
//var xxprocess = child_process.exec('npm install meshcentral' + version, { maxBuffer: Infinity, cwd: obj.path.join(__dirname, '../..') }, function (error, stdout, stderr) { });
//xxprocess.data = '';
//xxprocess.stdout.on('data', function (data) { xxprocess.data += data; });
//xxprocess.stderr.on('data', function (data) { xxprocess.data += data; });
//xxprocess.on('close', function (code) { console.log('Update completed...'); setTimeout(function () { obj.launchChildServer(startLine); }, 1000); });
} else {
if (error != null) {
// This is an un-expected restart
@ -195,6 +198,7 @@ function CreateMeshCentralServer(config, args) {
// Get current and latest MeshCentral server versions using NPM
obj.getLatestServerVersion = function (callback) {
if (callback == null) return;
if (typeof obj.args.selfupdate == 'string') { callback(obj.currentVer, obj.args.selfupdate); return; } // If we are targetting a specific version, return that one as current.
var child_process = require('child_process');
var xprocess = child_process.exec('npm view meshcentral dist-tags.latest', { maxBuffer: 512000 }, function (error, stdout, stderr) { });
xprocess.data = '';
@ -218,6 +222,12 @@ function CreateMeshCentralServer(config, args) {
//var wincmd = require('node-windows');
//wincmd.list(function (svc) { console.log(svc); }, true);
// If we are targetting a specific version, update now.
if (typeof obj.args.selfupdate == 'string') {
obj.args.selfupdate = obj.args.selfupdate.toLowerCase();
if (obj.currentVer !== obj.args.selfupdate) { obj.performServerUpdate(); return; } // We are targetting a specific version, run self update now.
}
// Write the server state
obj.updateServerState('state', 'starting');
@ -561,8 +571,11 @@ function CreateMeshCentralServer(config, args) {
// Perform maintenance operations (called every hour)
obj.maintenanceActions = function () {
// Check for self-update that targets a specific version
if ((typeof obj.args.selfupdate == 'string') && (obj.currentVer === obj.args.selfupdate)) { obj.args.selfupdate = false; }
// Check if we need to perform server self-update
if ((obj.args.selfupdate == true) && (obj.serverSelfWriteAllowed == true)) {
if ((obj.args.selfupdate) && (obj.serverSelfWriteAllowed == true)) {
obj.db.getValueOfTheDay('performSelfUpdate', 1, function (performSelfUpdate) {
if (performSelfUpdate.value > 0) {
performSelfUpdate.value--;

View File

@ -1,6 +1,6 @@
{
"name": "meshcentral",
"version": "0.2.6-x",
"version": "0.2.6-y",
"keywords": [
"Remote Management",
"Intel AMT",

View File

@ -10,35 +10,38 @@ var MeshServerCreateControl = function (domain) {
obj.connectstate = 0;
obj.pingTimer = null;
obj.xxStateChange = function (newstate) {
obj.xxStateChange = function (newstate, errCode) {
if (obj.State == newstate) return;
var previousState = obj.State;
obj.State = newstate;
if (obj.onStateChanged) obj.onStateChanged(obj, obj.State);
if (obj.onStateChanged) obj.onStateChanged(obj, obj.State, previousState, errCode);
}
obj.Start = function () {
if (obj.connectstate != 0) return;
obj.connectstate = 0;
obj.socket = new WebSocket(window.location.protocol.replace("http", "ws") + "//" + window.location.host + domain + "control.ashx");
obj.socket.onopen = function () { obj.connectstate = 1; obj.xxStateChange(2); }
obj.socket.onopen = function (e) { obj.connectstate = 1; }
obj.socket.onmessage = obj.xxOnMessage;
obj.socket.onclose = function () { obj.Stop(); }
obj.xxStateChange(1);
obj.socket.onclose = function(e) { obj.Stop(e.code); }
obj.xxStateChange(1, 0);
if (obj.pingTimer != null) { clearInterval(obj.pingTimer); }
obj.pingTimer = setInterval(function () { obj.send({ action: 'ping' }); }, 29000); // Ping the server every 29 seconds, stops corporate proxies from disconnecting.
}
obj.Stop = function () {
obj.Stop = function (errCode) {
obj.connectstate = 0;
if (obj.socket) { obj.socket.close(); delete obj.socket; }
if (obj.pingTimer != null) { clearInterval(obj.pingTimer); obj.pingTimer = null; }
obj.xxStateChange(0);
obj.xxStateChange(0, errCode);
}
obj.xxOnMessage = function (e) {
// console.log('xxOnMessage', e.data);
if (obj.State == 1) { obj.xxStateChange(2); }
//console.log('xxOnMessage', e.data);
var message;
try { message = JSON.parse(e.data); } catch (e) { return; }
if (message.action == 'pong') { return; }
if ((typeof message != 'object') || (message.action == 'pong')) { return; }
if (obj.onMessage) obj.onMessage(obj, message);
};

View File

@ -195,13 +195,13 @@
<strong><font style="font-size:12px;font-family:Arial,Helvetica,sans-serif">{{{title2}}}</font></strong>
</div>
</div>
<img class=noselect style="position:absolute;right:0;top:10px;bottom:50px;color:#c8c8c8;font-size:44px;margin-right:8px;cursor:pointer" onclick=topMenu() src="/images/3bars-30.png" width=30 height=30 />
<img id="topMenuIcon" class=noselect style="position:absolute;right:0;top:10px;bottom:50px;color:#c8c8c8;font-size:44px;margin-right:8px;cursor:pointer;display:none" onclick=topMenu() src="/images/3bars-30.png" width=30 height=30 />
</div>
<div id=page_content style="overflow-y:scroll;position:absolute;bottom:32px;top:50px;width:100%">
<div id=column_l style="width:100%;padding:0;position:absolute;bottom:0px;top:0px">
<div id=p0 style=display:none;width:100%;height:100%>
<div style="display:flex;align-items:center;width:100%;height:100%">
<div id=p0message style=text-align:center;width:100%>Server disconnected, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
<div id=p0message style=text-align:center;width:100%><span id="p0span">Server disconnected</span>, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
</div>
</div>
<div id=p1 style=display:none;width:100%;height:100%>
@ -617,13 +617,12 @@
applyDesktopSettings();
}
function onStateChanged(server, state) {
function onStateChanged(server, state, prevState, errorCode) {
if (state == 0) {
// Control web socket disconnected
setDialogMode(0); // Close any dialog boxes if present
go(0); // Go to disconnection panel
setTimeout(serverPoll, 5000); // Start polling for the server
if (prevState == 2) { setTimeout(serverPoll, 5000); } else { QH('p0span', 'Unable to connect web socket'); }
// Clean up here
} else if (state == 2) {
@ -633,6 +632,7 @@
meshserver.send({ action: 'files' });
if (xxcurrentView < 2) { go(2); }
}
QV('topMenuIcon', state == 2);
}
// Poll the server, if it responds, refresh the page.

View File

@ -147,7 +147,7 @@
</div>
<div id="column_l">
<div id=p0 style=display:none>
<div id=p0message style=margin:50px;text-align:center>Server disconnected, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
<div id=p0message style=margin:50px;text-align:center><span id=p0span>Server disconnected</span>, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
</div>
<div id=p1 style=display:none>
<div style="float:right;display:none" id="devListToolbarViewIcons">
@ -1025,7 +1025,7 @@
function getNodeFromId(id) { for (var i in nodes) { if (nodes[i]._id == id) return nodes[i]; } return null; }
function reload() { window.location.href = window.location.href; }
function onStateChanged(server, state) {
function onStateChanged(server, state, prevState, errorCode) {
if (state == 0) {
// Control web socket disconnected
setDialogMode(0); // Close any dialog boxes if present
@ -1040,12 +1040,13 @@
hideContextMenu(); // Hide the context menu if present
QV('verifyEmailId2', false);
QV('logoutControl', false);
setTimeout(serverPoll, 5000);
if (prevState == 2) { setTimeout(serverPoll, 5000); } else { QH('p0span', 'Unable to connect web socket'); }
} else if (state == 2) {
// Fetch list of meshes, nodes, files
meshserver.send({ action: 'meshes' });
meshserver.send({ action: 'nodes' });
meshserver.send({ action: 'files' });
go(1);
}
}
@ -6493,7 +6494,7 @@
var xxdialogFunc;
var xxdialogButtons;
var xxdialogTag;
var xxcurrentView = 0;
var xxcurrentView = -1;
// Display a dialog box
// Parameters: Dialog Mode (0 = none), Dialog Title, Buttons (1 = OK, 2 = Cancel, 3 = OK & Cancel), Call back function(0 = Cancel, 1 = OK), Dialog Content (Mode 2 only)