ddns-scripts: load ddsn service provider parameter from json
authorFlorian Eckert <fe@dev.tdt.de>
Fri, 18 Sep 2020 09:52:16 +0000 (11:52 +0200)
committerFlorian Eckert <fe@dev.tdt.de>
Mon, 21 Sep 2020 08:17:01 +0000 (10:17 +0200)
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
net/ddns-scripts/files/dynamic_dns_functions.sh
net/ddns-scripts/files/dynamic_dns_updater.sh

index f9d1b23c0502df5448617ffcb2aa6431887635ee..b5dc144ca183759a19a21c46d411fc357b5f7346 100755 (executable)
@@ -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
index 2076c0d9245e2c05600529f74006360cf1ab2f63..9e75552d9995f3b0b66459bfdddbcf8101b0bde2 100755 (executable)
@@ -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!"