Skip to content

Commit

Permalink
Update cf-v4-ddns.sh
Browse files Browse the repository at this point in the history
Use new authenticate way, see https://api.cloudflare.com/#getting-started-requests.
Change auth header to `Authorization: Bearer`.
Remove `X-Auth-Key` and `X-Auth-Email` header.
Remove $CFUSER and -u option.

fix issue yulewang#22
  • Loading branch information
TeCHiScy committed Oct 4, 2022
1 parent 1336b07 commit bf2f7c9
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions cf-v4-ddns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ set -o pipefail

# Usage:
# cf-ddns.sh -k cloudflare-api-key \
# -u [email protected] \
# -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
Expand All @@ -30,9 +29,6 @@ set -o pipefail
# incorrect api-key results in E_UNAUTH error
CFKEY=

# Username, eg: [email protected]
CFUSER=

# Zone name, eg: example.com
CFZONE_NAME=

Expand Down Expand Up @@ -64,7 +60,6 @@ fi
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} ;;
Expand All @@ -78,11 +73,6 @@ 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"
Expand Down Expand Up @@ -120,8 +110,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 "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 )
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 -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 "Authorization: Bearer $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
Expand All @@ -132,8 +122,7 @@ 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 "X-Auth-Email: $CFUSER" \
-H "X-Auth-Key: $CFKEY" \
-H "Authorization: Bearer $CFKEY" \
-H "Content-Type: application/json" \
--data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$WAN_IP\", \"ttl\":$CFTTL}")

Expand Down

0 comments on commit bf2f7c9

Please sign in to comment.