forked from joshuaavalon/docker-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare.sh
executable file
·164 lines (136 loc) · 4.7 KB
/
cloudflare.sh
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
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
# Enforces required env variables
required_vars=(ZONE HOST TOKEN)
for required_var in "${required_vars[@]}"; do
if [[ -z ${!required_var} ]]; then
error=1
echo >&2 "Error: $required_var env variable not set."
fi
done
if [[ -n $error ]]; then
exit 1
fi
# PROXY defaults to true
PROXY=${PROXY:-true}
# TTL defaults to 1 (automatic), and is validated
TTL=${TTL:-1}
if [[ $TTL != 1 ]] && [[ $TTL -lt 120 || $TTL -gt 2147483647 ]]; then
echo >&2 "Error: Invalid TTL value $TTL; must be either 1 (automatic) or between 120 and 2147483647 inclusive."
exit 1
fi
echo "Current time: $(date "+%Y-%m-%d %H:%M:%S")"
if [[ -z $IPV6 ]]; then
ip_curl="curl -4s"
record_type="A"
else
ip_curl="curl -6s"
record_type="AAAA"
fi
# Determines the current IP address
new_ip=$($ip_curl http://ipecho.net/plain)
# IP address service fallbacks
if [[ -z $new_ip ]]; then
new_ip=$($ip_curl http://whatismyip.akamai.com)
fi
if [[ -z $new_ip ]]; then
new_ip=$($ip_curl http://icanhazip.com/)
fi
if [[ -z $new_ip ]]; then
new_ip=$($ip_curl https://tnx.nl/ip)
fi
if [[ -z $new_ip ]]; then
echo >&2 "Error: Unable to reach any service to determine the IP address."
exit 1
fi
# Compares with last IP address set, if any
ip_file="ip.dat"
if [[ -f $ip_file ]]; then
ip=$(<$ip_file)
if [[ $ip = "$new_ip" ]]; then
echo "IP is unchanged : $ip. Exiting."
exit 0
fi
fi
ip="$new_ip"
echo "IP: $ip"
# Fetches the zone information for the account
zone_response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Accept: application/json' \
-H "Content-Type: application/json")
# If not successful, errors out
if [[ $(jq <<<"$zone_response" -r '.success') != "true" ]]; then
messages=$(jq <<<"$zone_response" -r '[.errors[] | .message] |join(" - ")')
echo >&2 "Error: $messages"
exit 1
fi
# Selects the zone id
zone_id=$(jq <<<"$zone_response" -r ".result[] | select(.name==\"$ZONE\") .id")
# If no zone id was found for the account, errors out.
if [[ -z $zone_id ]]; then
echo >&2 "Error: Could not determine Zone ID for $ZONE"
exit 1
fi
echo "Zone $ZONE id : $zone_id"
# Tries to fetch the record of the host
dns_record_response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$HOST" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Accept: application/json' \
-H "Content-Type: application/json")
if [[ $(jq <<<"$dns_record_response" -r '.success') != "true" ]]; then
messages=$(jq <<<"$dns_record_response" -r '[.errors[] | .message] |join(" - ")')
echo >&2 "Error: $messages"
exit 1
fi
# DNS record to add or update
read -r -d '' new_dns_record <<EOF
{
"type": "$record_type",
"name": "$HOST",
"content": "$ip",
"ttl": $TTL,
"priority": 10,
"proxied": $PROXY
}
EOF
# Adds or updates the record
dns_record_id=$(jq <<<"$dns_record_response" -r ".result[] | select(.type ==\"$record_type\") |.id")
if [[ -z $dns_record_id ]]; then
# Makes sure we don't have a CNAME by the same name first
cname_dns_record=$(jq <<<"$dns_record_response" -r '.result[] | select(.type =="CNAME")')
if [[ -n $(jq <<<"$cname_dns_record" -r '.id') ]]; then
dns_record_content=$(jq <<<"$cname_dns_record" -r '.content')
echo >&2 "Error: CNAME entry found for $HOST. Remove it or set HOST=$dns_record_content instead."
exit 1
fi
# If not asked to create a record, stops
if [[ -z $FORCE_CREATE ]]; then
echo >&2 "Error: No existing record. Set FORCE_CREATE to any value to force its creation."
exit 1
fi
# Creates a new record
echo "Creating new record for host $HOST"
dns_record_response=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Accept: application/json' \
-H "Content-Type: application/json" \
--data "$new_dns_record")
else
# If a record is found, updates the existing record
echo "Updating record $dns_record_id for host $HOST"
dns_record_response=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$dns_record_id" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Accept: application/json' \
-H "Content-Type: application/json" \
--data "$new_dns_record")
fi
if [[ $(jq <<<"$dns_record_response" -r '.success') = "true" ]]; then
# Records the IP set set if successful
echo "IP changed to: $ip"
echo "$ip" > $ip_file
else
# Prints out error messages if unsuccessful
messages=$(jq <<<"$dns_record_response" -r '[.errors[] | .error.message] |join(" - ")')
echo >&2 "Error: $messages"
exit 1
fi