From e6b05f2b4a98dc2687b8a0d1dd616e81764ba4b9 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sun, 27 Jan 2019 12:23:38 -0800 Subject: [PATCH] Improved web socket error handling, Selfupdate can now be set to specific version. --- db.js | 2 +- meshcentral.js | 27 ++++++++++++++++++++------- package.json | 2 +- public/scripts/meshcentral.js | 21 ++++++++++++--------- views/default-mobile.handlebars | 10 +++++----- views/default.handlebars | 9 +++++---- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/db.js b/db.js index 81bdaf5d..2935ac3c 100644 --- a/db.js +++ b/db.js @@ -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) { diff --git a/meshcentral.js b/meshcentral.js index 728e80f5..8cbb816c 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -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--; diff --git a/package.json b/package.json index fdefaeb1..a03587d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meshcentral", - "version": "0.2.6-x", + "version": "0.2.6-y", "keywords": [ "Remote Management", "Intel AMT", diff --git a/public/scripts/meshcentral.js b/public/scripts/meshcentral.js index 1cf016fc..09832a7f 100644 --- a/public/scripts/meshcentral.js +++ b/public/scripts/meshcentral.js @@ -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); }; diff --git a/views/default-mobile.handlebars b/views/default-mobile.handlebars index 3178f3c7..e3111178 100644 --- a/views/default-mobile.handlebars +++ b/views/default-mobile.handlebars @@ -195,13 +195,13 @@ {{{title2}}} - +