mirror of
https://github.com/pi-hole/pi-hole
synced 2024-11-18 06:08:21 +00:00
further simplify the function, per @dl6er's suggestion
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
parent
fe30ce10d3
commit
bbfbf67fcf
@ -1017,30 +1017,16 @@ valid_ip() {
|
|||||||
local ip=${1}
|
local ip=${1}
|
||||||
local stat=1
|
local stat=1
|
||||||
|
|
||||||
# If the IP matches the format xxx.xxx.xxx.xxx (optional port of range #0-65536), also ensure string ends with 0-9
|
# One IPv4 element is 8bit: 0 - 256
|
||||||
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(#[0-9]{1,5})?$ ]]; then
|
local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)";
|
||||||
# Save the old Internal Field Separator in a variable
|
# optional port number starting '#' with range of 1-65536
|
||||||
OIFS=$IFS
|
local portelem="(#([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6]))?"
|
||||||
# and set the new one to a dot (period)
|
# build a full regex string from the above parts
|
||||||
IFS='.#'
|
local regex="^${ipv4elem}\.${ipv4elem}\.${ipv4elem}\.${ipv4elem}${portelem}$"
|
||||||
# Put the IP into an array
|
|
||||||
read -r -a ip <<< "${ip}"
|
[[ $ip =~ ${regex} ]]
|
||||||
# Restore the IFS to what it was
|
|
||||||
IFS=${OIFS}
|
|
||||||
|
|
||||||
## Evaluate each octet by checking if it's less than or equal to 255 (the max for each octet)
|
|
||||||
[[ "${ip[0]}" -le 255 && "${ip[1]}" -le 255 && "${ip[2]}" -le 255 && "${ip[3]}" -le 255 ]]
|
|
||||||
# Save the exit code
|
|
||||||
stat=$?
|
stat=$?
|
||||||
|
|
||||||
# If there is a 5th part to the array, then it is a port number - check it is between 1 and 65536
|
|
||||||
if [[ "${ip[4]}" ]]; then
|
|
||||||
[[ "${ip[4]}" -ge 1 && "${ip[4]}" -le 65536 ]]
|
|
||||||
# Save the exit code
|
|
||||||
stat=$?
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
|
||||||
# Return the exit code
|
# Return the exit code
|
||||||
return "${stat}"
|
return "${stat}"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user