From e3183a3312ddfe2d51cd592ab5a49ececd612205 Mon Sep 17 00:00:00 2001
From: Cristian Le <git@lecris.me>
Date: Tue, 31 Aug 2021 21:57:30 +0900
Subject: [PATCH] ddns-script-gandi: Better error management

- Changed DNS endpoint to LiveDNS
- Now using `jshn.sh`
- Better handle errors and ouput

Signed-off-by: Cristian Le <git@lecris.me>
---
 net/ddns-scripts/Makefile                     |  2 +-
 .../files/usr/lib/ddns/update_gandi_net.sh    | 27 ++++++++++++++++---
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/net/ddns-scripts/Makefile b/net/ddns-scripts/Makefile
index 0c3190ff7..48d28d43f 100644
--- a/net/ddns-scripts/Makefile
+++ b/net/ddns-scripts/Makefile
@@ -8,7 +8,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=ddns-scripts
 PKG_VERSION:=2.8.2
-PKG_RELEASE:=12
+PKG_RELEASE:=13
 
 PKG_LICENSE:=GPL-2.0
 
diff --git a/net/ddns-scripts/files/usr/lib/ddns/update_gandi_net.sh b/net/ddns-scripts/files/usr/lib/ddns/update_gandi_net.sh
index 553e7e7aa..8953072e4 100644
--- a/net/ddns-scripts/files/usr/lib/ddns/update_gandi_net.sh
+++ b/net/ddns-scripts/files/usr/lib/ddns/update_gandi_net.sh
@@ -1,19 +1,38 @@
 #!/bin/sh
 # Thanks goes to Alex Griffin who provided this script.
 
+. /usr/share/libubox/jshn.sh
+
 local __TTL=600
 local __RRTYPE
-local __ENDPOINT="https://dns.api.gandi.net/api/v5"
+local __ENDPOINT="https://api.gandi.net/v5/livedns"
+local __STATUS
 
 [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing subdomain as 'username'"
 [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing API Key as 'password'"
 
 [ $use_ipv6 -ne 0 ] && __RRTYPE="AAAA" || __RRTYPE="A"
 
-curl -s -X PUT "$__ENDPOINT/domains/$domain/records/$username/$__RRTYPE" \
-	-H "X-Api-Key: $password" \
+# Construct JSON payload
+json_init
+json_add_int rrset_ttl "$__TTL"
+json_add_array rrset_values
+json_add_string "" "$__IP"
+json_close_array
+
+__STATUS=$(curl -s -X PUT "$__ENDPOINT/domains/$domain/records/$username/$__RRTYPE" \
+	-H "Authorization: Apikey $password" \
 	-H "Content-Type: application/json" \
-	-d "{\"rrset_ttl\": $__TTL, \"rrset_values\": [\"$__IP\"]}" >$DATFILE
+	-d "$(json_dump)" \
+	-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE)
+
+if [ $? -ne 0 ]; then
+	write_log 14 "Curl failed: $(cat $ERRFILE)"
+	return 1
+elif [ -z $__STATUS ] || [ $__STATUS != 201 ]; then
+	write_log 14 "LiveDNS failed: $__STATUS \ngandi.net answered: $(cat $DATFILE)"
+	return 1
+fi
 
 write_log 7 "gandi.net answered: $(cat $DATFILE)"