From 9d2e7fcc8a47d7ab3b90bc7c15a4cc0dd76c9df6 Mon Sep 17 00:00:00 2001 From: 9cco Date: Fri, 25 Feb 2022 12:24:17 +0100 Subject: [PATCH] Taking feedback from timetoexpire Changed back so that curl is used by default, however dig can be used as a backup if curl is not installed on the system. Additionally took my fear of code injection into account by sanitizing the remote input through a regex. --- cloudflare-template.sh | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cloudflare-template.sh b/cloudflare-template.sh index 2537c26..8f9e863 100644 --- a/cloudflare-template.sh +++ b/cloudflare-template.sh @@ -17,18 +17,16 @@ slackuri="" # URI for Slack WebHook "http ########################################### ## Check if we have a public IP ########################################### -command -v dig &> /dev/null -# Use the DNS lookup if dig is available. -if [[ $? -eq 0 ]]; then +# Use curl if curl is available +if [[ $(command -v curl &> /dev/null; echo $?) ]]; then + ip=$(curl -s https://api.ipify.org || curl -s https://ipv4.icanhazip.com/) +elif [[ $(command -v dig &> /dev/null; echo $?) ]]; then ip=$(dig +short myip.opendns.com @resolver1.opendns.com); fi -if [[ $ip -eq "" ]]; then - ip=$(curl -s https://api.ipify.org || curl -s https://ipv4.icanhazip.com/) -fi -if [[ "${ip}" == "" ]]; then - logger -s "DDNS Updater: No public IP found" - exit 1 +if [[ ! $ip =~ [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]]; then + logger -s "DDNS Updater: Failed to find a valid IP." + exit 1 fi ###########################################