-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdns.js
472 lines (423 loc) · 14.7 KB
/
mdns.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
// import console from './console';
let _os = require('os');
let _mdnsServer = require('mdns-server');
let _debugMdns = false;
let _logMdns = true;
let _debug = function(s, _bs) {
if (!_debugMdns) {
return;
}
console.info(`mDNS debug: ${s}`);
};
let _log = function(s, _bs) {
if (!_logMdns) {
return;
}
console.info(`mDNS debug: ${s}`);
};
let _important = function(s, _bs) {
console.info(`mDNS ${s}`);
};
let _warn = function(s, _bs) {
console.info(`mDNS ${s}`);
};
// let _stripTrailingPeriod = function(val) {
// return _.replace(val, /\..+$/, '');
// };
let _buildDiscoveryMessage = function(entry) {
_debug(`building discovery message from ${JSON.stringify(entry)}`);
return {
deviceId: entry.deviceId,
found: entry.found,
deviceType: 'G3',
ipAddress: `${entry.ipAddress}:${entry.port}`,
ipv6: entry.ipv6,
hostname: entry.hostname,
origin: entry.hostname
};
};
let _buildServiceEntry = function(deviceId, found, deviceType, ipAddress, ipv6, lastIpAddress, lastIpv6, hostname,
text, lastSeenIpv4, lastSeenIpv6, upTrigger) {
return {
deviceId,
found, // Found means that we have at least one proper ip adress for the glasses!
deviceType,
ipAddress,
ipv6,
lastIpAddress,
lastIpv6,
hostname,
text, // Typically path=/rest/
lastSeenIpv4, // Timestamp for most recent A answer for ipv4.
lastSeenIpv6, // Timestamp for most recent AAA answer for ipv6.
upTrigger // Trigger to send up, this is delayed by 5 sec to get potential second ip address.
};
};
class MDNS {
constructor() {
this.services = new Map(); // Remember each discovered glasses here.
this.onUpCallback = undefined; // Callback when glasses is detected as up.
this.onDownCallback = undefined; // Callback when glasses is detected as down.
this.networkInterfaces = ''; // Summary of the existing network interfaces.
this.mdnsService = undefined; // The mdns-server instance.
this.nextGlassesQuery = undefined; // Timeout to next query.
this.ifaceSummary = '';
}
onUp = (callback) => {
this.onUpCallback = callback;
};
onDown = (callback) => {
this.onDownCallback = callback;
};
sendUp = (entry) => {
if (entry.found === false ||
(entry.ipAddress !== '' && entry.ipAddress !== entry.lastIpAddress) ||
(entry.ipv6 !== '' && entry.ipv6 !== entry.lastIpv6)) {
let ipv4 = entry.ipAddress;
let {ipv6} = entry;
if (entry.ipAddress !== entry.lastIpAddress || entry.ipv6 !== entry.lastIpv6) {
this.sendDown(entry);
}
if (ipv4 !== 'delete') {
entry.ipAddress = ipv4;
}
if (ipv6 !== 'delete') {
entry.ipv6 = ipv6;
}
let clear_up_trigger = false;
if (entry.ipAddress !== '' && entry.lastIpAddress !== entry.ipAddress) {
entry.lastIpAddress = entry.ipAddress;
clear_up_trigger = true;
}
if (entry.ipv6 !== '' && entry.lastIpv6 !== entry.ipv6) {
entry.lastIpv6 = entry.ipv6;
clear_up_trigger = true;
}
if (clear_up_trigger) {
clearTimeout(entry.upTrigger);
_debug(`clearing upTrigger and queuing up for ${entry.deviceId}`);
// Add a delay of 5 seconds, so that we can receive a second ip adress before upping the service.
// Typically first the A adress is set, then the AAAA is set. The delay
// prevents the service from going down/up unecessarily. It really takes quite a bit of time here....
entry.upTrigger = setTimeout(() => {
_debug(`should up? ${entry.deviceId}`);
if (entry.ipAddress !== '' || entry.ipv6 !== '') {
_debug(`yep got at leas one ip, sending up for ${entry.deviceId}`);
entry.found = true;
this.onUpCallback(_buildDiscoveryMessage(entry));
} else {
_warn(`Oups, time to send up, but no valid ip address! ${entry.deviceId}`);
}
}, 5000);
}
} else {
_debug(`Not sending up for ${entry.deviceId} already up and no change detected.`);
}
};
sendDown = (entry) => {
if (entry.found === true) {
clearTimeout(entry.upTrigger);
entry.found = false;
_debug(`sending down for ${entry.deviceId} and clear its upTrigger`);
// Install the addresses used when this service was taken up!
// Maybe not strictly necessary, but will reduce confusion of the callback receipient.
entry.ipAddress = entry.lastIpAddress;
entry.ipv6 = entry.lastIpv6;
// Now invoke callback.
this.onDownCallback(_buildDiscoveryMessage(entry));
entry.ipAddress = '';
entry.ipv6 = '';
entry.lastIpAddress = '';
entry.lastIpv6 = '';
entry.lastSeenIpv4 = 0;
entry.lastSeenIpv6 = 0;
} else {
_debug(`Not sending down for ${entry.deviceId} already down.`);
}
};
idExistsInCache = (id) => this.services.has(id);
lookupCacheFromId = (id) => this.services.get(id);
gotPTR = (id, type) => {
let entry = {};
if (this.services.has(id)) {
entry = this.services.get(id);
} else {
entry = _buildServiceEntry(id, false, type, '', '', '', '', '', '', 0, 0, undefined);
this.services.set(id, entry);
}
_debug(`service ${id} is alive"`);
};
markLastSeen = (hostname, ipversion) => {
for (let id of this.services.keys()) {
let entry = this.lookupCacheFromId(id);
if (entry.hostname === hostname) {
if (ipversion === 'ipv4') {
entry.lastSeenIpv4 = (new Date()).getTime();
}
if (ipversion === 'ipv6') {
entry.lastSeenIpv6 = (new Date()).getTime();
}
}
}
};
updateTextInfo = (id, info) => {
if (this.idExistsInCache(id)) {
let entry = this.lookupCacheFromId(id);
if (entry.text !== info) {
entry.text = info;
_log(`service ${id} uses text ${info}`);
}
}
};
updateServerInfo = (id, hostname, port) => {
if (this.idExistsInCache(id)) {
let entry = this.lookupCacheFromId(id);
if (entry.hostname !== hostname || entry.port !== port) {
entry.hostname = hostname;
entry.port = port;
_log(`service ${id} uses hostname ${hostname} and port ${port}`);
}
}
};
updateIP = (hostname, ipv4, ipv6) => {
for (let id of this.services.keys()) {
let entry = this.lookupCacheFromId(id);
if (entry.hostname === hostname) {
this.takeUpIfPossible(entry, ipv4, ipv6);
}
}
};
takeUpIfPossible = (entry, ipv4, ipv6) => {
if (ipv4 !== '' && entry.ipAddress !== ipv4) {
entry.ipAddress = ipv4;
_log(`new ipv4 address ${ipv4} for hostname ${entry.hostname}`);
}
if (ipv6 !== '' && entry.ipv6 !== ipv6) {
entry.ipv6 = ipv6;
_log(`new ipv6 address ${ipv6} for hostname ${entry.hostname}`);
}
this.sendUp(entry);
};
clearSummary = () => {
this.ifaceSummary = '';
};
addToSummary = (x) => {
this.ifaceSummary = `${this.ifaceSummary}--${x}`;
};
checkIfNetworkInterfacesChanged = () => {
this.clearSummary();
let ifaces = _os.networkInterfaces();
// Iterate over the existing named interfaces: lo, etho, wlp4s0 etc.
for (let iface in ifaces) {
let arr = ifaces[iface];
// Iterate over the network addresses stored for the named interfaces.
let arrok = arr.filter(function(o) {
return o.internal === false;
});
arrok.forEach((x) => {
// Accumlate only the non-internal network address into the status summary.
this.addToSummary(`(${iface},${x.family},${x.address},${x.netmask})`);
});
}
if (this.networkInterfaceSummary !== this.ifaceSummary) {
_debug(`old network interfaces: ${this.networkInterfaceSummary}`);
_debug(`new network interfaces: ${this.ifaceSummary}`);
let info = this.ifaceSummary;
if (info === '') {
info = '<no interfaces found>';
}
_important(`network interfaces changed from: ${this.networkInterfaceSummary}`);
_important(` to: ${info}`);
this.networkInterfaceSummary = this.ifaceSummary;
_log('network interfaces changed, trigger a restart of the mDNS service');
this.restart();
return true;
}
return false;
};
sendGlassesQuery = () => {
clearTimeout(this.nextGlassesQuery);
if (this.mdnsService === undefined) {
// This module is shutting down! Bail out.
return;
}
if (this.checkIfNetworkInterfacesChanged()) {
// Bail out and expect to be called again, when the mDNS service has restarted.
return;
}
this.detectLostGlasses();
_debug('sending query for _tobii-g3api._tcp.local');
try {
this.mdnsService.query({
questions: [{
name: '_tobii-g3api._tcp.local',
type: 'PTR'
}]
});
this.nextGlassesQuery = setTimeout(this.sendGlassesQuery, 5000);
} catch (err) {
this.handleError(err);
}
};
detectLostGlasses = () => {
let now = new Date();
let now_millis = now.getTime();
_debug(`checking lastSeen for services ${now}`);
for (let id of this.services.keys()) {
let entry = this.lookupCacheFromId(id);
let delta_ipv4_s = (now_millis - entry.lastSeenIpv4) / 1000;
if (entry.lastSeenIpv4 === 0) {
delta_ipv4_s = -1;
}
let delta_ipv6_s = (now_millis - entry.lastSeenIpv6) / 1000;
if (entry.lastSeenIpv6 === 0) {
delta_ipv6_s = -1;
}
_log(`service ${id} seen seconds ago: ipv4 ${delta_ipv4_s} ipv6 ${delta_ipv6_s}`);
let must_update = false;
let new_ipv4 = entry.ipAddress;
let new_ipv6 = entry.ipv6;
if (delta_ipv4_s > 12 && entry.ipAddress !== '') {
new_ipv4 = 'delete';
must_update = true;
}
if (delta_ipv6_s > 12 && entry.ipv6 !== '') {
new_ipv6 = 'delete';
must_update = true;
}
// Oups! It has gone more than 12 seconds since we last saw an A or AAAA response.
if (must_update) {
// This will trigger a down of the services with this hostname.
_important(`service ${id} must update ips ipv4 ${new_ipv4} ipv6 ${new_ipv6} `);
this.updateIP(entry.hostname, new_ipv4, new_ipv6);
}
}
};
handleQuery = (query) => {
_debug(`received query packet: ${JSON.stringify(query.questions)}`, this);
};
handleDestroy = () => {
_debug('mDNS service destroyed.', this);
};
handleReady = () => {
_log('mDNS service started');
this.sendGlassesQuery();
};
handleResponse = (response) => {
_debug(`received a response packet with ${response.answers.length} answers.`);
response.answers.forEach((answer) => {
let data_as_json = JSON.stringify(answer.data);
_debug(`answer ${answer.name}, type: ${answer.type}, data: ${data_as_json}`);
if (answer.name === '_tobii-g3api._tcp.local' &&
answer.type === 'PTR') {
// Example: Name: _tobii-g3api._tcp.local, type: PTR, data: "TG03B-080200100321._tobii-g3api._tcp.local"
let id = answer.data;
let type = answer.name;
this.gotPTR(id, type);
}
if (answer.type === 'TXT') {
// Example: mDNS debug: answer TG03B-080200100321._tobii-g3api._tcp.local, type: TXT,
// data: {"type":"Buffer","data":[11,112,97,116,104,61,47,114,101,115,116,47]}
let id = answer.name;
if (Buffer.isBuffer(answer.data)) {
let info = answer.data.toString(); // Strip initial char 11, why is it there? A length indicator?
this.updateTextInfo(id, info);
}
}
if (answer.type === 'SRV') {
// Example: Name: TG03B-080200100321._tobii-g3api._tcp.local, type: SRV,
// data: {"priority":0,"weight":0,"port":80,"target":"TG03B-080200100321.local"
let id = answer.name;
let hostname = answer.data.target;
let {port} = answer.data;
this.updateServerInfo(id, hostname, port);
}
if (answer.type === 'A') {
// Example: Name: TG03B-080200100321.local, type: A, data: "192.168.75.51"
let hostname = answer.name;
let ipv4 = answer.data;
this.updateIP(hostname, ipv4, '');
this.markLastSeen(hostname, 'ipv4');
}
if (answer.type === 'AAAA') {
// Example: Name: TG03B-080200100321.local, type: A, data: "1111:2222:3333:4444:5555:6666:7777:8888"
let hostname = answer.name;
let ipv6 = answer.data;
this.updateIP(hostname, '', ipv6);
this.markLastSeen(hostname, 'ipv6');
}
});
};
restart = () => {
this.stop();
this.start();
};
stop = () => {
if (this.mdnsService) {
try {
_debug('stopping mDNS service');
this.mdnsService.destroy();
} catch (err) {
_debug(`error message: ${JSON.stringify(err)}`);
_warn('error when stopping mDNS service');
}
this.mdnsService = undefined;
_debug('stopped mDNS service');
}
};
handleError = (err) => {
if (this.mdnsService) {
_warn(`${err} Restarting service in 5 seconds`);
this.stop();
// Try to start again in 5 seconds....
this.detectLostGlasses();
setTimeout(this.start, 5000);
} else {
_warn(`${err}`);
}
};
handleWarning = (warn) => {
_warn(`${warn}`, this);
};
start = () => {
if (this.onUpCallback === undefined) {
_warn('cannot start mDNS service because onUp callback is not set!');
return;
}
if (this.onDownCallback === undefined) {
_warn('cannot start mDNS service because onDown callback is not set!');
return;
}
if (this.mdnsService !== undefined) {
_warn('cannot start mDNS service since it seems to be already started!');
return;
}
if (this.checkIfNetworkInterfacesChanged()) {
// Bail out and expect to be called again.
return;
}
_debug('starting mDNS service');
try {
this.mdnsService = _mdnsServer({
reuseAddr: true, // in case other mdns service is running
loopback: false, // receive our own mdns messages
noInit: true // do not initialize on creation
});
this.mdnsService.on('error', this.handleError);
this.mdnsService.on('warning', this.handleWarning);
this.mdnsService.on('query', this.handleQuery);
this.mdnsService.on('response', this.handleResponse);
this.mdnsService.on('destroyed', this.handleDestroy);
this.mdnsService.on('ready', this.handleReady);
this.mdnsService.initServer();
_debug('started mDNS');
} catch (err) {
this.handleError(err);
}
};
}
// export default MDNS;
let discovery = new MDNS();
discovery.onUp( (dm) => { console.log('\n\n********* UP '+JSON.stringify(dm)+'\n\n'); });
discovery.onDown( (dm) => { console.log('\n\n********* DOWN '+JSON.stringify(dm)+'\n\n'); });
discovery.start();