Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(macos/scan): parse networks when shifted
A line was shifted in airport output (cf scan-shifted.log) and it was breaking parser for this line. It is now fixed
- Loading branch information
Showing
12 changed files
with
206 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
# travis.yml | ||
language: node_js | ||
node_js: | ||
- '8' | ||
- '10' | ||
install: | ||
- npm install | ||
script: | ||
- npm run commitlint | ||
- npm run lint | ||
- npm run format | ||
- npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,9 @@ | ||
var execFile = require('child_process').execFile; | ||
var env = require('./env'); | ||
const getCommand = require('./macOS/scan/command.js'); | ||
const execute = require('./utils/executer'); | ||
const promiser = require('./utils/promiser'); | ||
const command = require('./macOS/scan/command.js'); | ||
const parse = require('./macOS/scan/parser'); | ||
|
||
function scanWifi(config, callback) { | ||
const { cmd, args } = getCommand(config); | ||
const scanWifi = config => | ||
execute(command(config)).then(output => parse(output)); | ||
|
||
execFile(cmd, args, { env }, function(err, scanResults) { | ||
if (err) { | ||
callback && callback(err); | ||
} | ||
|
||
var resp = parse(scanResults); | ||
callback && callback(null, resp); | ||
}); | ||
} | ||
|
||
module.exports = function(config) { | ||
return function(callback) { | ||
if (callback) { | ||
scanWifi(config, callback); | ||
} else { | ||
return new Promise(function(resolve, reject) { | ||
scanWifi(config, function(err, networks) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(networks); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
}; | ||
module.exports = promiser(scanWifi); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
$ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s | ||
SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group) | ||
NERMNET 18:ff:7b:43:b5:26 -64 149 Y US WPA(PSK/TKIP,AES/TKIP) WPA2(PSK/TKIP,AES/TKIP) | ||
Linksys02787-invité 12:23:03:18:9f:1c -33 11 Y US NONE | ||
Linksys02787 10:23:03:1a:9f:1c -33 11 Y US WPA2(PSK/AES/AES) | ||
NERMNET 18:ff:7b:43:b5:27 -53 1,+1 Y -- WPA2(PSK/TKIP,AES/TKIP) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s | ||
SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group) | ||
Linksys02787 10:23:03:1a:9f:1c -33 11 Y US WPA2(PSK/AES/AES) | ||
Terminus 1 1e:27:e2:fa:c6:32 -26 4 Y -- WPA2(PSK/AES/AES) | ||
Linksys02787_5GHz 10:23:03:1a:9f:1d -51 36 Y US WPA2(PSK/AES/AES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
const getCommand = () => ({ | ||
const command = () => ({ | ||
cmd: | ||
'/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', | ||
args: ['-s'] | ||
}); | ||
|
||
module.exports = getCommand; | ||
module.exports = command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,60 @@ | ||
var networkUtils = require('../../network-utils.js'); | ||
|
||
const terms = { | ||
BSSID: 'BSSID', | ||
RSSI: 'RSSI', | ||
CHANNEL: 'CHANNEL', | ||
HT: 'HT', | ||
SECURITY: 'SECURITY', | ||
CC: 'CC' | ||
const isNotEmpty = line => line.trim() !== ''; | ||
|
||
const parseSecurity = security => { | ||
const securities = | ||
security === 'NONE' | ||
? [{ protocole: 'NONE', flag: '' }] | ||
: security | ||
.split(' ') | ||
.map(s => s.match(/(.*)\((.*)\)/)) | ||
.filter(Boolean) | ||
.map(([, protocole, flag]) => ({ | ||
protocole, | ||
flag | ||
})); | ||
|
||
return { | ||
security: securities.map(s => s.protocole).join(' '), | ||
security_flags: securities.filter(s => s.flag).map(s => `(${s.flag})`) | ||
}; | ||
}; | ||
|
||
const parse = stdout => { | ||
var lines = stdout.split('\n'); | ||
var colMac = lines[0].indexOf(terms.BSSID); | ||
var colRssi = lines[0].indexOf(terms.RSSI); | ||
var colChannel = lines[0].indexOf(terms.CHANNEL); | ||
var colHt = lines[0].indexOf(terms.HT); | ||
var colSec = lines[0].indexOf(terms.SECURITY); | ||
//var colCC = lines[0].indexOf(terms.CC); | ||
|
||
var wifis = []; | ||
for (var i = 1, l = lines.length; i < l; i++) { | ||
var bssid = lines[i].substr(colMac, colRssi - colMac).trim(); | ||
var securityFlags = lines[i].substr(colSec).trim(); | ||
var security = 'none'; | ||
if (securityFlags != 'NONE') { | ||
security = securityFlags.replace(/\(.*?\)/g, ''); | ||
securityFlags = securityFlags.match(/\((.*?)\)/g); | ||
} else { | ||
security = 'none'; | ||
securityFlags = []; | ||
} | ||
wifis.push({ | ||
mac: bssid, // for retrocompatibility | ||
bssid: bssid, | ||
ssid: lines[i].substr(0, colMac).trim(), | ||
channel: parseInt(lines[i].substr(colChannel, colHt - colChannel)), | ||
frequency: parseInt( | ||
networkUtils.frequencyFromChannel( | ||
lines[i].substr(colChannel, colHt - colChannel).trim() | ||
) | ||
), | ||
signal_level: lines[i].substr(colRssi, colChannel - colRssi).trim(), | ||
quality: networkUtils.dBFromQuality( | ||
lines[i].substr(colRssi, colChannel - colRssi).trim() | ||
), | ||
security: security, | ||
security_flags: securityFlags | ||
}); | ||
} | ||
wifis.pop(); | ||
return wifis; | ||
const lines = stdout.split('\n'); | ||
|
||
const [, ...otherLines] = lines; | ||
|
||
const networks = otherLines | ||
.filter(isNotEmpty) | ||
.map(line => line.trim()) | ||
.map(line => { | ||
const match = line.match( | ||
/(.*)\s+([a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2})\s+(-[0-9]+)\s+([0-9]+).*\s+([A-Z]+)\s+([a-zA-Z-]+)\s+([A-Z0-9(,)\s/]+)/ | ||
); | ||
|
||
if (match) { | ||
// eslint-disable-next-line no-unused-vars | ||
const [, ssid, bssid, rssi, channel, ht, countryCode, security] = match; | ||
|
||
return { | ||
mac: bssid, // for retrocompatibility | ||
bssid: bssid, | ||
ssid, | ||
channel: parseInt(channel), | ||
frequency: parseInt(networkUtils.frequencyFromChannel(channel)), | ||
signal_level: rssi, | ||
quality: networkUtils.dBFromQuality(rssi), | ||
...parseSecurity(security) | ||
}; | ||
} | ||
|
||
return false; | ||
}) | ||
.filter(Boolean); | ||
|
||
return networks; | ||
}; | ||
|
||
module.exports = parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
module.exports = func => config => callback => { | ||
if (typeof callback === 'function') { | ||
func(config, callback); | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
func(config, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
func(config) | ||
.then(response => { | ||
callback(null, response); | ||
}) | ||
.catch(error => { | ||
callback(error); | ||
}); | ||
}); | ||
} else { | ||
return func(config); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
const execute = require('../src/utils/executer'); | ||
const getCommand = require('../src/macOS/scan/command'); | ||
const command = require('../src/macOS/scan/command'); | ||
|
||
const { cmd, args } = getCommand(); | ||
const { cmd, args } = command(); | ||
|
||
console.log(`$ ${cmd} ${args.join(' ')}`); | ||
|
||
execute(cmd, args) | ||
execute({ cmd, args }) | ||
.then(output => console.log(output)) | ||
.catch(error => console.error(error)); |