node.jsnpmdnsbonjourmdns

Resolve obfuscated IP addresses Node.js using the DNS module


I know on a Mac OSX I can run this command: dns-sd -q a5b3ef18-2e66-4e24-91d2-893b93bbc1c1.local and it returns an IP address. Can I do this in Node.js? It seems like the dns module is mainly used for website -> IP, not IP -> IP (resolved) conversions. Any help is appreciated. Thanks!

Note: The imputted addresses will be mDNS, converted by Bonjour. I found the Bonjour npm package/library, but don't think it works in this case. Also, I found mdns which has the mdns.dns_sd function but I cannot seem to figure out how to use it in my case.

Thanks!


Solution

  • I found a Node module that does exactly what you need. Multicast-DNS is capable of querying mDNS IPs to the standard IP format. The snippet on their README does what you need:

    var mdns = require('multicast-dns')()
    
    mdns.on('response', function(response) {
      console.log('got a response packet:', response)
    })
    
    mdns.on('query', function(query) {
      console.log('got a query packet:', query)
    })
    
    // lets query for an A record for 'brunhilde.local'
    mdns.query({
      questions:[{
        name: 'brunhilde.local',
        type: 'A'
      }]
    })
    

    Obviously you need to replace brunhilde.local with a valid mDNS ip. I simplified the code into this:

    function query(mdns_ip){
        return new Promise((resolve, reject)=>{
            mdns.on('response', function(response) {
                if(response.rcode === 'NOERROR'){
                    resolve(response.answers[0].data)
                    mdns.destroy()
                } else {
                    reject(response.rcode)
                    mdns.destroy()
                }
            })
            
            mdns.query({
              questions:[{
                name: mdns_ip,
                type: 'A'
              }]
            })
        })
    }