UNPKG

domotz-remote-pawn

Version:

Domotz Agent

110 lines (93 loc) 3.84 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * This module wraps the nmap commands used for IP devices' interfaces binds (mac-ip-hostname) * Created by Iacopo Papalini <iacopo@domotz.com> on 18/05/16. */ const MODULE_NAME = 'IP_DISCOVERY'; module.exports.factory = function (q, util, proc, hostInfo, nmapCommands, nmapXml2Js, fastParser) { "use strict"; var iteration; function setIteration(i) { iteration = i; } function executeDiscoveryCommands(commandList) { var promises = []; commandList.forEach(function (command) { promises.push(proc.exec(command). then(nmapXml2Js.toJSON). then(fastParser.parse)); }); return q.all(promises); } /** * Discovers all the IP devices in the given interfaces. When done the callback is invoked with, as argument, * an Array of Object similar to: * { * 'mac': '[the mac address]', * 'ip': '[the ip address]', * 'host_name': '[the host name]' * } */ function discoverAll() { var interfaces = hostInfo.getInterfacesList(); var name; var commandList = []; var myInterfacesBindings = {}; console.debug('%s - (%d): DiscoverAll called - Interfaces: %s', MODULE_NAME, iteration, JSON.stringify(interfaces)); for (name in interfaces) { if (interfaces.hasOwnProperty(name)) { var iface = interfaces[name]; var ip = iface.ip; var mac = iface.mac; var netmask = iface.netmask; var l3disc = (mac === null); if (l3disc) { console.info('%s - (%d): Custom Interface for Monitoring ' + 'External Network: %s/%s', MODULE_NAME, iteration, ip, netmask); } else { console.info('%s - (%d): Real Interface for Monitoring ' + 'Internal Network %s/%s. Add Binding: %s --> ' + '%s', MODULE_NAME, iteration, ip, netmask, mac, ip); if (!myInterfacesBindings.hasOwnProperty(mac)) { myInterfacesBindings[mac] = []; } myInterfacesBindings[mac].push({ip: ip, host_name: ''}); } var commands = nmapCommands.getFastNmapCmd(ip, netmask, l3disc); commandList = commandList.concat(commands); } } return executeDiscoveryCommands(commandList). then(function(devices) { devices.push(myInterfacesBindings); return devices; }); } function discover(ipList, arpDisabled) { console.debug('%s - (%d): Discover called - IP addresses: %s', MODULE_NAME, iteration, JSON.stringify(ipList)); var commandList = [nmapCommands.getRetryCmd(ipList, arpDisabled)]; return executeDiscoveryCommands(commandList); } return { setIteration: setIteration, discoverAll: discoverAll, discover: discover }; };