dns.ts 630 B

1234567891011121314151617181920212223242526272829
  1. import { lookup } from 'dns'
  2. import ipaddr from 'ipaddr.js'
  3. function dnsLookupAll (hostname: string) {
  4. return new Promise<string[]>((res, rej) => {
  5. lookup(hostname, { family: 0, all: true }, (err, adresses) => {
  6. if (err) return rej(err)
  7. return res(adresses.map(a => a.address))
  8. })
  9. })
  10. }
  11. async function isResolvingToUnicastOnly (hostname: string) {
  12. const addresses = await dnsLookupAll(hostname)
  13. for (const address of addresses) {
  14. const parsed = ipaddr.parse(address)
  15. if (parsed.range() !== 'unicast') return false
  16. }
  17. return true
  18. }
  19. export {
  20. dnsLookupAll,
  21. isResolvingToUnicastOnly
  22. }