#!/bin/sh
g_pslfile=/usr/share/public_suffix_list.dat.gz
[ -f "$g_pslfile" ] || g_pslfile="$(dirname $0)/public_suffix_list.dat.gz"
g_pslerr=0
g_cfgfile="ddns"
mod_cloudflare_v1_domain() {
[ -f "$g_pslfile" ] || return 1
[ $# -ne 1 -o -z "$1" ] && \
{ printf "%s\\n" "mod_cloudflare_v1_domain() - Invalid number of parameters" >&2; return 1; }
local mcd_fqdn=$1
local mcd_fsub=""
local mcd_fdom=""
local mcd_ctld=""
local mcd_ftld=""
if [ $( printf "%s" "$mcd_fqdn" | grep -cF "@" 2>/dev/null ) -gt 0 ]; then
printf "%s" "$mcd_fqdn"
return 0
fi
set -- $(printf %s "$mcd_fqdn" | tr [A-Z] [a-z] | tr "." " " \
| awk '{do printf "%s"(NF>1?OFS:ORS),$NF;while (--NF)}' )
while [ -n "${1:-}" ] ; do
if [ -z "$mcd_ctld" ]; then
mcd_ctld="$1"
shift
else
mcd_ctld="$1.$mcd_ctld"
shift
fi
zcat $g_pslfile | grep -E "^$mcd_ctld$" >/dev/null 2>&1 && {
mcd_ftld="$mcd_ctld"
mcd_fdom="${1:-}"
continue
}
zcat $g_pslfile | grep -E "^\*.$mcd_ctld$" >/dev/null 2>&1 && {
[ -z "${1:-}" ] && break
if zcat $g_pslfile | grep -E "^!$1.$mcd_ctld$" >/dev/null 2>&1 ; then
mcd_ftld="$mcd_ctld"
else
mcd_ftld="$1.$mcd_ctld"
shift
fi
mcd_fdom="$1"; shift
}
[ -n "$mcd_ftld" ] && break
done
while [ -n "${1:-}" ]; do
mcd_fsub="${1}${mcd_fsub:+.$mcd_fsub}"
shift
done
[ -z "$mcd_ftld" ] && { printf "%s\\n" "mod_cloudflare_v1_domain() - no TLD not found in '$mcd_fqdn'" >&1; return 1; }
[ -z "$mcd_fdom" ] && { printf "%s\\n" "mod_cloudflare_v1_domain() - no registrable Domain not found in '$mcd_fqdn'" >&1; return 1; }
printf "%s" "${mcd_fsub:+${mcd_fsub}@}${mcd_fdom}.${mcd_ftld}"
return 0
}
timer2dhms() {
local t=0
case $2 in
days)		t=$(( $1 * 86400 ));;
hours)		t=$(( $1 * 3600 ));;
minutes)	t=$(( $1 * 60 ));;
*)		t=$1;;
esac
local d=$(( $t / 86400 ))
local h=$(( $t % 86400 / 3600 ))
local m=$(( $t % 3600 / 60 ))
local s=$(( $t % 60 ))
if [ $d -gt 0 ]; then printf "%dd %02dh %02dm %02ds" "$d" "$h" "$m" "$s"
elif [ $h -gt 0 ]; then printf "%dh %02dm %02ds" "$h" "$m" "$s"
elif [ $m -gt 0 ]; then printf "%dm %02ds" "$m" "$s"
else printf "%ds" "$s"; fi
unset d h m s t
return 0
}
update_config() {
uc_uci="$(which uci) -q"
uc_cfg=""
uc_name=""
uc_var=""
uc_val=""
package() { return 0; }
config () {
uc_cfg="$1"
uc_name="$2"
if [ "$uc_cfg" = "$g_cfgfile" -a "$uc_name" = "global" ]; then
option() {
uc_var="$1"; shift
uc_val="$*"
case "$uc_var" in
allow_local_ip)	$uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_privateip";;
date_format)	$uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_dateformat";;
log_lines)	$uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_loglines";;
log_dir)	$uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_logdir";;
run_dir)	$uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_rundir";;
*)	;;
esac
}
elif [ "$uc_cfg" = "service" ]; then
option() {
uc_var="$1"; shift
uc_val="$*"
case "$uc_var" in
service_name|upd_provider)
case "$uc_val" in
freedns\.afraid\.org|afraid\.org)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="afraid.org-keyauth";;
Bind-nsupdate)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="bind-nsupdate";;
CloudFlare|cloudflare\.com|cloudflare\.com-v1)
$uc_uci get $g_cfgfile.$uc_name.lookup_host >/dev/null 2>&1 || {
ucv_domain=$($uc_uci get $g_cfgfile.$uc_name.domain 2>/dev/null)
$uc_uci set $g_cfgfile.$uc_name.lookup_host="$ucv_domain"
}
if [ -f "$g_pslfile" ]; then
ucv_domain=$($uc_uci get $g_cfgfile.$uc_name.domain 2>/dev/null)
ucv_object=$($uc_uci get $g_cfgfile.$uc_name.upd_object 2>/dev/null)
if [ -n "$ucv_domain" ]; then
ucv_new=$(mod_cloudflare_v1_domain "$ucv_domain") || g_pslerr=1
[ $g_pslerr -eq 0 ] && \
$uc_uci set $g_cfgfile.$uc_name.domain="$ucv_new"
fi
if [ -n "$ucv_object" ]; then
ucv_new=$(mod_cloudflare_v1_domain "$ucv_object") || g_pslerr=1
[ $g_pslerr -eq 0 ] && \
$uc_uci set $g_cfgfile.$uc_name.upd_object="$ucv_new"
fi
fi
unset ucv_domain ucv_object ucv_new
$uc_uci set $g_cfgfile.$uc_name.$uc_var="cloudflare.com-v1"
;;
dyndns\.org|dyndns\.com)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="dyn.com";;
free\.editdns\.net)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="editdns.net";;
domains\.google\.com)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="google.com";;
loopia\.com)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="loopia.se";;
NoIP\.com|No-IP\.com)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="no-ip.com";;
spdns\.de)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="spdyn.de";;
strato\.de)
$uc_uci set $g_cfgfile.$uc_name.$uc_var="strato.com";;
*)
;;
esac
;;
domain|upd_object)
$uc_uci get $g_cfgfile.$uc_name.lookup_host >/dev/null 2>&1 || \
$uc_uci set $g_cfgfile.$uc_name.lookup_host="$uc_val"
if [ -f "$g_pslfile" ]; then
uco_provider=$($uc_uci get $g_cfgfile.$uc_name.upd_provider 2>/dev/null) || \
uco_provider=$($uc_uci get $g_cfgfile.$uc_name.service_name 2>/dev/null)
if [ "$uco_provider" = "CloudFlare" \
-o "$uco_provider" = "cloudflare.com" \
-o "$uco_provider" = "cloudflare.com-v1" ]; then
ucv_new=$(mod_cloudflare_v1_domain "$uc_val") || g_pslerr=1
[ $g_pslerr -eq 0 ] && \
$uc_uci set $g_cfgfile.$uc_name.$uc_var="$ucv_new"
unset ucv_new
fi
unset uco_provider
fi
;;
*)	;;
esac
return 0
}
return 0
else
return 0
fi
}
uc_data=$($uc_uci -S -n export "$g_cfgfile")
uc_ret="$?"
[ $uc_ret -ne 0 ] && {
touch /etc/config/$uc_cfgfile
chmod 644 /etc/config/$uc_cfgfile
}
[ $uc_ret -eq 0 -a -n "$uc_data" ] && eval "$uc_data"
$uc_uci set ddns.global="$g_cfgfile"
$uc_uci commit "$g_cfgfile"
unset uc_uci uc_cfg uc_name uc_var uc_val uc_ret uc_data
return 0
}
rm -f /tmp/luci-indexcache >/dev/null 2>&1
update_config
#cleanup
[ $g_pslerr -ne 0 ] && {
unset g_pslfile g_pslerr g_cfgfile
return 1
}
[ -f "$g_pslfile" ] && rm -f "$g_pslfile"
unset g_pslfile g_pslerr g_cfgfile
return 0
