16 lines
543 B
TypeScript
16 lines
543 B
TypeScript
export function isLoopbackHost(host: string): boolean {
|
|||
|
|
let normalized = host.trim().toLowerCase();
|
||
|
|
if (normalized.endsWith(".")) normalized = normalized.slice(0, -1);
|
||
|
|
if (normalized.startsWith("[") && normalized.endsWith("]")) {
|
||
|
|
normalized = normalized.slice(1, -1);
|
||
|
|
}
|
||
|
|
if (normalized === "localhost" || normalized === "::1") return true;
|
||
|
|
|
||
|
|
const octets = normalized.split(".");
|
||
|
|
return (
|
||
|
|
octets.length === 4 &&
|
||
|
|
octets[0] === "127" &&
|
||
|
|
octets.every((octet) => /^\d{1,3}$/.test(octet) && Number(octet) <= 255)
|
||
|
|
);
|
||
|
|
}
|