registry.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict'
  2. var dnsEqual = require('dns-equal')
  3. var flatten = require('array-flatten')
  4. var Service = require('./service')
  5. var REANNOUNCE_MAX_MS = 60 * 60 * 1000
  6. var REANNOUNCE_FACTOR = 3
  7. module.exports = Registry
  8. function Registry (server) {
  9. this._server = server
  10. this._services = []
  11. }
  12. Registry.prototype.publish = function (opts) {
  13. var service = new Service(opts)
  14. service.start = start.bind(service, this)
  15. service.stop = stop.bind(service, this)
  16. service.start({ probe: opts.probe !== false })
  17. return service
  18. }
  19. Registry.prototype.unpublishAll = function (cb) {
  20. teardown(this._server, this._services, cb)
  21. this._services = []
  22. }
  23. Registry.prototype.destroy = function () {
  24. this._services.forEach(function (service) {
  25. service._destroyed = true
  26. })
  27. }
  28. function start (registry, opts) {
  29. if (this._activated) return
  30. this._activated = true
  31. registry._services.push(this)
  32. if (opts.probe) {
  33. var service = this
  34. probe(registry._server.mdns, this, function (exists) {
  35. if (exists) {
  36. service.stop()
  37. service.emit('error', new Error('Service name is already in use on the network'))
  38. return
  39. }
  40. announce(registry._server, service)
  41. })
  42. } else {
  43. announce(registry._server, this)
  44. }
  45. }
  46. function stop (registry, cb) {
  47. if (!this._activated) return // TODO: What about the callback?
  48. teardown(registry._server, this, cb)
  49. var index = registry._services.indexOf(this)
  50. if (index !== -1) registry._services.splice(index, 1)
  51. }
  52. /**
  53. * Check if a service name is already in use on the network.
  54. *
  55. * Used before announcing the new service.
  56. *
  57. * To guard against race conditions where multiple services are started
  58. * simultaneously on the network, wait a random amount of time (between
  59. * 0 and 250 ms) before probing.
  60. *
  61. * TODO: Add support for Simultaneous Probe Tiebreaking:
  62. * https://tools.ietf.org/html/rfc6762#section-8.2
  63. */
  64. function probe (mdns, service, cb) {
  65. var sent = false
  66. var retries = 0
  67. var timer
  68. mdns.on('response', onresponse)
  69. setTimeout(send, Math.random() * 250)
  70. function send () {
  71. // abort if the service have or is being stopped in the meantime
  72. if (!service._activated || service._destroyed) return
  73. mdns.query(service.fqdn, 'ANY', function () {
  74. // This function will optionally be called with an error object. We'll
  75. // just silently ignore it and retry as we normally would
  76. sent = true
  77. timer = setTimeout(++retries < 3 ? send : done, 250)
  78. timer.unref()
  79. })
  80. }
  81. function onresponse (packet) {
  82. // Apparently conflicting Multicast DNS responses received *before*
  83. // the first probe packet is sent MUST be silently ignored (see
  84. // discussion of stale probe packets in RFC 6762 Section 8.2,
  85. // "Simultaneous Probe Tiebreaking" at
  86. // https://tools.ietf.org/html/rfc6762#section-8.2
  87. if (!sent) return
  88. if (packet.answers.some(matchRR) || packet.additionals.some(matchRR)) done(true)
  89. }
  90. function matchRR (rr) {
  91. return dnsEqual(rr.name, service.fqdn)
  92. }
  93. function done (exists) {
  94. mdns.removeListener('response', onresponse)
  95. clearTimeout(timer)
  96. cb(!!exists)
  97. }
  98. }
  99. /**
  100. * Initial service announcement
  101. *
  102. * Used to announce new services when they are first registered.
  103. *
  104. * Broadcasts right away, then after 3 seconds, 9 seconds, 27 seconds,
  105. * and so on, up to a maximum interval of one hour.
  106. */
  107. function announce (server, service) {
  108. var delay = 1000
  109. var packet = service._records()
  110. server.register(packet)
  111. ;(function broadcast () {
  112. // abort if the service have or is being stopped in the meantime
  113. if (!service._activated || service._destroyed) return
  114. server.mdns.respond(packet, function () {
  115. // This function will optionally be called with an error object. We'll
  116. // just silently ignore it and retry as we normally would
  117. if (!service.published) {
  118. service._activated = true
  119. service.published = true
  120. service.emit('up')
  121. }
  122. delay = delay * REANNOUNCE_FACTOR
  123. if (delay < REANNOUNCE_MAX_MS && !service._destroyed) {
  124. setTimeout(broadcast, delay).unref()
  125. }
  126. })
  127. })()
  128. }
  129. /**
  130. * Stop the given services
  131. *
  132. * Besides removing a service from the mDNS registry, a "goodbye"
  133. * message is sent for each service to let the network know about the
  134. * shutdown.
  135. */
  136. function teardown (server, services, cb) {
  137. if (!Array.isArray(services)) services = [services]
  138. services = services.filter(function (service) {
  139. return service._activated // ignore services not currently starting or started
  140. })
  141. var records = flatten.depth(services.map(function (service) {
  142. service._activated = false
  143. var records = service._records()
  144. records.forEach(function (record) {
  145. record.ttl = 0 // prepare goodbye message
  146. })
  147. return records
  148. }), 1)
  149. if (records.length === 0) return cb && cb()
  150. server.unregister(records)
  151. // send goodbye message
  152. server.mdns.respond(records, function () {
  153. services.forEach(function (service) {
  154. service.published = false
  155. })
  156. if (cb) cb.apply(null, arguments)
  157. })
  158. }