UNPKG

domotz-remote-pawn

Version:

Domotz Agent

111 lines (105 loc) 4.53 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 command performs a simple http port scan: it executes the same HTTP request * on different ports of the same host and returns a dictionary, indexed by port, with * the results of an httpRequest for each port. * @param message the following fields are read: * - host: the host name or ip address * - path: the http resource, the URL part after the port * - method: the http method used (f.e. GET, POST) * - body [optional]: the body of the various http requests * - ports: either an array of integers or a string 'n:m' where n < m * * @param resourceLocator * @returns {{execute: Function, describe: Function}} */ var httpScanPorts = function (message, resourceLocator) { var payload = message.payload; if (typeof payload.ports === 'string') { var tmp = payload.ports.split(':'); payload.ports = []; for (var i = tmp[0]; i <= tmp[1]; i++) { payload.ports.push(parseInt(i, 10)); } } return { execute: function (callback) { var subCommands = {}; /** * Creates an httpRequest command that perform a request on the * n-th port of the list of the server * @param n * @returns command */ var createHttpRequestCommand = function (n) { var httpRequest = resourceLocator.commandHttpRequest; var port = payload.ports[n]; return httpRequest.command({ payload: { path: payload.path, host: payload.host, port: port, method: payload.method, headers: payload.headers, body: payload.body }, correlationId: message.correlationId + ':' + port }, resourceLocator); }; /** * Checks whether all the pending httpRequests finished * @returns {boolean} */ var allResultsArrived = function () { for (var i = 0; i < payload.ports.length; i++) { if (subCommands[payload.ports[i]] === undefined) { console.log("Message not arrived for port: " + payload.ports[i] + ': ' + subCommands[payload.ports[i]]); return false; } } return true; }; /** * Callback that handles the response to the request on the specified port * If the current one is the last request to have been arrived, calls the * httpScanPorts callback with the final result * @param port * @returns {Function} */ var processNthResult = function (port) { return function (result) { console.log(message.correlationId + " - Response arrived for port: " + port); subCommands[port] = result; if (allResultsArrived()) { console.log(message.correlationId + " - All messages arrived"); callback(subCommands); } }; }; for (var i = 0; i < payload.ports.length; i++) { var port = payload.ports[i]; createHttpRequestCommand(i).execute(processNthResult(port)); } }, describe: function () { return message.correlationId + " - httpScanPorts - " + payload.method + ' - http://' + payload.host + ':' + JSON.stringify(payload.ports) + payload.path; } }; }; module.exports.command = httpScanPorts;