mirror of
https://github.com/buildplan/du_setup.git
synced 2025-12-29 16:14:59 +00:00
improve ip validation
This commit is contained in:
@@ -2559,7 +2559,6 @@ validate_ip_or_cidr() {
|
|||||||
local input="$1"
|
local input="$1"
|
||||||
# IPv4 address (simple check)
|
# IPv4 address (simple check)
|
||||||
if [[ "$input" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
|
if [[ "$input" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
|
||||||
# Validate octets are <= 255
|
|
||||||
local -a octets
|
local -a octets
|
||||||
IFS='.' read -ra octets <<< "$input"
|
IFS='.' read -ra octets <<< "$input"
|
||||||
for octet in "${octets[@]}"; do
|
for octet in "${octets[@]}"; do
|
||||||
@@ -2573,7 +2572,6 @@ validate_ip_or_cidr() {
|
|||||||
if [[ "$input" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]; then
|
if [[ "$input" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]; then
|
||||||
local ip="${input%/*}"
|
local ip="${input%/*}"
|
||||||
local cidr="${input##*/}"
|
local cidr="${input##*/}"
|
||||||
# Validate IP part
|
|
||||||
local -a octets
|
local -a octets
|
||||||
IFS='.' read -ra octets <<< "$ip"
|
IFS='.' read -ra octets <<< "$ip"
|
||||||
for octet in "${octets[@]}"; do
|
for octet in "${octets[@]}"; do
|
||||||
@@ -2581,18 +2579,17 @@ validate_ip_or_cidr() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
# Validate CIDR is 0-32
|
|
||||||
if [[ "$cidr" -ge 0 && "$cidr" -le 32 ]]; then
|
if [[ "$cidr" -ge 0 && "$cidr" -le 32 ]]; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
# IPv6 address (basic check)
|
# IPv6 address (basic check)
|
||||||
if [[ "$input" =~ ^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$ ]]; then
|
if [[ "$input" =~ ^[0-9a-fA-F:]+$ && "$input" == *":"* && "$input" != *"/"* ]]; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
# IPv6 CIDR (e.g., 2001:db8::/32)
|
# IPv6 CIDR (permissive check, allows compressed ::)
|
||||||
if [[ "$input" =~ ^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}/[0-9]{1,3}$ ]]; then
|
if [[ "$input" =~ ^[0-9a-fA-F:]+/[0-9]{1,3}$ && "$input" == *":"* ]]; then
|
||||||
local cidr="${input##*/}"
|
local cidr="${input##*/}"
|
||||||
if [[ "$cidr" -ge 0 && "$cidr" -le 128 ]]; then
|
if [[ "$cidr" -ge 0 && "$cidr" -le 128 ]]; then
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user