domotz-remote-pawn
Version:
Domotz Agent
63 lines (58 loc) • 2.09 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/>.
*
* Created by a.lulli@domotz.com on 28/04/2021.
*/
module.exports = function (result, statusMapping) {
try {
if (!statusMapping) {
statusMapping = {};
}
if (result.nmaprun.runstats[0].hosts[0].$.up === '0') {
throw new Error('Host seems down');
}
var tmp = [];
result.nmaprun.host.forEach(function (host) {
var ports = host.ports[0].port;
if (ports === undefined || ports.length === 0) {
return;
}
for (var i = 0; i < ports.length; i++) {
var currentPort = ports[i];
if (!currentPort.state) {
continue;
}
var status = currentPort.state[0].$.state.toUpperCase();
if (status in statusMapping) {
status = statusMapping[status];
}
tmp[parseInt(currentPort.$.portid, 10)] = {
port: currentPort.$.portid,
status: status,
};
}
});
var output = {
udp_ports: [],
};
tmp.forEach(function (port) {
output.udp_ports.push(port);
});
return output;
} catch (err) {
throw new Error('Error during parsing: ' + err.message);
}
};