Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

npm - 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!

question from:https://stackoverflow.com/questions/65912856/resolve-obfuscated-ip-addresses-node-js-using-the-dns-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'
          }]
        })
    })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...