UNPKG

domotz-remote-pawn

Version:

Domotz Agent

158 lines (133 loc) 5.71 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/>. **/ var parsers = require('./netinfo_utils.js'); const MODULE = 'NETINFO'; const NO_ERROR = null; const ERROR_GET_PROVIDER = MODULE + ' (getProvider): IP address not specified'; const ERROR_EXTERNAL_IP_NAME = MODULE + ' (getExternalIPName): IP address not specified'; const ERROR_EXTERNAL_IP_ADDRESS = MODULE + ' (getExternalIPAddress): Get external IP address failed with message'; module.exports.factory = function (os, path, http, dns, whois, procUtils, fileUtils, hostInfo) { var exec = procUtils.exec; function getInterfaces(callback) { var netInfoStyleInterfaces = [], interfacesList = hostInfo.getInterfacesList(), interfaces = Object.keys(interfacesList); interfaces.forEach(function (iface) { netInfoStyleInterfaces.push({ name: iface, address: hostInfo.getIP(iface), netmask: hostInfo.getNetmask(iface), mac: hostInfo.getMAC(iface) }); }); callback(NO_ERROR, netInfoStyleInterfaces); } function getGateway(callback) { var gateway = hostInfo.getDefaultGateway(); callback(NO_ERROR, gateway); } function getDNSServers(callback) { if (dns.hasOwnProperty('getServers')) { var dnsServers = dns.getServers(); callback(NO_ERROR, dnsServers); } else { fileUtils.readFile('/etc/resolv.conf', parsers.parseResolvConf, true) .then(function (dnsServers) { callback(NO_ERROR, dnsServers); }) .catch(callback); } } /** * @function getDHCPServers * @abstract Performs auto-discovery of DHCP servers * * @param {string} luascript - name of the LUA script used to discover DHCP servers * @param {Function} callback - will be called on success * */ function getDHCPServers(luascript, callback) { var scriptPath = path.resolve(__dirname, luascript); var cmd = (process.getuid() === 0 ? '' : 'sudo ') + 'domotz_nmap -Pn -p67 --script ' + scriptPath; exec(cmd) .then(function (report) { var dhcpservers = []; console.info('%s - (getDHCPServers): nmap report = %s', MODULE, report); var nmap_data = parsers.parseDHCPDiscoverOutput('broadcast-dhcp-discover', report); if (nmap_data['Server Identifier']) { dhcpservers.push(nmap_data['Server Identifier']); } callback(NO_ERROR, dhcpservers); }) .catch(function (err) { console.warn('%s - (getDHCPServers): Error in DHCP server recovery: %s', MODULE, err.message); callback(NO_ERROR, []); }); } function getExternalIPAddress(url, callback) { http.get(url, function (res) { if (res.statusCode !== 200) { console.warn(ERROR_EXTERNAL_IP_ADDRESS + ' . code = ' + res.statusCode); return callback(new Error(ERROR_EXTERNAL_IP_ADDRESS)); } res.on("data", function (ip_external) { return callback(NO_ERROR, ip_external.toString().replace(/(\r\n|\n|\r)/gm, "")); }); }).on('error', function (err) { console.error(ERROR_EXTERNAL_IP_ADDRESS + ' ' + err.message); return callback(err); }); } function getExternalIPName(ip, callback) { if (!ip) { console.warn(ERROR_EXTERNAL_IP_NAME); return callback(new Error(ERROR_EXTERNAL_IP_NAME)); } dns.reverse(ip, function (err, resolvedip) { if (err) { // it is possible that an IP address does not have a reverse DNS name so the warn logging level console.warn('%s - (getExternalIpName): Unable to reverse resolve IP address %s. Error code: %s', MODULE, ip, err.code); resolvedip = [ip]; } return callback(NO_ERROR, resolvedip[0]); }); } function getProvider(ip, callback) { if (!ip) { console.warn(ERROR_GET_PROVIDER); return callback(new Error(ERROR_GET_PROVIDER)); } whois.lookup(ip, {timeout: 10000}, function (err, data) { if (err) { console.warn('%s - (getProvider): Failed to lookup whois of ' + 'IP address %s . Error = "%s"', MODULE, ip, err.message); callback(err); } callback(NO_ERROR, parsers.parseWhoisOutput(data)); }); } return { getInterfaces: getInterfaces, getGateway: getGateway, getDNSServers: getDNSServers, getDHCPServers: getDHCPServers, getExternalIPAddress: getExternalIPAddress, getExternalIPName: getExternalIPName, getProvider: getProvider }; };