diff --git a/scripts/cnode-helper-scripts/cntools.sh b/scripts/cnode-helper-scripts/cntools.sh index 22d8f68af..f975c04c8 100755 --- a/scripts/cnode-helper-scripts/cntools.sh +++ b/scripts/cnode-helper-scripts/cntools.sh @@ -2195,14 +2195,14 @@ function main { ;; 1) getAnswerAnyCust relay_ip_enter "Enter relays's IPv4/v6 address" if [[ -n "${relay_ip_enter}" ]]; then - if ! isValidIPv4 "${relay_ip_enter}" && ! isValidIPv6 "${relay_ip_enter}"; then - println ERROR "${FG_RED}ERROR${NC}: invalid IPv4/v6 address format!" + if ! isValidIPv4 "${relay_ip_enter}" && ! isValidIPv6 "${relay_ip_enter}" && ! isValidHostnameOrDomain "${relay_ip_enter}"; then + println ERROR "${FG_RED}ERROR${NC}: Invalid IPv4/v6 address format or hostname/domain name format!" else getAnswerAnyCust relay_port_enter "Enter relays's port" if [[ -n "${relay_port_enter}" ]]; then if ! isNumber ${relay_port_enter} || [[ ${relay_port_enter} -lt 1 || ${relay_port_enter} -gt 65535 ]]; then println ERROR "${FG_RED}ERROR${NC}: invalid port number!" - elif isValidIPv4 "${relay_ip_enter}"; then + elif isValidIPv4 "${relay_ip_enter}" || isValidHostnameOrDomain "${relay_ip_enter}"; then relay_array+=( "type" "IPv4" "address" "${relay_ip_enter}" "port" "${relay_port_enter}" ) relay_output+="--pool-relay-port ${relay_port_enter} --pool-relay-ipv4 ${relay_ip_enter} " else diff --git a/scripts/cnode-helper-scripts/env b/scripts/cnode-helper-scripts/env index e4cf22a1a..9b74a9cf9 100644 --- a/scripts/cnode-helper-scripts/env +++ b/scripts/cnode-helper-scripts/env @@ -488,7 +488,24 @@ fractionToPCT() { isValidIPv4() { local ip=$1 [[ -z ${ip} ]] && return 1 - if [[ ${ip} =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ || ${ip} =~ ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9_\-]*[A-Za-z0-9])$ ]]; then + ipv4_regex="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" + if [[ ${ip} =~ ${ipv4_regex} ]]; then + return 0 + fi + return 1 +} + +# Description : Helper function to validate hostname or domain name +# : $1 = hostname or domain name +isValidHostnameOrDomain() { + local name=$1 + [[ -z ${name} ]] && return 1 + + # Regular expression for valid hostnames and domain names + hostname_regex="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9_\-]*[A-Za-z0-9])$" + + # Check if the input matches the hostname/domain name format + if [[ ${name} =~ ${hostname_regex} ]]; then return 0 fi return 1 @@ -1163,14 +1180,8 @@ fi [[ -z ${EKG_TIMEOUT} ]] && EKG_TIMEOUT=3 [[ -z ${EKG_HOST} ]] && EKG_HOST=127.0.0.1 -if [[ ${EKG_HOST} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - IFS='.' read -ra EKG_OCTETS <<< ${EKG_HOST} - if ! [[ ${EKG_OCTETS[0]} -le 255 && ${EKG_OCTETS[1]} -le 255 && ${EKG_OCTETS[2]} -le 255 && ${EKG_OCTETS[3]} -le 255 ]]; then - echo "Not a valid IP range set for EKG host, please check env file for value of EKG_HOST (currently it is ${EKG_HOST} )!" - return 1 - fi -else - echo "Not a valid IP format set for EKG host, please check env file!" +if ! isValidIPv4 "${EKG_HOST}" && ! isValidHostnameOrDomain "${EKG_HOST}"; then + echo "Not a valid IP or hostname set for EKG host, please check the env file (currently it is ${EKG_HOST})!" return 1 fi diff --git a/scripts/cnode-helper-scripts/topologyUpdater.sh b/scripts/cnode-helper-scripts/topologyUpdater.sh index 52e2b105e..91340ac72 100755 --- a/scripts/cnode-helper-scripts/topologyUpdater.sh +++ b/scripts/cnode-helper-scripts/topologyUpdater.sh @@ -155,8 +155,9 @@ if [[ ${TU_FETCH} = "Y" ]]; then *) echo "ERROR: Invalid Custom Peer definition '${cpeer}'. Please double check CUSTOM_PEERS definition" exit 1 ;; esac - if [[ ${addr} = *.* ]]; then - ! isValidIPv4 "${addr}" && echo "ERROR: Invalid IPv4 address or hostname '${addr}'. Please check CUSTOM_PEERS definition" && continue + if ! isValidIPv4 "${addr}" && ! isValidHostnameOrDomain "${addr}"; then + echo "ERROR: Invalid IPv4 address or hostname '${addr}'. Please check CUSTOM_PEERS definition" + continue elif [[ ${addr} = *:* ]]; then ! isValidIPv6 "${addr}" && echo "ERROR: Invalid IPv6 address '${addr}'. Please check CUSTOM_PEERS definition" && continue fi