MPS server crash fix, #3027

Ylian Saint-Hilaire committed Aug 12, 2021 at 13:05 UTC b07a6820441de66b39a78df554a736befa6199ff
1 file changed +12 -12
mpsserver.js
+12 -12
@@ -356,7 +356,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
356
357 // Setup the CIRA keep alive timer
358 socket.setTimeout(MAX_IDLE);
359 - socket.on('timeout', () => { ciraTimeoutCount++; parent.debug('mps', "CIRA timeout, disconnecting."); try { socket.end(); } catch (e) { } });
359 + socket.on('timeout', () => { ciraTimeoutCount++; parent.debug('mps', "CIRA timeout, disconnecting."); obj.close(socket); });
360
361 socket.addListener('close', function () {
362 // Traffic accounting
@@ -390,7 +390,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
390 // Detect if this is an HTTPS request, if it is, return a simple answer and disconnect. This is useful for debugging access to the MPS port.
391 if (socket.tag.first == true) {
392 if (socket.tag.accumulator.length < 5) return;
393 - //if (!socket.tag.clientCert.subject) { console.log("MPS Connection, no client cert: " + socket.remoteAddress); socket.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nMeshCentral2 MPS server.\r\nNo client certificate given.'); socket.end(); return; }
393 + //if (!socket.tag.clientCert.subject) { console.log("MPS Connection, no client cert: " + socket.remoteAddress); socket.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nMeshCentral2 MPS server.\r\nNo client certificate given.'); obj.close(socket); return; }
394 if ((socket.tag.accumulator.substring(0, 4) == 'GET ') || (socket.tag.accumulator.substring(0, 5) == 'HEAD ')) {
395 if (args.mpsdebug) { console.log("MPS Connection, HTTP request detected: " + socket.remoteAddress); }
396 socket.removeAllListeners('data');
@@ -441,7 +441,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
441
442 // Check the incoming domain
443 var domain = obj.parent.config.domains[domainid];
444 - if (domain == null) { console.log('CIRA connection for invalid domain. meshid: ' + meshid); socket.end(); return; }
444 + if (domain == null) { console.log('CIRA connection for invalid domain. meshid: ' + meshid); obj.close(socket); return; }
445
446 socket.tag.domain = domain;
447 socket.tag.domainid = domainid;
@@ -458,7 +458,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
458 if (mesh == null) {
459 unknownTlsMeshIdCount++;
460 console.log('ERROR: Intel AMT CIRA connected with unknown groupid: ' + socket.tag.meshid);
461 - socket.end();
461 + obj.close(socket);
462 return;
463 } else if (mesh.mtype == 1) {
464 // Check if we already have too many devices for this domain
@@ -468,7 +468,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
468 // Too many devices in this domain.
469 maxDomainDevicesReached++;
470 console.log('Too many devices on this domain to accept the CIRA connection. meshid: ' + socket.tag.meshid);
471 - socket.end();
471 + obj.close(socket);
472 } else {
473 // Attempts reverse DNS loopup on the device IP address
474 require('dns').reverse(socket.remoteAddr, function (err, hostnames) {
@@ -513,7 +513,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
513 // New CIRA connection for unknown node, disconnect.
514 unknownTlsNodeCount++;
515 console.log('CIRA connection for unknown node with incorrect group type. meshid: ' + socket.tag.meshid);
516 - socket.end();
516 + obj.close(socket);
517 return;
518 }
519 } else {
@@ -536,7 +536,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
536 // Parse all of the APF data we can
537 var l = 0;
538 do { l = ProcessCommand(socket); if (l > 0) { socket.tag.accumulator = socket.tag.accumulator.substring(l); } } while (l > 0);
539 - if (l < 0) { socket.end(); }
539 + if (l < 0) { obj.close(socket); }
540 } catch (e) {
541 console.log(e);
542 }
@@ -675,7 +675,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
675 // Too many devices in this domain.
676 maxDomainDevicesReached++;
677 console.log('Too many devices on this domain to accept the CIRA connection. meshid: ' + socket.tag.meshid);
678 - socket.end();
678 + obj.close(socket);
679 } else {
680 // Attempts reverse DNS loopup on the device IP address
681 require('dns').reverse(socket.remoteAddr, function (err, hostnames) {
@@ -741,7 +741,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
741 // New CIRA connection for unknown node, disconnect.
742 unknownNodeCount++;
743 console.log('CIRA connection for unknown node. groupid: ' + initialMesh._id + ', uuid: ' + socket.tag.SystemId);
744 - socket.end();
744 + obj.close(socket);
745 return;
746 }
747
@@ -758,7 +758,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
758 // New CIRA connection for unknown node, disconnect.
759 unknownNodeCount++;
760 console.log('CIRA connection for unknown node. candidate(s): ' + nodes.length + ', groupid: ' + initialMesh._id + ', uuid: ' + socket.tag.SystemId);
761 - socket.end();
761 + obj.close(socket);
762 return;
763 }
764
@@ -776,7 +776,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
776 // New CIRA connection for unknown node, disconnect.
777 unknownMeshIdCount++;
778 console.log('CIRA connection to a unknown group type. groupid: ' + socket.tag.meshid);
779 - socket.end();
779 + obj.close(socket);
780 return;
781 }
782 return 18 + usernameLen + serviceNameLen + methodNameLen + passwordLen;
@@ -1047,7 +1047,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
1047
1048 // Disconnect CIRA tunnel
1049 obj.close = function (socket) {
1050 - try { socket.end(); } catch (e) { }
1050 + try { socket.end(); } catch (e) { try { socket.close(); } catch (e) { } }
1051 removeCiraConnection(socket);
1052 };
1053