52 lines
1.3 KiB
Bash
Executable File
52 lines
1.3 KiB
Bash
Executable File
#! /usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
mkdir -p .var
|
|
|
|
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"
|
|
|
|
function info() {
|
|
echo "$(date --iso-8601=minutes) CLOUDFLARE_DNS_UPDATE [INFO]: ${*}"
|
|
}
|
|
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 --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: ' \
|
|
--data '{
|
|
"content": "'${CACHED_PUBLIC_IP}'",
|
|
"name": "tylencloud.com",
|
|
"proxied": false,
|
|
"type": "A",
|
|
"comment": "Main Record",
|
|
"ttl": 3600
|
|
}' \
|
|
-H "Authorization: Bearer ${AUTHORIZATION}";
|