-
Notifications
You must be signed in to change notification settings - Fork 12
/
cloudflare-ddns.sh
52 lines (44 loc) · 1.54 KB
/
cloudflare-ddns.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
#!/bin/bash
set -eu
if [[ "${DEBUG:-}" == "true" ]]; then # permit debugging
echo "Debug mode is on, this will expose secrets on stderr" 1>&2
set -x
fi
# The -v test determines if a variable is set, even if empty. (Bash 4.3+)
if [[ ! -v AUTH_HEADERS ]]; then
if [[ -v API_TOKEN ]]; then # API_TOKEN is set, prefer to use it
echo "Using API_TOKEN for authentication" 1>&2
AUTH_HEADERS=(-H "Authorization: Bearer $API_TOKEN")
elif [[ -v AUTH_EMAIL && -v AUTH_KEY ]]; then
echo "Using AUTH_EMAIL/AUTH_KEY for authentication" 1>&2
AUTH_HEADERS=(-H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY")
else
echo "Neither API_TOKEN nor AUTH_EMAIL and AUTH_KEY were provided, failing" 1>&2
exit 1
fi
fi
: "${IP:=$(curl ifconfig.co)}"
: "${PROXIED:=false}"
echo "Using IP: $IP" 1>&2
# Dig the old IP and exit out if they are the same
if [[ "$PROXIED" == "false" ]]; then # Execute dig only if PROXIED is set to false
: "${OLD_IP:=$(dig +short @1.1.1.1 $NAME | tail -n1)}"
echo "Using Old IP: $OLD_IP" 1>&2
if [[ "${IP}" == "${OLD_IP}" ]]; then
echo "IP has not changed. Exiting..."
exit 0
fi
fi
PAYLOAD="{\"type\":\"A\",\"name\":\"$NAME\",\"content\":\"$IP\",\"proxied\":$PROXIED,\"ttl\":1}"
echo "Sending payload: $PAYLOAD" 1>&2
# Assemble the whole command into an array, then execute.
COMMAND=(
curl
-X PUT
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID"
"${AUTH_HEADERS[@]}"
-H "Content-Type: application/json"
--data "$PAYLOAD"
)
"${COMMAND[@]}"
echo # end with line break