Added reverse DNS when Intel AMT device added using meshcmd.

Ylian Saint-Hilaire committed Mar 8, 2021 at 11:06 UTC 0a24dd8290d021e94b001876dbf260b645f46d90
2 files changed +67 -43
meshagent.js
+1 -1
@@ -873,7 +873,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
873
874 // Close the duplicate agent
875 parent.agentStats.duplicateAgentCount++;
876 - parent.setAgentIssue(obj, "duplicateAgent");
876 + parent.setAgentIssue(obj, 'duplicateAgent');
877 if (obj.nodeid != null) { parent.parent.debug('agent', 'Duplicate agent ' + obj.nodeid + ' (' + obj.remoteaddrport + ')'); }
878 dupAgent.close(3);
879 } else {
mpsserver.js
+66 -42
@@ -440,32 +440,44 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
440 console.log('Too many devices on this domain to accept the CIRA connection. meshid: ' + socket.tag.meshid);
441 socket.end();
442 } else {
443 - // We are under the limit, create the new device.
444 - // Node is not in the database, add it. Credentials will be empty until added by the user.
445 - var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: socket.remoteAddr, domain: domainid, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
446 - if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
447 - obj.db.Set(device);
448 -
449 - // Event the new node
450 - addedTlsDeviceCount++;
451 - var change = 'CIRA added device ' + socket.tag.name + ' to mesh ' + mesh.name;
452 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: domainid });
453 -
454 - // Add the connection to the MPS connection list
455 - addCiraConnection(socket);
443 + // Attempts reverse DNS loopup on the device IP address
444 + require('dns').reverse(socket.remoteAddr, function (err, hostnames) {
445 + var hostname = socket.remoteAddr;
446 + if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
447 +
448 + // We are under the limit, create the new device.
449 + // Node is not in the database, add it. Credentials will be empty until added by the user.
450 + var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: domainid, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
451 + if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
452 + obj.db.Set(device);
453 +
454 + // Event the new node
455 + addedTlsDeviceCount++;
456 + var change = 'CIRA added device ' + socket.tag.name + ' to mesh ' + mesh.name;
457 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: domainid });
458 +
459 + // Add the connection to the MPS connection list
460 + addCiraConnection(socket);
461 + });
462 }
463 });
464 return;
465 } else {
460 - // Node is not in the database, add it. Credentials will be empty until added by the user.
461 - var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: socket.remoteAddr, domain: domainid, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
462 - if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
463 - obj.db.Set(device);
466 + // Attempts reverse DNS loopup on the device IP address
467 + require('dns').reverse(socket.remoteAddr, function (err, hostnames) {
468 + var hostname = socket.remoteAddr;
469 + if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
470
465 - // Event the new node
466 - addedTlsDeviceCount++;
467 - var change = 'CIRA added device ' + socket.tag.name + ' to mesh ' + mesh.name;
468 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: domainid });
471 + // Node is not in the database, add it. Credentials will be empty until added by the user.
472 + var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: domainid, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
473 + if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
474 + obj.db.Set(device);
475 +
476 + // Event the new node
477 + addedTlsDeviceCount++;
478 + var change = 'CIRA added device ' + socket.tag.name + ' to mesh ' + mesh.name;
479 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: domainid });
480 + });
481 }
482 } else {
483 // New CIRA connection for unknown node, disconnect.
@@ -596,33 +608,45 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
608 console.log('Too many devices on this domain to accept the CIRA connection. meshid: ' + socket.tag.meshid);
609 socket.end();
610 } else {
599 - // We are under the limit, create the new device.
600 - // Node is not in the database, add it. Credentials will be empty until added by the user.
601 - var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: socket.remoteAddr, domain: mesh.domain, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
602 - if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
603 - obj.db.Set(device);
611 + // Attempts reverse DNS loopup on the device IP address
612 + require('dns').reverse(socket.remoteAddr, function (err, hostnames) {
613 + var hostname = socket.remoteAddr;
614 + if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
615
605 - // Event the new node
606 - addedDeviceCount++;
607 - var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
608 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
616 + // We are under the limit, create the new device.
617 + // Node is not in the database, add it. Credentials will be empty until added by the user.
618 + var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: mesh.domain, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
619 + if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
620 + obj.db.Set(device);
621 +
622 + // Event the new node
623 + addedDeviceCount++;
624 + var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
625 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
626
610 - // Add the connection to the MPS connection list
611 - addCiraConnection(socket);
612 - SendUserAuthSuccess(socket); // Notify the auth success on the CIRA connection
627 + // Add the connection to the MPS connection list
628 + addCiraConnection(socket);
629 + SendUserAuthSuccess(socket); // Notify the auth success on the CIRA connection
630 + });
631 }
632 });
633 return;
634 } else {
617 - // Node is not in the database, add it. Credentials will be empty until added by the user.
618 - var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState && socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: socket.remoteAddr, domain: mesh.domain, intelamt: { user: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtuser == 'string')) ? socket.tag.meiState.amtuser : '', pass: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtpass == 'string')) ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
619 - if ((socket.tag.meiState != null) && (typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
620 - obj.db.Set(device);
621 -
622 - // Event the new node
623 - addedDeviceCount++;
624 - var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
625 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
635 + // Attempts reverse DNS loopup on the device IP address
636 + require('dns').reverse(socket.remoteAddr, function (err, hostnames) {
637 + var hostname = socket.remoteAddr;
638 + if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
639 +
640 + // Node is not in the database, add it. Credentials will be empty until added by the user.
641 + var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState && socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: mesh.domain, intelamt: { user: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtuser == 'string')) ? socket.tag.meiState.amtuser : '', pass: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtpass == 'string')) ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
642 + if ((socket.tag.meiState != null) && (typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
643 + obj.db.Set(device);
644 +
645 + // Event the new node
646 + addedDeviceCount++;
647 + var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
648 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
649 + });
650 }
651 } else {
652 // Node is already present