diff --git a/lib/service.js b/lib/service.js index 52a1606..1d4cef8 100644 --- a/lib/service.js +++ b/lib/service.js @@ -5,6 +5,7 @@ var util = require('util') var EventEmitter = require('events').EventEmitter var serviceName = require('multicast-dns-service-types') var txt = require('dns-txt')() +var net = require('net') var TLD = '.local' @@ -26,6 +27,7 @@ function Service (opts) { this.subtypes = opts.subtypes || null this.txt = opts.txt || null this.published = false + this.ip = opts.ip || false this._activated = false // indicates intent - true: starting/started, false: stopping/stopped } @@ -34,17 +36,30 @@ 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 allInterfaces = function () { + 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)) + } + }) }) - }) + } + if (this.ip) { + if (net.isIP(this.ip) === 4) { + records.push(rrA(self, this.ip)) + } else if (net.isIP(this.ip) === 6) { + records.push(rrAaaa(self, this.ip)) + } else { + allInterfaces() + } + } else { + allInterfaces() + } return records }