merged pull request networkInterfaces(), renamed properties, osInfo() macos added catalina detection

This commit is contained in:
Sebastian Hildebrandt 2020-01-02 22:06:15 +01:00
parent 0feb6469b8
commit 6e98139272
3 changed files with 115 additions and 106 deletions

4
lib/index.d.ts vendored
View File

@ -318,6 +318,10 @@ export namespace Systeminformation {
duplex: string; duplex: string;
mtu: number; mtu: number;
speed: number; speed: number;
dhcp: boolean;
dnsSuffix: string;
ieee8021xAuth: string;
ieee8021xState: string;
carrier_changes: number; carrier_changes: number;
} }

View File

@ -187,13 +187,13 @@ function parseLinesWindowsNics(sections, nconfigsections) {
let nics = []; let nics = [];
for (let i in sections) { for (let i in sections) {
if (sections.hasOwnProperty(i)) { if (sections.hasOwnProperty(i)) {
if (sections[i].trim() !== '') { if (sections[i].trim() !== '') {
let lines = sections[i].trim().split('\r\n'); let lines = sections[i].trim().split('\r\n');
let linesNicConfig = nconfigsections[i].trim().split('\r\n'); let linesNicConfig = nconfigsections[i].trim().split('\r\n');
let netEnabled = util.getValue(lines, 'NetEnabled', '='); let netEnabled = util.getValue(lines, 'NetEnabled', '=');
if (netEnabled) { if (netEnabled) {
const speed = parseInt(util.getValue(lines, 'speed', '=').trim(), 10) / 1000000; const speed = parseInt(util.getValue(lines, 'speed', '=').trim(), 10) / 1000000;
nics.push({ nics.push({
@ -225,7 +225,7 @@ function getWindowsNics() {
} }
function getWindowsDNSsuffixes() { function getWindowsDNSsuffixes() {
let iface = {}; let iface = {};
let dnsSuffixes = { let dnsSuffixes = {
@ -233,90 +233,90 @@ function getWindowsDNSsuffixes() {
exitCode: 0, exitCode: 0,
ifaces: [], ifaces: [],
}; };
try { try {
const ipconfig = execSync('ipconfig /all', util.execOptsWin); const ipconfig = execSync('ipconfig /all', util.execOptsWin);
const ipconfigArray = ipconfig.split('\r\n\r\n'); const ipconfigArray = ipconfig.split('\r\n\r\n');
ipconfigArray.forEach( (element, index) => { ipconfigArray.forEach((element, index) => {
if(index == 1) { if (index == 1) {
const longPrimaryDNS = element.split('\r\n').filter((element) => { const longPrimaryDNS = element.split('\r\n').filter((element) => {
return element.toUpperCase().includes('DNS'); return element.toUpperCase().includes('DNS');
}); });
const primaryDNS = longPrimaryDNS[0].substring(longPrimaryDNS[0].lastIndexOf(":")+1); const primaryDNS = longPrimaryDNS[0].substring(longPrimaryDNS[0].lastIndexOf(":") + 1);
dnsSuffixes.primaryDNS = primaryDNS.trim(); dnsSuffixes.primaryDNS = primaryDNS.trim();
if(!dnsSuffixes.primaryDNS) dnsSuffixes.primaryDNS = 'Not defined'; if (!dnsSuffixes.primaryDNS) dnsSuffixes.primaryDNS = 'Not defined';
} }
if(index > 1) { if (index > 1) {
if(index % 2 == 0){ if (index % 2 == 0) {
const name = element.substring(element.lastIndexOf(" ")+1).replace(':', ''); const name = element.substring(element.lastIndexOf(" ") + 1).replace(':', '');
iface.name = name; iface.name = name;
}else { } else {
const connectionSpecificDNS = element.split('\r\n').filter((element) => { const connectionSpecificDNS = element.split('\r\n').filter((element) => {
return element.toUpperCase().includes('DNS') return element.toUpperCase().includes('DNS')
}); });
const dnsSuffix = connectionSpecificDNS[0].substring(connectionSpecificDNS[0].lastIndexOf(":")+1); const dnsSuffix = connectionSpecificDNS[0].substring(connectionSpecificDNS[0].lastIndexOf(":") + 1);
iface.dnsSuffix = dnsSuffix.trim(); iface.dnsSuffix = dnsSuffix.trim();
dnsSuffixes.ifaces.push(iface); dnsSuffixes.ifaces.push(iface);
iface = {}; iface = {};
} }
} }
}); });
return dnsSuffixes; return dnsSuffixes;
} catch (error) { } catch (error) {
console.log('An error occurred trying to bring the Connection-specific DNS suffix', error.message); // console.log('An error occurred trying to bring the Connection-specific DNS suffix', error.message);
return { return {
primaryDNS: '', primaryDNS: '',
exitCode: 0, exitCode: 0,
ifaces: [], ifaces: [],
}; };
} }
} }
function getWindowsIfaceDNSsuffix(ifaces, ifacename) { function getWindowsIfaceDNSsuffix(ifaces, ifacename) {
let dnsSuffix = ''; let dnsSuffix = '';
// Adding (.) to ensure ifacename compatibility when duplicated iface-names // Adding (.) to ensure ifacename compatibility when duplicated iface-names
const interfaceName = ifacename + '.'; const interfaceName = ifacename + '.';
try { try {
const connectionDnsSuffix = ifaces.filter((iface) => { const connectionDnsSuffix = ifaces.filter((iface) => {
return interfaceName.includes(iface.name + '.'); return interfaceName.includes(iface.name + '.');
}).map((iface) => iface.dnsSuffix); }).map((iface) => iface.dnsSuffix);
if(connectionDnsSuffix[0]) { if (connectionDnsSuffix[0]) {
dnsSuffix = connectionDnsSuffix[0]; dnsSuffix = connectionDnsSuffix[0];
} }
if(!dnsSuffix) dnsSuffix = ''; if (!dnsSuffix) dnsSuffix = '';
return dnsSuffix ; return dnsSuffix;
} catch (error) { } catch (error) {
console.log('Error getting Connection-specific DNS suffix: ', error.message); // console.log('Error getting Connection-specific DNS suffix: ', error.message);
return 'Unknown'; return 'Unknown';
} }
} }
function getWindowsWiredProfilesInformation() { function getWindowsWiredProfilesInformation() {
try { try {
const result = execSync('netsh lan show profiles', util.execOptsWin); const result = execSync('netsh lan show profiles', util.execOptsWin);
const profileList = result.split('\r\nProfile on interface'); const profileList = result.split('\r\nProfile on interface');
return profileList; return profileList;
} catch (error) { } catch (error) {
if(error.status === 1 && error.stdout.includes('AutoConfig')){ if (error.status === 1 && error.stdout.includes('AutoConfig')) {
return 'Disabled'; return 'Disabled';
} }
return []; return [];
} }
} }
function getWindowsWirelessIfaceSSID(interfaceName){ function getWindowsWirelessIfaceSSID(interfaceName) {
try { try {
const result = execSync(`netsh wlan show interface name="${interfaceName}" | findstr "SSID"`, util.execOptsWin); const result = execSync(`netsh wlan show interface name="${interfaceName}" | findstr "SSID"`, util.execOptsWin);
const SSID = result.split('\r\n').shift(); const SSID = result.split('\r\n').shift();
const parseSSID = SSID.split(':').pop(); const parseSSID = SSID.split(':').pop();
return parseSSID; return parseSSID;
} catch (error) { } catch (error) {
return 'Unknown'; return 'Unknown';
} }
} }
function getWindowsIEEE8021x(connectionType, iface, ifaces) { function getWindowsIEEE8021x(connectionType, iface, ifaces) {
let i8021x = { let i8021x = {
@ -324,70 +324,70 @@ function getWindowsIEEE8021x(connectionType, iface, ifaces) {
protocol: 'Unknown', protocol: 'Unknown',
}; };
if(ifaces === 'Disabled'){ if (ifaces === 'Disabled') {
i8021x.state = "Disabled"; i8021x.state = "Disabled";
i8021x.protocol = "Not defined"; i8021x.protocol = "Not defined";
return i8021x; return i8021x;
} }
if(connectionType == 'wired' && ifaces.length > 0){ if (connectionType == 'wired' && ifaces.length > 0) {
try { try {
// Get 802.1x information by interface name // Get 802.1x information by interface name
const iface8021xInfo = ifaces.find((element) => { const iface8021xInfo = ifaces.find((element) => {
return element.includes(iface + '\r\n'); return element.includes(iface + '\r\n');
}); });
const arrayIface8021xInfo = iface8021xInfo.split('\r\n') const arrayIface8021xInfo = iface8021xInfo.split('\r\n')
const state8021x = arrayIface8021xInfo.find((element) => { const state8021x = arrayIface8021xInfo.find((element) => {
return element.includes('802.1x'); return element.includes('802.1x');
}); });
if(state8021x.includes('Disabled')){ if (state8021x.includes('Disabled')) {
i8021x.state = "Disabled"; i8021x.state = "Disabled";
i8021x.protocol = "Not defined"; i8021x.protocol = "Not defined";
} else if (state8021x.includes('Enabled')) { } else if (state8021x.includes('Enabled')) {
const protocol8021x = arrayIface8021xInfo.find((element) => { const protocol8021x = arrayIface8021xInfo.find((element) => {
return element.includes('EAP'); return element.includes('EAP');
}); });
i8021x.protocol = protocol8021x.split(':').pop(); i8021x.protocol = protocol8021x.split(':').pop();
i8021x.state = "Enabled"; i8021x.state = "Enabled";
} }
} catch (error) { } catch (error) {
// console.log('Error getting wired information:', error); // console.log('Error getting wired information:', error);
return i8021x; return i8021x;
} }
} else if (connectionType == 'wireless'){ } else if (connectionType == 'wireless') {
let i8021xState = ''; let i8021xState = '';
let i8021xProtocol = ''; let i8021xProtocol = '';
try { try {
const SSID = getWindowsWirelessIfaceSSID(iface); const SSID = getWindowsWirelessIfaceSSID(iface);
if(SSID !== 'Unknown') { if (SSID !== 'Unknown') {
i8021xState = execSync(`netsh wlan show profiles "${SSID}" | findstr "802.1X"`, util.execOptsWin); i8021xState = execSync(`netsh wlan show profiles "${SSID}" | findstr "802.1X"`, util.execOptsWin);
i8021xProtocol = execSync(`netsh wlan show profiles "${SSID}" | findstr "EAP"`, util.execOptsWin); i8021xProtocol = execSync(`netsh wlan show profiles "${SSID}" | findstr "EAP"`, util.execOptsWin);
} }
if (i8021xState.includes(':') && i8021xProtocol.includes(':')) { if (i8021xState.includes(':') && i8021xProtocol.includes(':')) {
i8021x.state = i8021xState.split(':').pop(); i8021x.state = i8021xState.split(':').pop();
i8021x.protocol = i8021xProtocol.split(':').pop(); i8021x.protocol = i8021xProtocol.split(':').pop();
} }
} catch (error) { } catch (error) {
// console.log('Error getting wireless information:', error); // console.log('Error getting wireless information:', error);
if(error.status === 1 && error.stdout.includes('AutoConfig')){ if (error.status === 1 && error.stdout.includes('AutoConfig')) {
i8021x.state = "Disabled"; i8021x.state = "Disabled";
i8021x.protocol = "Not defined"; i8021x.protocol = "Not defined";
} }
return i8021x; return i8021x;
} }
} }
return i8021x; return i8021x;
} }
@ -483,7 +483,7 @@ function getLinuxIfaceConnectionName(interfaceName) {
try { try {
const result = execSync(cmd).toString(); const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g,' ').trim(); const resultFormat = result.replace(/\s+/g, ' ').trim();
const connectionNameLines = resultFormat.split(" ").slice(3); const connectionNameLines = resultFormat.split(" ").slice(3);
const connectionName = connectionNameLines.join(' '); const connectionName = connectionNameLines.join(' ');
return connectionName != '--' ? connectionName : ''; return connectionName != '--' ? connectionName : '';
@ -493,18 +493,18 @@ function getLinuxIfaceConnectionName(interfaceName) {
} }
function getLinuxIfaceDHCPstatus(connectionName) { function getLinuxIfaceDHCPstatus(connectionName) {
if(connectionName) { if (connectionName) {
const cmd = `nmcli connection show "${connectionName}" \| grep ipv4.method;`; const cmd = `nmcli connection show "${connectionName}" \| grep ipv4.method;`;
try { try {
const result = execSync(cmd).toString(); const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g,' ').trim(); const resultFormat = result.replace(/\s+/g, ' ').trim();
let dhcStatus = resultFormat.split(" ").slice(1).toString(); let dhcStatus = resultFormat.split(" ").slice(1).toString();
switch (dhcStatus) { switch (dhcStatus) {
case 'auto': case 'auto':
dhcStatus = true; dhcStatus = true;
break; break;
default: default:
dhcStatus = false; dhcStatus = false;
break; break;
@ -519,13 +519,13 @@ function getLinuxIfaceDHCPstatus(connectionName) {
} }
function getLinuxIfaceDNSsuffix(connectionName) { function getLinuxIfaceDNSsuffix(connectionName) {
if(connectionName) { if (connectionName) {
const cmd = `nmcli connection show "${connectionName}" \| grep ipv4.dns-search;`; const cmd = `nmcli connection show "${connectionName}" \| grep ipv4.dns-search;`;
try { try {
const result = execSync(cmd).toString(); const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g,' ').trim(); const resultFormat = result.replace(/\s+/g, ' ').trim();
const dnsSuffix = resultFormat.split(" ").slice(1).toString(); const dnsSuffix = resultFormat.split(" ").slice(1).toString();
return dnsSuffix == '--' ? 'Not defined': dnsSuffix; return dnsSuffix == '--' ? 'Not defined' : dnsSuffix;
} catch (e) { } catch (e) {
return 'Unknown'; return 'Unknown';
} }
@ -534,16 +534,16 @@ function getLinuxIfaceDNSsuffix(connectionName) {
} }
} }
function getLinuxIfaceAuth8021x(connectionName) { function getLinuxIfaceIEEE8021xAuth(connectionName) {
if(connectionName) { if (connectionName) {
const cmd = `nmcli connection show "${connectionName}" \| grep 802-1x.eap;`; const cmd = `nmcli connection show "${connectionName}" \| grep 802-1x.eap;`;
try { try {
const result = execSync(cmd).toString(); const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g,' ').trim(); const resultFormat = result.replace(/\s+/g, ' ').trim();
const authenticationProtocol = resultFormat.split(" ").slice(1).toString(); const authenticationProtocol = resultFormat.split(" ").slice(1).toString();
return authenticationProtocol == '--' ? '': authenticationProtocol; return authenticationProtocol == '--' ? '' : authenticationProtocol;
} catch (e) { } catch (e) {
return 'Not defined'; return 'Not defined';
} }
@ -552,9 +552,9 @@ function getLinuxIfaceAuth8021x(connectionName) {
} }
} }
function getLinuxIfaceState8021x(authenticationProtocol) { function getLinuxIfaceIEEE8021xState(authenticationProtocol) {
if(authenticationProtocol) { if (authenticationProtocol) {
if(authenticationProtocol == 'Not defined'){ if (authenticationProtocol == 'Not defined') {
return 'Disabled'; return 'Disabled';
} }
return 'Enabled'; return 'Enabled';
@ -606,6 +606,10 @@ function networkInterfaces(callback) {
duplex: nic.duplex, duplex: nic.duplex,
mtu: nic.mtu, mtu: nic.mtu,
speed: nic.speed, speed: nic.speed,
dhcp: false,
dnsSuffix: '',
ieee8021xAuth: '',
ieee8021xState: '',
carrierChanges: 0 carrierChanges: 0
}); });
}); });
@ -636,8 +640,8 @@ function networkInterfaces(callback) {
let operstate = 'down'; let operstate = 'down';
let dhcp = false; let dhcp = false;
let dnsSuffix = ''; let dnsSuffix = '';
let auth8021x = ''; let ieee8021xAuth = '';
let state8021x = ''; let ieee8021xState = '';
let type = ''; let type = '';
if (ifaces.hasOwnProperty(dev)) { if (ifaces.hasOwnProperty(dev)) {
@ -687,17 +691,17 @@ function networkInterfaces(callback) {
echo -n "type: "; cat /sys/class/net/${iface}/type 2>/dev/null; echo; echo -n "type: "; cat /sys/class/net/${iface}/type 2>/dev/null; echo;
echo -n "wireless: "; cat /proc/net/wireless 2>/dev/null \| grep ${iface}; echo; echo -n "wireless: "; cat /proc/net/wireless 2>/dev/null \| grep ${iface}; echo;
echo -n "wirelessspeed: "; iw dev ${iface} link 2>&1 \| grep bitrate; echo;`; echo -n "wirelessspeed: "; iw dev ${iface} link 2>&1 \| grep bitrate; echo;`;
let lines = []; let lines = [];
try { try {
const connectionName = getLinuxIfaceConnectionName(iface); const connectionName = getLinuxIfaceConnectionName(iface);
dhcp = getLinuxIfaceDHCPstatus(connectionName); dhcp = getLinuxIfaceDHCPstatus(connectionName);
dnsSuffix = getLinuxIfaceDNSsuffix(connectionName); dnsSuffix = getLinuxIfaceDNSsuffix(connectionName);
auth8021x = getLinuxIfaceAuth8021x(connectionName); ieee8021xAuth = getLinuxIfaceIEEE8021xAuth(connectionName);
state8021x = getLinuxIfaceState8021x(auth8021x); ieee8021xState = getLinuxIfaceIEEE8021xState(ieee8021xAuth);
lines = execSync(cmd).toString().split('\n'); lines = execSync(cmd).toString().split('\n');
} catch (e) { } catch (e) {
util.noop(); util.noop();
} }
@ -717,8 +721,8 @@ function networkInterfaces(callback) {
if (iface === 'lo' || iface.startsWith('bond')) { type = 'virtual'; } if (iface === 'lo' || iface.startsWith('bond')) { type = 'virtual'; }
} }
if (_windows) { if (_windows) {
dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, dev); dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, dev);
nics.forEach(detail => { nics.forEach(detail => {
if (detail.mac === mac) { if (detail.mac === mac) {
@ -729,14 +733,14 @@ function networkInterfaces(callback) {
type = detail.type; type = detail.type;
} }
}); });
if (dev.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('wlan') >= 0|| ifaceName.toLowerCase().indexOf('802.11n') >= 0 || ifaceName.toLowerCase().indexOf('wireless') >= 0 || ifaceName.toLowerCase().indexOf('wi-fi') >= 0 || ifaceName.toLowerCase().indexOf('wifi') >= 0) { if (dev.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('802.11n') >= 0 || ifaceName.toLowerCase().indexOf('wireless') >= 0 || ifaceName.toLowerCase().indexOf('wi-fi') >= 0 || ifaceName.toLowerCase().indexOf('wifi') >= 0) {
type = 'wireless'; type = 'wireless';
} }
const IEEE8021x = getWindowsIEEE8021x(type, dev, nics8021xInfo); const IEEE8021x = getWindowsIEEE8021x(type, dev, nics8021xInfo);
auth8021x = IEEE8021x.protocol; ieee8021xAuth = IEEE8021x.protocol;
state8021x = IEEE8021x.state; ieee8021xState = IEEE8021x.state;
} }
let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : null; let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : null;
const virtual = internal ? false : testVirtualNic(dev, ifaceName, mac); const virtual = internal ? false : testVirtualNic(dev, ifaceName, mac);
@ -755,8 +759,8 @@ function networkInterfaces(callback) {
speed, speed,
dhcp, dhcp,
dnsSuffix, dnsSuffix,
auth8021x, ieee8021xAuth,
state8021x, ieee8021xState,
carrierChanges, carrierChanges,
}); });
} }

View File

@ -264,6 +264,7 @@ function osInfo(callback) {
result.codename = (result.release.indexOf('10.12') > -1 ? 'macOS Sierra' : result.codename); result.codename = (result.release.indexOf('10.12') > -1 ? 'macOS Sierra' : result.codename);
result.codename = (result.release.indexOf('10.13') > -1 ? 'macOS High Sierra' : result.codename); result.codename = (result.release.indexOf('10.13') > -1 ? 'macOS High Sierra' : result.codename);
result.codename = (result.release.indexOf('10.14') > -1 ? 'macOS Mojave' : result.codename); result.codename = (result.release.indexOf('10.14') > -1 ? 'macOS Mojave' : result.codename);
result.codename = (result.release.indexOf('10.15') > -1 ? 'macOS Catalina' : result.codename);
result.codepage = util.getCodepage(); result.codepage = util.getCodepage();
if (callback) { if (callback) {