36 lines
No EOL
1.6 KiB
JavaScript
36 lines
No EOL
1.6 KiB
JavaScript
'use strict';
|
|
|
|
function isValidIPv4(str) {
|
|
const ipv4Regex = /^(?:(?: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]?)$/;
|
|
return ipv4Regex.test(str);
|
|
}
|
|
|
|
function isValidIPv6(str) {
|
|
const ipv6Regex = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/i;
|
|
return ipv6Regex.test(str);
|
|
}
|
|
|
|
function isValidIpOrCidr(str) {
|
|
const parts = str.split('/');
|
|
if (parts.length > 2) return false;
|
|
|
|
const isIP = isValidIPv4(parts[0]) || isValidIPv6(parts[0]);
|
|
if (!isIP) return false;
|
|
|
|
if (parts.length === 2) {
|
|
const mask = parseInt(parts[1], 10);
|
|
if (isNaN(mask) || mask < 0 || mask > (isValidIPv4(parts[0]) ? 32 : 128)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function isValidFqdn(str) {
|
|
if (isValidIPv4(str) || isValidIPv6(str)) {
|
|
return false;
|
|
}
|
|
const fqdnRegex = /^(?!-)(?:[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.){1,126}(?!-d$)[a-zA-Z0-9-]{2,63}$/;
|
|
return fqdnRegex.test(str);
|
|
} |