PeerTube/server/core/helpers/dns.ts

30 lines
630 B
TypeScript
Raw Normal View History

2022-02-07 04:21:25 -06:00
import { lookup } from 'dns'
import ipaddr from 'ipaddr.js'
2022-02-07 04:21:25 -06:00
function dnsLookupAll (hostname: string) {
return new Promise<string[]>((res, rej) => {
lookup(hostname, { family: 0, all: true }, (err, adresses) => {
if (err) return rej(err)
return res(adresses.map(a => a.address))
})
})
}
async function isResolvingToUnicastOnly (hostname: string) {
const addresses = await dnsLookupAll(hostname)
for (const address of addresses) {
const parsed = ipaddr.parse(address)
2022-02-07 04:21:25 -06:00
if (parsed.range() !== 'unicast') return false
}
return true
}
export {
dnsLookupAll,
isResolvingToUnicastOnly
}