forked from friedrith/node-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac-current-connections.js
78 lines (72 loc) · 2.3 KB
/
mac-current-connections.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var execFile = require('child_process').execFile;
var macProvider =
'/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
var env = require('./env');
var networkUtils = require('./network-utils.js');
function parseAirport(stdout) {
var lines = stdout.split('\n');
var connections = [];
var connection = {};
lines.forEach(function(line) {
if (line.match(/[ ]*agrCtlRSSI: (.*)/)) {
connection.signal_level = parseInt(line.match(/[ ]*agrCtlRSSI: (.*)/)[1]);
connection.quality = networkUtils.qualityFromDB(
parseInt(line.match(/[ ]*agrCtlRSSI: (.*)/)[1])
);
} else if (line.match(/[ ]*BSSID: ([a-zA-Z0-1:]*)/)) {
var bssid = line.match(/[ ]*BSSID: ([0-9A-Fa-f:]*)/)[1];
bssid = bssid
.split(':')
.map(function(part) {
return part.length === 1 ? '0' + part : part;
})
.join(':');
connection.mac = bssid;
connection.bssid = bssid;
} else if (line.match(/[ ]*SSID: (.*)/)) {
connection.ssid = line.match(/[ ]*SSID: (.*)/)[1];
} else if (line.match(/[ ]*link auth: (.*)/)) {
connection.security = line.match(/[ ]*link auth: (.*)/)[1];
connection.security_flags = [];
} else if (line.match(/[ ]*channel: (.*)/)) {
connection.channel = parseInt(
line.match(/[ ]*channel: (.*)/)[1].split(',')[0]
);
connection.frequency = parseInt(
networkUtils.frequencyFromChannel(
parseInt(line.match(/[ ]*channel: (.*)/)[1].split(',')[0])
)
);
connections.push(connection);
connection = {};
}
});
return connections;
}
function getCurrentConnections(config, callback) {
var args = ['--getinfo'];
execFile(macProvider, args, env, function(err, stdout) {
if (err) {
callback && callback(err);
} else {
callback && callback(null, parseAirport(stdout));
}
});
}
module.exports = function(config) {
return function(callback) {
if (callback) {
getCurrentConnections(config, callback);
} else {
return new Promise(function(resolve, reject) {
getCurrentConnections(config, function(err, currentConnections) {
if (err) {
reject(err);
} else {
resolve(currentConnections);
}
});
});
}
};
};