-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare_update_ddns.sh
More file actions
152 lines (130 loc) · 5.88 KB
/
cloudflare_update_ddns.sh
File metadata and controls
152 lines (130 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#
# Cloudflare Update DDNS
# MIT License
# (C) holger@rusch.name
#
# Run via cron every minute.
#
# > cat /etc/cron.d/cloudflare_update_ddns
# * * * * * root /usr/local/bin/cloudflare_update_ddns.sh >/dev/null 2>&1
#
# IPv6 prefix is /64.
#
# All IPv6 addresses in the cloudflare record need to be uncompressed (no :: inside address).
# Config
auth_email="email@address.com" # Cloudflare login email
auth_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Zone.DNS.Edit API token
zone_identifier="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Zone identifier
current_ipv4_file="/tmp/current_ipv4" # File to store current IPv4
current_ipv6_prefix_file="/tmp/current_ipv6_prefix" # File to store current IPv6 prefix
run_minute="35" # At which minute to forced run check each hour
# Don't touch
REGEX_IPV4="([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"
REGEX_IPV6="([0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}):([0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4})"
# IPv4
RAW_IP=$(curl -4 -s https://one.one.one.one/cdn-cgi/trace)
if [[ $RAW_IP =~ ip=$REGEX_IPV4 ]]; then
CURRENT_IP=${BASH_REMATCH[1]}
else
logger -s "DDNS Updater: IPv4 detection failed."
exit 2
fi
# Only if there is change or we are in run_minute
if [[ ( $(date +"%M") == $run_minute ) || ( ! ( -f $current_ipv4_file && ( $CURRENT_IP == $(< $current_ipv4_file) ) ) ) ]]; then
echo $CURRENT_IP > $current_ipv4_file
# Get A records
records=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=A" \
-H "X-Auth-Email: $auth_email" \
-H "Authorization: Bearer $auth_key" \
-H "Content-Type: application/json")
record_numbers=$(echo $records | jq .result_info.total_count)
# For all records
for ((record_number=0;record_number<$record_numbers;record_number++)); do
record_identifier=$(echo $records | jq -r .result[$record_number].id)
record_name=$(echo $records | jq -r .result[$record_number].name)
record_content=$(echo $records | jq -r .result[$record_number].content)
record_comment=$(echo $records | jq -r .result[$record_number].comment)
# Continue if comment is not "auto-update"
if [[ "auto-update" != $record_comment ]]; then
continue
fi
# Continue if no change
if [[ $CURRENT_IP == $record_content ]]; then
logger -s "DDNS Updater: IP ($record_content) for ${record_name} has not changed."
continue
fi
# Update record
update=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "Authorization: Bearer $auth_key" \
-H "Content-Type: application/json" \
--data "{\"name\":\"$record_name\",\"content\":\"$CURRENT_IP\"}")
if [[ "true" == $(echo $update | jq .success) ]]; then
logger -s "DDNS Updater: IP ($record_content) => ($CURRENT_IP) for ${record_name} set."
else
logger -s "DDNS Updater: IP ($record_content) => ($CURRENT_IP) for ${record_name} could not be set."
fi
done
fi
# IPv6
RAW_IP=$(curl -6 -s https://one.one.one.one/cdn-cgi/trace)
if [[ $RAW_IP =~ ip=$REGEX_IPV6 ]]; then
CURRENT_IP_PREFIX=${BASH_REMATCH[1]}
CURRENT_IP=${CURRENT_IP_PREFIX}:${BASH_REMATCH[2]}
else
logger -s "DDNS Updater: IPv6 detection failed."
exit 2
fi
# Only if there is change or we are in run_minute
if [[ ( $(date +"%M") == $run_minute ) || ( ! ( -f $current_ipv6_prefix_file && ( $CURRENT_IP_PREFIX == $(< $current_ipv6_prefix_file) ) ) ) ]]; then
echo $CURRENT_IP_PREFIX > $current_ipv6_prefix_file
# Get AAAA records
records=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=AAAA" \
-H "X-Auth-Email: $auth_email" \
-H "Authorization: Bearer $auth_key" \
-H "Content-Type: application/json")
record_numbers=$(echo $records | jq .result_info.total_count)
# For all records
for ((record_number=0;record_number<$record_numbers;record_number++)); do
record_identifier=$(echo $records | jq -r .result[$record_number].id)
record_name=$(echo $records | jq -r .result[$record_number].name)
record_content=$(echo $records | jq -r .result[$record_number].content)
record_comment=$(echo $records | jq -r .result[$record_number].comment)
# Should not happen
if [[ $record_content =~ $REGEX_IPV6 ]]; then
record_prefix=${BASH_REMATCH[1]}
else
logger -s "DDNS Updater: Could not extract prefix."
continue
fi
# Continue if comment is not "auto-update"
if [[ "auto-update" != $record_comment ]]; then
continue
fi
# Continue if no change
if [[ $CURRENT_IP_PREFIX == $record_prefix ]]; then
logger -s "DDNS Updater: Prefix ($record_prefix) for ${record_name} has not changed."
continue
fi
# Should not happen
if [[ $record_content =~ $REGEX_IPV6 ]]; then
record_postfix=${BASH_REMATCH[2]}
NEW_IP=${CURRENT_IP_PREFIX}:${record_postfix}
else
logger -s "DDNS Updater: Could not extract postfix."
continue
fi
# Update record
update=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "Authorization: Bearer $auth_key" \
-H "Content-Type: application/json" \
--data "{\"name\":\"$record_name\",\"content\":\"$NEW_IP\"}")
if [[ "true" == $(echo $update | jq .success) ]]; then
logger -s "DDNS Updater: IP ($record_content) => ($NEW_IP) for ${record_name} set."
else
logger -s "DDNS Updater: IP ($record_content) => ($NEW_IP) for ${record_name} could not be set."
fi
done
fi