From e9efa4bd65fddedce4d17c79b46216f68f4dcbcc Mon Sep 17 00:00:00 2001 From: Christian Rotzoll Date: Thu, 14 Mar 2019 14:19:00 +0100 Subject: [PATCH] #371 sanaity check on public ip data --- home.admin/40addHDD.sh | 13 +++++++++++++ home.admin/_background.sh | 2 +- home.admin/_bootstrap.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/home.admin/40addHDD.sh b/home.admin/40addHDD.sh index 7db2ed0..864e92a 100755 --- a/home.admin/40addHDD.sh +++ b/home.admin/40addHDD.sh @@ -69,6 +69,19 @@ if [ ${existsHDD} -gt 0 ]; then # try to determine publicIP and if not possible use localIP as placeholder # https://github.com/rootzoll/raspiblitz/issues/312#issuecomment-462675101 freshPublicIP=$(curl -s http://v4.ipv6-test.com/api/myip.php) + + # sanity check on IP data + # see https://github.com/rootzoll/raspiblitz/issues/371#issuecomment-472416349 + echo "-> sanity check of IP data: ${freshPublicIP}" + if [[ $freshPublicIP =~ ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$ ]]; then + echo "OK IPv6" + elif [[ $freshPublicIP =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then + echo "OK IPv4" + else + echo "FAIL - not an IPv4 or IPv6 address" + freshPublicIP="" + fi + if [ ${#freshPublicIP} -eq 0 ]; then localIP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/') echo "WARNING: No publicIP information at all yet - working with placeholder : ${localIP}" diff --git a/home.admin/_background.sh b/home.admin/_background.sh index 5e9d8d2..f9310a1 100644 --- a/home.admin/_background.sh +++ b/home.admin/_background.sh @@ -67,7 +67,7 @@ do # every 15min - not too often # because its a ping to external service - recheckPublicIP=$((($counter % 9)+1)) + recheckPublicIP=$((($counter % 900)+1)) updateDynDomain=0 if [ ${recheckPublicIP} -eq 1 ]; then echo "*** RECHECK PUBLIC IP ***" diff --git a/home.admin/_bootstrap.sh b/home.admin/_bootstrap.sh index d89f857..456b5eb 100644 --- a/home.admin/_bootstrap.sh +++ b/home.admin/_bootstrap.sh @@ -310,6 +310,19 @@ if [ ${configExists} -eq 1 ]; then # wait otherwise looking for publicIP fails sleep 5 freshPublicIP=$(curl -s http://v4.ipv6-test.com/api/myip.php) + + # sanity check on IP data + # see https://github.com/rootzoll/raspiblitz/issues/371#issuecomment-472416349 + echo "-> sanity check of IP data: ${freshPublicIP}" + if [[ $freshPublicIP =~ ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$ ]]; then + echo "OK IPv6" + elif [[ $freshPublicIP =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then + echo "OK IPv4" + else + echo "FAIL - not an IPv4 or IPv6 address" + freshPublicIP="" + fi + if [ ${#freshPublicIP} -eq 0 ]; then # prevent having no publicIP set at all and LND getting stuck # https://github.com/rootzoll/raspiblitz/issues/312#issuecomment-462675101