Dependency updates, some code cleaned up

This commit is contained in:
Christian Kuster
2019-09-21 11:56:22 +02:00
parent d1eac84058
commit 32b516e302
10 changed files with 125 additions and 99 deletions

View File

@ -3,43 +3,45 @@
* Created by kc on 04.04.16.
*/
const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
const cmdLine = tool + ' -s';
const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
const cmdLine = tool + ' -s';
const detector = tool + ' -getInfo';
const macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/;
/**
* Parsing the output of airport (Mac OS X)
* @param str output of the tool
* @param callback
*/
function parseOutput(str, callback) {
var err = null;
let err = null;
let wifis = [];
try {
var lines = str.split('\n');
var wifis = [];
for (var i = 1, l = lines.length; i < l; i++) {
var mac = lines[i].match(macRegex);
let lines = str.split('\n');
for (let i = 1, l = lines.length; i < l; i++) {
let mac = lines[i].match(macRegex);
if (!mac) {
continue;
}
var macStart = lines[i].indexOf(mac[0]);
var elements = lines[i].substr(macStart).split(/[ ]+/);
let macStart = lines[i].indexOf(mac[0]);
let elements = lines[i].substr(macStart).split(/[ ]+/);
wifis.push({
'ssid' : lines[i].substr(0, macStart).trim(),
'mac' : elements[0].trim(),
'channel' : parseInt(elements[2].trim(), 10),
'rssi' : parseInt(elements[1].trim(), 10)
'ssid' : lines[i].substr(0, macStart).trim(),
'mac' : elements[0].trim(),
'channel': parseInt(elements[2].trim(), 10),
'rssi' : parseInt(elements[1].trim(), 10)
});
}
}
catch (ex) {
err = ex;
}
callback(err, wifis);
finally {
callback(err, wifis);
}
}

View File

@ -3,31 +3,32 @@
* Created by kc on 04.04.16.
*/
const _ = require('lodash');
const _ = require('lodash');
// usually located in /usr/bin/ but as it could be at another location, allow all found in PATH (but we're in trouble
// when the default location, /usr/bin/ is not in the PATH!). GitHub issue #1
const tool = 'iwlist';
const cmdLine = tool + ' scan';
const tool = 'iwlist';
const cmdLine = tool + ' scan';
const detector = tool + ' --help';
const macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/;
const cellRegex = /Cell [0-9]{2,} - Address:/;
/**
* Parsing the output of iwlist, tool having a lot of different faces :-(
* @param str output of the tool
* @param callback
*/
function parseOutput(str, callback) {
var err = null;
var wifis = [];
let err = null;
let wifis = [];
try {
var blocks = str.split(cellRegex);
let blocks = str.split(cellRegex);
blocks.forEach(block => {
var network = {};
var lines = block.split('\n');
let network = {};
let lines = block.split('\n');
if (macRegex.exec(lines[0])) {
// First line is the mac address (always! (?))
network.mac = lines[0].trim();
@ -46,22 +47,21 @@ function parseOutput(str, callback) {
else if (_.startsWith(line.trim(), 'Channel:')) {
network.channel = parseInt(_.trim(line, ' )').split(/:/)[1]);
}
// Another ugly thing, the signal which can have different formats, even worse als
// having different identifiers
else if (line.indexOf('Signal level') > -1) {
if (line.indexOf('Quality') > -1) {
// This is a "Quality=40/70 Signal level=-70 dBm" line
network.rssi = parseInt(line.substr(line.indexOf('Signal level') + 13), 10);
}
else {
} else {
// This is a "Signal level=60/100" line
var elements = line.split('=');
let elements = line.split('=');
elements.forEach(e => {
if (e.indexOf('/') > 0) {
// that's our part
var parts = e.split('/');
var level = Math.floor(100 * parseInt(parts[0], 10) / parseInt(parts[1], 10));
let parts = e.split('/');
let level = Math.floor(100 * parseInt(parts[0], 10) / parseInt(parts[1], 10));
network.rssi = level / 2 - 100;
}
})

View File

@ -15,9 +15,9 @@ const detector = tool + ' show alias';
* an approach of analyzing the structure of the output
*/
function parseOutput(str, callback) {
var blocks = str.split('\n\n');
var wifis = [];
var err = null;
let blocks = str.split('\n\n');
let wifis = [];
let err = null;
try {
if (blocks.length < 2) {
// 2nd try, with \r\n
@ -40,18 +40,18 @@ function parseOutput(str, callback) {
// Channel : 6
// Basic rates (MBit/s) : 1 2 5.5 11
// Other rates (MBit/s) : 6 9 12 18 24 36 48 54
for (var i = 1, l = blocks.length; i < l; i++) {
var network = {};
var lines = blocks[i].split('\n');
var regexChannel = /[a-zA-Z0-9()\s]+:[\s]*[0-9]+$/g;
for (let i = 1, l = blocks.length; i < l; i++) {
let network = {};
let lines = blocks[i].split('\n');
let regexChannel = /[a-zA-Z0-9()\s]+:[\s]*[0-9]+$/g;
if (!lines || lines.length < 2) {
continue;
}
// First line is always the SSID (which can be empty)
var ssid = lines[0].substring(lines[0].indexOf(':') + 1).trim();
let ssid = lines[0].substring(lines[0].indexOf(':') + 1).trim();
for (var t = 1, n = lines.length; t < n; t++) {
for (let t = 1, n = lines.length; t < n; t++) {
if (lines[t].split(':').length === 7) {
// This is the mac address, use this one as trigger for a new network
if (network.mac) {
@ -61,14 +61,12 @@ function parseOutput(str, callback) {
ssid: ssid,
mac : lines[t].substring(lines[t].indexOf(':') + 1).trim()
};
}
else if (lines[t].indexOf('%') > 0) {
} else if (lines[t].indexOf('%') > 0) {
// Network signal strength, identified by '%'
var level = parseInt(lines[t].split(':')[1].split('%')[0].trim(), 10);
let level = parseInt(lines[t].split(':')[1].split('%')[0].trim(), 10);
network.rssi = (level / 2) - 100;
}
else if (!network.channel) {
} else if (!network.channel) {
// A tricky one: the channel is the first one having just ONE number. Set only
// if the channel is not already set ("Basic Rates" can be a single number also)
if (regexChannel.exec(lines[t].trim())) {