#!/bin/bash ######################################################################## # Copyright (C) 2016 Max Mehl ######################################################################## # # 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. # # 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. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # ######################################################################## # # Client part of the "Snap DynDNS Server", grabbing public IP address # and pushing it to the server handling the more complicated API stuff # ######################################################################## cd "$(dirname "$(readlink -f "$0")")" # Test if config.cfg exists and set needed variables if [ ! -e config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi source config.cfg # 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 testinst wget # Does the old.ip file exist? If not, fill it with unrealistic input if [ ! -e old.ip ]; then echo "0.0.0.0" > old.ip fi # Read old IP, gather new IP via IP return service OLDIP=$(cat old.ip) NEWIP=$(wget -T $TIMEOUT -q -O - $IPSERV) # Post current timestamp in logfile echo "[$(date "+%Y-%m-%d %H:%M:%S")]" > update.log # check whether the IP has changed since the last update if [ "$OLDIP" == "$NEWIP" ]; then echo "IP ($OLDIP) did not change. Aborting." >> update.log exit 0 fi # Check whether IP is on ignore list if [ $(echo "$IGNOREIP" | grep "$NEWIP") ]; then echo "Current IP ($NEWIP) is ignored. Aborting without DNS update." >> update.log exit 0 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" wget -O tor-ips.txt $EXITNODES 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." >> update.log exit 0 fi fi # Run update if old IP and current IP are not equal if [ ! "$OLDIP" == "$NEWIP" ]; then echo $NEWIP > old.ip curl -X POST -d "domain=$DOMAIN&user=$USER&pass=$PASS&ip=$NEWIP" "$SERVER" >> update.log echo "" >> update.log echo "Updated IP address from $OLDIP to $NEWIP." >> update.log fi