30 lines
792 B
JavaScript
30 lines
792 B
JavaScript
import crypto from 'crypto'
|
|
import dns from 'dns'
|
|
|
|
export const toMD5 = str => crypto.createHash('md5').update(str).digest('hex')
|
|
|
|
|
|
const ipMap = new Map()
|
|
export const getHostIp = hostname => {
|
|
const result = ipMap.get(hostname)
|
|
if (typeof result === 'object') return result
|
|
if (result === true) return
|
|
ipMap.set(hostname, true)
|
|
// console.log(hostname)
|
|
dns.lookup(hostname, {
|
|
// family: 4,
|
|
all: false,
|
|
}, (err, address, family) => {
|
|
if (err) return console.log(err)
|
|
// console.log(address, family)
|
|
ipMap.set(hostname, { address, family })
|
|
})
|
|
}
|
|
|
|
export const dnsLookup = (hostname, options, callback) => {
|
|
const result = getHostIp(hostname)
|
|
if (result) return callback(null, result.address, result.family)
|
|
|
|
dns.lookup(hostname, options, callback)
|
|
}
|