UNPKG

domotz-remote-pawn

Version:

Domotz Agent

116 lines (108 loc) 5 kB
/** This file is part of Domotz Agent. * Copyright (C) 2024 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 Andrea Azzara <a.azzara@domotz.com> on 18/01/2024. */ var lodash = require('lodash'); const API_VERSION = 1; var jamDriver = function (engineRequest, path, q, log, interfacesBindingStorage) { var myConsole = log.decorateLogs(); var blockingFeatureAvailable = null; var jamStateManager = require('./netgearRouterJamStateManager'); var stateManager = jamStateManager.netgearJamStateManager(log); var netgearManager = require('./netgearManager')(q, log); var utils = require('./netgearUtils').utils(); function convertMacAddressesToIP(macAddressList) { var freshBindings = interfacesBindingStorage.listFresh(); myConsole.debug('Fresh bindings %s', JSON.stringify(freshBindings)); var macToIPMap = {}; macAddressList.forEach(function (macAddress) { myConsole.debug('Converting mac address %s to IP', macAddress); var ipAddressesList = utils.getIPsFromMACAddress(macAddress, freshBindings); macToIPMap[macAddress] = ipAddressesList; myConsole.debug('IP addresses list %s', JSON.stringify(ipAddressesList)); }); return macToIPMap; } function handleServerResponse(macAddressList) { myConsole.info('Storing the rules in the state manager'); stateManager.storeRules(macAddressList); } function updateMacAddresses() { myConsole.info('Updating mac addresses list'); return engineRequest .sendWithPromise('/device/jammed', 'GET') .then(function (data) { myConsole.debug('List of (MAC) addresses, to be jammed', JSON.stringify(data)); handleServerResponse(data); }) .catch(function (error) { myConsole.error('Cannot update list, assuming no device to jam, %s', JSON.stringify(error)); if (error.status.toString()[0] === '4') { myConsole.error('Client error %s, set jammed list as empty, unlock all devices', error.status); handleServerResponse([]); } else { myConsole.error('Server error %s, leave jammed list untouched', error.status); } }); } return { updateMacAddresses: function () { if (blockingFeatureAvailable === true) { return updateMacAddresses(); } if (blockingFeatureAvailable === null) { try { blockingFeatureAvailable = true; return updateMacAddresses(); } catch (error) { blockingFeatureAvailable = false; } } }, killDeviceJammer: function () { myConsole.info('Killing device jammer'); return netgearManager.unblockAllDevices().catch(function (error) { myConsole.error('Failed to unblock all devices : %s', error); }); }, isBlockingFeatureAvailable: function () { return blockingFeatureAvailable; }, updateNetgearFirewallRules: function () { myConsole.info('Updating Netgear firewall rules'); var devicesToBlock = convertMacAddressesToIP(stateManager.getBlockedMACAddresses()); myConsole.debug('Devices to block %s', JSON.stringify(devicesToBlock)); return netgearManager .getBlockedDevices() .then(function (currentlyBlockedDevices) { var blockedDeviceList = lodash.map(currentlyBlockedDevices, 'name'); var diff = stateManager.calculateDiff(blockedDeviceList, devicesToBlock); myConsole.debug('Diff %s', JSON.stringify(diff)); return netgearManager.blockDeviceKeys(diff.keysToBlock).then(function () { netgearManager.unblockDeviceKeys(diff.keysToUnblock); }); }) .catch(function () { myConsole.error('Failed to update Netgear firewall rules'); }); }, netgearApiVersion: function () { return API_VERSION; }, }; }; module.exports.jamDriver = jamDriver;