Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allow Specification of IP For Service Record #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 25 additions & 10 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down