Added agent trouble detection on the server.
Ylian Saint-Hilaire committed
Jan 22, 2019 at 19:34 UTC
cd09f64b47488c79b9124c2d56280ab4d304be47
3 files changed
+30
-1
meshagent.js
+24
@@ -326,6 +326,13 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
326
if (obj.nodeid != null) {
327
//console.log('Agent disconnect ' + obj.nodeid + ' (' + obj.remoteaddrport + ') id=' + obj.agentInfo.agentId);
328
obj.parent.parent.debug(1, 'Agent disconnect ' + obj.nodeid + ' (' + obj.remoteaddrport + ') id=' + obj.agentInfo.agentId);
329
+
330
+ // Log the agent disconnection
331
+ if (obj.parent.wsagentsDisconnections[obj.nodeid] == null) {
332
+ obj.parent.wsagentsDisconnections[obj.nodeid] = 1;
333
+ } else {
334
+ obj.parent.wsagentsDisconnections[obj.nodeid] = ++obj.parent.wsagentsDisconnections[obj.nodeid];
335
+ }
336
}
337
obj.close(0);
338
});
@@ -411,9 +418,25 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
418
delete obj.pendingCompleteAgentConnection;
419
obj.authenticated = 2;
420
421
+ // Check how many times this agent disconnected in the last few minutes.
422
+ var disconnectCount = obj.parent.wsagentsDisconnections[obj.nodeid];
423
+ if (disconnectCount > 6) {
424
+ console.log('Agent in big trouble: NodeId=' + obj.nodeid + ', IP=' + obj.remoteaddrport + ', Agent=' + obj.agentInfo.agentId + '.');
425
+ // TODO: Log or do something to recover?
426
+ return;
427
+ }
428
+
429
// Command 4, inform mesh agent that it's authenticated.
430
obj.send(obj.common.ShortToStr(4));
431
432
+ if (disconnectCount > 4) {
433
+ // Too many disconnections, this agent has issues. Just clear the core.
434
+ obj.send(obj.common.ShortToStr(10) + obj.common.ShortToStr(0));
435
+ console.log('Agent in trouble: NodeId=' + obj.nodeid + ', IP=' + obj.remoteaddrport + ', Agent=' + obj.agentInfo.agentId + '.');
436
+ // TODO: Log or do something to recover?
437
+ return;
438
+ }
439
+
440
// Check if we need to make an native update check
441
obj.agentExeInfo = obj.parent.parent.meshAgentBinaries[obj.agentInfo.agentId];
442
const corename = obj.parent.parent.meshAgentsArchitectureNumbers[obj.agentInfo.agentId].core;
@@ -468,6 +491,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
491
}
492
});
493
}
494
+
495
});
496
}
497
package.json
+1
-1
@@ -1,6 +1,6 @@
1
{
2
"name": "meshcentral",
3
- "version": "0.2.6-t",
3
+ "version": "0.2.6-v",
4
"keywords": [
5
"Remote Management",
6
"Intel AMT",
webserver.js
+5
@@ -140,6 +140,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
140
141
// Main lists
142
obj.wsagents = {};
143
+ obj.wsagentsDisconnections = {};
144
+ obj.wsagentsDisconnectionsTimer = null;
145
obj.wssessions = {}; // UserId --> Array Of Sessions
146
obj.wssessions2 = {}; // "UserId + SessionRnd" --> Session (Note that the SessionId is the UserId + / + SessionRnd)
147
obj.wsPeerSessions = {}; // ServerId --> Array Of "UserId + SessionRnd"
@@ -2004,6 +2006,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2006
2007
// Indicates to ExpressJS that the public folder should be used to serve static files.
2008
obj.app.use(url, obj.express.static(obj.path.join(__dirname, 'public')));
2009
+
2010
+ // Start regular disconnection list flush every 2 minutes.
2011
+ obj.wsagentsDisconnectionsTimer = setInterval(function () { obj.wsagentsDisconnections = {}; }, 120000);
2012
}
2013
2014
// Start server on a free port