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

Ylian Saint-Hilaire committed Jan 27, 2019 at 12:23 UTC 635c8fb4414e60f6bbd5dba5856c96029eec4a42
6 files changed +44 -27
db.js
+1 -1
@@ -48,7 +48,7 @@ module.exports.CreateDB = function (parent) {
48 var datastoreOptions = { filename: obj.parent.getConfigFilePath('meshcentral.db'), autoload: true };
49
50 // If a DB encryption key is provided, perform database encryption
51 - if (typeof obj.parent.args.dbencryptkey == 'string') {
51 + if ((typeof obj.parent.args.dbencryptkey == 'string') && (obj.parent.args.dbencryptkey.length != 0)) {
52 // Hash the database password into a AES256 key and setup encryption and decryption.
53 obj.dbKey = obj.parent.crypto.createHash('sha384').update(obj.parent.args.dbencryptkey).digest("raw").slice(0, 32);
54 datastoreOptions.afterSerialization = function (plaintext) {
meshcentral.js
+20 -7
@@ -94,7 +94,7 @@ function CreateMeshCentralServer(config, args) {
94 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.
95
96 // Check for invalid arguments
97 - 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'];
97 + 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'];
98 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; } }
99 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; }
100 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) {
168 process.exit(); // User CTRL-C exit.
169 } else if (xprocess.xrestart == 3) {
170 // Server self-update exit
171 + var version = '';
172 + if (typeof obj.args.selfupdate == 'string') { version = '@' + obj.args.selfupdate; }
173 var child_process = require('child_process');
172 - var xxprocess = child_process.exec('npm install meshcentral', { maxBuffer: Infinity, cwd: obj.path.join(__dirname, '../..') }, function (error, stdout, stderr) { });
173 - xxprocess.data = '';
174 - xxprocess.stdout.on('data', function (data) { xxprocess.data += data; });
175 - xxprocess.stderr.on('data', function (data) { xxprocess.data += data; });
176 - xxprocess.on('close', function (code) { console.log('Update completed...'); setTimeout(function () { obj.launchChildServer(startLine); }, 1000); });
174 + console.log('npm install meshcentral' + version);
175 + //var xxprocess = child_process.exec('npm install meshcentral' + version, { maxBuffer: Infinity, cwd: obj.path.join(__dirname, '../..') }, function (error, stdout, stderr) { });
176 + //xxprocess.data = '';
177 + //xxprocess.stdout.on('data', function (data) { xxprocess.data += data; });
178 + //xxprocess.stderr.on('data', function (data) { xxprocess.data += data; });
179 + //xxprocess.on('close', function (code) { console.log('Update completed...'); setTimeout(function () { obj.launchChildServer(startLine); }, 1000); });
180 } else {
181 if (error != null) {
182 // This is an un-expected restart
@@ -195,6 +198,7 @@ function CreateMeshCentralServer(config, args) {
198 // Get current and latest MeshCentral server versions using NPM
199 obj.getLatestServerVersion = function (callback) {
200 if (callback == null) return;
201 + 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.
202 var child_process = require('child_process');
203 var xprocess = child_process.exec('npm view meshcentral dist-tags.latest', { maxBuffer: 512000 }, function (error, stdout, stderr) { });
204 xprocess.data = '';
@@ -218,6 +222,12 @@ function CreateMeshCentralServer(config, args) {
222 //var wincmd = require('node-windows');
223 //wincmd.list(function (svc) { console.log(svc); }, true);
224
225 + // If we are targetting a specific version, update now.
226 + if (typeof obj.args.selfupdate == 'string') {
227 + obj.args.selfupdate = obj.args.selfupdate.toLowerCase();
228 + if (obj.currentVer !== obj.args.selfupdate) { obj.performServerUpdate(); return; } // We are targetting a specific version, run self update now.
229 + }
230 +
231 // Write the server state
232 obj.updateServerState('state', 'starting');
233
@@ -561,8 +571,11 @@ function CreateMeshCentralServer(config, args) {
571
572 // Perform maintenance operations (called every hour)
573 obj.maintenanceActions = function () {
574 + // Check for self-update that targets a specific version
575 + if ((typeof obj.args.selfupdate == 'string') && (obj.currentVer === obj.args.selfupdate)) { obj.args.selfupdate = false; }
576 +
577 // Check if we need to perform server self-update
565 - if ((obj.args.selfupdate == true) && (obj.serverSelfWriteAllowed == true)) {
578 + if ((obj.args.selfupdate) && (obj.serverSelfWriteAllowed == true)) {
579 obj.db.getValueOfTheDay('performSelfUpdate', 1, function (performSelfUpdate) {
580 if (performSelfUpdate.value > 0) {
581 performSelfUpdate.value--;
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.2.6-x",
3 + "version": "0.2.6-y",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
public/scripts/meshcentral.js
+12 -9
@@ -10,35 +10,38 @@ var MeshServerCreateControl = function (domain) {
10 obj.connectstate = 0;
11 obj.pingTimer = null;
12
13 - obj.xxStateChange = function (newstate) {
13 + obj.xxStateChange = function (newstate, errCode) {
14 if (obj.State == newstate) return;
15 + var previousState = obj.State;
16 obj.State = newstate;
16 - if (obj.onStateChanged) obj.onStateChanged(obj, obj.State);
17 + if (obj.onStateChanged) obj.onStateChanged(obj, obj.State, previousState, errCode);
18 }
19
20 obj.Start = function () {
21 + if (obj.connectstate != 0) return;
22 obj.connectstate = 0;
23 obj.socket = new WebSocket(window.location.protocol.replace("http", "ws") + "//" + window.location.host + domain + "control.ashx");
22 - obj.socket.onopen = function () { obj.connectstate = 1; obj.xxStateChange(2); }
24 + obj.socket.onopen = function (e) { obj.connectstate = 1; }
25 obj.socket.onmessage = obj.xxOnMessage;
24 - obj.socket.onclose = function () { obj.Stop(); }
25 - obj.xxStateChange(1);
26 + obj.socket.onclose = function(e) { obj.Stop(e.code); }
27 + obj.xxStateChange(1, 0);
28 if (obj.pingTimer != null) { clearInterval(obj.pingTimer); }
29 obj.pingTimer = setInterval(function () { obj.send({ action: 'ping' }); }, 29000); // Ping the server every 29 seconds, stops corporate proxies from disconnecting.
30 }
31
30 - obj.Stop = function () {
32 + obj.Stop = function (errCode) {
33 obj.connectstate = 0;
34 if (obj.socket) { obj.socket.close(); delete obj.socket; }
35 if (obj.pingTimer != null) { clearInterval(obj.pingTimer); obj.pingTimer = null; }
34 - obj.xxStateChange(0);
36 + obj.xxStateChange(0, errCode);
37 }
38
39 obj.xxOnMessage = function (e) {
38 - // console.log('xxOnMessage', e.data);
40 + if (obj.State == 1) { obj.xxStateChange(2); }
41 + //console.log('xxOnMessage', e.data);
42 var message;
43 try { message = JSON.parse(e.data); } catch (e) { return; }
41 - if (message.action == 'pong') { return; }
44 + if ((typeof message != 'object') || (message.action == 'pong')) { return; }
45 if (obj.onMessage) obj.onMessage(obj, message);
46 };
47
views/default-mobile.handlebars
+5 -5
@@ -195,13 +195,13 @@
195 <strong><font style="font-size:12px;font-family:Arial,Helvetica,sans-serif">{{{title2}}}</font></strong>
196 </div>
197 </div>
198 - <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 />
198 + <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 />
199 </div>
200 <div id=page_content style="overflow-y:scroll;position:absolute;bottom:32px;top:50px;width:100%">
201 <div id=column_l style="width:100%;padding:0;position:absolute;bottom:0px;top:0px">
202 <div id=p0 style=display:none;width:100%;height:100%>
203 <div style="display:flex;align-items:center;width:100%;height:100%">
204 - <div id=p0message style=text-align:center;width:100%>Server disconnected, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
204 + <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>
205 </div>
206 </div>
207 <div id=p1 style=display:none;width:100%;height:100%>
@@ -617,13 +617,12 @@
617 applyDesktopSettings();
618 }
619
620 - function onStateChanged(server, state) {
620 + function onStateChanged(server, state, prevState, errorCode) {
621 if (state == 0) {
622 // Control web socket disconnected
623 setDialogMode(0); // Close any dialog boxes if present
624 go(0); // Go to disconnection panel
625 - setTimeout(serverPoll, 5000); // Start polling for the server
626 -
625 + if (prevState == 2) { setTimeout(serverPoll, 5000); } else { QH('p0span', 'Unable to connect web socket'); }
626 // Clean up here
627
628 } else if (state == 2) {
@@ -633,6 +632,7 @@
632 meshserver.send({ action: 'files' });
633 if (xxcurrentView < 2) { go(2); }
634 }
635 + QV('topMenuIcon', state == 2);
636 }
637
638 // Poll the server, if it responds, refresh the page.
views/default.handlebars
+5 -4
@@ -147,7 +147,7 @@
147 </div>
148 <div id="column_l">
149 <div id=p0 style=display:none>
150 - <div id=p0message style=margin:50px;text-align:center>Server disconnected, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
150 + <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>
151 </div>
152 <div id=p1 style=display:none>
153 <div style="float:right;display:none" id="devListToolbarViewIcons">
@@ -1025,7 +1025,7 @@
1025 function getNodeFromId(id) { for (var i in nodes) { if (nodes[i]._id == id) return nodes[i]; } return null; }
1026 function reload() { window.location.href = window.location.href; }
1027
1028 - function onStateChanged(server, state) {
1028 + function onStateChanged(server, state, prevState, errorCode) {
1029 if (state == 0) {
1030 // Control web socket disconnected
1031 setDialogMode(0); // Close any dialog boxes if present
@@ -1040,12 +1040,13 @@
1040 hideContextMenu(); // Hide the context menu if present
1041 QV('verifyEmailId2', false);
1042 QV('logoutControl', false);
1043 - setTimeout(serverPoll, 5000);
1043 + if (prevState == 2) { setTimeout(serverPoll, 5000); } else { QH('p0span', 'Unable to connect web socket'); }
1044 } else if (state == 2) {
1045 // Fetch list of meshes, nodes, files
1046 meshserver.send({ action: 'meshes' });
1047 meshserver.send({ action: 'nodes' });
1048 meshserver.send({ action: 'files' });
1049 + go(1);
1050 }
1051 }
1052
@@ -6493,7 +6494,7 @@
6494 var xxdialogFunc;
6495 var xxdialogButtons;
6496 var xxdialogTag;
6496 - var xxcurrentView = 0;
6497 + var xxcurrentView = -1;
6498
6499 // Display a dialog box
6500 // 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)