From 1b3bb98fcfab7f951042729f3f4c5b108c86bd53 Mon Sep 17 00:00:00 2001 From: Florian Eckert Date: Fri, 18 Sep 2020 11:52:16 +0200 Subject: [PATCH] ddns-scripts: load ddsn service provider parameter from json Signed-off-by: Florian Eckert --- .../files/dynamic_dns_functions.sh | 67 ++++++++++--------- net/ddns-scripts/files/dynamic_dns_updater.sh | 5 +- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/net/ddns-scripts/files/dynamic_dns_functions.sh b/net/ddns-scripts/files/dynamic_dns_functions.sh index f9d1b23c0..b5dc144ca 100755 --- a/net/ddns-scripts/files/dynamic_dns_functions.sh +++ b/net/ddns-scripts/files/dynamic_dns_functions.sh @@ -313,45 +313,50 @@ urlencode() { } # extract url or script for given DDNS Provider from -# file /etc/ddns/services for IPv4 or from -# file /etc/ddns/services_ipv6 for IPv6 +# directory /usr/share/ddns/services/ipv4/ for IPv4 +# or from +# directory /usr/share/ddns/services/ipv6/ for IPv6 +# $1 Name of the provider # $1 Name of Variable to store url to # $2 Name of Variable to store script to # $3 Name of Variable to store service answer to get_service_data() { - local __FILE __SERVICE __DATA __ANSWER __URL __SCRIPT __PIPE + local provider="$1" + shift - [ $# -ne 3 ] && write_log 12 "Error calling 'get_service_data()' - wrong number of parameters" - - __FILE="/etc/ddns/services" # IPv4 - [ $use_ipv6 -ne 0 ] && __FILE="/etc/ddns/services_ipv6" # IPv6 - - # workaround with variables; pipe create subshell with no give back of variable content - __PIPE="$ddns_rundir/pipe_$$" - mkfifo "$__PIPE" - - # only grep without # or whitespace at linestart | remove " - sed '/^#/d; /^[ \t]*$/d; s/\"//g' "$__FILE" > "$__PIPE" & + . /usr/share/libubox/jshn.sh + local dir="/usr/share/ddns/services" + local name data url answer script - while read __SERVICE __DATA __ANSWER; do - if [ "$__SERVICE" = "$service_name" ]; then - # check if URL or SCRIPT is given - __URL=$(echo "$__DATA" | grep "^http") - [ -z "$__URL" ] && __SCRIPT="/usr/lib/ddns/$__DATA" + [ $# -ne 3 ] && write_log 12 "Error calling 'get_service_data()' - wrong number of parameters" - eval "$1=\"$__URL\"" - eval "$2=\"$__SCRIPT\"" - eval "$3=\"$__ANSWER\"" - rm "$__PIPE" - return 0 - fi - done < "$__PIPE" - rm "$__PIPE" + [ -f "${dir}/${provider}.json" ] || { + eval "$1=\"\"" + eval "$2=\"\"" + eval "$3=\"\"" + return 1 + } - eval "$1=\"\"" # no service match clear variables - eval "$2=\"\"" - eval "$3=\"\"" - return 1 + json_load_file "${dir}/${provider}.json" + json_get_var name "name" + if [ "$use_ipv6" -eq "1" ]; then + json_select "ipv6" + else + json_select "ipv4" + fi + json_get_var data "url" + json_get_var answer "answer" + json_select ".." + json_cleanup + + # check if URL or SCRIPT is given + url=$(echo "$data" | grep "^http") + [ -z "$url" ] && script="/usr/lib/ddns/${data}" + + eval "$1=\"$url\"" + eval "$2=\"$script\"" + eval "$3=\"$answer\"" + return 0 } # Calculate seconds from interval and unit diff --git a/net/ddns-scripts/files/dynamic_dns_updater.sh b/net/ddns-scripts/files/dynamic_dns_updater.sh index 2076c0d92..9e75552d9 100755 --- a/net/ddns-scripts/files/dynamic_dns_updater.sh +++ b/net/ddns-scripts/files/dynamic_dns_updater.sh @@ -232,7 +232,10 @@ esac # determine what update url we're using if a service_name is supplied # otherwise update_url is set inside configuration (custom update url) # or update_script is set inside configuration (custom update script) -[ -n "$service_name" ] && get_service_data update_url update_script UPD_ANSWER +[ -n "$service_name" ] && { + get_service_data "$service_name" update_url update_script UPD_ANSWER +} + [ -z "$update_url" -a -z "$update_script" ] && write_log 14 "No update_url found/defined or no update_script found/defined!" [ -n "$update_script" -a ! -f "$update_script" ] && write_log 14 "Custom update_script not found!"