From b8e4eed771aa59b949144c136ddc89d4935c0d55 Mon Sep 17 00:00:00 2001 From: Brandon Lehmann Date: Tue, 14 Feb 2017 17:26:08 -0500 Subject: [PATCH 1/2] allows for the specification of a single IP address for the service that is being published. This can be a local or foreign IP --- lib/service.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/service.js b/lib/service.js index 52a1606..0ddb39d 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,11 @@ function Service (opts) { this.subtypes = opts.subtypes || null this.txt = opts.txt || null this.published = false + this.ip = (function () { + if (net.isIP(opts.ip) === 4) return { family: 'IPv4', ip: opts.ip } + if (net.isIP(opts.ip) === 6) return { family: 'IPv6', ip: opts.ip } + return false + })() this._activated = false // indicates intent - true: starting/started, false: stopping/stopped } @@ -34,17 +40,22 @@ 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)) - } + if (!this.ip) { + 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)) + } + }) }) - }) + } else { + if (this.ip.family === 'IPv4') records.push(rrA(self, this.ip.address)) + if (this.ip.family === 'IPv6') records.push(rrAaaa(self, this.ip.address)) + } return records } From d3f5f81f816bd80fd472a42e7dbfbb87762ad460 Mon Sep 17 00:00:00 2001 From: Brandon Lehmann Date: Tue, 14 Feb 2017 20:00:55 -0500 Subject: [PATCH 2/2] Cleaned up service IP option --- lib/service.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/service.js b/lib/service.js index 0ddb39d..1d4cef8 100644 --- a/lib/service.js +++ b/lib/service.js @@ -27,11 +27,7 @@ function Service (opts) { this.subtypes = opts.subtypes || null this.txt = opts.txt || null this.published = false - this.ip = (function () { - if (net.isIP(opts.ip) === 4) return { family: 'IPv4', ip: opts.ip } - if (net.isIP(opts.ip) === 6) return { family: 'IPv6', ip: opts.ip } - return false - })() + this.ip = opts.ip || false this._activated = false // indicates intent - true: starting/started, false: stopping/stopped } @@ -40,7 +36,7 @@ Service.prototype._records = function () { var records = [rrPtr(this), rrSrv(this), rrTxt(this)] var self = this - if (!this.ip) { + var allInterfaces = function () { var interfaces = os.networkInterfaces() Object.keys(interfaces).forEach(function (name) { interfaces[name].forEach(function (addr) { @@ -52,9 +48,17 @@ Service.prototype._records = function () { } }) }) + } + 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 { - if (this.ip.family === 'IPv4') records.push(rrA(self, this.ip.address)) - if (this.ip.family === 'IPv6') records.push(rrAaaa(self, this.ip.address)) + allInterfaces() } return records