domotz-remote-pawn
Version:
Domotz Agent
138 lines (117 loc) • 4.35 kB
JavaScript
/** 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/>.
*
* Created by Iacopo Papalini <iacopo@domotz.com> on 04/08/2017.
*/
const MAIN_INTERFACE = 'eth0';
function ipWrapper(resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var proc = resourceLocator.utils.proc;
var lodash = resourceLocator.lodash;
var q = resourceLocator.q;
var platform = resourceLocator.utils.platform;
var ipCall = function (tool, cmd, options) {
var ipCommand = ['ip', tool, cmd];
if (options.constructor === Array) {
ipCommand = ipCommand.concat(options);
} else {
for (var key in options) {
if (options.hasOwnProperty(key)) {
ipCommand.push(key);
ipCommand.push(options[key]);
}
}
}
var ipCmd = ipCommand.join(' ');
myConsole.debug('Executing ip command: ' + ipCmd);
return proc.exec(ipCmd);
};
function _flush() {
return ipCall('addr', 'flush', { dev: MAIN_INTERFACE });
}
function activateDhcp() {
'use strict';
return proc.spawn('/sbin/dhclient', ['-i', MAIN_INTERFACE]);
}
function ping(ip, count, timeout, rawOutput) {
'use strict';
var extraCommands = [];
if (platform.isWindows()) {
extraCommands = [ip, '-n', count, '-w', timeout];
} else {
extraCommands = [ip, '-c', count, '-t', timeout];
}
return proc
.spawn('ping', extraCommands)
.then(function (data) {
if (rawOutput) {
return data;
}
var lostPackets = /^.+, (\d+)% packet loss,/gm.exec(data);
return { lost_packets_ratio: parseFloat(lostPackets[1]) };
})
.catch(function (error) {
return { error: error };
});
}
function _parseVlan(rawOutput) {
var links = [];
var output = rawOutput.split('\n');
for (var line = 0, outputLength = output.length - 1; line < outputLength; line += 2) {
var linkLine = output[line];
var linkFields = linkLine.trim().split(/\s/g);
var wasDeleted = linkFields[0] === 'Deleted';
if (wasDeleted) {
linkFields.shift();
}
var name = linkFields[1].split(':')[0];
if (name.split('@').length === 2) {
links.push(name.split('@')[0]);
}
}
return links;
}
function _getVlansIP(vlanNames) {
var promises = [];
lodash.forOwn(vlanNames, function (name) {
var deferred = q.defer();
ipCall('addr', 'show', ['dev', name])
.then(function (rawOutput) {
var output = rawOutput.split('\n');
var addrLine = output[2];
var address = addrLine.trim().split(/\s/g)[1];
deferred.resolve({ name: name, address: address });
})
.catch(function () {
myConsole.error('Error to retrieve VLANs IPs');
});
promises.push(deferred.promise);
});
return q.all(promises);
}
function retrieveVlans() {
return ipCall('link', 'show', {}).then(_parseVlan).then(_getVlansIP);
}
return {
call: ipCall,
flush: _flush,
activate_dhcp: activateDhcp,
ping: ping,
retrieveVlans: retrieveVlans,
_parseVlan: _parseVlan,
};
}
module.exports.ipWrapper = ipWrapper;