-
Notifications
You must be signed in to change notification settings - Fork 29
/
mdns-register.js
62 lines (57 loc) · 1.6 KB
/
mdns-register.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
//#!/usr/bin/env node
//process.env.DEBUG="dweb-mirror:mdns";
/*
* Register multicast DNS
*
* Adapted from https://github.com/mafintosh/register-multicast-dns that uses his `multicast-dns`
*
*
*/
const addr = require('network-address')
const multicastdns = require('multicast-dns')
const debug = require('debug')('dweb-mirror:mdns');
let mdns;
function registerMDNS(name) {
name = name.replace('.local', '')
debug("MDNS registering %s.local", name);
if (typeof mdns === "undefined") { // Setup if haven't already done so
mdns = multicastdns(); }
mdns.on('error', function () {
// ignore errors
})
mdns.on('query', function (query) {
query.questions.forEach(q => {
if ((q.name.replace('.local', '') === name) && ["A","AAAA"].includes(q.type)) {
debug("MDNS responding to query %s %s", q.type, q.name);
mdns.respond({
answers: [{
name: q.name,
type: 'A',
ttl: 300,
data: addr.ipv4()
}],
additionals: [{
name: q.name,
type: 'AAAA',
ttl: 300,
data: addr.ipv6()
}]
})
} else {
// This can get very verbose as Apple especially use MDNS a lot for services, just uncomment for debugging
// debug("MDNS ignoring query %s %s %O", q.type, q.name, query);
}
});
});
}
function destroyMDNS() {
if (mdns) {
debug("MDNS Destroying");
mdns.destroy;
mdns = undefined;
} else {
debug("MDNS Not started, no destroying");
}
}
//mdns.destroy;
exports = module.exports = {registerMDNS, destroyMDNS};