domotz-remote-pawn
Version:
Domotz Agent
99 lines (90 loc) • 3.97 kB
JavaScript
/** 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 Iacopo Papalini <iacopo@domotz.com>.
*/
const COOL_DOWN_IN_MS = 2 * 1000;
var factory = function (resourceLocator, specificUtils) {
var async = resourceLocator.async;
var commonUtils = require('./utils');
var indexBindingStorageByMac = commonUtils.indexBindingStorageByMac;
var history = {};
var isRunning = false;
var executionsCounter = 0;
var host = resourceLocator.discovery.host;
var baseConsole = resourceLocator.log.decorateLogs();
function getScanSample(device) {
if (history[device.mac] === undefined) {
history[device.mac] = specificUtils.ScanSample(device.mac);
}
var scanSample = history[device.mac];
scanSample.ips = device.ips;
return scanSample;
}
function performDiscovery() {
executionsCounter += 1;
var myConsole = baseConsole.decorate(String(executionsCounter));
if (isRunning) {
myConsole.warn('Skipping execution, another one still running');
return;
}
isRunning = true;
var devicesList = indexBindingStorageByMac(resourceLocator.interfacesBindingStorage);
myConsole.info('There are %s candidate devices', devicesList.length);
var functions = [];
devicesList.forEach(function (device) {
/* Filter devices by removing the ones present in L3 Discovery Deny list */
if (!host.isDeviceMacInL3DiscoveryDenyList(device.mac)) {
var sample = getScanSample(device);
if (sample.stillValid()) {
myConsole.debug("Last sample for mac %s still valid, don't scan", sample.mac);
return;
}
var myConsoleSpecific = myConsole.decorate(sample.mac);
var context = specificUtils.Context(sample, myConsoleSpecific, resourceLocator);
functions.push(commonUtils.scanAndReportBuilder(context, resourceLocator));
} else {
myConsole.debug('Skip Port Scan for device %s [mac %s] in L3 Discovery Deny List', device.ip, device.mac);
}
});
myConsole.info('Executing on %s devices', functions.length);
if (functions.length > 0) {
// Wait a cool-down period before starting the scan so that there a pause between an nmap execution and another
// mitigating the defensive strategies of some devices that reject too frequent packets
functions.unshift(function (callback) {
myConsole.info('Waiting %sms', COOL_DOWN_IN_MS);
setTimeout(callback, COOL_DOWN_IN_MS);
});
}
async.series(functions, function (err) {
if (err) {
myConsole.error('Execution interrupted by error: ' + err);
}
myConsole.info('Execution finished');
isRunning = false;
});
}
return {
tcpPortScan: performDiscovery,
udpPortScan: performDiscovery,
isRunning: function () {
return isRunning;
},
};
};
module.exports.factory = factory;