mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-28 07:46:36 +00:00
improve IPv6 validation to support all variants using ipaddr.js
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import z from "zod";
|
import z from "zod";
|
||||||
|
import ipaddr from "ipaddr.js";
|
||||||
|
|
||||||
export function isValidCIDR(cidr: string): boolean {
|
export function isValidCIDR(cidr: string): boolean {
|
||||||
return z.string().cidr().safeParse(cidr).success;
|
return z.string().cidr().safeParse(cidr).success;
|
||||||
@@ -83,12 +84,15 @@ export function isTargetValid(value: string | undefined) {
|
|||||||
|
|
||||||
const DOMAIN_REGEX =
|
const DOMAIN_REGEX =
|
||||||
/^[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9_])?(?:\.[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9_])?)*$/;
|
/^[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9_])?(?:\.[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9_])?)*$/;
|
||||||
const IPV4_REGEX =
|
// const IPV4_REGEX =
|
||||||
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
// /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
||||||
const IPV6_REGEX = /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/i;
|
// const IPV6_REGEX = /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/i;
|
||||||
|
|
||||||
if (IPV4_REGEX.test(value) || IPV6_REGEX.test(value)) {
|
try {
|
||||||
return true;
|
const addr = ipaddr.parse(value);
|
||||||
|
return addr.kind() === "ipv4" || addr.kind() === "ipv6";
|
||||||
|
} catch {
|
||||||
|
// fall through to domain regex check
|
||||||
}
|
}
|
||||||
|
|
||||||
return DOMAIN_REGEX.test(value);
|
return DOMAIN_REGEX.test(value);
|
||||||
|
|||||||
Reference in New Issue
Block a user