From b33de0ce941e8328132211e8c25e56959120bbb3 Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Wed, 8 Jun 2016 19:41:44 +0200 Subject: [PATCH 1/6] Lint issues only --- lib/airport.js | 2 +- lib/netsh.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/airport.js b/lib/airport.js index 739d449..da25aef 100644 --- a/lib/airport.js +++ b/lib/airport.js @@ -30,7 +30,7 @@ function parseOutput(str, callback) { 'ssid' : lines[i].substr(0, macStart).trim(), 'mac' : elements[0].trim(), 'channel' : parseInt(elements[2].trim(), 10), - 'rssi' : parseInt(elements[1].trim()) + 'rssi' : parseInt(elements[1].trim(), 10) }); } } diff --git a/lib/netsh.js b/lib/netsh.js index 0c0c753..06e8a35 100644 --- a/lib/netsh.js +++ b/lib/netsh.js @@ -71,7 +71,7 @@ function parseOutput(str, callback) { // 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())) { - network.channel = parseInt(lines[t].split(':')[1].trim()); + network.channel = parseInt(lines[t].split(':')[1].trim(), 10); } } } From 834dc4210e905513b750beaa7224e2124594a59b Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Fri, 9 Dec 2016 09:25:20 +0100 Subject: [PATCH 2/6] Test for GitHub issue #1: removed absolute path for iwlist --- lib/iwlist.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/iwlist.js b/lib/iwlist.js index 627a95c..801e17b 100644 --- a/lib/iwlist.js +++ b/lib/iwlist.js @@ -4,7 +4,9 @@ */ const _ = require('lodash'); -const tool = '/usr/bin/iwlist'; +// 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 macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/; From 6ce58993a2df434461136f4a65c8418f0e755f6d Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Fri, 9 Dec 2016 16:55:17 +0100 Subject: [PATCH 3/6] Refactoring tool detection, work in progress --- index.js | 39 ++++++++++++++++++++++++++------------- lib/airport.js | 2 ++ lib/iwlist.js | 3 +++ lib/netsh.js | 2 ++ package.json | 3 ++- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index badf58f..c2abdc7 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ const fs = require('fs'); const exec = require('child_process').exec; +const async = require('async'); +const _ = require('lodash'); // The tools const airport = require('./lib/airport'); const iwlist = require('./lib/iwlist'); @@ -14,25 +16,36 @@ var scanner; // Initializing the tools function initTools(callback) { - fs.stat(airport.tool, function (err, stats) { - if (stats) { - return callback(null, airport); - } - fs.stat(iwlist.tool, function (err, stats) { - if (stats) { - return callback(null, iwlist); + // When a command is not found, an error is issued and async would finish. Therefore we pack + // the error into the result and check it later on. + async.parallel([ + function (cb) { + exec(airport.detector, err => cb(null, {err: err, scanner: airport})); + }, + function (cb) { + exec(iwlist.detector, err => cb(null, {err: err, scanner: iwlist})); + }, + function (cb) { + exec(netsh.detector, err => cb(null, {err: err, scanner: netsh})); } - fs.stat(netsh.tool, function (err, stats) { - if (stats) { - return callback(null, netsh); - } - callback(new Error('No scanner found')); + ], + function (err, results) { + let res = _.find(results, f => { + return !f.err }); + + if (res) { + return callback(null, res.scanner); + } + callback(new Error('No scanner found')); }); - }); } +/** + * Scan the networks with the scanner detected before + * @param callback + */ function scanNetworks(callback) { exec(scanner.cmdLine, function (err, stdout) { if (err) { diff --git a/lib/airport.js b/lib/airport.js index da25aef..86a37c4 100644 --- a/lib/airport.js +++ b/lib/airport.js @@ -5,6 +5,7 @@ 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}/; /** @@ -45,5 +46,6 @@ function parseOutput(str, callback) { module.exports = { parseOutput: parseOutput, cmdLine : cmdLine, + detector : detector, tool : tool }; diff --git a/lib/iwlist.js b/lib/iwlist.js index 801e17b..a8dd1ab 100644 --- a/lib/iwlist.js +++ b/lib/iwlist.js @@ -8,6 +8,8 @@ const _ = require('lodash'); // when the default location, /usr/bin/ is not in the PATH!). GitHub issue #1 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:/; @@ -81,5 +83,6 @@ function parseOutput(str, callback) { module.exports = { parseOutput: parseOutput, cmdLine : cmdLine, + detector : detector, tool : tool }; diff --git a/lib/netsh.js b/lib/netsh.js index 06e8a35..da4dbcf 100644 --- a/lib/netsh.js +++ b/lib/netsh.js @@ -6,6 +6,7 @@ const systemRoot = process.env.SystemRoot || 'C:\\Windows'; const tool = systemRoot + '\\System32\\netsh.exe'; const cmdLine = tool + ' wlan show networks mode=Bssid'; +const detector = tool + ' -h'; /** * Parsing netnsh output. Unfortunately netsh supplies the network information @@ -90,5 +91,6 @@ function parseOutput(str, callback) { module.exports = { parseOutput: parseOutput, cmdLine : cmdLine, + detector : detector, tool : tool }; diff --git a/package.json b/package.json index c6b627b..69481f4 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "test": "mocha test" }, "dependencies": { - "lodash": "4.8.1" + "async": "2.1.4", + "lodash": "4.17.2" }, "devDependencies": { "grunt": "0.4.5", From 8a8005e2efc1ddc30a04c687c749dc797e00431c Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Fri, 9 Dec 2016 17:04:27 +0100 Subject: [PATCH 4/6] Windows fixes (work still in progress) --- index.js | 2 +- lib/netsh.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c2abdc7..3153032 100644 --- a/index.js +++ b/index.js @@ -74,4 +74,4 @@ module.exports = { } scanNetworks(callback); } -}; \ No newline at end of file +}; diff --git a/lib/netsh.js b/lib/netsh.js index da4dbcf..53ed0cf 100644 --- a/lib/netsh.js +++ b/lib/netsh.js @@ -6,7 +6,7 @@ const systemRoot = process.env.SystemRoot || 'C:\\Windows'; const tool = systemRoot + '\\System32\\netsh.exe'; const cmdLine = tool + ' wlan show networks mode=Bssid'; -const detector = tool + ' -h'; +const detector = tool + ' show alias'; /** * Parsing netnsh output. Unfortunately netsh supplies the network information From cfacdd6438beb387a8e8f266e3b8a1eb19c67db0 Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Fri, 9 Dec 2016 17:53:43 +0100 Subject: [PATCH 5/6] node 4 compliant again --- index.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 3153032..8a05da1 100644 --- a/index.js +++ b/index.js @@ -21,25 +21,39 @@ function initTools(callback) { // the error into the result and check it later on. async.parallel([ function (cb) { - exec(airport.detector, err => cb(null, {err: err, scanner: airport})); + exec(airport.detector, function (err) { + cb(null, {err: err, scanner: airport} + ) + } + ); }, function (cb) { - exec(iwlist.detector, err => cb(null, {err: err, scanner: iwlist})); + exec(iwlist.detector, function (err) { + cb(null, {err: err, scanner: iwlist} + ) + } + ); }, function (cb) { - exec(netsh.detector, err => cb(null, {err: err, scanner: netsh})); + exec(netsh.detector, function (err) { + cb(null, {err: err, scanner: netsh} + ) + } + ); } ], function (err, results) { - let res = _.find(results, f => { - return !f.err - }); + var res = _.find(results, + function (f) { + return !f.err + }); if (res) { return callback(null, res.scanner); } callback(new Error('No scanner found')); - }); + } + ); } /** @@ -63,7 +77,7 @@ module.exports = { */ scan: function (callback) { if (!scanner) { - initTools((err, s) => { + initTools(function (err, s) { if (err) { return callback(err); } From 530512408407e8e354efc1270f322cf6dab19665 Mon Sep 17 00:00:00 2001 From: Christian Kuster Date: Fri, 9 Dec 2016 19:12:16 +0100 Subject: [PATCH 6/6] New version added v1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 69481f4..d325494 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-wifi-scanner", - "version": "1.0.0", + "version": "1.1.0", "description": "node.js module for WiFi network detection", "main": "index.js", "keywords": [