Update CloudFlare/cloudflare_ddns.ps1

This commit is contained in:
git_administrator 2023-12-22 21:52:42 +00:00
parent b653b0c99f
commit f99a1a99ed

View File

@ -1,8 +1,16 @@
# tomaszjkostrzewa@gmail.com
# gitea.RdzeN.net
# DDNS Cloudflare - KeepUpIP
#
<#
The script checks the current external IP based on defined websites.
In case of a change in the external IP, it updates the CloudFlare service using a dedicated API.
It requires providing 'api_key' and 'dns_zone' IDs.
Parameters, such as the number of logs set to 10k lines, can be customized.
The frequency of execution should be defined in cron/schedule.
#>
# Variable section
$location = Get-Location
$path_ip = "$($location)\cloudflare_ddns_ip" # store last set IP
$path_log = "$($location)\cloudflare_ddns_log" # store logs
@ -13,6 +21,12 @@ $ip_actuall = $null
$ip_ddns = $null
$api_key = 'enter_api_key' # https://dash.cloudflare.com/profile/api-tokens
$dns_zone = 'enter_dns_zone_id' # cloudflare -> websites -> your site -> dns_zone
# if as Page list;
# Alternatively, it checks whether the file already exists.
# The file changes the order of pages by rotating like a die.
# The first element becomes the last, and so on, in a rolling cube fashion.
if (-not (Test-Path $path_sites)) {
$ip_sites = @(
'http://ipinfo.io/ip'
@ -24,6 +38,8 @@ if (-not (Test-Path $path_sites)) {
[array]$ip_sites = Get-Content $path_sites
}
# function for logs.
$logs = @()
function log {
param (
@ -45,6 +61,8 @@ function log {
}
return $log_message
}
# function for storing logs.
function log_store {
$logs >> $path_log
if (Test-Path $path_log) {
@ -114,12 +132,15 @@ function get-ip {
$ip_actuall = get-ip
# If the retrieved IP is NULL or does not match the regex, then...
if (($null -eq $ip_actuall) -or ($ip_actuall -notmatch "^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$")) {
$logs += log -l_level 2 -l_message "Error durring geting public IP | EXIT"
log_store
exit
}
# Verification of the existence of a file with the saved IP;
# If the file exists, then verify if the received IP matches the saved one. If yes, close.
if (Test-Path $path_ip) {
$tmp = Get-Content $path_ip
if ($tmp -eq $ip_actuall) {
@ -135,16 +156,22 @@ $headers= @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $api_key"
}
# "Try" for fetching a list of DNS records from Cloudflare.
try {
$response = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$($dns_zone)/dns_records" -Method GET -Headers $headers
$cloudflare_dns = $response.result
$logs += log -l_level 0 -l_message "Downloaded $([array]$cloudflare_dns.count) records."
}
# In case of an error, log the details to a file and close.
catch {
$logs += log -l_level 2 -l_message "Error durring geting dns_records from cloud_flare | EXIT"
log_store
exit
}
# "Processing each entry from Cloudflare.
# If the script encounters the current IP, it will terminate with log recording.
# The script is designed to update all entries with a consistent IP in case of a change.
for ($i = 0; $i -lt $cloudflare_dns.Count; $i++) {
$id = $cloudflare_dns[$i].id
$address = $cloudflare_dns[$i].name
@ -178,6 +205,7 @@ for ($i = 0; $i -lt $cloudflare_dns.Count; $i++) {
}
}
# Saving the new IP to a file for future verification.
$ip_actuall > $path_ip
log_store