UNPKG

domotz-remote-pawn

Version:

Domotz Agent

88 lines (77 loc) 3.47 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 executes the scanning of the known TCP Ports for the currently available * devices, * Created by tom on 18/12/15. */ const LOW_POWER_ARCH = ['openwrt', 'marvell', 'freescale', 'armada370', 'armada375']; const SERVICE_NAME = "port_discovery"; var factory = function (resourceLocator, nmapXml2js, interfacesBindingStorage, nmapCommands, parser) { var proc = resourceLocator.utils.proc; var devicesInfo = resourceLocator.devices.info; var engineRequest = resourceLocator.engineRequest; var limit = (LOW_POWER_ARCH.indexOf(process.env.DARCHITECTURE) !== -1) ? 1 : (resourceLocator.configuration.nmap_max_processes || 10); function storeValues(result, mac, ip, deviceId) { var ports = parser(result); console.debug('PORT_MAP: Get result for ip %s: %s', ip, JSON.stringify(ports)); if (deviceId) { engineRequest.sendWithPromise('/device/' + deviceId + '/ip-port', 'PUT', ports) .catch(function (err) { console.error("Error on device ip ports sending: %s", err); }); } else { console.error("IP Ports: deviceId not fount for ip address %s", ip); } devicesInfo.setUpdateTimeToNow(mac, SERVICE_NAME); } function map() { try { var count = 0; var devices = interfacesBindingStorage.listFresh(); var ipToId = resourceLocator.discovery.utils.getDevicesMaps().ipToId; console.debug('PORT_MAP: Start Port Mapping'); devices.forEach(function (dev) { var mac = dev.mac; if (count === limit) { throw new Error("Count exceeded: " + count + " processes running."); } if (devicesInfo.isToBeUpdated(mac, SERVICE_NAME)) { var ip = dev.ip; var cmd = nmapCommands.getPortNmapCmd(ip); count++; console.info("PORT_MAP: Scanning IP ports of device: " + dev.ip + " (mac:" + dev.mac + ")"); proc.exec(cmd) .then(nmapXml2js.toJSON) .then(function (result) { storeValues(result, mac, ip, ipToId[ip]); }) .catch(function (error) { console.error('PORT_MAP: Error on collecting nmap XML'); console.error(error.stack); }); } }); } catch (err) { console.warn('PORT_MAP: ' + err.name + ' ' + err.message); } } return { map: map }; }; module.exports.factory = factory;