Files
OmniRoute/open-sse/utils/proxyFamily.ts
Diego Rodrigues de Sa e Souza 76a07cf7a5 Release v3.8.24 (#3747)
Release v3.8.24 — see CHANGELOG.md [3.8.24] for the full notes and the PR description for the contributors hall. Integration of release/v3.8.24 into main.
2026-06-13 17:27:40 -03:00

25 lines
847 B
TypeScript

import { isIP } from "node:net";
export type ProxyFamily = "auto" | "ipv4" | "ipv6";
/** Remove the surrounding brackets from an IPv6 literal host (`[::1]` -> `::1`). */
export function stripIpv6Brackets(host: string): string {
if (typeof host !== "string") return "";
if (host.startsWith("[") && host.endsWith("]")) {
return host.slice(1, -1);
}
return host;
}
/** 4 / 6 if the host is an IP literal (brackets tolerated), null if it is a hostname. */
export function detectIpLiteralFamily(host: string): 4 | 6 | null {
const bare = stripIpv6Brackets(host);
const v = isIP(bare);
return v === 0 ? null : (v as 4 | 6);
}
/** Normalize a stored family directive; anything unknown means "auto". */
export function parseProxyFamily(value: unknown): ProxyFamily {
return value === "ipv4" || value === "ipv6" ? value : "auto";
}