networtInterfaces() optimized for maxos

This commit is contained in:
Sebastian Hildebrandt 2019-06-23 20:41:33 +02:00
parent b90c1c8a97
commit f361f43c82

View File

@ -232,10 +232,13 @@ function parseLinesDarwinNics(sections) {
iface: '',
mtu: -1,
mac: '',
ip6: '',
ip4: '',
speed: -1,
type: '',
operstate: '',
duplex: '',
internal: false
};
const first = section[0];
nic.iface = first.split(':')[0].trim();
@ -244,9 +247,16 @@ function parseLinesDarwinNics(sections) {
if (isNaN(nic.mtu)) {
nic.mtu = -1;
}
nic.internal = parts[0].indexOf('LOOPBACK') > -1;
section.forEach(line => {
if (line.trim().startsWith('ether ')) {
nic.mac = line.split('ether ')[1].toLowerCase();
nic.mac = line.split('ether ')[1].toLowerCase().trim();
}
if (line.trim().startsWith('inet6 ')) {
nic.ip6 = line.split('inet6 ')[1].toLowerCase().split('%')[0];
}
if (line.trim().startsWith('inet ')) {
nic.ip4 = line.split('inet ')[1].toLowerCase().split(' ')[0];
}
});
let speed = util.getValue(section, 'link rate');
@ -265,7 +275,9 @@ function parseLinesDarwinNics(sections) {
nic.type = util.getValue(section, 'type').toLowerCase().indexOf('wi-fi') > -1 ? 'wireless' : 'wired';
nic.operstate = util.getValue(section, 'status').toLowerCase().indexOf('active') > -1 ? 'up' : 'down';
nic.duplex = util.getValue(section, 'media').toLowerCase().indexOf('half-duplex') > -1 ? 'half' : 'full';
if (nic.ip6 || nic.ip4 || nic.mac) {
nics.push(nic);
}
});
return nics;
}
@ -282,8 +294,16 @@ function getDarwinNics() {
}
function testVirtualNic(iface, ifaceName, mac) {
const virtualMacs = ['00:03:FF', '00:05:69', '00:0C:29', '00:0F:4B', '00:0F:4B', '00:13:07', '00:13:BE', '00:15:5d', '00:16:3E', '00:1C:42', '00:21:F6', '00:21:F6', '00:24:0B', '00:24:0B', '00:50:56', '00:A0:B1', '00:E0:C8', '08:00:27', '0A:00:27', '18:92:2C', '3C:F3:92', '54:52:00', 'FC:15:97']
return virtualMacs.indexOf(mac.toUpperCase()) > -1 || iface.toLowerCase().indexOf(' virtual ') > -1 || ifaceName.toLowerCase().indexOf(' virtual ') > -1 || ifaceName.toLowerCase().indexOf('vethernet ') > -1 || ifaceName.toLowerCase().indexOf('vethernet ') > -1;
const virtualMacs = ['00:00:00:00:00:00', '00:03:FF', '00:05:69', '00:0C:29', '00:0F:4B', '00:0F:4B', '00:13:07', '00:13:BE', '00:15:5d', '00:16:3E', '00:1C:42', '00:21:F6', '00:21:F6', '00:24:0B', '00:24:0B', '00:50:56', '00:A0:B1', '00:E0:C8', '08:00:27', '0A:00:27', '18:92:2C', '16:DF:49', '3C:F3:92', '54:52:00', 'FC:15:97']
if (mac) {
return virtualMacs.filter(item => { return mac.toUpperCase().toUpperCase().startsWith(item.substr(0, mac.length)); }).length > 0 ||
iface.toLowerCase().indexOf(' virtual ') > -1 ||
ifaceName.toLowerCase().indexOf(' virtual ') > -1 ||
iface.toLowerCase().indexOf('vethernet ') > -1 ||
ifaceName.toLowerCase().indexOf('vethernet ') > -1 ||
iface.toLowerCase().startsWith('veth ') ||
ifaceName.toLowerCase().startsWith('veth ');
} else return false;
}
function networkInterfaces(callback) {
@ -293,6 +313,33 @@ function networkInterfaces(callback) {
let ifaces = os.networkInterfaces();
let result = [];
let nics = [];
// seperate handling in OSX
if (_darwin || _freebsd || _openbsd || _netbsd) {
nics = getDarwinNics();
// console.log(nics);
// console.log('-------');
// console.log(ifaces);
nics.forEach(nic => {
result.push({
iface: nic.iface,
ifaceName: nic.iface,
ip4: nic.ip4,
ip6: nic.ip6,
mac: nic.mac,
internal: nic.internal,
virtual: testVirtualNic(nic.iface, nic.iface, nic.mac),
operstate: nic.operstate,
type: nic.type,
duplex: nic.duplex,
mtu: nic.mtu,
speed: nic.speed,
carrierChanges: 0
});
});
if (callback) { callback(result); }
resolve(result);
} else {
if (JSON.stringify(ifaces) === JSON.stringify(_ifaces)) {
// no changes - just return object
result = _networkInterfaces;
@ -304,9 +351,6 @@ function networkInterfaces(callback) {
if (_windows) {
nics = getWindowsNics();
}
if (_darwin) {
nics = getDarwinNics();
}
for (let dev in ifaces) {
let ip4 = '';
let ip6 = '';
@ -406,17 +450,6 @@ function networkInterfaces(callback) {
type = 'wireless';
}
}
if (_darwin || _freebsd || _openbsd || _netbsd) {
nics.forEach(nic => {
if (nic.iface === dev) {
mtu = nic.mtu;
duplex = nic.duplex;
speed = nic.speed;
type = nic.type;
operstate = nic.operstate;
}
});
}
let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : null;
const virtual = testVirtualNic(dev, ifaceName, mac);
result.push({
@ -440,6 +473,7 @@ function networkInterfaces(callback) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
}