This repository has been archived on 2019-08-11. You can view files and clone it, but cannot push or open issues or pull requests.
Manager/plugins/cloudflare/main.sh

66 lines
2.7 KiB
Bash

source "${CWDIR}/.env"
# Record name $3
# Record type $4
# Record content $5 (default: own IP)
# Cloudflare Proxy $6 (true/false)
if FETCH=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${AUTH_KEY}" \
-H "Content-Type: application/json"); then
DOMAIN=($(echo "${FETCH}" | jq -r '.result[].name'))
case "$2" in
"list")
for ((i=0;i<${#DOMAIN[@]};++i)); do
echo "$(echo "${FETCH}" | jq -r ".result[${i}].name") $(echo "${FETCH}" | jq -r ".result[${i}].type") $(echo "${FETCH}" | jq -r ".result[${i}].content") $(echo "${FETCH}" | jq -r ".result[${i}].id")" | output info $(</dev/stdin)
done
;;
"set")
if ! (containsElement "$3" "${DOMAIN[@]}"); then
if $(curl -sX POST "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${AUTH_KEY}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"${4}\",\"name\":\"${3}\",\"content\":\"${5}\",\"proxied\":"${6}"}" | jq -r '.success'); then
output done "Registered entry successfully."
else
output erro "Failed to add entry"
fi
else
for ((i=0;i<${#DOMAIN[@]};++i)); do
if [[ "${DOMAIN[$i]}" = "$3" ]]; then
if $(curl -sX PUT "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$(echo "${FETCH}" | jq --compact-output -r "{(.result[$i].id): .result[$i].name}" | grep -oP "(?<=\{\")(.*)(?=\"\:\".*\"\})")" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${AUTH_KEY}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"${4}\",\"name\":\"${3}\",\"content\":\"${5}\",\"proxied\":"${6}"}" | jq -r '.success'); then
output done "Edited entry successfully."
else
output erro "Failed to edit entry"
fi
fi
done
fi
;;
"remove")
if (containsElement "$3" "${DOMAIN[@]}"); then
for ((i=0;i<${#DOMAIN[@]};++i)); do
if [[ "${DOMAIN[$i]}" = "$3" ]]; then
if $(curl -sX DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$(echo "${FETCH}" | jq --compact-output -r "{(.result[$i].id): .result[$i].name}" | grep -oP "(?<=\{\")(.*)(?=\"\:\".*\"\})")" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${AUTH_KEY}" \
-H "Content-Type: application/json" | jq -r '.success'); then
output done "Removed entry successfully."
else
output erro "Failed to remove entry"
fi
fi
done
else
output warn "Entry ${3} does not exist."
fi
;;
esac
fi