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 #!/bin/bash
toolName=coturn toolName=coturn
toolDeps="coturn miniupnpc"
toolConf="/etc/turnserver.conf" toolConf="/etc/turnserver.conf"
toolServiceName="coturn.service" toolServiceName="coturn.service"
publicIpRetry=20
# Get script working directory # Get script working directory
# (when called from a different directory) # (when called from a different directory)
@@ -36,40 +38,57 @@ step_1_info() { echo "Install $toolName"; }
step_1_alias() { ALIAS="install"; } step_1_alias() { ALIAS="install"; }
step_1() { step_1() {
exe apt update exe apt update
exe apt install coturn $APTOPT exe apt install $toolDeps $APTOPT
} }
step_10_info() { step_10_info() {
echo "Update $toolName 'external-ip' using dig [OPTION] [CUSTOM DNS]" echo "Update $toolName 'external-ip' using dig [OPTION] [CUSTOM DNS]"
echoinfo " [OPTION]" 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_alias() { ALIAS="updateip"; }
step_10() { step_10() {
exitIfRunning
shift 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" local lecho="echoseq"
if [ "$1" == "-l" ]; then if [ "$1" == "-l" ]; then
lecho="echo" lecho="echo"
shift shift
fi fi
local dnsUrl="46.182.19.48" #digitalcourage.de/support/zensurfreier-dns-server local pubIp
[ ! -z "$1" ] && dnsUrl="$1"
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 confIp=`cat "$toolConf" | grep "^external-ip" | cut -d'=' -f2`
local locDate=`date`
if [ "$pubIp" != "$confIp" ]; then if [ "$pubIp" != "$confIp" ]; then
$lecho " [I] Update required. New public ip: $pubIp" $lecho "[$(date)] [I] Update required (via $ipUpdater). New public ip: $pubIp"
$lecho " $locDate"
exe sed -i "s/^external-ip[[:space:]]*=.*/external-ip=${pubIp}/" "$toolConf" exe sed -i "s/^external-ip[[:space:]]*=.*/external-ip=${pubIp}/" "$toolConf"
exe sleep 1 exe sleep 1
$lecho " [I] Restarting $toolName" $lecho "[$(date)] [I] Restarting $toolName"
exe /bin/systemctl restart $toolServiceName exe /bin/systemctl restart $toolServiceName
else else
echoseq " [I] No update required. Current ip: $confIp" echoseq "[$(date)] [I] No update required (via $ipUpdater). Current ip: $confIp"
echoseq " $locDate"
fi fi
} }
@@ -82,5 +101,5 @@ step_12() {
ipCronLoc="/etc/cron.d/update_public_ip" ipCronLoc="/etc/cron.d/update_public_ip"
ipCron="*/5 * * * * root $WDIR/$SCRIPT_FILE -qq updateip" ipCron="*/5 * * * * root $WDIR/$SCRIPT_FILE -qq updateip"
VERSION_SEQREV=12 VERSION_SEQREV=13
. /usr/local/bin/sequencer.sh . /usr/local/bin/sequencer.sh