forked from clayauld/asus-merlin-cloudflare-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare_ddns
120 lines (99 loc) · 2.49 KB
/
cloudflare_ddns
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
#!/bin/sh
CONFIG=$(dirname $0)/.cloudflare
if [ -r $CONFIG ]; then
. $CONFIG
else
( >&2 echo "Missing or unreadable $CONFIG" )
fi
# script settings
LOGFILE=$(basename $0).log
THROTTLE_SECONDS=300 # terminate prematurely if run too often
CLOUDFLARE_AUTH_HEADERS="X-Auth-Email: $CLOUDFLARE_EMAIL
X-Auth-Key: $CLOUDFLARE_API_KEY"
# cloudflare prefers token-based auth, so use that if available
if [ -n $CLOUDFLARE_API_TOKEN ]; then
CLOUDFLARE_AUTH_HEADERS="Authorization: Bearer $CLOUDFLARE_API_TOKEN"
fi
# user input IP address
NEW_IP=$1
log_output()
{
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "$timestamp $1 $2"
} >> $LOGFILE
list_dns_records()
{
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$DNS_ZONE_ID/dns_records" \
-H @- <<- HEADERS
$CLOUDFLARE_AUTH_HEADERS
HEADERS
}
list_dns_records_onsuccess()
{
log_output LIST_SUCCESS "$1"
}
list_dns_records_onfailure()
{
log_output LIST_FAILED "$1"
}
list_dns_records_onthrottled()
{
log_output LIST_THROTTLED
}
update_record()
{
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$DNS_ZONE_ID/dns_records/$DNS_RECORD_ID" \
--data "{\"type\":\"$DNS_RECORD_TYPE\",\"name\":\"$DNS_RECORD_NAME\",\"content\":\"$NEW_IP\",\"ttl\":1,\"proxied\":$DNS_RECORD_PROXIED}" \
-H @- <<- HEADERS
$CLOUDFLARE_AUTH_HEADERS
Content-Type: application/json
HEADERS
}
update_record_onsuccess()
{
/sbin/ddns_custom_updated 1
log_output UPDATE_SUCCESS "$1"
}
update_record_onfailure()
{
/sbin/ddns_custom_updated 0
log_output UPDATE_FAILED "$1"
}
update_record_onthrottled()
{
/sbin/ddns_custom_updated 0
log_output UPDATE_THROTTLED
}
throttle()
{
# if we can't read the log file, assume it's a first run
if [ ! -r $LOGFILE ]; then
return
fi
last_run_time=$(date -d "$(sort -r $LOGFILE | \
grep '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]' | \
grep -vm1 THROTTLED | cut -f 1)" +%s)
current_time=$(date +%s)
if [ $(expr $last_run_time + $THROTTLE_SECONDS) -gt $current_time ]; then
$throttled
exit 1
fi
}
command=update_record
success=update_record_onsuccess
failure=update_record_onfailure
throttled=update_record_onthrottled
# if argument is the word 'list', query for a listing of all zone records
if [ "$NEW_IP" = "list" ]; then
command=list_dns_records
success=list_dns_records_onsuccess
failure=list_dns_records_onfailure
throttled=list_dns_records_onthrottled
fi
throttle
OUTPUT=$($command)
if echo $OUTPUT | grep -q '"success"\s*:\s*true'; then
$success "$OUTPUT"
else
$failure "$OUTPUT"
fi