mdns-server.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict'
  2. var multicastdns = require('multicast-dns')
  3. var dnsEqual = require('dns-equal')
  4. var flatten = require('array-flatten')
  5. var deepEqual = require('deep-equal')
  6. module.exports = Server
  7. function Server (opts) {
  8. this.mdns = multicastdns(opts)
  9. this.mdns.setMaxListeners(0)
  10. this.registry = {}
  11. this.mdns.on('query', this._respondToQuery.bind(this))
  12. }
  13. Server.prototype.register = function (records) {
  14. var self = this
  15. if (Array.isArray(records)) records.forEach(register)
  16. else register(records)
  17. function register (record) {
  18. var subRegistry = self.registry[record.type]
  19. if (!subRegistry) subRegistry = self.registry[record.type] = []
  20. else if (subRegistry.some(isDuplicateRecord(record))) return
  21. subRegistry.push(record)
  22. }
  23. }
  24. Server.prototype.unregister = function (records) {
  25. var self = this
  26. if (Array.isArray(records)) records.forEach(unregister)
  27. else unregister(records)
  28. function unregister (record) {
  29. var type = record.type
  30. if (!(type in self.registry)) return
  31. self.registry[type] = self.registry[type].filter(function (r) {
  32. return r.name !== record.name
  33. })
  34. }
  35. }
  36. Server.prototype._respondToQuery = function (query) {
  37. var self = this
  38. query.questions.forEach(function (question) {
  39. var type = question.type
  40. var name = question.name
  41. // generate the answers section
  42. var answers = type === 'ANY'
  43. ? flatten.depth(Object.keys(self.registry).map(self._recordsFor.bind(self, name)), 1)
  44. : self._recordsFor(name, type)
  45. if (answers.length === 0) return
  46. // generate the additionals section
  47. var additionals = []
  48. if (type !== 'ANY') {
  49. answers.forEach(function (answer) {
  50. if (answer.type !== 'PTR') return
  51. additionals = additionals
  52. .concat(self._recordsFor(answer.data, 'SRV'))
  53. .concat(self._recordsFor(answer.data, 'TXT'))
  54. })
  55. // to populate the A and AAAA records, we need to get a set of unique
  56. // targets from the SRV record
  57. additionals
  58. .filter(function (record) {
  59. return record.type === 'SRV'
  60. })
  61. .map(function (record) {
  62. return record.data.target
  63. })
  64. .filter(unique())
  65. .forEach(function (target) {
  66. additionals = additionals
  67. .concat(self._recordsFor(target, 'A'))
  68. .concat(self._recordsFor(target, 'AAAA'))
  69. })
  70. }
  71. self.mdns.respond({ answers: answers, additionals: additionals }, function (err) {
  72. if (err) throw err // TODO: Handle this (if no callback is given, the error will be ignored)
  73. })
  74. })
  75. }
  76. Server.prototype._recordsFor = function (name, type) {
  77. if (!(type in this.registry)) return []
  78. return this.registry[type].filter(function (record) {
  79. var _name = ~name.indexOf('.') ? record.name : record.name.split('.')[0]
  80. return dnsEqual(_name, name)
  81. })
  82. }
  83. function isDuplicateRecord (a) {
  84. return function (b) {
  85. return a.type === b.type &&
  86. a.name === b.name &&
  87. deepEqual(a.data, b.data)
  88. }
  89. }
  90. function unique () {
  91. var set = []
  92. return function (obj) {
  93. if (~set.indexOf(obj)) return false
  94. set.push(obj)
  95. return true
  96. }
  97. }