From 6f0227a6ee5cc1887c9045e659a9a06d75b51f53 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Thu, 1 Apr 2021 14:12:59 +0200 Subject: [PATCH] Enhanced public ip detection First try with upnpc to acquire ip via router, then fallback to extrenal dns query --- seqs/coturn.sh | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/seqs/coturn.sh b/seqs/coturn.sh index 2a82934..5f36b1b 100755 --- a/seqs/coturn.sh +++ b/seqs/coturn.sh @@ -1,8 +1,10 @@ #!/bin/bash toolName=coturn +toolDeps="coturn miniupnpc" toolConf="/etc/turnserver.conf" toolServiceName="coturn.service" +publicIpRetry=20 # Get script working directory # (when called from a different directory) @@ -36,40 +38,57 @@ step_1_info() { echo "Install $toolName"; } step_1_alias() { ALIAS="install"; } step_1() { exe apt update - exe apt install coturn $APTOPT + exe apt install $toolDeps $APTOPT } step_10_info() { echo "Update $toolName 'external-ip' using dig [OPTION] [CUSTOM DNS]" echoinfo " [OPTION]" - echoinfo " -l : Always output informations (even with -qq)" + echoinfo " -l : Always output update required and error information" + echoinfo " (even with -qq)" } step_10_alias() { ALIAS="updateip"; } step_10() { + exitIfRunning shift + local retryCount=$publicIpRetry + local ipUpdater + local ipRegex='^[0-2]*[0-9]{1,2}\.[0-2]*[0-9]{1,2}\.[0-2]*[0-9]{1,2}\.[0-2]*[0-9]{1,2}\/*[0-9]*$' + local dnsUrl="46.182.19.48" #digitalcourage.de/support/zensurfreier-dns-server + local dnsFallbackUrl="194.150.168.168" #dns.as250.net; Berlin/Frankfurt local lecho="echoseq" if [ "$1" == "-l" ]; then lecho="echo" shift fi - - local dnsUrl="46.182.19.48" #digitalcourage.de/support/zensurfreier-dns-server - [ ! -z "$1" ] && dnsUrl="$1" - local pubIp=$(dig @$dnsUrl +short cloud.imoff.de) + local pubIp + + while [ $retryCount -gt 0 ]; do + pubIp=`"$(command -v upnpc)" -s | grep ^ExternalIPAddress | cut -c21-` + [ $? -eq 0 ] && ipUpdater="upnpc" && break || "$lecho" "[$(date)] [W] Upnpc failed" + pubIp=$(dig @$dnsUrl +short +timeout=1 cloud.imoff.de 2>>/dev/null) + [ $? -eq 0 ] && ipUpdater="DNS" && break || "$lecho" "[$(date) [W] DNS lookup to $dnsUrl failed" + pubIp=$(dig @$dnsFallbackUrl +short +timeout=1 cloud.imoff.de 2>>/dev/null) + [ $? -eq 0 ] && ipUpdater="DNS Fallback" && break || "$lecho" "[$(date)] [W] DNS lookup to $dnsFallbackUrl failed" + ((retryCount--)) + done + + if [[ ! $pubIp =~ $ipRegex ]]; then + "$lecho" "[$(date)] [E] Couldn't aquire public IP. Giving up." + return 1 + fi + local confIp=`cat "$toolConf" | grep "^external-ip" | cut -d'=' -f2` - local locDate=`date` if [ "$pubIp" != "$confIp" ]; then - $lecho " [I] Update required. New public ip: $pubIp" - $lecho " $locDate" + $lecho "[$(date)] [I] Update required (via $ipUpdater). New public ip: $pubIp" exe sed -i "s/^external-ip[[:space:]]*=.*/external-ip=${pubIp}/" "$toolConf" exe sleep 1 - $lecho " [I] Restarting $toolName" + $lecho "[$(date)] [I] Restarting $toolName" exe /bin/systemctl restart $toolServiceName else - echoseq " [I] No update required. Current ip: $confIp" - echoseq " $locDate" + echoseq "[$(date)] [I] No update required (via $ipUpdater). Current ip: $confIp" fi } @@ -82,5 +101,5 @@ step_12() { ipCronLoc="/etc/cron.d/update_public_ip" ipCron="*/5 * * * * root $WDIR/$SCRIPT_FILE -qq updateip" -VERSION_SEQREV=12 +VERSION_SEQREV=13 . /usr/local/bin/sequencer.sh