UNPKG

domotz-remote-pawn

Version:

Domotz Agent

104 lines (95 loc) 3.85 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 netInfo = function (message, resourceLocator) { var step = resourceLocator.step; var netInfo = resourceLocator.networkTools.netInfo; var engineRequest = resourceLocator.engineRequest; var myConsole = resourceLocator.log.decorateLogs(); var results = { local_network: { servers: { dns: null, dhcp: null, gateway: null, default_gateway: null, }, interfaces: null, }, internet_access: { ip: { address: null, name: null, }, provider: null, }, }; function uploadResult(results) { myConsole.info('Uploading results: %s', JSON.stringify(results)); engineRequest.cachedSendWithPromise('/network/information', 'POST', results, undefined, undefined, { cache_until: resourceLocator.httpOutCache.expirationInNHours(24), }); } function finalize(callback) { uploadResult(results); callback(results); } return { describe: function () { return message.correlationId + ' - netInfo '; }, execute: function (callback) { step( function (err) { if (err) { return callback(null, err); } netInfo.getDHCPServers('broadcast-dhcp-discover.nse', this.parallel()); netInfo.getExternalIPAddress(this.parallel()); netInfo.getInterfaces(this.parallel()); netInfo.getGatewayFull(this.parallel()); netInfo.getDNSServers(this.parallel()); }, function (err, dhcpServers, ip, interfaces, gateway, dnsServers) { if (err) { myConsole.error(err.message + '\n' + err.stack); } results.local_network.servers.dhcp = dhcpServers; results.local_network.interfaces = interfaces; results.local_network.servers.gateway = gateway.ip; results.local_network.servers.default_gateway = gateway; results.local_network.servers.dns = dnsServers; results.internet_access.ip.address = ip || null; if (ip) { netInfo.getProvider(ip, this.parallel()); netInfo.getExternalIPName(ip, this.parallel()); } else { finalize(callback); } }, function (err, provider, externalIpName) { if (err) { myConsole.error(err.message + '\n' + err.stack); } results.internet_access.provider = provider; results.internet_access.ip.name = externalIpName; finalize(callback); } ); }, }; }; module.exports.command = netInfo;