-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi.js
304 lines (253 loc) · 10.3 KB
/
wifi.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(function() {
const WiFi = require(`windows.devices.wifi`);
const PasswordCredential = require(`windows.security.credentials`).PasswordCredential;
const devEnum = require(`windows.devices.enumeration`);
const Connectivity = require(`windows.networking.connectivity`);
const deasync = require('deasync');
const devInfo = devEnum.DeviceInformation;
const EventEmitter = require('events').EventEmitter;
const WiFiReconnectionKind = WiFi.WiFiReconnectionKind;
const WiFiConnectionStatus = WiFi.WiFiConnectionStatus;
const WiFiAdapter = WiFi.WiFiAdapter;
var adapter;
// Rough approximation of percentage quality based on quadratics and assuming a range of -100 dBm to -40 dBm (weak to strong).
function _rssiToPercent(rssi) {
if(rssi > -50) {
return 100;
}
rssi += 50;
return Math.max(Math.min(100 - Math.floor(100 * Math.abs(rssi)/50), 100), 1);
}
class wifi extends EventEmitter {
constructor (adapt) {
super();
if (deasync(WiFiAdapter.requestAccessAsync)() !== WiFi.WiFiAccessStatus.allowed) {
throw new Error('Current user does not have access to wifi adapters.');
}
if (adapt) {
// If an adapter is provided, try to use that.
if (adapt instanceof devInfo) {
if (adapt.isEnabled) {
adapter = deasync(WiFiAdapter.fromIdAsync)(adapt.id);
} else {
throw new Error('Provided adapter is disabled.');
}
} else {
throw new Error('Invalid adapter provided. Use wifi.listAdapters() to retrieve a list of valid adapters.');
}
} else {
// Otherwise, find the best adapter available.
// Looks for a default and if there is none takes the first enabled adapter.
let adapters = listAdapters();
var best;
for(adapt in adapters) {
adapt = adapters[adapt];
if (adapt.isEnabled) {
let wifiadapt = deasync(WiFiAdapter.fromIdAsync)(adapt.id);
if (adapt.isDefault) {
adapter = wifiadapt;
} else if (!best) {
best = wifiadapt;
}
}
}
if (!best) {
throw new Error('No enabled wifi adapters!');
} else {
adapter = best;
}
}
adapter.on('availableNetworksChanged', () => {
this.emit('availableNetworksChanged', availableNetworks());
});
}
}
wifi.listAdapters = listAdapters = function() {
let queryString = WiFiAdapter.getDeviceSelector();
let adapters = deasync(devEnum.DeviceInformation.findAllAsync)(queryString);
adapters = adapters.first();
let list = [];
var best;
while(adapters.hasCurrent) {
let adapt = adapters.current;
adapters.moveNext();
list.push(adapt);
}
return list;
}
// CONNECTION STATUSES-------------------
// unspecifiedFailure: 0,
// success: 1,
// accessRevoked: 2,
// invalidCredential: 3,
// networkNotAvailable: 4,
// timeout: 5,
// unsupportedAuthenticationProtocol: 6
// --------------------------------------
wifi.connectionStatus = WiFiConnectionStatus;
/**
* Usage: connect(ssid, [opts])
* auto defaults to true, but will not change a known network. To change a known network, use remember instead.
* if security is defined, there is no guarantee that the resulting profile will be accurate
* otherwise, it will scan for the given ssid and attempt to extract security information
* valid options: auto, password
*/
wifi.prototype.connect = connect = function (ssid, opts) {
if (!opts) {
opts = {};
}
let self = this;
let auto = opts.auto || true;
let password = opts.password;
return new Promise((resolve, reject) => {
let networks = adapter.networkReport.availableNetworks.first();
let network;
while(networks.hasCurrent) {
let net = networks.current;
networks.moveNext();
if (net.ssid === ssid) {
// Find the strongest BSSID associated with the given SSID.
if (!network || net.networkRssiInDecibelMilliwatts > network.networkRssiInDecibelMilliwatts) {
network = net;
}
}
}
if (network) {
let reconnectKind = auto ? WiFiReconnectionKind.automatic : WiFiReconnectionKind.manual;
if (password) {
let passcred = new PasswordCredential();
if (password && typeof password === 'string' && password.length > 0) {
passcred.password = password;
}
adapter.connectAsync(network, reconnectKind, passcred, handleConnectionStatus);
} else {
adapter.connectAsync(network, reconnectKind, handleConnectionStatus);
}
function handleConnectionStatus(err, result) {
let status = result.connectionStatus;
if (err) {
reject(err);
} else {
if (status === WiFiConnectionStatus.success) {
self.emit('networkConnected', ssid);
}
resolve(status);
}
}
} else {
reject(new Error('No networks available with the given SSID.'));
}
});
}
// Usage: wifi.disconnect()
wifi.prototype.disconnect = disconnect = function() {
adapter.disconnect();
}
// Usage: availableNetworks([ssid])
// ssid is anticipated as a regular expression. Will not match exactly unless the regex specifies such.
wifi.prototype.availableNetworks = availableNetworks = function(ssid) {
let connections = currentConnections();
let networks = adapter.networkReport.availableNetworks.first();
let results = {};
while(networks.hasCurrent) {
let net = networks.current;
networks.moveNext();
let network = {};
network.ssid = net.ssid;
network.signal = _rssiToPercent(net.networkRssiInDecibelMilliwatts)/100;
network.connected = connections.indexOf(net.ssid) >= 0;
network.security = _rewriteSecurity(net.securitySettings);
// Check if the ssid matches the given regex.
if ((!ssid || net.ssid.match(ssid)) &&
(!results[net.ssid] || network.signal > results[net.ssid].signal)) {
// If this is a new SSID, add it to the results.
// If this isn't new but is stronger, overwrtie the last result.
results[net.ssid] = network;
}
}
return results;
}
// Usage: scan()
wifi.prototype.scan = scan = function() {
adapter.scanAsync(() => {});
}
/* Auth Types
"none": 0,
"unknown": 1,
"open80211": 2,
"sharedKey80211": 3,
"wpa": 4,
"wpaPsk": 5,
"wpaNone": 6,
"rsna": 7,
"rsnaPsk": 8,
"ihv": 9
*/
function _rewriteSecurity(security) {
if (!security instanceof Connectivity.NetworkSecuritySettings) {
throw new Error("Invalid NetworkSecuritySettings object provided.");
}
const eTypes = Connectivity.NetworkEncryptionType;
const aTypes = Connectivity.NetworkAuthenticationType;
let authType = security.networkAuthenticationType;
let encryptType = security.networkEncryptionType;
switch(authType) {
// Secure types
case aTypes.wpa:
return 'WPA-Enterprise';
case aTypes.wpaPsk:
return 'WPA-Personal';
case aTypes.wpaNone:
return 'WPA-None';
case aTypes.rsna:
return 'WPA2-Enterprise';
case aTypes.rsnaPsk:
return 'WPA2-Personal';
// Not recommended
case aTypes.sharedKey80211:
return 'WEP Shared';
// Unknown types
case aTypes.unknown:
return 'Unknown'
case aTypes.ihv:
return 'IHV';
// Unsecure types
case aTypes.open80211:
return 'Open'
case aTypes.none:
return 'None';
default:
throw new Error(`Invalid auth type "${authType}"`);
}
}
// Usage: currentConnections()
wifi.currentConnections = currentConnections = function() {
let filter = new Connectivity.ConnectionProfileFilter();
filter.isConnected = true;
filter.isWlanConnectionProfile = true;
let netInfo = deasync(Connectivity.NetworkInformation.findConnectionProfilesAsync)(filter);
let connections = [];
netInfo = netInfo.first();
while(netInfo.hasCurrent) {
let conn = netInfo.current;
netInfo.moveNext();
connections.push(conn.wlanConnectionProfileDetails.getConnectedSsid());
}
return connections;
}
// Incomplete
// knownNetworks = function() {
// let filter = new Connectivity.ConnectionProfileFilter();
// filter.isWlanConnectionProfile = true;
// let profiles = deasync(Connectivity.NetworkInformation.findConnectionProfilesAsync)(filter);
// let known = [];
// profiles = profiles.first();
// while(profiles.hasCurrent) {
// let conn = profiles.current;
// profiles.moveNext();
// known.push(conn.profileName);
// }
// return known;
// }
module.exports = wifi;
})();