mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-12-23 21:30:17 +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
1 changed files with 8 additions and 22 deletions
|
@ -1017,30 +1017,16 @@ valid_ip() {
|
|||
local ip=${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
|
||||
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(#[0-9]{1,5})?$ ]]; then
|
||||
# Save the old Internal Field Separator in a variable
|
||||
OIFS=$IFS
|
||||
# and set the new one to a dot (period)
|
||||
IFS='.#'
|
||||
# Put the IP into an array
|
||||
read -r -a ip <<< "${ip}"
|
||||
# Restore the IFS to what it was
|
||||
IFS=${OIFS}
|
||||
# One IPv4 element is 8bit: 0 - 256
|
||||
local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)";
|
||||
# optional port number starting '#' with range of 1-65536
|
||||
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]))?"
|
||||
# build a full regex string from the above parts
|
||||
local regex="^${ipv4elem}\.${ipv4elem}\.${ipv4elem}\.${ipv4elem}${portelem}$"
|
||||
|
||||
## 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=$?
|
||||
[[ $ip =~ ${regex} ]]
|
||||
|
||||
# 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
|
||||
stat=$?
|
||||
# Return the exit code
|
||||
return "${stat}"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue