From b653b0c99f52308fb5ad87506602d1f69f368756 Mon Sep 17 00:00:00 2001 From: Tomasz Kostrzewa Date: Fri, 22 Dec 2023 22:20:46 +0100 Subject: [PATCH] new file: CloudFlare/cloudflare_ddns.ps1 --- CloudFlare/cloudflare_ddns.ps1 | 184 +++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 CloudFlare/cloudflare_ddns.ps1 diff --git a/CloudFlare/cloudflare_ddns.ps1 b/CloudFlare/cloudflare_ddns.ps1 new file mode 100644 index 0000000..7d92bc5 --- /dev/null +++ b/CloudFlare/cloudflare_ddns.ps1 @@ -0,0 +1,184 @@ +# tomaszjkostrzewa@gmail.com +# DDNS Cloudflare - KeepUpIP +# + + +$location = Get-Location +$path_ip = "$($location)\cloudflare_ddns_ip" # store last set IP +$path_log = "$($location)\cloudflare_ddns_log" # store logs +$path_sites = "$($location)\cloudflare_ddns_sites" +$log_limit = 10000 +$ip = $null +$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 (-not (Test-Path $path_sites)) { + $ip_sites = @( + 'http://ipinfo.io/ip' + 'http://ifconfig.me/ip' + 'http://icanhazip.com' + 'http://ident.me' +) +} else { + [array]$ip_sites = Get-Content $path_sites +} + +$logs = @() +function log { + param ( + $l_level, + $l_message + ) + $l_timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.ffff K" + switch ($l_level) { + 0 { + $log_message = "$($l_timestamp) | [INFO] | $($l_message)" + } + 1 { + $log_message = "$($l_timestamp) | [WARNING] | $($l_message)" + } + 2 { + $l_error = $Error[0].Exception + $log_message = "$($l_timestamp) | [ERROR] | $($l_message) | $l_error" + } + } + return $log_message +} +function log_store { + $logs >> $path_log + if (Test-Path $path_log) { + $tmp = Get-Content $path_log -Tail $log_limit + $tmp | Set-Content $path_log + } +} + +# 01 - Get the actual IP +function get-ip { + for ($i = 0; $i -lt $ip_sites.Count; $i++) { + switch ($ip_sites[$i]) { + 'http://ipinfo.io/ip' { + $uri = $ip_sites[$i] + try { + $test = Test-Connection -TargetName $uri.Replace("http://","").Split("/")[0] -Count 2 + $ip = (Invoke-RestMethod -Uri $uri -Method Get).Trim() + } + catch { + $global:logs += log -l_level 1 -l_message "IP: ERROR | URL: $($uri)" + continue + } + } + 'http://ifconfig.me/ip' { + $uri = $ip_sites[$i] + try { + $test = Test-Connection -TargetName $uri.Replace("http://","").Split("/")[0] -Count 2 + $ip = (Invoke-RestMethod -Uri $uri -Method Get).Trim() + } + catch { + $global:logs += log -l_level 1 -l_message "IP: ERROR | URL: $($uri)" + continue + } + } + 'http://icanhazip.com' { + $uri = $ip_sites[$i] + try { + $test = Test-Connection -TargetName $uri.Replace("http://","").Split("/")[0] -Count 2 + $ip = (Invoke-RestMethod -Uri $uri -Method Get).Trim() + } + catch { + $global:logs += log -l_level 1 -l_message "IP: ERROR | URL: $($uri)" + continue + } + } + 'http://ident.me' { + $uri = $ip_sites[$i] + try { + $test = Test-Connection -TargetName $uri.Replace("http://","").Split("/")[0] -Count 2 + $ip = (Invoke-RestMethod -Uri $uri -Method Get).Trim() + } + catch { + $global:logs += log -l_level 1 -l_message "IP: ERROR | URL: $($uri)" + continue + } + } + } + if ($null -ne $ip) { + $global:logs += log -l_level 0 -l_message "IP: $($ip) | URL: $($uri)" + $ip_sites = $ip_sites[1..($ip_sites.Count)] + $ip_sites[0] + $ip_sites > $path_sites + break + } + } + return $ip +} + +$ip_actuall = get-ip + +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 +} + +if (Test-Path $path_ip) { + $tmp = Get-Content $path_ip + if ($tmp -eq $ip_actuall) { + $logs += log -l_level 0 -l_message "IP MATCH! | EXIT" + log_store + exit + } +} + +# 02 - Get the actuall DNS record from cloudflare + +$headers= @{ +"Content-Type" = "application/json" +"Authorization" = "Bearer $api_key" +} +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." +} +catch { + $logs += log -l_level 2 -l_message "Error durring geting dns_records from cloud_flare | EXIT" + log_store + exit +} +for ($i = 0; $i -lt $cloudflare_dns.Count; $i++) { + $id = $cloudflare_dns[$i].id + $address = $cloudflare_dns[$i].name + $ip_ddns = $cloudflare_dns[$i].content + $proxied = $cloudflare_dns[$i].proxied + + + if ($ip_ddns -eq $ip_actuall) { + $logs += log -l_level 0 -l_message "IP MATCH! | EXIT" + log_store + exit + } + + $body = @{ + content = $ip_actuall + name = $address + type = "A" + proxied = $proxied + comment = log -l_level 0 -l_message "Updated from $($ip_ddns)" + } + + $body = $body | ConvertTo-Json + + $url = "https://api.cloudflare.com/client/v4/zones/$($dns_zone)/dns_records/$($id)" + try { + $response = Invoke-RestMethod -Uri $url -Method Put -Headers $headers -Body $body + $logs += log -l_level 0 -l_message "Updated from $($ip_ddns) to $($ip_actuall) at $($address)" + } + catch { + $logs += log -l_level 2 -l_message "Not updated from $($ip_ddns) to $($ip_actuall) at $($address)" + } +} + +$ip_actuall > $path_ip + +log_store +exit \ No newline at end of file