-
Notifications
You must be signed in to change notification settings - Fork 3
/
NetworkScanner.js
executable file
·123 lines (114 loc) · 3.62 KB
/
NetworkScanner.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
const EventEmitter = require('events');
const Pup = require('./Pup');
const DeviceManager = require('./DeviceManager');
const DeviceInstanceManager = require('./DeviceInstanceManager');
const DeviceState = require('./DeviceState');
const Discovery = require('./discovery');
const DB = require('./Database');
const Log = require('debug')('scan');
const DEFAULT_PAGE_TIMEOUT = 2000;
class Scanner extends EventEmitter {
constructor(config) {
super();
this.config = config;
this.running = false;
}
async start() {
this.running = true;
const addresses = [].concat(this.config.addresses);
// Sort the net adresses to the end (they're the slowest to identify)
addresses.sort((a, b) => a.type === 'net' ? 1 : -1);
Log('scan:', addresses);
this.emit('status', { op: 'running' });
for (let i = 0; i < addresses.length && this.running; i++) {
if (!DeviceInstanceManager.getDeviceByIP(addresses[i].ip)) {
this.emit('status', { op: 'scanning', address: addresses[i] });
const device = await this.identify(addresses[i]);
if (device && this.running) {
this.emit('status', { op: 'found', address: addresses[i] });
const state = DeviceState.newInstance();
state.mergeIntoState(device.description.constants);
state.writeKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS, addresses[i].ip, { track: false });
state.writeKV(DeviceState.KEY_SYSTEM_IPV4_PORT, addresses[i].port, { track: false });
DeviceInstanceManager.addDevice(device.newInstance({ _id: DB.newDeviceId() }, state));
}
}
}
if (this.running) {
this.emit('status', { op: 'done' });
this.running = false;
}
}
stop() {
this.emit('status', { op: 'canceled' });
this.running = false;
}
async identify(target) {
Log('identify:', target);
const devices = DeviceManager.getDevices();
const page = await Pup.connect();
try {
Log('goto', target.ip);
await page.goto(`http://${target.ip}:${target.port}`, { timeout: this.config.pageTimeout || DEFAULT_PAGE_TIMEOUT, waitUntil: [ 'load', 'networkidle2' ] });
//Log('goneto', await page.content());
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
if (!device.description.generic && await device.identify(page, false, target)) {
Log('identified:', device.description.id);
return device;
}
}
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
if (device.description.generic && await device.identify(page, false, target)) {
Log('identified:', device.description.id, '(generic)');
return device;
}
}
}
catch (e) {
Log(e);
}
finally {
Pup.disconnect(page);
}
return null;
}
};
module.exports = {
start: function() {
let scanner = null;
let pending = false;
const backgroundScan = () => {
if (scanner) {
pending = true;
return;
}
Log('starting background scan');
pending = false;
scanner = new Scanner({
addresses: Discovery.getAddresses(),
pageTimeout: 20000
});
scanner.on('status', evt => {
switch (evt.op) {
case 'running':
break;
case 'done':
Log('finished background scan');
scanner = null;
if (pending) {
setTimeout(backgroundScan, 0);
}
break;
default:
break;
}
});
scanner.start();
}
Discovery.on('update', backgroundScan);
},
stop: function() {
}
}