UNPKG

domotz-remote-pawn

Version:

Domotz Agent

82 lines (75 loc) 2.69 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/>. * * Created by Iacopo Papalini <iacopo@domotz.com>. */ /** * Compacts the list of fresh interface bindings in an array with mac addresses and lists of IP addresses. * * See spec for example. * @param interfacesBindingStorage * @returns {[]} */ module.exports.indexBindingStorageByMac = function (interfacesBindingStorage) { var ret = []; var tmp = {}; var devices = interfacesBindingStorage.listFresh(); devices.forEach(function (dev) { if (dev.ip === null) { return; } if (tmp[dev.mac] === undefined) { tmp[dev.mac] = []; } tmp[dev.mac].push(dev.ip); }); for (var mac in tmp) { if (tmp.hasOwnProperty(mac)) { ret.push({ mac: mac, ips: tmp[mac] }); } } return ret; }; /** * Builds an asynchronous function that performs the scan and send the result to the engine if needed. * @returns {scanAndReport} */ module.exports.scanAndReportBuilder = function (context, resourceLocator) { function scanAndReport(callback) { context.scanSample.start(); var exec = resourceLocator.utils.childProcess.exec; var cmd = context.nmapCommand(context.scanSample.ips); exec(cmd, {}, module.exports.parseNmapResultBuilder(context, callback)); } return scanAndReport; }; module.exports.parseNmapResultBuilder = function (context, callback) { var xml2js = context.resourceLocator.xml2js; var myConsole = context.myConsole; return function (error, stdout) { if (error) { myConsole.error('Error during nmap execution: %s', error); return callback(); } try { xml2js.parseString(stdout, context.processJsonAndSendPayloadBuilder(context, callback)); } catch (error) { context.scanSample.signalError(); myConsole.error(error.message + '\n' + error.stack); return callback(); } }; };