snap-dyndns-client/dnsupdate.sh

115 lines
3.8 KiB
Bash
Raw Permalink Normal View History

2016-04-27 23:00:39 +02:00
#!/bin/bash
########################################################################
# Copyright (C) 2016 Max Mehl <mail [at] mehl [dot] mx>
########################################################################
#
2016-04-27 23:00:39 +02:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2016-04-27 23:00:39 +02:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2016-04-27 23:00:39 +02:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2016-04-27 23:00:39 +02:00
########################################################################
#
# Client part of the "Snap DynDNS Server", grabbing public IP address
2016-04-27 23:00:39 +02:00
# and pushing it to the server handling the more complicated API stuff
#
2016-04-27 23:00:39 +02:00
########################################################################
cd "$(dirname "$(readlink -f "$0")")"
print_usage() {
echo "dnsupdate.sh [-c configfile]"
}
while getopts c:h OPT; do
case $OPT in
c) CONFIG=$OPTARG;; # type: normal or docker
h) print_usage; exit 0;;
*) echo "Unknown option: -$OPTARG"; print_usage; exit 1;;
esac
done
if [ -z $CONFIG ]; then
CONFIG="config.cfg"
fi
2016-04-27 23:00:39 +02:00
# Test if config.cfg exists and set needed variables
if [ ! -e "$CONFIG" ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
source "$CONFIG"
2016-04-27 23:00:39 +02:00
# Test whether necessary applications are installed
function testinst {
APP=$1
if [ $(which $APP | wc -l) != 1 ]; then
echo "$APP does not seem to be installed. Aborting."
exit 1
fi
}
testinst curl
# Loop over all hosts. $i will be key
for i in "${!USER[@]}"; do
oldip="old-$i.ip"
logfile="update-$i.log"
2016-04-27 23:00:39 +02:00
# Does the old.ip file exist? If not, fill it with unrealistic input
if [ ! -e "$oldip" ]; then
echo "0.0.0.0" > "$oldip"
fi
2016-04-27 23:00:39 +02:00
# Read old IP, gather new IP via IP return service
OLDIP=$(cat "$oldip")
NEWIP=$(curl -m "$TIMEOUT" -s "$IPSERV")
2016-04-27 23:00:39 +02:00
# Post current timestamp in logfile
echo "[$(date "+%Y-%m-%d %H:%M:%S")]" > "$logfile"
2016-04-27 23:00:39 +02:00
# check whether the IP has changed since the last update
if [ "$OLDIP" == "$NEWIP" ]; then
echo "IP ($OLDIP) did not change. Aborting." >> "$logfile"
continue
fi
2016-04-27 23:00:39 +02:00
# Check whether IP is on ignore list
if [ $(echo "$IGNOREIP" | grep "$NEWIP") ]; then
echo "Current IP ($NEWIP) is ignored. Aborting without DNS update." >> "$logfile"
continue
2016-04-27 23:00:39 +02:00
fi
# Check whether user is using a Tor exit node IP
if [ "$CHECKTOR" = true ]; then
# Only download list of exit nodes if tor-ips.txt file isn't existing yet, or if it is older than 7 days
if [ ! -e tor-ips.txt ] || [ $[ $(date +%s) - $(stat -L -c %Y tor-ips.txt) ] -gt "604800" ]; then
# download list, extract plain IPs, move back to file
echo "download list"
curl "$EXITNODES" > tor-ips.txt
TORIPS=$(grep "^ExitAddress" tor-ips.txt | cut -d" " -f2)
echo $TORIPS | sed 's/ /\n/g' > tor-ips.txt
fi
# Grep current IP in list of exit node IPs
if [ $(grep "^$NEWIP$" tor-ips.txt) ]; then
echo "Current IP ($NEWIP) is ignored because it is a Tor exit IP. Aborting without DNS update." >> "$logfile"
continue
fi
2016-04-27 23:00:39 +02:00
fi
# Run update if old IP and current IP are not equal
if [ ! "$OLDIP" == "$NEWIP" ]; then
echo $NEWIP > "$oldip"
curl -X POST -d "domain=${DOMAIN[$i]}&user=${USER[$i]}&pass=${PASS[$i]}&ip=$NEWIP" "$SERVER" >> "$logfile"
echo "" >> "$logfile"
echo "Updated IP address from $OLDIP to $NEWIP." >> "$logfile"
fi
done