From 8ab429c1e6b739475187da423e12dd06573b10bf Mon Sep 17 00:00:00 2001 From: Kiril Vatev Date: Fri, 1 Jun 2018 12:46:54 -0400 Subject: [PATCH 1/3] handling A additionals, so that the correct IP address can be parsed --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index e2140c4..fb06af9 100644 --- a/index.js +++ b/index.js @@ -263,6 +263,15 @@ module.exports = function () { emit(casts[name]) } } + + if (a.type === 'A') { + var aName = name.replace('.local', ''); + Object.keys(casts).forEach(function (castName) { + if (castName.indexOf(aName) > -1) { + casts[castName].host = a.data; + } + }); + } } response.additionals.forEach(onanswer) From 2371ff88916e46ccf5f8a8fd8a04f4dc75d502f5 Mon Sep 17 00:00:00 2001 From: Kiril Vatev Date: Mon, 11 Jun 2018 22:39:36 -0400 Subject: [PATCH 2/3] handling race condition between multicast-dns and node-ssdp and cleaning up result parsing or multicast-dns results --- index.js | 60 +++++++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index fb06af9..c790755 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,24 @@ var toSubtitles = function (url, i) { } } +var getName = function (records) { + if (records['TXT']) { + var text = txt.decode(records['TXT'].data) + if (text.fn) return text.fn + } + + if (records['PTR'] && records['PTR'].name === '_googlecast._tcp.local') return records['PTR'].data.replace('._googlecast._tcp.local', '') + if (records['SVR']) return records['SRV'].name + if (records['TXT']) return records['TXT'].name + return null +} + +var getHost = function (records) { + if (records['A']) return records['A'].data + if (records['SRV']) return records['SRV'].data.target + return null +} + module.exports = function () { var dns = mdns() var that = new events.EventEmitter() @@ -239,43 +257,19 @@ module.exports = function () { } dns.on('response', function (response) { - response.answers.forEach(function (a) { - if (a.type === 'PTR' && a.name === '_googlecast._tcp.local') { - var name = a.data - var shortname = a.data.replace('._googlecast._tcp.local', '') - if (!casts[name]) casts[name] = {name: shortname, host: null} - } - }) + var records = response.additionals.concat(response.answers).reduce(function (memo, record) { + memo[record.type] = record + return memo + }, {}) - var onanswer = function (a) { - debug('got answer %j', a) + var device = { host: getHost(records), name: getName(records) } - var name = a.name - if (a.type === 'SRV' && casts[name] && !casts[name].host) { - casts[name].host = a.data.target - emit(casts[name]) - } + if (!device.name || !device.host) return - if (a.type === 'TXT' && casts[name]) { - var text = txt.decode(a.data) - if (text.fn) { - casts[name].name = text.fn - emit(casts[name]) - } - } - - if (a.type === 'A') { - var aName = name.replace('.local', ''); - Object.keys(casts).forEach(function (castName) { - if (castName.indexOf(aName) > -1) { - casts[castName].host = a.data; - } - }); - } + if (!casts[device.name]) { + casts[device.name] = device + emit(device) } - - response.additionals.forEach(onanswer) - response.answers.forEach(onanswer) }) if (ssdp) { From b2374820294f3ae36321b7259b9301b452d5022a Mon Sep 17 00:00:00 2001 From: Kiril Vatev Date: Tue, 12 Jun 2018 20:05:37 -0400 Subject: [PATCH 3/3] use only TXT records for determining a device name --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index c790755..2103d36 100644 --- a/index.js +++ b/index.js @@ -39,10 +39,6 @@ var getName = function (records) { var text = txt.decode(records['TXT'].data) if (text.fn) return text.fn } - - if (records['PTR'] && records['PTR'].name === '_googlecast._tcp.local') return records['PTR'].data.replace('._googlecast._tcp.local', '') - if (records['SVR']) return records['SRV'].name - if (records['TXT']) return records['TXT'].name return null }