#! /usr/bin/env bash set -euo pipefail ZONE_ID="$(cat .var/zone_id)" DNS_RECORD="$(cat .var/dns_record)" CACHED_PUBLIC_IP="$(cat .var/cached_public_ip)" AUTHORIZATION="$(cat .var/bearer)" IP_URL="ifconfig.me/ip" SEND_TELEGRAM="$(pwd)/send_telegram" LOG_FILE_PARENT="/var/log/cloudflare_update" LOG_FILE="${LOG_FILE_PARENT}/$(date +%d_%m_%Y_update_dns).log" mkdir -p "${LOG_FILE_PARENT}" function info() { echo "$(date --iso-8601=minutes) CLOUDFLARE_DNS_UPDATE [INFO]: ${*}" echo "$(date --iso-8601=minutes) CLOUDFLARE_DNS_UPDATE [INFO]: ${*}" >> "${LOG_FILE}" } function error() { echo "$(date --iso-8601=minutes) CLOUDFLARE_DNS_UPDATE [ERROR]: ${*}" } function check_new_ip() { local new_ip new_ip="$(curl -s ${IP_URL})" if [[ "${new_ip}" == "${CACHED_PUBLIC_IP}" ]]; then info "Public IP has not changed from ${new_ip}. Exiting..." exit 0; else info "Public IP has changed from ${CACHED_PUBLIC_IP} to ${new_ip}. Updating records..." "${SEND_TELEGRAM}" "Public IP has changed from ${CACHED_PUBLIC_IP} to ${new_ip}. Updating records..." CACHED_PUBLIC_IP="${new_ip}" echo "${new_ip}" > .var/cached_public_ip fi } check_new_ip curl_answer="$(curl --request PATCH \ --url "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${DNS_RECORD}" \ --header 'Content-Type: application/json' \ --header 'X-Auth-Email: ' \ --silent \ --data '{ "content": "'${CACHED_PUBLIC_IP}'", "name": "tylencloud.com", "proxied": false, "type": "A", "comment": "Main Record", "ttl": 3600 }' \ -H "Authorization: Bearer ${AUTHORIZATION}")" info "${curl_answer}" "${SEND_TELEGRAM}" "$(echo ${curl_answer} | jq)"