services: add script to update cloudflare dns records
This commit is contained in:
parent
36f11132d7
commit
74a6b83d75
1
services/.gitignore
vendored
Normal file
1
services/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
**/.var
|
||||||
51
services/cloudflare_update_dns
Executable file
51
services/cloudflare_update_dns
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
#! /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}";
|
||||||
@ -7,8 +7,8 @@ if [ "$#" -ne 1 ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Set the API token and chat ID
|
# Set the API token and chat ID
|
||||||
API_TOKEN="7074090911:AAEoazbp1LcqRE7qq9Jf71sXpQaQOz-D0fc"
|
API_TOKEN="$(cat .var/telegram_bot_api_token)"
|
||||||
CHAT_ID="1622364607"
|
CHAT_ID="$(cat .var/telegram_bot_chat_id)"
|
||||||
|
|
||||||
MESSAGE="$1"
|
MESSAGE="$1"
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user