domotz-remote-pawn
Version:
Domotz Agent
94 lines (85 loc) • 3.58 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2021 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/>.
*
* NetBIOS data scan.
*
*/
var SPAWN_HARD_TIMEOUT = 180000;
var SEND_SAME_SAMPLE_AFTER_IN_HOURS = 15 * 24; // 15 days cache duration
module.exports.factory = function (resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var host = resourceLocator.discovery.host;
var alreadyScannedDevices = [];
function networkScan() {
/*
* Using nmap, performs a NetBIOS on a list of IPs.
*
* A device is scanned once (for every agent reboot).
*/
var devices = resourceLocator.interfacesBindingStorage.listFresh();
var command = resourceLocator.networkTools.nmapCommands.getNETBIOSCmd();
var count = 0;
/* Filter devices by removing the ones present in L3 Discovery Deny list */
devices = devices.filter(function (binding) {
var isDeviceDenied = host.isDeviceMacInL3DiscoveryDenyList(binding.mac);
if (isDeviceDenied) {
myConsole.debug('NETBIOS scan skipped for device IP = %s [ MAC = %s ] because in L3 Discovery Deny List', binding.ip, binding.mac);
}
return !isDeviceDenied;
});
devices.forEach(function (binding) {
if (alreadyScannedDevices[binding.mac] === undefined) {
command += ' ' + binding.ip;
alreadyScannedDevices[binding.mac] = 1;
count++;
}
});
if (count === 0) {
myConsole.debug('No device needs NETBIOS scan');
} else {
myConsole.info('%s devices are being scanned for NETBIOS data', devices.length);
return executeDiscoveriesAndReport(command, handleScanResult);
}
}
function executeDiscoveriesAndReport(command) {
myConsole.info('Executing %s NETBIOS discovery command: ', command);
return resourceLocator.utils.proc
.exec(command, { timeout: SPAWN_HARD_TIMEOUT })
.then(resourceLocator.discovery.netbios.parse)
.then(handleScanResult)
.catch(function (error) {
myConsole.error("Error while executing NETBIOS discovery command: %s", error.toString());
});
}
function handleScanResult(scanResult) {
scanResult.forEach(function (deviceData) {
resourceLocator.engineRequest.cachedSendWithPromise(
'/subnet/ip/device/' + deviceData.mac + '/inspection-data/netbios',
'PUT',
deviceData,
undefined,
undefined,
{
cache_until: resourceLocator.httpOutCache.expirationInNHours(SEND_SAME_SAMPLE_AFTER_IN_HOURS),
}
);
});
}
return {
networkScan: networkScan,
_alreadyScannedDevices: alreadyScannedDevices,
};
};