diff options
author | Chocobozzz <me@florianbigard.com> | 2022-02-07 11:21:25 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-02-07 11:21:25 +0100 |
commit | f33e515991a32885622b217bf2ed1d1b0d9d6832 (patch) | |
tree | 43bf9e63c821f2b363ee60e8b1de07ab7c883580 /server/helpers/dns.ts | |
parent | 4afec7357129590b0e0f3558ecb9ac20e0903600 (diff) | |
download | PeerTube-f33e515991a32885622b217bf2ed1d1b0d9d6832.tar.gz PeerTube-f33e515991a32885622b217bf2ed1d1b0d9d6832.tar.zst PeerTube-f33e515991a32885622b217bf2ed1d1b0d9d6832.zip |
Correctly check import target URL IP
Diffstat (limited to 'server/helpers/dns.ts')
-rw-r--r-- | server/helpers/dns.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/server/helpers/dns.ts b/server/helpers/dns.ts new file mode 100644 index 000000000..da8b666c2 --- /dev/null +++ b/server/helpers/dns.ts | |||
@@ -0,0 +1,29 @@ | |||
1 | import { lookup } from 'dns' | ||
2 | import { parse as parseIP } from 'ipaddr.js' | ||
3 | |||
4 | function dnsLookupAll (hostname: string) { | ||
5 | return new Promise<string[]>((res, rej) => { | ||
6 | lookup(hostname, { family: 0, all: true }, (err, adresses) => { | ||
7 | if (err) return rej(err) | ||
8 | |||
9 | return res(adresses.map(a => a.address)) | ||
10 | }) | ||
11 | }) | ||
12 | } | ||
13 | |||
14 | async function isResolvingToUnicastOnly (hostname: string) { | ||
15 | const addresses = await dnsLookupAll(hostname) | ||
16 | |||
17 | for (const address of addresses) { | ||
18 | const parsed = parseIP(address) | ||
19 | |||
20 | if (parsed.range() !== 'unicast') return false | ||
21 | } | ||
22 | |||
23 | return true | ||
24 | } | ||
25 | |||
26 | export { | ||
27 | dnsLookupAll, | ||
28 | isResolvingToUnicastOnly | ||
29 | } | ||