chat-ui_test / src /lib /server /isURLLocal.ts
nsarrazin's picture
nsarrazin HF staff
Add websearch controls for assistants (#812)
3f5871c unverified
raw
history blame
687 Bytes
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;
}