domotz-remote-pawn
Version:
Domotz Agent
96 lines (85 loc) • 3.06 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/>.
*
* Created by Iacopo Papalini <iacopo@domotz.com>.
*/
var MIN_SCANNING_INTERVAL_IN_SECONDS = 3 * 60 * 60; // 3 hr min between scans
module.exports.getDescription = function () {
return 'TCP_PORT_SCANNER';
};
module.exports.Context = function (scanSample, myConsole, resourceLocator) {
return {
scanSample: scanSample,
myConsole: myConsole,
resourceLocator: resourceLocator,
nmapCommand: resourceLocator.networkTools.nmapCommands.getPortNmapCmdMultiIp,
processJsonAndSendPayloadBuilder: module.exports.processJsonAndSendPayloadBuilder,
};
};
module.exports.ScanSample = function (mac) {
var time = null;
return {
mac: mac,
ips: null,
time: time,
payload: null,
start: function () {
this.time = new Date();
},
signalError: function () {
this.time = null;
},
stillValid: function () {
if (this.time === null) {
return false;
}
var diffInSeconds = (new Date().getTime() - this.time.getTime()) / 1000;
return diffInSeconds < MIN_SCANNING_INTERVAL_IN_SECONDS;
},
};
};
module.exports.processJsonAndSendPayloadBuilder = function (context, callback) {
var myConsole = context.myConsole;
var scanSample = context.scanSample;
var resourceLocator = context.resourceLocator;
var parser = require('./parser');
return function (error, result) {
if (error) {
myConsole.error('Cannot parse nmap XML');
return callback();
}
var payload;
try {
payload = parser(result, myConsole);
} catch (error) {
myConsole.error(error.message);
return callback();
}
function onSuccess() {
myConsole.debug('Payload successfully sent to engine');
return callback();
}
function onError() {
myConsole.info('Cannot send payload to engine, resetting it');
scanSample.payload = null;
return callback();
}
resourceLocator.engineRequest
.cachedSendWithPromise('/subnet/ip/device/' + scanSample.mac + '/open-port', 'PUT', payload)
.then(onSuccess)
.catch(onError);
};
};