Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Options are:
- `protocol` (string, optional) - `udp` or `tcp` (default)
- `txt` (object, optional) - a key/value object to broadcast as the TXT
record
- `addresses` (array, optional) - array of IP addresses to advertise.
Array elements are IP address specifications as returned by
`os.networkInterfaces()`

IANA maintains a [list of official service types and port
numbers](http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml).
Expand Down
13 changes: 9 additions & 4 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function Browser (mdns, opts, onup) {

this._mdns = mdns
this._onresponse = null
this._serviceMap = {}
this._txt = dnsTxt(opts.txt)

if (!opts || !opts.type) {
Expand Down Expand Up @@ -82,7 +81,15 @@ Browser.prototype.start = function () {
if (matches.length === 0) return

matches.forEach(function (service) {
if (self._serviceMap[service.fqdn]) return // ignore already registered services
var oldservice = self.services.find(function(s) { return s.fqdn === service.fqdn })
if (oldservice !== undefined &&
JSON.stringify(oldservice.addresses) === JSON.stringify(service.addresses) &&
oldservice.name === service.name &&
oldservice.host === service.host &&
oldservice.port === service.port &&
oldservice.type === service.type &&
oldservice.protocol === service.protocol) return // ignore already registered services
self._removeService(service.fqdn)
self._addService(service)
})
})
Expand All @@ -105,7 +112,6 @@ Browser.prototype.update = function () {

Browser.prototype._addService = function (service) {
this.services.push(service)
this._serviceMap[service.fqdn] = true
this.emit('up', service)
}

Expand All @@ -120,7 +126,6 @@ Browser.prototype._removeService = function (fqdn) {
})
if (!service) return
this.services.splice(index, 1)
delete this._serviceMap[fqdn]
this.emit('down', service)
}

Expand Down
21 changes: 17 additions & 4 deletions lib/mdns-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var multicastdns = require('multicast-dns')
var dnsEqual = require('dns-equal')
var flatten = require('array-flatten')
var deepEqual = require('deep-equal')
var ipRangeCheck = require('ip-range-check')
var os = require('os')

module.exports = Server

Expand Down Expand Up @@ -43,7 +45,7 @@ Server.prototype.unregister = function (records) {
}
}

Server.prototype._respondToQuery = function (query) {
Server.prototype._respondToQuery = function (query, rinfo) {
var self = this
query.questions.forEach(function (question) {
var type = question.type
Expand Down Expand Up @@ -77,9 +79,20 @@ Server.prototype._respondToQuery = function (query) {
})
.filter(unique())
.forEach(function (target) {
additionals = additionals
.concat(self._recordsFor(target, 'A'))
.concat(self._recordsFor(target, 'AAAA'))
additionals = additionals.concat(
Object.values(os.networkInterfaces())
.flat()
.filter(
(addressInfo) =>
ipRangeCheck(rinfo.address, addressInfo.cidr)
)
.map((addressInfo) => ({
name: target,
type: addressInfo.family === 'IPv4' ? 'A' : 'AAAA',
ttl: 120,
data: addressInfo.address
}))
)
})
}

Expand Down
19 changes: 9 additions & 10 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Service (opts) {
this.subtypes = opts.subtypes || null
this.txt = opts.txt || null
this.published = false
this.addresses = opts.addresses || null

this._activated = false // indicates intent - true: starting/started, false: stopping/stopped
}
Expand All @@ -34,16 +35,14 @@ Service.prototype._records = function () {
var records = [rrPtr(this), rrSrv(this), rrTxt(this)]

var self = this
var interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach(function (name) {
interfaces[name].forEach(function (addr) {
if (addr.internal) return
if (addr.family === 'IPv4') {
records.push(rrA(self, addr.address))
} else {
records.push(rrAaaa(self, addr.address))
}
})
var addresses = self.addresses || Object.values(os.networkInterfaces()).flat()
addresses.forEach(function (addr) {
if (addr.internal) return
if (addr.family === 'IPv4') {
records.push(rrA(self, addr.address))
} else {
records.push(rrAaaa(self, addr.address))
}
})

return records
Expand Down
Loading