UNPKG

domotz-remote-pawn

Version:

Domotz Agent

85 lines (76 loc) 2.89 kB
/** 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/>. * * Created by a.lulli@domotz.com on 28/04/2021. */ module.exports.getDescription = function () { return 'UDP_PORT_SCANNER'; }; module.exports.Context = function (scanSample, myConsole, resourceLocator) { return { scanSample: scanSample, myConsole: myConsole, resourceLocator: resourceLocator, nmapCommand: resourceLocator.networkTools.nmapCommands.getPortUDPNmapCmdMultiIp, processJsonAndSendPayloadBuilder: module.exports.processJsonAndSendPayloadBuilder, }; }; module.exports.ScanSample = function (mac) { return { mac: mac, ips: null, payload: null, start: function () {}, signalError: function () {}, stillValid: function () { return false; }, }; }; module.exports.processJsonAndSendPayloadBuilder = function (context, callback) { var myConsole = context.myConsole; var scanSample = context.scanSample; var resourceLocator = context.resourceLocator; var parser = require('./parser'); var discovery = context.resourceLocator.settingsManager.getDiscoverySettings(); return function (error, result) { if (error) { myConsole.error('Cannot parse nmap XML'); return callback(); } var payload; try { var statusMapping = discovery.udp_port_status_mapping; payload = parser(result, statusMapping); myConsole.debug('Payload %s, Status Mapping %s', JSON.stringify(payload), JSON.stringify(statusMapping)); } 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'); return callback(); } resourceLocator.engineRequest .cachedSendWithPromise('/subnet/ip/device/' + scanSample.mac + '/open-udp-port', 'PUT', payload) .then(onSuccess) .catch(onError); }; };