Added for agent debugging.

Ylian Saint-Hilaire committed Apr 24, 2020 at 13:17 UTC fc4f469e63e929d60a0ceed43146181f80ff8729
1 file changed +28 -4
meshagent.js
+28 -4
@@ -19,6 +19,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
19 const forge = parent.parent.certificateOperations.forge;
20 const common = parent.parent.common;
21 parent.agentStats.createMeshAgentCount++;
22 + parent.parent.debug('agent', 'New agent at ' + req.ip + ':' + ws._socket.remotePort);
23
24 var obj = {};
25 obj.domain = domain;
@@ -398,6 +399,8 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
399 parent.parent.updateProxyCertificates(false);
400 }
401 parent.agentStats.agentBadWebCertHashCount++;
402 + parent.parent.debug('agent', 'Agent bad web cert hash (Agent:' + (Buffer.from(msg.substring(2, 50), 'binary').toString('hex').substring(0, 10)) + ' != Server:' + (Buffer.from(getWebCertHash(domain), 'binary').toString('hex').substring(0, 10)) + ' or ' + (Buffer.from(getWebCertFullHash(domain), 'binary').toString('hex').substring(0, 10)) + '), holding connection (' + obj.remoteaddrport + ').');
403 + parent.parent.debug('agent', 'Agent reported web cert hash:' + (Buffer.from(msg.substring(2, 50), 'binary').toString('hex')) + '.');
404 console.log('Agent bad web cert hash (Agent:' + (Buffer.from(msg.substring(2, 50), 'binary').toString('hex').substring(0, 10)) + ' != Server:' + (Buffer.from(getWebCertHash(domain), 'binary').toString('hex').substring(0, 10)) + ' or ' + (Buffer.from(getWebCertFullHash(domain), 'binary').toString('hex').substring(0, 10)) + '), holding connection (' + obj.remoteaddrport + ').');
405 console.log('Agent reported web cert hash:' + (Buffer.from(msg.substring(2, 50), 'binary').toString('hex')) + '.');
406 return;
@@ -429,6 +432,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
432 if (obj.unauthsign != null) {
433 if (processAgentSignature(obj.unauthsign) == false) {
434 parent.agentStats.agentBadSignature1Count++;
435 + parent.parent.debug('agent', 'Agent connected with bad signature, holding connection (' + obj.remoteaddrport + ').');
436 console.log('Agent connected with bad signature, holding connection (' + obj.remoteaddrport + ').'); return;
437 } else { completeAgentConnection(); }
438 }
@@ -441,13 +445,14 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
445 // Decode the certificate
446 const certlen = common.ReadShort(msg, 2);
447 obj.unauth = {};
444 - try { obj.unauth.nodeid = Buffer.from(forge.pki.getPublicKeyFingerprint(forge.pki.certificateFromAsn1(forge.asn1.fromDer(msg.substring(4, 4 + certlen))).publicKey, { md: forge.md.sha384.create() }).data, 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$'); } catch (ex) { console.log(ex); return; }
448 + try { obj.unauth.nodeid = Buffer.from(forge.pki.getPublicKeyFingerprint(forge.pki.certificateFromAsn1(forge.asn1.fromDer(msg.substring(4, 4 + certlen))).publicKey, { md: forge.md.sha384.create() }).data, 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$'); } catch (ex) { console.log(ex); parent.parent.debug('agent', ex); return; }
449 obj.unauth.nodeCertPem = '-----BEGIN CERTIFICATE-----\r\n' + Buffer.from(msg.substring(4, 4 + certlen), 'binary').toString('base64') + '\r\n-----END CERTIFICATE-----';
450
451 // Check the agent signature if we can
452 if (obj.agentnonce == null) { obj.unauthsign = msg.substring(4 + certlen); } else {
453 if (processAgentSignature(msg.substring(4 + certlen)) == false) {
454 parent.agentStats.agentBadSignature2Count++;
455 + parent.parent.debug('agent', 'Agent connected with bad signature, holding connection (' + obj.remoteaddrport + ').');
456 console.log('Agent connected with bad signature, holding connection (' + obj.remoteaddrport + ').'); return;
457 }
458 }
@@ -494,7 +499,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
499 });
500
501 // If error, do nothing
497 - ws.on('error', function (err) { console.log('AGENT WSERR: ' + err); obj.close(0); });
502 + ws.on('error', function (err) { parent.parent.debug('agent', 'AGENT WSERR: ' + err); console.log('AGENT WSERR: ' + err); obj.close(0); });
503
504 // If the mesh agent web socket is closed, clean up.
505 ws.on('close', function (req) {
@@ -652,11 +657,16 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
657
658 if (holdConnection == true) {
659 // If we disconnect, the agent will just reconnect. We need to log this or tell agent to connect in a few hours.
660 + parent.parent.debug('agent', 'Agent connected with invalid domain/mesh, holding connection (' + obj.remoteaddrport + ', ' + obj.dbMeshKey + ').');
661 console.log('Agent connected with invalid domain/mesh, holding connection (' + obj.remoteaddrport + ', ' + obj.dbMeshKey + ').');
662 return;
663 }
664 }
659 - if (mesh.mtype != 2) { console.log('Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').'); return; } // If we disconnect, the agnet will just reconnect. We need to log this or tell agent to connect in a few hours.
665 + if (mesh.mtype != 2) { // If we disconnect, the agnet will just reconnect. We need to log this or tell agent to connect in a few hours.
666 + parent.parent.debug('agent', 'Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').');
667 + console.log('Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').');
668 + return;
669 + }
670 */
671
672 // Check that the node exists
@@ -699,6 +709,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
709 if (mesh == null) {
710 // If we disconnect, the agent will just reconnect. We need to log this or tell agent to connect in a few hours.
711 parent.agentStats.invalidDomainMesh2Count++;
712 + parent.parent.debug('agent', 'Agent connected with invalid domain/mesh, holding connection (' + obj.remoteaddrport + ', ' + obj.dbMeshKey + ').');
713 console.log('Agent connected with invalid domain/mesh, holding connection (' + obj.remoteaddrport + ', ' + obj.dbMeshKey + ').');
714 return;
715 }
@@ -707,6 +718,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
718 if (mesh.mtype != 2) {
719 // If we disconnect, the agent will just reconnect. We need to log this or tell agent to connect in a few hours.
720 parent.agentStats.invalidMeshType2Count++;
721 + parent.parent.debug('agent', 'Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').');
722 console.log('Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').');
723 return;
724 }
@@ -757,6 +769,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
769 if (mesh == null) {
770 // If we disconnect, the agent will just reconnect. We need to log this or tell agent to connect in a few hours.
771 parent.agentStats.invalidDomainMeshCount++;
772 + parent.parent.debug('agent', 'Agent connected with invalid domain/mesh, holding connection (' + obj.remoteaddrport + ', ' + obj.dbMeshKey + ').');
773 console.log('Agent connected with invalid domain/mesh, holding connection (' + obj.remoteaddrport + ', ' + obj.dbMeshKey + ').');
774 return;
775 }
@@ -765,6 +778,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
778 if (mesh.mtype != 2) {
779 // If we disconnect, the agent will just reconnect. We need to log this or tell agent to connect in a few hours.
780 parent.agentStats.invalidMeshTypeCount++;
781 + parent.parent.debug('agent', 'Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').');
782 console.log('Agent connected with invalid mesh type, holding connection (' + obj.remoteaddrport + ').');
783 return;
784 }
@@ -824,6 +838,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
838 // Check how many times this agent disconnected in the last few minutes.
839 const disconnectCount = parent.wsagentsDisconnections[obj.nodeid];
840 if (disconnectCount > 6) {
841 + parent.parent.debug('agent', 'Agent in big trouble: NodeId=' + obj.nodeid + ', IP=' + obj.remoteaddrport + ', Agent=' + obj.agentInfo.agentId + '.');
842 console.log('Agent in big trouble: NodeId=' + obj.nodeid + ', IP=' + obj.remoteaddrport + ', Agent=' + obj.agentInfo.agentId + '.');
843 // TODO: Log or do something to recover?
844 return;
@@ -835,6 +850,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
850 if (disconnectCount > 4) {
851 // Too many disconnections, this agent has issues. Just clear the core.
852 obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0));
853 + parent.parent.debug('agent', 'Agent in trouble: NodeId=' + obj.nodeid + ', IP=' + obj.remoteaddrport + ', Agent=' + obj.agentInfo.agentId + '.');
854 //console.log('Agent in trouble: NodeId=' + obj.nodeid + ', IP=' + obj.remoteaddrport + ', Agent=' + obj.agentInfo.agentId + '.');
855 // TODO: Log or do something to recover?
856 return;
@@ -1086,7 +1102,13 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1102 function processAgentData(msg) {
1103 var i, str = msg.toString('utf8'), command = null;
1104 if (str[0] == '{') {
1089 - try { command = JSON.parse(str); } catch (ex) { parent.agentStats.invalidJsonCount++; console.log('Unable to parse agent JSON (' + obj.remoteaddrport + '): ' + str, ex); return; } // If the command can't be parsed, ignore it.
1105 + try { command = JSON.parse(str); } catch (ex) {
1106 + // If the command can't be parsed, ignore it.
1107 + parent.agentStats.invalidJsonCount++;
1108 + parent.parent.debug('agent', 'Unable to parse agent JSON (' + obj.remoteaddrport + ')');
1109 + console.log('Unable to parse agent JSON (' + obj.remoteaddrport + '): ' + str, ex);
1110 + return;
1111 + }
1112 if (typeof command != 'object') { return; }
1113 switch (command.action) {
1114 case 'msg':
@@ -1307,12 +1329,14 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1329 try {
1330 parent.parent.pluginHandler.plugins[command.plugin].serveraction(command, obj, parent);
1331 } catch (e) {
1332 + parent.parent.debug('agent', 'Error loading plugin handler (' + e + ')');
1333 console.log('Error loading plugin handler (' + e + ')');
1334 }
1335 break;
1336 }
1337 default: {
1338 parent.agentStats.unknownAgentActionCount++;
1339 + parent.parent.debug('agent', 'Unknown agent action (' + obj.remoteaddrport + '): ' + command.action + '.');
1340 console.log('Unknown agent action (' + obj.remoteaddrport + '): ' + command.action + '.');
1341 break;
1342 }