Refactoring tool detection, work in progress

This commit is contained in:
Christian Kuster 2016-12-09 16:55:17 +01:00
parent 834dc4210e
commit 6ce58993a2
5 changed files with 35 additions and 14 deletions

View File

@ -5,6 +5,8 @@
const fs = require('fs'); const fs = require('fs');
const exec = require('child_process').exec; const exec = require('child_process').exec;
const async = require('async');
const _ = require('lodash');
// The tools // The tools
const airport = require('./lib/airport'); const airport = require('./lib/airport');
const iwlist = require('./lib/iwlist'); const iwlist = require('./lib/iwlist');
@ -14,25 +16,36 @@ var scanner;
// Initializing the tools // Initializing the tools
function initTools(callback) { function initTools(callback) {
fs.stat(airport.tool, function (err, stats) {
if (stats) {
return callback(null, airport);
}
fs.stat(iwlist.tool, function (err, stats) { // When a command is not found, an error is issued and async would finish. Therefore we pack
if (stats) { // the error into the result and check it later on.
return callback(null, iwlist); 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) { function (err, results) {
return callback(null, netsh); let res = _.find(results, f => {
} return !f.err
callback(new Error('No scanner found'));
}); });
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) { function scanNetworks(callback) {
exec(scanner.cmdLine, function (err, stdout) { exec(scanner.cmdLine, function (err, stdout) {
if (err) { if (err) {

View File

@ -5,6 +5,7 @@
const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'; const tool = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
const cmdLine = tool + ' -s'; 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}/; 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 = { module.exports = {
parseOutput: parseOutput, parseOutput: parseOutput,
cmdLine : cmdLine, cmdLine : cmdLine,
detector : detector,
tool : tool tool : tool
}; };

View File

@ -8,6 +8,8 @@ const _ = require('lodash');
// when the default location, /usr/bin/ is not in the PATH!). GitHub issue #1 // when the default location, /usr/bin/ is not in the PATH!). GitHub issue #1
const tool = 'iwlist'; const tool = 'iwlist';
const cmdLine = tool + ' scan'; 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 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:/; const cellRegex = /Cell [0-9]{2,} - Address:/;
@ -81,5 +83,6 @@ function parseOutput(str, callback) {
module.exports = { module.exports = {
parseOutput: parseOutput, parseOutput: parseOutput,
cmdLine : cmdLine, cmdLine : cmdLine,
detector : detector,
tool : tool tool : tool
}; };

View File

@ -6,6 +6,7 @@
const systemRoot = process.env.SystemRoot || 'C:\\Windows'; const systemRoot = process.env.SystemRoot || 'C:\\Windows';
const tool = systemRoot + '\\System32\\netsh.exe'; const tool = systemRoot + '\\System32\\netsh.exe';
const cmdLine = tool + ' wlan show networks mode=Bssid'; const cmdLine = tool + ' wlan show networks mode=Bssid';
const detector = tool + ' -h';
/** /**
* Parsing netnsh output. Unfortunately netsh supplies the network information * Parsing netnsh output. Unfortunately netsh supplies the network information
@ -90,5 +91,6 @@ function parseOutput(str, callback) {
module.exports = { module.exports = {
parseOutput: parseOutput, parseOutput: parseOutput,
cmdLine : cmdLine, cmdLine : cmdLine,
detector : detector,
tool : tool tool : tool
}; };

View File

@ -35,7 +35,8 @@
"test": "mocha test" "test": "mocha test"
}, },
"dependencies": { "dependencies": {
"lodash": "4.8.1" "async": "2.1.4",
"lodash": "4.17.2"
}, },
"devDependencies": { "devDependencies": {
"grunt": "0.4.5", "grunt": "0.4.5",