From 6f1f77076f9f612b4681dff35111e3ea4726be06 Mon Sep 17 00:00:00 2001 From: zhojielun <137139821+zhojielun@users.noreply.github.com> Date: Wed, 12 Mar 2025 00:10:11 +0800 Subject: [PATCH 1/2] Update cf-v4-ddns.sh --- cf-v4-ddns.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cf-v4-ddns.sh b/cf-v4-ddns.sh index 3cc4ffc..1da0661 100755 --- a/cf-v4-ddns.sh +++ b/cf-v4-ddns.sh @@ -44,13 +44,13 @@ CFTTL=60 # Ignore local file, update ip anyway FORCE=false -WANIPSITE="http://ipv4.icanhazip.com" +WANIPSITE="https://api4.ipify.org" # Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ... if [ "$CFRECORD_TYPE" = "A" ]; then : elif [ "$CFRECORD_TYPE" = "AAAA" ]; then - WANIPSITE="http://ipv6.icanhazip.com" + WANIPSITE="https://api6.ipify.org" else echo "$CFRECORD_TYPE specified is invalid, CFRECORD_TYPE can only be A(for IPv4)|AAAA(for IPv6)" exit 2 From ec64d0412c8d961cc285a13338de76c462e888e0 Mon Sep 17 00:00:00 2001 From: zhojielun <137139821+zhojielun@users.noreply.github.com> Date: Wed, 12 Mar 2025 00:26:51 +0800 Subject: [PATCH 2/2] Update cf-v4-ddns.sh --- cf-v4-ddns.sh | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cf-v4-ddns.sh b/cf-v4-ddns.sh index 1da0661..47fe685 100755 --- a/cf-v4-ddns.sh +++ b/cf-v4-ddns.sh @@ -7,7 +7,7 @@ set -o pipefail # Can retrieve cloudflare Domain id and list zone's, because, lazy # Place at: -# curl https://raw.githubusercontent.com/aipeach/cloudflare-api-v4-ddns/dev/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh +# curl https://raw.githubusercontent.com/yulewang/cloudflare-api-v4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh # run `crontab -e` and add next line: # */1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1 # or you need log: @@ -16,6 +16,7 @@ set -o pipefail # Usage: # cf-ddns.sh -k cloudflare-api-key \ +# -u user@example.com \ # -h host.example.com \ # fqdn of the record you want to update # -z example.com \ # will show you all zones if forgot, but you need this # -t A|AAAA # specify ipv4/ipv6, default: ipv4 @@ -25,10 +26,13 @@ set -o pipefail # default config -# API key, see https://dash.cloudflare.com/profile/api-tokens, +# API key, see https://www.cloudflare.com/a/account/my-account, # incorrect api-key results in E_UNAUTH error CFKEY= +# Username, eg: user@example.com +CFUSER= + # Zone name, eg: example.com CFZONE_NAME= @@ -36,10 +40,10 @@ CFZONE_NAME= CFRECORD_NAME= # Record type, A(IPv4)|AAAA(IPv6), default IPv4 -CFRECORD_TYPE=A +CFRECORD_TYPE=AAAA # Cloudflare TTL for record, between 120 and 86400 seconds -CFTTL=60 +CFTTL=120 # Ignore local file, update ip anyway FORCE=false @@ -57,9 +61,10 @@ else fi # get parameter -while getopts k:h:z:t:f: opts; do +while getopts k:u:h:z:t:f: opts; do case ${opts} in k) CFKEY=${OPTARG} ;; + u) CFUSER=${OPTARG} ;; h) CFRECORD_NAME=${OPTARG} ;; z) CFZONE_NAME=${OPTARG} ;; t) CFRECORD_TYPE=${OPTARG} ;; @@ -73,6 +78,11 @@ if [ "$CFKEY" = "" ]; then echo "and save in ${0} or using the -k flag" exit 2 fi +if [ "$CFUSER" = "" ]; then + echo "Missing username, probably your email-address" + echo "and save in ${0} or using the -u flag" + exit 2 +fi if [ "$CFRECORD_NAME" = "" ]; then echo "Missing hostname, what host do you want to update?" echo "save in ${0} or using the -h flag" @@ -110,8 +120,8 @@ if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ] \ CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE") else echo "Updating zone_identifier & record_identifier" - CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "Authorization: Bearer $CFKEY" -H "Content-Type: application/json" | grep -Eo '"id":"[^"]*'|sed 's/"id":"//' | head -1 ) - CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "Authorization: Bearer $CFKEY" -H "Content-Type: application/json" | grep -Eo '"id":"[^"]*'|sed 's/"id":"//' | head -1 ) + CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 ) + CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 ) echo "$CFZONE_ID" > $ID_FILE echo "$CFRECORD_ID" >> $ID_FILE echo "$CFZONE_NAME" >> $ID_FILE @@ -122,7 +132,8 @@ fi echo "Updating DNS to $WAN_IP" RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" \ - -H "Authorization: Bearer $CFKEY" \ + -H "X-Auth-Email: $CFUSER" \ + -H "X-Auth-Key: $CFKEY" \ -H "Content-Type: application/json" \ --data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$WAN_IP\", \"ttl\":$CFTTL}")