Skip to content

Commit

Permalink
refactored wifiConections() (macOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Dec 22, 2024
1 parent 8acbce7 commit 8a801ae
Showing 1 changed file with 53 additions and 76 deletions.
129 changes: 53 additions & 76 deletions lib/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,85 +645,62 @@ function wifiConnections(callback) {
}
resolve(result);
} else if (_darwin) {
let cmd = 'system_profiler SPNetworkDataType';
let cmd = 'system_profiler SPNetworkDataType SPAirPortDataType -xml 2>/dev/null; echo "######" ; ioreg -n AppleBCMWLANSkywalkInterface -r 2>/dev/null';
exec(cmd, function (error, stdout) {
const parts1 = stdout.toString().split('\n\n Wi-Fi:\n\n');
if (parts1.length > 1) {
const lines = parts1[1].split('\n\n')[0].split('\n');
const iface = util.getValue(lines, 'BSD Device Name', ':', true);
const model = util.getValue(lines, 'hardware', ':', true);
cmd = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I 2>/dev/null; echo "######" ; ioreg -n AppleBCMWLANSkywalkInterface -r 2>/dev/null';
exec(cmd, function (error, stdout) {
const parts = stdout.toString().split('######');
const lines2 = parts[0].split('\n');
let lines3 = [];
if (parts[1].indexOf(' | {') > 0 && parts[1].indexOf(' | }') > parts[1].indexOf(' | {')) {
lines3 = parts[1].split(' | {')[1].split(' | }')[0].replace(/ \| /g, '').replace(/"/g, '').split('\n');
}
if (lines2.length > 10) {
const ssid = util.getValue(lines2, 'ssid', ':', true);
const bssid = util.getValue(lines2, 'bssid', ':', true) || formatBssid(util.getValue(lines3, 'IO80211BSSID', '=', true));
const security = util.getValue(lines2, 'link auth', ':', true);
const txRate = util.getValue(lines2, 'lastTxRate', ':', true);
const channel = util.getValue(lines2, 'channel', ':', true).split(',')[0];
const type = '802.11';
const rssi = util.toInt(util.getValue(lines2, 'agrCtlRSSI', ':', true));
/// const noise = util.toInt(util.getValue(lines2, 'agrCtlNoise', ':', true));
const signalLevel = rssi;
if (ssid || bssid) {
result.push({
id: 'Wi-Fi',
iface,
model,
ssid,
bssid,
channel: util.toInt(channel),
frequency: channel ? wifiFrequencyFromChannel(channel) : null,
type,
security,
signalLevel,
quality: wifiQualityFromDB(signalLevel),
txRate
});
}
}
if (lines3.length > 10) {
const ssid = util.getValue(lines3, 'IO80211SSID', '=', true);
const bssid = formatBssid(util.getValue(lines3, 'IO80211BSSID', '=', true));
const security = '';
const txRate = -1;
const signalLevel = -1;
const quality = -1;
const channel = util.getValue(lines3, 'IO80211Channel', '=', true);
const type = '802.11';
if ((ssid || bssid) && !result.length) {
result.push({
id: 'Wi-Fi',
iface,
model,
ssid,
bssid,
channel: util.toInt(channel),
frequency: channel ? wifiFrequencyFromChannel(channel) : null,
type,
security,
signalLevel,
quality,
txRate
});
}
}
if (callback) {
callback(result);
}
resolve(result);
});
} else {
if (callback) {
callback(result);
try {
const parts = stdout.toString().split('######');
const profilerObj = util.plistParser(parts[0]);
const networkObj = profilerObj[0]._SPCommandLineArguments.indexOf('SPNetworkDataType') >= 0 ? profilerObj[0]._items : profilerObj[1]._items;
const airportObj = profilerObj[0]._SPCommandLineArguments.indexOf('SPAirPortDataType') >= 0 ? profilerObj[0]._items[0].spairport_airport_interfaces : profilerObj[1]._items[0].spairport_airport_interfaces;

// parts[1] : ioreg
let lines3 = [];
if (parts[1].indexOf(' | {') > 0 && parts[1].indexOf(' | }') > parts[1].indexOf(' | {')) {
lines3 = parts[1].split(' | {')[1].split(' | }')[0].replace(/ \| /g, '').replace(/"/g, '').split('\n');
}
resolve(result);

const networkWifiObj = networkObj.find((item) => { return item._name === 'Wi-Fi'; });
const airportWifiObj = airportObj[0].spairport_current_network_information;

const channel = parseInt(('' + airportWifiObj.spairport_network_channel).split(' ')[0]) || 0;
const signalLevel = airportWifiObj.spairport_signal_noise || null;

let security = [];
const sm = airportWifiObj.spairport_security_mode;
if (sm === 'spairport_security_mode_wep') {
security.push('WEP');
} else if (sm === 'spairport_security_mode_wpa2_personal') {
security.push('WPA2');
} else if (sm.startsWith('spairport_security_mode_wpa2_enterprise')) {
security.push('WPA2 EAP');
} else if (sm.startsWith('pairport_security_mode_wpa3_transition')) {
security.push('WPA2/WPA3');
} else if (sm.startsWith('pairport_security_mode_wpa3')) {
security.push('WPA3');
}

result.push({
id: networkWifiObj._name || 'Wi-Fi',
iface: networkWifiObj.interface || '',
model: networkWifiObj.hardware || '',
ssid: airportWifiObj._name || '',
bssid: airportWifiObj.spairport_network_bssid || '',
channel,
frequency: channel ? wifiFrequencyFromChannel(channel) : null,
type: airportWifiObj.spairport_network_phymode || '802.11',
security,
signalLevel: signalLevel ? parseInt(signalLevel, 10) : null,
quality: wifiQualityFromDB(signalLevel),
txRate: airportWifiObj.spairport_network_rate || null,
});

} catch (e) {
util.noop();
}
if (callback) {
callback(result);
}
resolve(result);
});
} else if (_windows) {
let cmd = 'netsh wlan show interfaces';
Expand Down

0 comments on commit 8a801ae

Please sign in to comment.