aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/dns.ts
blob: da8b666c2d7391640f001d609b7e9b10d4131106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { lookup } from 'dns'
import { parse as parseIP } from 'ipaddr.js'

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 = parseIP(address)

    if (parsed.range() !== 'unicast') return false
  }

  return true
}

export {
  dnsLookupAll,
  isResolvingToUnicastOnly
}