1
- # https://github.com/fire1ce/DDNS-Cloudflare-PowerShell
2
-
3
1
[Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
4
2
5
3
# ## updateDNS.log file of the last run for debug
@@ -16,16 +14,16 @@ Write-Output "==> $DATE" | Tee-Object $File_LOG -Append
16
14
17
15
# ## Load config file
18
16
Try {
19
- . $PSScriptRoot \update-cloudflare - dns_conf.ps1
17
+ . $PSScriptRoot \update-cloudflare - dns_conf.ps1
20
18
}
21
19
Catch {
22
20
Write-Output " ==> Error! Missing update-cloudflare-dns_conf.ps1 or invalid syntax" | Tee-Object $File_LOG - Append
23
21
Exit
24
22
}
25
23
26
24
# ## 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
29
27
Exit
30
28
}
31
29
@@ -37,22 +35,20 @@ if (!([string]$proxied) -or ($proxied.GetType().Name.Trim() -ne "Boolean")) {
37
35
38
36
39
37
# ## 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" )) {
41
39
Write-Output ' Error! Incorrect "what_ip" parameter choose "external" or "internal"' | Tee-Object $File_LOG - Append
42
40
Exit
43
41
}
44
42
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
53
47
}
48
+
49
+ # ## Get External ip from https://checkip.amazonaws.com
54
50
if ($what_ip -eq ' external' ) {
55
- $ip = Get-Ip - External - IPv6 $IPv6
51
+ $ip = ( Invoke-RestMethod - Uri " https://checkip.amazonaws.com " - TimeoutSec 10 ).Trim()
56
52
if (! ([bool ]$ip )) {
57
53
Write-Output " Error! Can't get external ip from https://checkip.amazonaws.com" | Tee-Object $File_LOG - Append
58
54
Exit
@@ -61,66 +57,18 @@ if ($what_ip -eq 'external') {
61
57
}
62
58
63
59
# ## 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
- }
88
60
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 " ) ) {
91
63
Write-Output " ==>Error! Can't get internal ip address" | Tee-Object $File_LOG - Append
92
64
Exit
93
65
}
94
66
Write-Output " ==> Internal IP is $ip " | Tee-Object $File_LOG - Append
95
67
}
96
68
97
-
98
69
# ## 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
- }
122
70
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()
124
72
if (! [bool ]$dns_record_ip ) {
125
73
Write-Output " Error! Can't resolve the ${dns_record} via 1.1.1.1 DNS server" | Tee-Object $File_LOG - Append
126
74
Exit
@@ -134,8 +82,8 @@ if ($proxied -eq $true) {
134
82
Uri = " https://api.cloudflare.com/client/v4/zones/$zoneid /dns_records?name=$dns_record "
135
83
Headers = @ {" Authorization" = " Bearer $cloudflare_zone_api_token " ; " Content-Type" = " application/json" }
136
84
}
137
-
138
- $response = Invoke-RestMethod - Proxy $http_proxy - ProxyCredential $proxy_credential @dns_record_info
85
+
86
+ $response = Invoke-RestMethod @dns_record_info
139
87
if ($response.success -ne " True" ) {
140
88
Write-Output " Error! Can't get dns record info from cloudflare's api" | Tee-Object $File_LOG - Append
141
89
}
@@ -158,7 +106,7 @@ $cloudflare_record_info = @{
158
106
Headers = @ {" Authorization" = " Bearer $cloudflare_zone_api_token " ; " Content-Type" = " application/json" }
159
107
}
160
108
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
162
110
if ($cloudflare_record_info_resposne.success -ne " True" ) {
163
111
Write-Output " Error! Can't get $dns_record record inforamiton from cloudflare API" | Tee-Object $File_LOG - Append
164
112
Exit
@@ -173,18 +121,15 @@ $update_dns_record = @{
173
121
Method = ' PUT'
174
122
Headers = @ {" Authorization" = " Bearer $cloudflare_zone_api_token " ; " Content-Type" = " application/json" }
175
123
Body = @ {
176
- " type" = switch ($IPv6 ) {
177
- $true { " AAAA" }
178
- $false { " A" }
179
- }
124
+ " type" = " A"
180
125
" name" = $dns_record
181
126
" content" = $ip
182
127
" ttl" = $ttl
183
128
" proxied" = $proxied
184
129
} | ConvertTo-Json
185
130
}
186
131
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
188
133
if ($update_dns_record_response.success -ne " True" ) {
189
134
Write-Output " Error! Update Failed" | Tee-Object $File_LOG - Append
190
135
Exit
@@ -203,15 +148,15 @@ if ($notify_me_telegram -eq "yes") {
203
148
Uri = " https://api.telegram.org/bot$telegram_bot_API_Token /sendMessage?chat_id=$telegram_chat_id &text=$dns_record DNS Record Updated To: $ip "
204
149
Method = ' GET'
205
150
}
206
- $telegram_notification_response = Invoke-RestMethod - Proxy $http_proxy - ProxyCredential $proxy_credential @telegram_notification
151
+ $telegram_notification_response = Invoke-RestMethod @telegram_notification
207
152
if ($telegram_notification_response.ok -ne " True" ) {
208
153
Write-Output " Error! Telegram notification failed" | Tee-Object $File_LOG - Append
209
154
Exit
210
155
}
211
156
}
212
157
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 )"
215
160
$discord_payload = [PSCustomObject ]@ {content = $discord_message } | ConvertTo-Json
216
161
$discord_notification = @ {
217
162
Uri = $discord_webhook_URL
@@ -220,11 +165,11 @@ if ($notify_me_discord -eq "yes") {
220
165
Headers = @ { " Content-Type" = " application/json" }
221
166
}
222
167
try {
223
- Invoke-RestMethod - Proxy $http_proxy - ProxyCredential $proxy_credential @discord_notification
168
+ Invoke-RestMethod @discord_notification
224
169
} catch {
225
170
Write-Host " ==> Discord notification request failed. Here are the details for the exception:" | Tee-Object $File_LOG - Append
226
171
Write-Host " ==> Request StatusCode:" $_.Exception.Response.StatusCode.value__ | Tee-Object $File_LOG - Append
227
172
Write-Host " ==> Request StatusDescription:" $_.Exception.Response.StatusDescription | Tee-Object $File_LOG - Append
228
173
}
229
174
Exit
230
- }
175
+ }
0 commit comments