Bug fixed

Added "record name" and "zone name" in the local file, and check whether they is equl with the user's input.
Prevent the make error when user changing these two properties.
This commit is contained in:
yulewang 2018-07-01 18:54:40 +08:00
parent b43f5e9aa3
commit d1c5b3ca58

View File

@ -32,7 +32,7 @@ CFKEY=
# Username, eg: user@example.com # Username, eg: user@example.com
CFUSER= CFUSER=
# Zone name, will list all possible if missing, eg: example.com # Zone name, eg: example.com
CFZONE_NAME= CFZONE_NAME=
# Hostname to update, eg: homeserver.example.com # Hostname to update, eg: homeserver.example.com
@ -90,24 +90,29 @@ else
OLD_WAN_IP="" OLD_WAN_IP=""
fi fi
# Get zone_identifier & record_identifier
ID_FILE=$HOME/.id-cf.txt
if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 2 ]; then
CFZONE_ID=$(head -1 $ID_FILE)
CFRECORD_ID=$(tail -1 $ID_FILE)
else
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":")[^"]*')
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
fi
# If WAN IP is unchanged an not -f flag, exit here # If WAN IP is unchanged an not -f flag, exit here
if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then
echo "WAN IP Unchanged, to update anyway use flag -f true" echo "WAN IP Unchanged, to update anyway use flag -f true"
exit 0 exit 0
fi fi
# Get zone_identifier & record_identifier
ID_FILE=$HOME/.id-cf.txt
if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ] \
&& [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ] \
&& [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then
CFZONE_ID=$(head -1 $ID_FILE)
CFRECORD_ID=$(tail -1 $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":")[^"]*')
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE
echo "$CFRECORD_NAME" >> $ID_FILE
fi
# If WAN is changed, update cloudflare # If WAN is changed, update cloudflare
echo "Updating DNS to $WAN_IP" echo "Updating DNS to $WAN_IP"
@ -119,7 +124,6 @@ RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID
if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ $(echo $RESPONSE | grep "\"success\":true") != "" ]; then if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ $(echo $RESPONSE | grep "\"success\":true") != "" ]; then
echo "Updated succesfuly!" echo "Updated succesfuly!"
echo $RESPONSE
echo $WAN_IP > $HOME/.wan_ip-cf.txt echo $WAN_IP > $HOME/.wan_ip-cf.txt
exit exit
else else