ddns-scripts: add multiple DNS records support for aliyun ddns
authormonokoo <monokoo@users.noreply.github.com>
Tue, 6 Jan 2026 06:57:46 +0000 (06:57 +0000)
committerFlorian Eckert <Eckert.Florian@googlemail.com>
Sun, 18 Jan 2026 16:11:00 +0000 (17:11 +0100)
Added logic to extract and match DNS record ID from parameters,
with fallback to default selection if no match is found.

Signed-off-by: QiLei Niu <qilei.niu@gmail.com>
net/ddns-scripts/files/usr/lib/ddns/update_aliyun_com.sh

index 9a181f6d256853c932a3595fdfe9e3b3008f0088..bef6c84324172866c928ad0cdb0baedc359feaff 100644 (file)
@@ -105,18 +105,85 @@ json_get_var __RECORD_COUNT TotalCount
        return 1
 }
 
-# if multiple records are found, only use the first one
-[ "$__RECORD_COUNT" -gt 1 ] && {
-       write_log 4 "WARNING: found multiple records of $__HOST, only use the first one"
+# extract RecordId from parameters
+extract_record_id() {
+    local param_enc="$1"
+    local extracted_id=""
+    
+    # Extract RecordId from parameters safely
+    if [ -n "$(echo "${param_enc}" | grep RecordId)" ]; then
+        extracted_id=$(echo "$param_enc" | grep -o 'RecordId=[^&]*' | cut -d'=' -f2)
+    fi
+    
+    echo "$extracted_id"
+}
+
+# find matching record by RecordId
+find_matching_record() {
+    local target_id="$1"
+    local found_match=false
+    
+    if [ -n "$target_id" ]; then
+        write_log 7 "specRecordId: ${target_id}"
+        local idx=1
+        while json_is_a $idx object; do
+            json_select $idx
+            json_get_var tmp RecordId
+            write_log 7 "The $idx Domain RecordId: ${tmp}"
+            if [ "$tmp" = "$target_id" ]; then
+                __RECORD_ID=$target_id
+                json_get_var __RECORD_VALUE Value
+                write_log 7 "The $idx Domain Record Value: ${__RECORD_VALUE}"
+                found_match=true
+                json_select ..
+                break
+            fi
+            idx=$((idx+1))
+            json_select ..
+        done
+    fi
+    
+    if [ "$found_match" = true ]; then
+        return 0
+    else
+        return 1
+    fi
+}
+
+# select the first record as fallback
+select_first_record() {
+    write_log 7 "Using default logic to select record"
+    
+    # Check if __RECORD_COUNT is set before using it
+    if [ "$__RECORD_COUNT" -gt 1 ]; then
+        write_log 4 "WARNING: found multiple records of $__HOST, only use the first one"
+    fi
+    
+    # Select the first DNS record
+    json_select 1
+    # Get the record id of the first DNS record
+    json_get_var __RECORD_ID RecordId
+    json_get_var __RECORD_VALUE Value
 }
 
-# select the first DNS record
 json_select DomainRecords
 json_select Record
-json_select 1
-# get the record id of the first DNS record
-json_get_var __RECORD_ID RecordId
-json_get_var __RECORD_VALUE Value
+
+# Log the original parameter
+write_log 7 "param_enc: `echo ${param_enc}`"
+paramEnc=${param_enc}
+
+# Try to extract RecordId from parameters
+specRecordId=$(extract_record_id "$paramEnc")
+
+# If RecordId is successfully extracted, try to match it
+if [ -n "$specRecordId" ] && find_matching_record "$specRecordId"; then
+    write_log 7 "Found matching record for ID: $specRecordId"
+else
+    # No matching record found or no RecordId to match, use default logic
+    select_first_record
+fi
+
 
 # dont update if the ip has not changed
 [ "$__RECORD_VALUE" = "$__IP" ] && {