Enhanced public ip detection

First try with upnpc to acquire ip via router, then fallback to extrenal dns query
This commit is contained in:
2021-04-01 14:12:59 +02:00
parent d2bf4d8677
commit 6f0227a6ee

View File

@@ -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
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 pubIp=$(dig @$dnsUrl +short cloud.imoff.de)
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