Skip to content

Commit b942912

Browse files
committed
Revert "Merge pull request #8 from Jack-Works/main"
This reverts commit 295965c, reversing changes made to ead3699.
1 parent 295965c commit b942912

File tree

3 files changed

+30
-89
lines changed

3 files changed

+30
-89
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ Log file name:
110110
update-cloudflare-dns.log
111111
```
112112

113+
## Limitations
114+
115+
- Does not support IPv6
116+
113117
## License
114118

115119
### MIT License

update-cloudflare-dns.ps1

+24-79
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# https://github.com/fire1ce/DDNS-Cloudflare-PowerShell
2-
31
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
42

53
### updateDNS.log file of the last run for debug
@@ -16,16 +14,16 @@ Write-Output "==> $DATE" | Tee-Object $File_LOG -Append
1614

1715
### Load config file
1816
Try {
19-
. $PSScriptRoot\update-cloudflare-dns_conf.ps1
17+
. $PSScriptRoot\update-cloudflare-dns_conf.ps1
2018
}
2119
Catch {
2220
Write-Output "==> Error! Missing update-cloudflare-dns_conf.ps1 or invalid syntax" | Tee-Object $File_LOG -Append
2321
Exit
2422
}
2523

2624
### Check validity of "ttl" parameter
27-
if (( $ttl -lt 60 ) -or ($ttl -gt 7200 ) -and ( $ttl -ne 1 )) {
28-
Write-Output 'Error! ttl out of range (60) or not set to 1' | Tee-Object $File_LOG -Append
25+
if (( $ttl -lt 120 ) -or ($ttl -gt 7200 ) -and ( $ttl -ne 1 )) {
26+
Write-Output 'Error! ttl out of range (120-7200) or not set to 1' | Tee-Object $File_LOG -Append
2927
Exit
3028
}
3129

@@ -37,22 +35,20 @@ if (!([string]$proxied) -or ($proxied.GetType().Name.Trim() -ne "Boolean")) {
3735

3836

3937
### Check validity of "what_ip" parameter
40-
if ( ($what_ip -ne "external") -and ($what_ip -ne "internal") ) {
38+
if ( ($what_ip -ne "external") -and ($what_ip -ne "internal")) {
4139
Write-Output 'Error! Incorrect "what_ip" parameter choose "external" or "internal"' | Tee-Object $File_LOG -Append
4240
Exit
4341
}
4442

45-
### Get External ip from internet
46-
function Get-Ip-External {
47-
param ([bool] $IPv6)
48-
if ($IPv6) {
49-
return (Invoke-RestMethod -Uri "http://v6.ident.me" -TimeoutSec 10).Trim()
50-
} else {
51-
return (Invoke-RestMethod -Uri "https://checkip.amazonaws.com" -TimeoutSec 10).Trim()
52-
}
43+
### Check if set to internal ip and proxy
44+
if (($what_ip -eq "internal") -and ($proxied)) {
45+
Write-Output 'Error! Internal IP cannot be Proxied' | Tee-Object $File_LOG -Append
46+
Exit
5347
}
48+
49+
### Get External ip from https://checkip.amazonaws.com
5450
if ($what_ip -eq 'external') {
55-
$ip = Get-Ip-External -IPv6 $IPv6
51+
$ip = (Invoke-RestMethod -Uri "https://checkip.amazonaws.com" -TimeoutSec 10).Trim()
5652
if (!([bool]$ip)) {
5753
Write-Output "Error! Can't get external ip from https://checkip.amazonaws.com" | Tee-Object $File_LOG -Append
5854
Exit
@@ -61,66 +57,18 @@ if ($what_ip -eq 'external') {
6157
}
6258

6359
### Get Internal ip from primary interface
64-
function Get-Ip-Internal {
65-
param ([bool] $IPv6)
66-
67-
if ($IsLinux) {
68-
if ($IPv6) {
69-
return ip -6 addr | grep inet6 | awk -F '[ \t]+|/' '{print $3}' | grep -v ^::1 | grep -v ^f | sort | head -1
70-
}
71-
else {
72-
return ip -4 addr | grep inet | awk -F '[ \t]+|/' '{print $3}' | grep -v ^127 | grep -v ^192 | grep -v ^f | head -1
73-
}
74-
} elseif ($IsWindows) {
75-
$InternalIPTestAddress = switch ($IPv6) {
76-
$true { "2606:4700::1111" }
77-
$false { "1.1.1.1" }
78-
}
79-
$addr = $((Find-NetRoute -RemoteIPAddress $InternalIPTestAddress).IPAddress|out-string).Trim()
80-
if ($addr -eq "127.0.0.1" -or $addr -eq "::1") {
81-
return $null
82-
}
83-
return $addr
84-
} elseif ($IsMacOS) {
85-
throw "Get-Internal-IpAddress is not implemented for MacOS."
86-
}
87-
}
8860
if ($what_ip -eq 'internal') {
89-
$ip = Get-Ip-Internal -IPv6 $IPv6
90-
if (![bool]$ip) {
61+
$ip = $((Find-NetRoute -RemoteIPAddress 1.1.1.1).IPAddress|out-string).Trim()
62+
if (!([bool]$ip) -or ($ip -eq "127.0.0.1")) {
9163
Write-Output "==>Error! Can't get internal ip address" | Tee-Object $File_LOG -Append
9264
Exit
9365
}
9466
Write-Output "==> Internal IP is $ip" | Tee-Object $File_LOG -Append
9567
}
9668

97-
9869
### Get IP address of DNS record from 1.1.1.1 DNS server when proxied is "false"
99-
function Resolve-DnsName-From-Cloudflare {
100-
param ([bool] $DoH, [string] $domain, [bool] $IPv6)
101-
$type = switch ($IPv6) {
102-
$true { "AAAA" }
103-
$false { "A" }
104-
}
105-
if ($DoH) {
106-
$response = Invoke-RestMethod -Proxy $http_proxy -ProxyCredential $proxy_credential -Uri https://cloudflare-dns.com/dns-query -Body @{
107-
name = $domain
108-
type = $type
109-
} -Headers @{ 'Accept' = 'application/dns-json' }
110-
if ($response) {
111-
return $response.Answer[0].data
112-
}
113-
return $null
114-
} else {
115-
$ip = (Resolve-DnsName -Name $domain -Server 1.1.1.1 -Type $type | Select-Object -First 1)
116-
if ($ip) {
117-
return $ip.IPAddress.Trim()
118-
}
119-
return $null
120-
}
121-
}
12270
if ($proxied -eq $false) {
123-
$dns_record_ip = Resolve-DnsName-From-Cloudflare -DoH $DNS_over_HTTPS -domain $dns_record -IPv6 $IPv6
71+
$dns_record_ip = (Resolve-DnsName -Name $dns_record -Server 1.1.1.1 -Type A | Select-Object -First 1).IPAddress.Trim()
12472
if (![bool]$dns_record_ip) {
12573
Write-Output "Error! Can't resolve the ${dns_record} via 1.1.1.1 DNS server" | Tee-Object $File_LOG -Append
12674
Exit
@@ -134,8 +82,8 @@ if ($proxied -eq $true) {
13482
Uri = "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?name=$dns_record"
13583
Headers = @{"Authorization" = "Bearer $cloudflare_zone_api_token"; "Content-Type" = "application/json" }
13684
}
137-
138-
$response = Invoke-RestMethod -Proxy $http_proxy -ProxyCredential $proxy_credential @dns_record_info
85+
86+
$response = Invoke-RestMethod @dns_record_info
13987
if ($response.success -ne "True") {
14088
Write-Output "Error! Can't get dns record info from cloudflare's api" | Tee-Object $File_LOG -Append
14189
}
@@ -158,7 +106,7 @@ $cloudflare_record_info = @{
158106
Headers = @{"Authorization" = "Bearer $cloudflare_zone_api_token"; "Content-Type" = "application/json" }
159107
}
160108

161-
$cloudflare_record_info_resposne = Invoke-RestMethod -Proxy $http_proxy -ProxyCredential $proxy_credential @cloudflare_record_info
109+
$cloudflare_record_info_resposne = Invoke-RestMethod @cloudflare_record_info
162110
if ($cloudflare_record_info_resposne.success -ne "True") {
163111
Write-Output "Error! Can't get $dns_record record inforamiton from cloudflare API" | Tee-Object $File_LOG -Append
164112
Exit
@@ -173,18 +121,15 @@ $update_dns_record = @{
173121
Method = 'PUT'
174122
Headers = @{"Authorization" = "Bearer $cloudflare_zone_api_token"; "Content-Type" = "application/json" }
175123
Body = @{
176-
"type" = switch ($IPv6) {
177-
$true { "AAAA" }
178-
$false { "A" }
179-
}
124+
"type" = "A"
180125
"name" = $dns_record
181126
"content" = $ip
182127
"ttl" = $ttl
183128
"proxied" = $proxied
184129
} | ConvertTo-Json
185130
}
186131

187-
$update_dns_record_response = Invoke-RestMethod -Proxy $http_proxy -ProxyCredential $proxy_credential @update_dns_record
132+
$update_dns_record_response = Invoke-RestMethod @update_dns_record
188133
if ($update_dns_record_response.success -ne "True") {
189134
Write-Output "Error! Update Failed" | Tee-Object $File_LOG -Append
190135
Exit
@@ -203,15 +148,15 @@ if ($notify_me_telegram -eq "yes") {
203148
Uri = "https://api.telegram.org/bot$telegram_bot_API_Token/sendMessage?chat_id=$telegram_chat_id&text=$dns_record DNS Record Updated To: $ip"
204149
Method = 'GET'
205150
}
206-
$telegram_notification_response = Invoke-RestMethod -Proxy $http_proxy -ProxyCredential $proxy_credential @telegram_notification
151+
$telegram_notification_response = Invoke-RestMethod @telegram_notification
207152
if ($telegram_notification_response.ok -ne "True") {
208153
Write-Output "Error! Telegram notification failed" | Tee-Object $File_LOG -Append
209154
Exit
210155
}
211156
}
212157

213-
if ($notify_me_discord -eq "yes") {
214-
$discord_message = "$dns_record DNS Record Updated To: $ip (was $dns_record_ip)"
158+
if ($notify_me_discord -eq "yes") {
159+
$discord_message = "$dns_record DNS Record Updated To: $ip (was $dns_record_ip)"
215160
$discord_payload = [PSCustomObject]@{content = $discord_message} | ConvertTo-Json
216161
$discord_notification = @{
217162
Uri = $discord_webhook_URL
@@ -220,11 +165,11 @@ if ($notify_me_discord -eq "yes") {
220165
Headers = @{ "Content-Type" = "application/json" }
221166
}
222167
try {
223-
Invoke-RestMethod -Proxy $http_proxy -ProxyCredential $proxy_credential @discord_notification
168+
Invoke-RestMethod @discord_notification
224169
} catch {
225170
Write-Host "==> Discord notification request failed. Here are the details for the exception:" | Tee-Object $File_LOG -Append
226171
Write-Host "==> Request StatusCode:" $_.Exception.Response.StatusCode.value__ | Tee-Object $File_LOG -Append
227172
Write-Host "==> Request StatusDescription:" $_.Exception.Response.StatusDescription | Tee-Object $File_LOG -Append
228173
}
229174
Exit
230-
}
175+
}

update-cloudflare-dns_conf.ps1

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,15 @@
55
$what_ip = "internal"
66
## DNS A record to be updated
77
$dns_record = "ddns.example.com"
8-
## Use IPv6
9-
$IPv6 = $false
10-
## if use DoH to query the current IP address
11-
$DNS_over_HTTPS = $false
128
## Cloudflare's Zone ID
139
$zoneid = "ChangeMe"
1410
## Cloudflare Zone API Token
1511
$cloudflare_zone_api_token = "ChangeMe"
1612
## Use Cloudflare proxy on dns record true/false
1713
$proxied = $false
18-
## 60-7200 in seconds or 1 for Auto
14+
## 120-7200 in seconds or 1 for Auto
1915
$ttl = 120
2016

21-
## Use proxy when connect to DoH, Cloudflare, Telegram or Discord API
22-
# $http_proxy = $null
23-
# $proxy_credential = $null
24-
2517
## Telegram Notifications yes/no (only sent if DNS is updated)
2618
$notify_me_telegram = "no"
2719
## Telegram Chat ID
@@ -32,4 +24,4 @@ $telegram_bot_API_Token = "ChangeMe"
3224
## Discord Server Notifications yes/no (only sent if DNS is updated)
3325
$notify_me_discord = "no"
3426
## Discord Webhook URL (create a webhook on your Discord server via Server Settings > Integrations)
35-
$discord_webhook_URL = "ChangeMe"
27+
$discord_webhook_URL = "ChangeMe"

0 commit comments

Comments
 (0)