domotz-remote-pawn
Version:
Domotz Agent
131 lines (124 loc) • 4.92 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 Tommaso Latini <tommaso@domotz.com> on 18/01/16.
*/
const IP_OCTET = '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])';
const IP_VALIDATOR = new RegExp('^(' + IP_OCTET + '.){3}' + IP_OCTET + '$');
const LINK_LOCAL = new RegExp('^169.254.' + IP_OCTET + '.' + IP_OCTET + '$');
const MAC_VALIDATOR = /^(?!(?:ff:ff:ff:ff:ff:ff|00:00:00:00:00:00))(?:[\da-f]{2}:){5}[\da-f]{2}$/i;
const COMMANDS = {
linux: {
interfaces: 'ls /sys/class/net',
mac: 'cat /sys/class/net/%s/address',
ip: 'ip -o -f inet addr show %s',
gateway: 'ip r',
netmask: 'ip -o -f inet addr show %s',
},
darwin: {}, // For running tests on MAC
win32: {}, // For running tests on Windows
};
module.exports.factory = function (util, platform) {
var commands = COMMANDS[platform];
if (!commands) {
throw new Error('Unsupported host platform: ' + platform);
}
return {
cmd: {
interfaces: function () {
return commands.interfaces;
},
mac: function (nic) {
return util.format(commands.mac, nic);
},
ip: function (nic) {
return util.format(commands.ip, nic);
},
netmask: function (nic) {
return util.format(commands.netmask, nic);
},
gateway: function () {
return commands.gateway;
},
},
validator: {
mac: function (mac) {
console.debug('Validating MAC: ' + mac);
if (!mac || !MAC_VALIDATOR.test(mac.toLowerCase())) {
throw new Error('Invalid MAC address [' + mac + ']. ' + 'Maybe not a valid interface!');
}
console.debug('Return MAC: ' + mac);
return mac;
},
ip: function (output) {
console.debug('Validating IP: ' + output);
var addr = output.split(/\s+/g)[3].split('/')[0];
if (!IP_VALIDATOR.test(addr) || LINK_LOCAL.test(addr)) {
throw new Error('Invalid IP address. [' + addr + '] ' + 'Maybe the interface is not connected.');
}
console.debug('Return IP: ' + addr);
return addr;
},
netmask: function (output) {
console.debug('Validating NETMASK: ' + output);
var netmask = parseInt(output.split(/\s+/g)[3].split('/')[1]);
console.debug('Return NETMASK: ' + netmask);
return netmask;
},
interfaces: function (ifaces) {
console.debug('Validating INTERFACES: ' + ifaces);
var list = ifaces.split('\n');
if (list.length === 0) {
throw new Error('No available network interfaces');
}
console.debug('Return INTERFACES: ' + list);
return list;
},
gateway: function (output) {
console.debug('Validating GATEWAY: ' + output);
var rows = output.split('\n'),
info = '';
for (var i = 0; i < rows.length; i++) {
var str = rows[i];
if (str.indexOf('default') !== -1) {
info = str.split(' ');
break;
}
}
if (info.length === 0) {
throw new Error('No default gateway found');
}
var gateway = {
ip: info[2],
interface: info[4],
};
console.debug('Return GATEWAY: ' + JSON.stringify(gateway));
return gateway;
},
},
getNicDescriptor: function (name, mac, ip, netmask) {
if (name && mac && ip && netmask) {
return {
name: name,
mac: mac.toUpperCase(),
ip: ip,
netmask: netmask,
};
}
},
};
};