From 32b516e3020ff404a54cf60321e3a98f3ce650b8 Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Sat, 21 Sep 2019 11:56:22 +0200 Subject: [PATCH] Dependency updates, some code cleaned up --- bin/scan | 4 +- index.js | 4 +- lib/airport.js | 34 ++++++++------- lib/iwlist.js | 28 ++++++------- lib/netsh.js | 26 ++++++------ package-lock.json | 104 +++++++++++++++++++++++++++++----------------- package.json | 6 +-- test/airport.js | 6 +-- test/iwlist.js | 6 +-- test/netsh.js | 6 +-- 10 files changed, 125 insertions(+), 99 deletions(-) diff --git a/bin/scan b/bin/scan index 60c3a7a..959eab7 100755 --- a/bin/scan +++ b/bin/scan @@ -5,7 +5,7 @@ * Created by kc on 04.04.16. */ -var scanner = require('../index'); +const scanner = require('../index'); scanner.scan((err, output) => { if (err) { @@ -13,4 +13,4 @@ scanner.scan((err, output) => { return; } console.log(output); -}); \ No newline at end of file +}); diff --git a/index.js b/index.js index 6609a9e..fbfa006 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ const airport = require('./lib/airport'); const iwlist = require('./lib/iwlist'); const netsh = require('./lib/netsh'); -var scanner; +let scanner; // Initializing the tools function initTools(callback) { @@ -42,7 +42,7 @@ function initTools(callback) { } ], function (err, results) { - var res = _.find(results, + let res = _.find(results, function (f) { return !f.err }); diff --git a/lib/airport.js b/lib/airport.js index 86a37c4..ce41671 100644 --- a/lib/airport.js +++ b/lib/airport.js @@ -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); + } } diff --git a/lib/iwlist.js b/lib/iwlist.js index b16708e..38ac993 100644 --- a/lib/iwlist.js +++ b/lib/iwlist.js @@ -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; } }) diff --git a/lib/netsh.js b/lib/netsh.js index 53ed0cf..aa95983 100644 --- a/lib/netsh.js +++ b/lib/netsh.js @@ -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())) { diff --git a/package-lock.json b/package-lock.json index 8173abe..82a7f6f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-wifi-scanner", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -55,12 +55,9 @@ "dev": true }, "async": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/async/-/async-2.6.2.tgz", - "integrity": "sha512-H1qVYh1MYhEEFLsP97cVKqCGo7KfCyTt6uEWqsTBr9SO84oK9Uwbyd/yCW+6rKJLHksBNUVWZDAjfS+Ccx0Bbg==", - "requires": { - "lodash": "^4.17.11" - } + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/async/-/async-3.1.0.tgz", + "integrity": "sha512-4vx/aaY6j/j3Lw3fbCHNWP0pPaTCew3F6F3hYyl/tHs/ndmV1q7NW9T5yuJ2XAGwdQrP+6Wu20x06U4APo/iQQ==" }, "balanced-match": { "version": "1.0.0", @@ -175,9 +172,9 @@ }, "dependencies": { "semver": { - "version": "5.7.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", - "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "dev": true } } @@ -256,17 +253,21 @@ } }, "es-abstract": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", - "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", + "version": "1.14.2", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.14.2.tgz", + "integrity": "sha512-DgoQmbpFNOofkjJtKwr87Ma5EW4Dc8fWhD0R+ndq7Oc456ivUfGOOP6oAZTTKl5/CcNMP+EN+e3/iUzgE0veZg==", "dev": true, "requires": { "es-to-primitive": "^1.2.0", "function-bind": "^1.1.1", "has": "^1.0.3", + "has-symbols": "^1.0.0", "is-callable": "^1.1.4", "is-regex": "^1.0.4", - "object-keys": "^1.0.12" + "object-inspect": "^1.6.0", + "object-keys": "^1.1.1", + "string.prototype.trimleft": "^2.0.0", + "string.prototype.trimright": "^2.0.0" } }, "es-to-primitive": { @@ -745,9 +746,9 @@ } }, "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, "log-symbols": { "version": "2.2.0", @@ -851,9 +852,9 @@ } }, "mocha": { - "version": "6.1.2", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.1.2.tgz", - "integrity": "sha512-BgD2/RozoSC3uQK5R0isDcxjqaWw2n5HWdk8njYUyZf2NC79ErO5FtYVX52+rfqGoEgMfJf4fuG0IWh2TMzFoA==", + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.2.0.tgz", + "integrity": "sha512-qwfFgY+7EKAAUAdv7VYMZQknI7YJSGesxHyhn6qD52DV8UcSZs5XwCifcZGMVIE4a5fbmhvbotxC0DLQ0oKohQ==", "dev": true, "requires": { "ansi-colors": "3.2.3", @@ -865,12 +866,12 @@ "glob": "7.1.3", "growl": "1.10.5", "he": "1.2.0", - "js-yaml": "3.13.0", + "js-yaml": "3.13.1", "log-symbols": "2.2.0", "minimatch": "3.0.4", "mkdirp": "0.5.1", "ms": "2.1.1", - "node-environment-flags": "1.0.4", + "node-environment-flags": "1.0.5", "object.assign": "4.1.0", "strip-json-comments": "2.0.1", "supports-color": "6.0.0", @@ -904,16 +905,6 @@ "path-is-absolute": "^1.0.0" } }, - "js-yaml": { - "version": "3.13.0", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.0.tgz", - "integrity": "sha512-pZZoSxcCYco+DIKBTimr67J6Hy+EYGZDY/HCWC+iAEA9h1ByhMXAIVUXMcMFpOCxQ/xjXmPI2MkDL5HRm5eFrQ==", - "dev": true, - "requires": { - "argparse": "^1.0.7", - "esprima": "^4.0.0" - } - }, "supports-color": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.0.0.tgz", @@ -938,12 +929,21 @@ "dev": true }, "node-environment-flags": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.4.tgz", - "integrity": "sha512-M9rwCnWVLW7PX+NUWe3ejEdiLYinRpsEre9hMkU/6NS4h+EEulYaDH1gCEZ2gyXsmw+RXYDaV2JkkTNcsPDJ0Q==", + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.5.tgz", + "integrity": "sha512-VNYPRfGfmZLx0Ye20jWzHUjyTW/c+6Wq+iLhDzUI4XmhrDd9l/FozXV3F2xOaXjvp0co0+v1YSR3CMP6g+VvLQ==", "dev": true, "requires": { - "object.getownpropertydescriptors": "^2.0.3" + "object.getownpropertydescriptors": "^2.0.3", + "semver": "^5.7.0" + }, + "dependencies": { + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", + "dev": true + } } }, "nopt": { @@ -988,6 +988,12 @@ "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", "dev": true }, + "object-inspect": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", + "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", + "dev": true + }, "object-keys": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", @@ -1055,9 +1061,9 @@ "dev": true }, "p-limit": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", - "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", + "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", "dev": true, "requires": { "p-try": "^2.0.0" @@ -1329,6 +1335,26 @@ "strip-ansi": "^4.0.0" } }, + "string.prototype.trimleft": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/string.prototype.trimleft/-/string.prototype.trimleft-2.1.0.tgz", + "integrity": "sha512-FJ6b7EgdKxxbDxc79cOlok6Afd++TTs5szo+zJTUyow3ycrRfJVE2pq3vcN53XexvKZu/DJMDfeI/qMiZTrjTw==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "function-bind": "^1.1.1" + } + }, + "string.prototype.trimright": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/string.prototype.trimright/-/string.prototype.trimright-2.1.0.tgz", + "integrity": "sha512-fXZTSV55dNBwv16uw+hh5jkghxSnc5oHq+5K/gXgizHwAvMetdAJlHqqoFC1FSDVPYWLkAKl2cxpUT41sV7nSg==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "function-bind": "^1.1.1" + } + }, "strip-ansi": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", diff --git a/package.json b/package.json index ec0e7f8..1697973 100644 --- a/package.json +++ b/package.json @@ -35,12 +35,12 @@ "test": "mocha test" }, "dependencies": { - "async": "2.6.2", - "lodash": "^4.17.11" + "async": "3.1.0", + "lodash": "4.17.15" }, "devDependencies": { "grunt": "^1.0.3", "grunt-bump": "0.8.0", - "mocha": "^6.0.2" + "mocha": "6.2.0" } } diff --git a/test/airport.js b/test/airport.js index 8c0cdfc..71ef3ea 100644 --- a/test/airport.js +++ b/test/airport.js @@ -16,7 +16,7 @@ describe('airport', () => { assert.ok(info); assert.equal(info.length, 36); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, '00:35:1a:90:56:03'); assert.equal(ap.ssid, 'OurTest'); assert.equal(ap.rssi, -70); @@ -49,7 +49,7 @@ describe('airport', () => { assert.ok(info); assert.equal(info.length, 4); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, '7c:b7:33:ae:3b:06'); assert.equal(ap.ssid, 'Raupo'); assert.equal(ap.rssi, -80); @@ -64,4 +64,4 @@ describe('airport', () => { done(err); }); }); -}); \ No newline at end of file +}); diff --git a/test/iwlist.js b/test/iwlist.js index 4ded456..907f0eb 100644 --- a/test/iwlist.js +++ b/test/iwlist.js @@ -22,7 +22,7 @@ describe('iwlist', () => { assert.ok(info); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, 'D4:D1:84:50:76:45'); assert.equal(ap.ssid, 'gsy-97796'); assert.equal(ap.rssi, -76); @@ -40,7 +40,7 @@ describe('iwlist', () => { iwlist.parseOutput(fs.readFileSync(path.join(__dirname, 'fixtures','iwlist','iwlist03_raspi.txt'), { encoding: 'utf8' }), (err, info) => { assert.ok(info); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, '00:35:1A:90:56:00'); assert.equal(ap.ssid, 'LORA-Wifi'); assert.equal(ap.rssi, -71); @@ -55,4 +55,4 @@ describe('iwlist', () => { }); }); -}); \ No newline at end of file +}); diff --git a/test/netsh.js b/test/netsh.js index 81b2f73..2d0e50a 100644 --- a/test/netsh.js +++ b/test/netsh.js @@ -16,7 +16,7 @@ describe('netsh', function () { assert.ok(info); assert.equal(info.length, 86); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, '00:f2:8b:8c:a6:88'); assert.equal(ap.ssid, ''); assert.equal(ap.rssi, -88.5); @@ -55,7 +55,7 @@ describe('netsh', function () { assert.ok(info); assert.equal(info.length, 86); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, '00:f2:8b:8c:a6:88'); assert.equal(ap.ssid, ''); assert.equal(ap.rssi, -88.5); @@ -94,7 +94,7 @@ describe('netsh', function () { assert.ok(info); assert.equal(info.length, 8); - var ap = info[0]; + let ap = info[0]; assert.equal(ap.mac, '98:fc:11:b6:88:9e'); assert.equal(ap.ssid, 'CARAMANZANAS_BAJA'); assert.equal(ap.rssi, -86);