File size: 687 Bytes
3f5871c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Address6, Address4 } from "ip-address";

import dns from "node:dns";

export async function isURLLocal(URL: URL): Promise<boolean> {
	const isLocal = new Promise<boolean>((resolve, reject) => {
		dns.lookup(URL.hostname, (err, address, family) => {
			if (err) {
				reject(err);
			}
			if (family === 4) {
				const addr = new Address4(address);
				resolve(addr.isInSubnet(new Address4("127.0.0.0/8")));
			} else if (family === 6) {
				const addr = new Address6(address);
				resolve(
					addr.isLoopback() || addr.isInSubnet(new Address6("::1/128")) || addr.isLinkLocal()
				);
			} else {
				reject(new Error("Unknown IP family"));
			}
		});
	});

	return isLocal;
}