-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBrowserDevice.js
executable file
·669 lines (621 loc) · 18.9 KB
/
BrowserDevice.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
const HTTP = require('http');
const URL = require('url').URL;
const SNMP = require('net-snmp');
const DeviceInstance = require('./DeviceInstance');
const Pup = require('./Pup');
const Eval = require('./BrowserEval');
const DB = require('./Database');
const TypeConversion = require('./utils/TypeConversion');
const DeviceState = require('./DeviceState');
const Barrier = require('./utils/Barrier');
const Log = require('debug')('browser');
const LogContent = Log.extend('content');
const LogState = Log.extend('state');
const LogQ = Log.extend('q');
const LogSNMP = require('debug')('snmp');
const LogDontWrite = require('debug')('nowrite');
const TIMEOUT = { // in mseconds
loginNavigation: 60000,
validateNavigation: 60000,
ping: 3000,
};
const REFRESH_TIMING = 60 * 1000; // 1 minute
const UPDATE_RETRIES = 2;
const HTTPCODE_OK = 200;
class BrowserDeviceInstance extends DeviceInstance {
constructor(config, state, description) {
super(config, state);
this.description = description;
this._q = [];
this._watchCount = 0;
this._watchRunning = false;
this._authenticated = false; // Login information valid
this._validated = false; // Device is logged-in
this._watchFail = 0;
}
//
// Attach the device instance to a browser page
//
async attach() {
if (!this._page) {
Log('attaching:');
this._page = await Pup.connect(this.description.pup);
}
}
//
// Detatch the device instance from the real device.
//
detach() {
if (this._page) {
Pup.disconnect(this._page);
this._page = null;
}
if (this.session) {
this.session.close();
this.session = null;
}
}
//
// Watch for changes on this device.
// We periodically update the state from the physical device while the devie is being
// watched.
//
watch() {
this._watchCount++;
Log('watch:', this._id, this._watchCount);
if (!this._watchRunning) {
this._watchRunning = true;
this._watchFail = 0;
const TopologyManager = require('./TopologyManager');
const CaptureManager = require('./CaptureManager');
const task = async () => {
if (this._watchCount === 0) {
this._watchRunning = false;
return;
}
const start = Date.now();
if (!TopologyManager.running && !CaptureManager.running) {
try {
if (await this.update()) {
this._watchFail = 0;
await DB.updateDeviceState(this._id, this.state.toDB());
}
else {
throw new Error('watch update failed');
}
}
catch (e) {
Log('watch:error:', e);
this._watchFail++;
this.emit('watch.error');
}
}
setTimeout(task, Math.max(0, Date.now() - start + REFRESH_TIMING));
}
task();
}
return this._watchCount;
}
//
// Unwatch a device.
// Stop updating the state from the physical device is no one is watching.
// We dont immediately stop the watch task to avoid quick start/stop situations.
//
unwatch() {
if (this._watchCount > 0) {
this._watchCount--;
}
else {
this._watchFail = 0;
}
Log('unwatch:', this._id, this._watchCount);
return this._watchCount;
}
//
// Queue a function to be executed in order. This is used to prevent parallel tasks
// trying to use the browser page at the same time.
//
async q(fn) {
const here = new Error();
return new Promise(async (resolve, reject) => {
this._q.push(async () => {
try {
resolve(await fn(this._page));
}
catch (e) {
LogQ(e);
LogQ('from:');
LogQ(here);
reject(e);
}
});
if (this._q.length === 1) {
while (this._q.length) {
await this._q[0]();
this._q.shift();
}
}
});
}
async login(username, password) {
if (this.description.login) {
if (await this.webLogin(username, password)) {
return true;
}
}
else if (this.description.basicAuth) {
if (await this.basicAuth(username, password)) {
return true;
}
}
else {
Log('no login option found:');
}
return false;
}
logout(unauth) {
this._validated = false;
if (unauth) {
this._authenticated = false;
}
this.detach();
}
//
// Login to the real device using the given username and password.
//
async webLogin(username, password) {
// Immediate invalidate so we must successfully login
this._validated = false;
const login = this.description.login;
const url = this.url(login.path);
Log('login', username, password, url);
return await this.q(async (page) => {
try {
// Sanity check the URL
Log('ping login url');
const ping = await this.pingURL();
Log('pinged');
if (!ping) {
throw new Error(`Cannot ping ${url}`);
}
// Start the login process by navigating to the root page of the device.
Log('goto', url);
await page.goto(url, { timeout: TIMEOUT.loginNavigation, waitUntil: [ 'load', 'networkidle2' ] });
Log('goneto', url);
const frame = await Eval.getFrame(page, login.frame);
// Wait until the page has what we need.
const selectors = [];
if (typeof login.username === 'string') {
selectors.push(await frame.waitForSelector(login.username), { timeout: TIMEOUT.loginNavigation });
}
selectors.push(await frame.waitForSelector(login.password), { timeout: TIMEOUT.loginNavigation });
await Promise.all(selectors);
// Some devices have a username (other do not). Select the place to enter it.
if (login.username) {
Log('login', username);
await this.eval('literal', typeof login.username === 'string' ? { $: 'type', value: username, arg: login.username } : Object.assign({ value: username }, login.username), frame);
}
// All devices have a password. Select and enter that.
Log('password', password);
await this.eval('literal', typeof login.password === 'string' ? { $: 'type', value: password, arg: login.password } : Object.assign({ value: password }, login.password), frame);
// Activate the login. This probably involves clocking a button but other actions are possible.
Log('activate & wait', login.activate);
const responses = await Promise.all([
frame.waitForNavigation({ timeout: TIMEOUT.validateNavigation, waitUntil: [ 'load', 'networkidle2' ] }),
this.eval('click', login.activate, frame)
]);
Log('activated & waited', login.activate);
//
// Validate that login was successful.
//
let success = false;
if (!login.valid) {
// Default validation is to wait for page navigation to occur. If it does, we assume login was successful.
Log('waited for page navigation');
if (!responses[0] || responses[0].status() !== 200) {
success = false;
}
else {
success = true;
}
}
else {
// Alternatively we can look for an explict selector to appear on the page
Log('wait for selector');
const response = await this.eval('wait', login.valid, frame);
Log('waited for selector');
success = !!response;
}
//console.log('success', success);
this._authenticated = success;
this._validated = success;
Log('login', success);
return success;
}
catch (e) {
Log(e);
Log('login failed:', this.name);
// Dont await on this because it seems we can hang here until the request finally completes.
page.content().then(html => LogContent(html)).catch(_ => _);
this._validated = false;
return false;
}
});
}
async isLoggedIn() {
Log('isLoggedIn:');
return await this.q(async (page) => {
if (!page) {
throw new Error('Not connected');
}
if (!this.description.identify.http.loggedIn) {
Log('no logged in check:');
return null;
}
const url = this.url();
Log('ping', url);
if (!await this.pingURL()) {
throw new Error(`Cannot ping ${url}`);
}
Log('goto', url);
await page.goto(url, { timeout: TIMEOUT.loginNavigation, waitUntil: [ 'load', 'networkidle2' ] });
Log('eval', this.description.identify.http.loggedIn);
try {
return TypeConversion.toBoolean(await this.eval('literal', this.description.identify.http.loggedIn, page.mainFrame()));
}
catch (_) {
return false;
}
});
}
async basicAuth(username, password) {
// Immediate invalidate so we must successfully login
this._validated = false;
const login = this.description.basicAuth;
const url = this.url(login.path);
Log('basicAuth:', username, password, url);
return await this.q(async (page) => {
try {
// Sanity check the URL
Log('ping login url');
const ping = await this.pingURL();
Log('pinged');
if (!ping) {
throw new Error(`Cannot ping ${url}`);
}
// Add authentication. This will persist on the page and be sent with every request.
await page.authenticate({ username: username, password: password });
// Start the login process by navigating to the root page of the device.
Log('goto', url);
const response = await page.goto(url, { timeout: TIMEOUT.loginNavigation, waitUntil: [ 'load', 'networkidle2' ] });
Log('goneto', url);
Log('status:', response.status());
const success = (response.status() === HTTPCODE_OK);
this._authenticated = success;
this._validated = success;
Log('basicAuth', success);
return success;
}
catch (e) {
Log('fail:');
Log(e);
this._validated = false;
return false;
}
});
}
//
// Populate the devices state by scraping information from the authenticated hardware device.
//
async read() {
if (!this._validated) {
throw new Error(`Unauthenticated: ${this.name}`);
}
await this.q(async (page) => {
LogState('read:');
const result = await this.eval('literal', {
$0: this.description.constants,
$1: this.description.read
}, page.mainFrame());
Log('reading:');
LogState(JSON.stringify(result, null, 1));
// Sanity check
try {
if (typeof result.system.macAddress[0] !== 'string') {
throw Error('Missing mac address');
}
if (typeof result.system.ipv4.address !== 'string') {
throw Error('Missing ip address');
}
}
catch (e) {
// Whatever data we read from the device fails our basic sanity checks
Log('read failed:', JSON.stringify(result, null, 1));
Log('error:', e);
throw Error('device read failed');
}
this.mergeIntoState(result, true);
Log('readd:');
LogState(JSON.stringify(this.state.state, null, 1));
});
}
statisticsInfo() {
if (!this.description.read.$statistics) {
return null;
}
if (this.description.statistics) {
return this.description.statistics;
}
return {
prefer: null,
scale: 1
};
}
async statistics() {
if (!this._validated) {
throw new Error(`Unauthenticated: ${this.name}`);
}
await this.q(async (page) => {
Log('statistics:');
const stats = await this.eval('literal', this.description.read.$statistics, page.mainFrame());
this.mergeIntoState(stats, false, 'statistics');
Log('statistics:');
LogState(JSON.stringify(stats, null, 1));
});
}
async write() {
if (!this._validated) {
throw new Error(`Unauthenticated: ${this.name}`);
}
await this.q(async (page) => {
Log('write:');
if (LogDontWrite.enabled) {
LogDontWrite(JSON.stringify(this.readKV('$', { changes: true }), null, 2));
}
else {
await this.eval('literal', this.description.write, page.mainFrame());
}
Log('written:');
});
}
async commit() {
if (!this._validated) {
throw new Error(`Unauthenticated: ${this.name}`);
}
await this.q(async (page) => {
Log('commit:', this.name);
if (this.description.commit) {
await this.eval('literal', this.description.commit, page.mainFrame());
}
await super.commit();
Log('committed:', this.name);
});
}
//
// Connect to the hardware device. Despite whatever state we start it, if we can, we will
// be connected and authenticated once we're done.
connect = Barrier(async function() {
if (this._validated) {
Log('already connected:');
return true;
}
await this.attach();
// Some devices keep us logged in even when we think we disconnected
if (await this.isLoggedIn()) {
Log('device has us logged in:');
this._validated = true;
return true;
}
const keychain = this.readKV(DeviceState.KEY_SYSTEM_KEYCHAIN);
if (await this.login(keychain.username, keychain.password)) {
return true;
}
Log('failed to connect:');
return false;
})
//
// Update the local state so it reflects the actual device state.
// Connect and authenticate if necessary.
//
async update() {
this.emit('updating');
for (let retry = 0; retry < UPDATE_RETRIES; retry++) {
Log('update:', retry);
try {
if (await this.connect()) {
await this.read();
return true;
}
}
catch (e) {
Log(e);
Log('error during update');
this._validated = false;
this.detach();
}
}
return false;
}
async updateStatistics() {
Log('updating statistics');
try {
if (await this.connect()) {
await this.statistics();
return true;
}
}
catch (e) {
Log(e);
Log('error during updateStatistics');
this._validated = false;
this.detach();
}
return false;
}
async verify() {
Log('verify connection:', this.name);
try {
if (await this.connect()) {
if (this.description.read.$verify) {
await this.q(async (page) => {
Log('verify:');
await this.eval('literal', this.description.read.$verify, page.mainFrame());
Log('verified:');
});
}
else {
Log('no verify available:');
}
return true;
}
}
catch (e) {
Log(e);
Log('error during verify:', this.name);
this._validated = false;
this.detach();
}
return false;
}
url(path) {
const ipv4 = this.readKV(DeviceState.KEY_SYSTEM_IPV4);
return (new URL(path || '/', `http://${ipv4.address}:${ipv4.port}/`)).toString();
}
async pingURL() {
for (let retry = 2; retry > 0; retry--) {
const ping = await new Promise(resolve => {
const url = this.url();
let timer = null;
let req = HTTP.get(url, { host: (new URL(url)).host }, res => {
Log('ping response:', res.statusCode);
clearTimeout(timer);
resolve(true);
});
timer = setTimeout(() => {
Log('ping failed');
req.abort();
resolve(false);
}, TIMEOUT.ping);
req.once('error', err => Log('ping err:', err.toString()));
});
if (ping) {
return true;
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
return false;
}
async eval(def$, value, context) {
return await Eval.eval(def$, value, context, '$', this);
}
getSNMPSession() {
if (!this.session) {
const snmp = this.description.snmp || {};
const ipv4 = this.readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS);
switch (snmp.version || '1') {
case '1':
default:
this.session = SNMP.createSession(ipv4, snmp.community || 'public');
break;
case '2c':
this.session = SNMP.createSession(ipv4, snmp.community || 'public', { version: SNMP.Version2c });
break;
case '3':
const user = {
name: snmp.name,
level: SNMP.SecurityLevel.noAuthNoPriv
};
if (snmp.auth) {
const pwd = this.readKV(DeviceState.KEY_SYSTEM_KEYCHAIN_PASSWORD);
user.level = SNMP.SecurityLevel.authNoPriv;
user.authProtocol = SNMP.AuthProtocols[snmp.auth];
user.authKey = pwd;
if (snmp.priv) {
user.level = SNMP.SecurityLevel.authPriv;
user.privProtocol = snmp.priv;
user.privKey = pwd;
}
}
this.session = SNMP.createV3Session(ipv4, user);
break;
}
this.session.on('error', err => {
LogSNMP(err);
});
}
return this.session;
}
toDB() {
return {
_id: this._id,
dmId: this.description.id
};
}
}
class BrowserDevice {
constructor(description) {
this.description = description;
}
async identify(page, loggedIn, target) {
try {
if (loggedIn && this.description.generic) {
return false;
}
for (let ident in this.description.identify) {
switch (ident) {
case 'nsdp':
case 'escp':
case 'mdns':
case 'mndp':
case 'ddp':
if (target && target.type === ident) {
let match = true;
for (let key in this.description.identify[ident].txt) {
if (target.txt[key] != this.description.identify[ident].txt[key]) {
match = false;
break;
}
}
if (match) {
return true;
}
const expr = this.description.identify[ident].loggedOut;
if (expr) {
const ok = TypeConversion.toBoolean(await Eval.eval('literal', expr, page, '$', null));
if (ok) {
return true;
}
}
}
break;
case 'http':
const expr = loggedIn ? this.description.identify.http.loggedIn : this.description.identify.http.loggedOut;
if (expr) {
const ok = TypeConversion.toBoolean(await Eval.eval('literal', expr, page, '$', null));
if (ok) {
return true;
}
}
break;
default:
break;
}
}
}
catch (_) {
//console.log(_);
}
return false;
}
newInstance(config, state) {
return new BrowserDeviceInstance(config, state, this.description);
}
newInstanceFromGeneric(generic) {
const device = new BrowserDeviceInstance(generic, generic.state, this.description);
device._page = generic._page;
device._authenticated = generic._authenticated;
device._validated = generic._validated;
return device;
}
}
module.exports = BrowserDevice;