UNPKG

domotz-remote-pawn

Version:

Domotz Agent

92 lines (78 loc) 3.08 kB
/** This file is part of Domotz Agent. * Copyright (C) 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 NI Team <dev@domotz.com> on 14/07/2023. */ module.exports = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var lodash = resourceLocator.lodash; function getSolvedConflicts(currentConflicts, previousConflicts) { var uniqueElements = {}; var solvedConflicts = []; lodash.forOwn(currentConflicts, function (currentConflict) { uniqueElements[JSON.stringify(currentConflict)] = true; }); lodash.forOwn(previousConflicts, function (previousConflict) { if (!uniqueElements.hasOwnProperty(JSON.stringify(previousConflict))) { solvedConflicts.push(previousConflict); } }); return solvedConflicts; } function getActiveConflicts(conflicts) { return conflicts .filter(function (conflict) { return conflict !== null; }) .sort(); } function parseOutput(data) { // Starting Nmap 7.92 ( https://nmap.org ) at 2023-07-17 14:50 CEST // Pre-scan script results: // |_ip-conflict: {"mac_addresses": ["e4:ce:02:10:7d:86", "e4:ce:02:10:7d:87"]} // WARNING: No targets were specified, so 0 hosts scanned. // Nmap done: 0 IP addresses (0 hosts up) scanned in 2.47 seconds const regex = /\|_ip-conflict: (.*)/g; const match = regex.exec(data); if (match && match[1]) { var result = JSON.parse(match[1]); var macAddresses = _removeDuplicatesAndSort(result.mac_addresses); if (macAddresses.length > 1) { myConsole.debug('Conflicting mac addresses: %s ', JSON.stringify(result)); return macAddresses; } } else { myConsole.debug('No match found'); } return []; } function _removeDuplicatesAndSort(macAddresses) { var uniqueArray = []; lodash.forOwn(macAddresses, function (macAddress) { if (uniqueArray.indexOf(macAddress) === -1) { uniqueArray.push(macAddress); } }); return uniqueArray.sort(); } return { parseOutput: parseOutput, getSolvedConflicts: getSolvedConflicts, getActiveConflicts: getActiveConflicts, }; };