added comments

This commit is contained in:
Daniel Steglich 2015-03-04 22:22:30 +01:00
parent 1075665a90
commit eab3ac2cfc

View File

@ -1,25 +1,40 @@
#!/bin/bash
#Todo: IPv6
#Todo: Other service types than gnudip (generic update URL)
#Todo: GET WAN IP from Router via UPnP if supported
################################################################
# #
# The script is part of Freedombox #
# #
# This script is a wrapper around ez-ipupdate and/or wget #
# to update a Dynamic DNS account. The script is used as an #
# interface between plinth and ez-ipupdate #
# the script will store configuration, return configuration #
# to plinth UI and do a dynamic DNS update. The script will #
# also determe if we are behind a NAT device, if we can use #
# ez-ipupdate tool or if we need to do some wget magic #
# #
# Todo: IPv6 #
# Todo: GET WAN IP from Router via UPnP if supported #
# Todo: licence string? #
# author: Daniel Steglich <steglich@datasystems24.de> #
# #
################################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#static values
# static values
WGET=$(which wget)
WGETOPTIONS="-4 -o /dev/null -t 3 -T 3"
EMPTYSTRING="none"
NOIP="0.0.0.0"
#how often do we poll for IP changes if we are behind a NAT?
# how often do we poll for IP changes if we are behind a NAT?
UPDATEMINUTES=5
#if we do not have a IP check URL, how often should we do a "blind" update
# if we do not have a IP check URL, how often should we do a "blind" update
UPDATEMINUTESUNKNOWN=3600
TOOLNAME=ez-ipupdate
UPDATE_TOOL=$(which ${TOOLNAME})
DISABLED_STRING='disabled'
ENABLED_STRING='enabled'
#Dirs and filenames
# Dirs and filenames
CFGDIR="/etc/${TOOLNAME}/"
CFG="${CFGDIR}${TOOLNAME}.conf"
CFG_disabled="${CFGDIR}${TOOLNAME}.inactive"
@ -30,6 +45,7 @@ HELPERCFG="${CFGDIR}${TOOLNAME}-plinth.cfg"
CRONJOB="/etc/cron.d/${TOOLNAME}"
PIDFILE="/var/run/ez-ipupdate.pid"
# this function will parse commandline options
doGetOpt()
{
basicauth=0
@ -77,25 +93,26 @@ doGetOpt()
done
}
# this function will write a persistent config file to disk
doWriteCFG()
{
mkdir ${CFGDIR} 2> /dev/null
#always write to the inactive config - needs to be enabled via "start" command later
# always write to the inactive config - needs to be enabled via "start" command later
local out_file=${CFG_disabled}
#reset the last update time
# reset the last update time
echo 0 > ${LASTUPDATE}
#reset the last updated IP
# reset the last updated IP
echo "0.0.0.0" > ${IPFILE}
#reset last update (if there is one)
# reset last update (if there is one)
rm ${STATUSFILE} 2> /dev/null
#find the interface (always the default gateway interface)
# find the interface (always the default gateway interface)
default_interface=$(ip route |grep default |awk '{print $5}')
#store the given options in ez-ipupdate compatible config file
# store the given options in ez-ipupdate compatible config file
echo "host=${host}" > ${out_file}
echo "server=${server}" >> ${out_file}
echo "user=${user}:${pass}" >> ${out_file}
@ -103,12 +120,12 @@ doWriteCFG()
echo "retrys=3" >> ${out_file}
echo "wildcard" >> ${out_file}
#store UPDATE URL params
# store UPDATE URL params
echo "POSTURL ${updateurl}" > ${HELPERCFG}
echo "POSTAUTH ${basicauth}" >> ${HELPERCFG}
echo "POSTSSLIGNORE ${ignoreCertError}" >> ${HELPERCFG}
#check if we are behind a NAT Router
# check if we are behind a NAT Router
echo "IPURL ${ipurl}" >> ${HELPERCFG}
if [ -z ${ipurl} ];then
echo "NAT unknown" >> ${HELPERCFG}
@ -116,20 +133,24 @@ doWriteCFG()
doGetWANIP
ISGLOBAL=$(ip addr ls ${default_interface} | grep ${wanip})
if [ -z ${ISGLOBAL} ];then
#we are behind NAT
# we are behind NAT
echo "NAT yes" >> ${HELPERCFG}
else
#we are directly connected
# we are directly connected
echo "NAT no" >> ${HELPERCFG}
#if this file is added ez-ipupdate will take ip form this interface
# if this file is added ez-ipupdate will take ip form this interface
echo "interface=${default_interface}" >> ${out_file}
#if this line is added to config file, ez-ipupdate will be launched on startup via init.d
# if this line is added to config file, ez-ipupdate will be launched on startup via init.d
echo "daemon" >> ${out_file}
echo "execute=${0} success" >> ${out_file}
fi
fi
}
# this function will read the config file from disk
# special treatment for empty strings is done here:
# plinth will give empty strings like: ''
# but we don't want this single quotes to be used
doReadCFG()
{
host=""
@ -153,6 +174,10 @@ doReadCFG()
fi
}
# replace vars from url: i.e.:
# https://example.com/update.php?domain=<Domain>&User=<User>&Pass=<Pass>
# also this function will remove the surounding single quotes from the URL string
# as plinth will add them
doReplaceVars()
{
local url=`echo ${updateurl} | sed "s/<Ip>/${wanip}/g"`
@ -163,6 +188,10 @@ doReplaceVars()
updateurl=$url
}
# doReadCFG() needs to be run before this
# this function will return all configured parameters in a way that
# plinth will understand (plinth know the order of
# parameters this function will return)
doStatus()
{
PROC=$(pgrep ${TOOLNAME})
@ -216,6 +245,8 @@ doStatus()
fi
}
# ask a public WEB Server for the WAN IP we are comming from
# and store this ip within $wanip
doGetWANIP()
{
if [ ! -z ${ipurl} ];then
@ -225,11 +256,13 @@ doGetWANIP()
rm ${outfile}
[ -z ${wanip} ] && wanip=${NOIP}
else
#no WAN IP found because of missing check URL
# no WAN IP found because of missing check URL
wanip=${NOIP}
fi
}
# actualy do the update (using wget or ez-ipupdate or even both)
# this function is called via cronjob
doUpdate()
{
if [ ! -z ${server} ];then
@ -238,7 +271,6 @@ doUpdate()
if [ ! -z ${updateurl} ];then
doReplaceVars
if [ "${basicauth}" = "enabled" ];then
local wgetoptions=" --user $user --password $pass "
fi
@ -247,7 +279,7 @@ doUpdate()
fi
$WGET ${wgetoptions} "${updateurl}"
#ToDo: check the returning text from WEB Server. User need to give expected string.
# ToDo: check the returning text from WEB Server. User need to give expected string.
if [ $? -eq 0 ];then
${0} success $wanip
else
@ -258,9 +290,12 @@ doUpdate()
cmd=${1}
shift
# decide which config to use
cfgfile="/tmp/none"
[ -f ${CFG_disabled} ] && cfgfile=${CFG_disabled}
[ -f ${CFG} ] && cfgfile=${CFG}
# check what action is requested
case ${cmd} in
configure)
doGetOpt ${@}
@ -274,14 +309,14 @@ case ${cmd} in
mv ${CFG_disabled} ${CFG}
/etc/init.d/${TOOLNAME} start
fi
#if we are not behind a NAT device and we use update-URL, add a cronjob
#(daemon tool does not support update-URL feature)
# if we are not behind a NAT device and we use update-URL, add a cronjob
# (daemon tool does not support update-URL feature)
if [ ! -z $(cat $HELPERCFG |grep ^POSTURL | awk '{print $2}') ];then
echo "*/${UPDATEMINUTES} * * * * root $0 update" > ${CRONJOB}
$0 update
fi
else
#if we are behind a NAT device, add a cronjob (daemon tool cannot monitor WAN IP changes)
# if we are behind a NAT device, add a cronjob (daemon tool cannot monitor WAN IP changes)
echo "*/${UPDATEMINUTES} * * * * root $0 update" > $CRONJOB
$0 update
fi
@ -300,11 +335,11 @@ case ${cmd} in
cat ${cfgfile} |grep -v execute > ${cfgfile}.tmp
mv ${cfgfile}.tmp ${cfgfile}
echo "execute=${0} success ${wanip}" >> ${cfgfile}
#if we know our WAN IP, only update if IP changes
# if we know our WAN IP, only update if IP changes
if [ "${oldip}" != "${wanip}" -a "${wanip}" != ${NOIP} ];then
doUpdate
fi
#if we don't know our WAN IP do a blind update once a hour
# if we don't know our WAN IP do a blind update once a hour
if [ "${wanip}" = ${NOIP} ];then
uptime=$(cat /proc/uptime |cut -d . -f 1)
LAST=0
@ -325,11 +360,11 @@ case ${cmd} in
date=$(date)
echo "last update done (${date})" > ${STATUSFILE}
cat /proc/uptime |awk '{print $1}' |cut -d . -f 1 > ${LASTUPDATE}
#if called from cronjob, the current IP is given as parameter
# if called from cronjob, the current IP is given as parameter
if [ $# -eq 1 ];then
echo ${1} > ${IPFILE}
else
#if called from ez-ipupdate daemon, no WAN IP is given as parameter
# if called from ez-ipupdate daemon, no WAN IP is given as parameter
doGetWANIP
echo ${wanip} > ${IPFILE}
fi