]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/dns.ts
Correctly check import target URL IP
[github/Chocobozzz/PeerTube.git] / server / helpers / dns.ts
diff --git a/server/helpers/dns.ts b/server/helpers/dns.ts
new file mode 100644 (file)
index 0000000..da8b666
--- /dev/null
@@ -0,0 +1,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
+}