-
Notifications
You must be signed in to change notification settings - Fork 72
/
iw.js
192 lines (174 loc) · 4.86 KB
/
iw.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) 2015 Christopher M. Baker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
var child_process = require('child_process');
/**
* The **iw** command is used to control nl80211 radios.
*
* @static
* @category iw
*
*/
var iw = module.exports = {
exec: child_process.exec,
scan: scan
};
/**
* Returns a truthy if the network has an ssid; falsy otherwise.
*
* @private
* @static
* @category iw
* @param {object} network The scanned network object.
* @returns {string} The ssid.
*
*/
function has_ssid(network) {
return network.ssid;
}
/**
* Returns a truthy if the network has any key; falsy otherwise.
*
* @private
* @static
* @category iw
* @param {object} network The scanned network object.
* @returns {boolean} True if any key.
*
*/
function has_keys(network) {
return Object.keys(network).length !== 0;
}
/**
* A comparison function to sort networks ordered by signal strength.
*
* @private
* @static
* @category iw
* @param {object} a A scanned network object.
* @param {object} b Another scanned network object.
* @returns {number} The comparison value.
*
*/
function by_signal(a, b) {
return b.signal - a.signal;
}
/**
* Parses a scanned wireless network cell.
*
* @private
* @static
* @category iw
* @param {string} cell The section of stdout for the cell.
* @returns {object} The scanned network object.
*
*/
function parse_cell(cell) {
var parsed = { };
var match;
if ((match = cell.match(/BSS ([0-9A-Fa-f:-]{17})\(on/))) {
parsed.address = match[1].toLowerCase();
}
if ((match = cell.match(/freq: ([0-9]+)/))) {
parsed.frequency = parseInt(match[1], 10);
}
if ((match = cell.match(/signal: (-?[0-9.]+) dBm/))) {
parsed.signal = parseFloat(match[1]);
}
if ((match = cell.match(/last seen: ([0-9]+) ms ago/))) {
parsed.lastSeenMs = parseInt(match[1], 10);
}
if ((match = cell.match(/SSID: \\x00/))) {
delete parsed.ssid;
}
else if ((match = cell.match(/SSID: ([^\n]*)/))) {
parsed.ssid = match[1];
}
if ((match = cell.match(/DS Parameter set: channel ([0-9]+)/))) {
parsed.channel = parseInt(match[1], 10);
}
else if ((match = cell.match(/\* primary channel: ([0-9]+)/))) {
parsed.channel = parseInt(match[1], 10);
}
if ((match = cell.match(/RSN:[\s*]+Version: 1/))) {
parsed.security = 'wpa2';
}
else if ((match = cell.match(/WPA:[\s*]+Version: 1/))) {
parsed.security = 'wpa';
}
else if ((match = cell.match(/capability: ESS Privacy/))) {
parsed.security = 'wep';
}
else if ((match = cell.match(/capability: ESS/))) {
parsed.security = 'open';
}
return parsed;
}
/**
* Parses all scanned wireless network cells.
*
* @private
* @static
* @category iw
* @param {function} callback The callback function.
*
*/
function parse_scan(show_hidden, callback) {
return function(error, stdout, stderr) {
if (error) callback(error);
else
if (show_hidden) {
callback(error, stdout
.split(/(^|\n)(?=BSS )/)
.map(parse_cell)
.filter(has_keys)
.sort(by_signal));
} else {
callback(error, stdout
.split(/(^|\n)(?=BSS )/)
.map(parse_cell)
.filter(has_ssid)
.sort(by_signal));
}
};
}
/**
* The **iw scan** command is used to scan for wireless networks
* visible to a wireless interface. For convenience, the networks are
* sorted by signal strength.
*
* @static
* @category iw
* @param {string} interface The wireless network interface.
* @param {function} callback The callback function.
*/
function scan(options, callback) {
var interface, show_hidden
if (typeof options === 'string') {
var interface = options;
var show_hidden = false;
} else {
var interface = options.iface;
var show_hidden = options.show_hidden || false;
}
this.exec('iw dev ' + interface + ' scan', parse_scan(show_hidden, callback));
}