Ticket #12500: 20141003-update_cloudflare.sh

File 20141003-update_cloudflare.sh, 4.6 KB (added by chris5560, 3 years ago)
Line 
1# update_script to update ddns service of cloudflare.com
2#
3# Written October 2014 by Christian Schonebeck <christian dot schoenebeck at gmail dot com>
4# Distributed under the terms of the GNU General Public License (GPL) version 2.0
5#
6# This script is parsed by dynamic_dns_functions.sh inside send_update() function
7#
8# using following options from /etc/config/ddns
9# option username - your cloudflare e-mail
10# option password - cloudflare api key, you can get it from cloudflare.com/my-account/
11# option domain   - your full hostname to update, in cloudflare its subdomain.domain
12#                       i.e. myhost.example.com where myhost is the subdomain and example.com is your domain
13#
14# Attention !!! script will only work if there is only one subdomain-level at your domain
15# subdomain2.subdomain1.domain i.e. mail.host.example.com will not work
16#
17# variable __IP already defined with the ip-address to use for update
18#
19[ $use_https -eq 0 ] && critical_error "Cloudflare only support updates via Secure HTTP (HTTPS). Please correct configuration!"
20
21local __RECID __URL __KEY __KEYS
22local __SUBDOM=$(echo $domain | awk -F "." '{print $1}')
23local __DOMAIN=$(echo $domain | awk -F "${__SUBDOMAIN}." '{print $2}')
24local __TMP="/tmp/"$(date +%s)".json"
25
26# parse OpenWrt script with
27# functions for parsing and generating json
28. /usr/share/libubox/jshn.sh
29
30# build url according to cloudflare client api at https://www.cloudflare.com/docs/client-api.html
31# to "rec_load_all" to detect rec_id needed for update
32__URL="https://www.cloudflare.com/api_json.html"        # https://www.cloudflare.com/api_json.html
33__URL="${__URL}?a=rec_load_all"                         #  -d 'a=rec_load_all'
34__URL="${__URL}&tkn=$password"                          #  -d 'tkn=8afbe6dea02407989af4dd4c97bb6e25'
35__URL="${__URL}&email=$password"                        #  -d 'email=sample@example.com'
36__URL="${__URL}&z=$__DOMAIN"                            #  -d 'z=example.com'
37
38# lets request the data
39__do_transfer __ANSWER "$__URL"
40__ERR=$?
41[ $__ERR -gt 0 ] && {
42        verbose_echo "\n!!!!!!!!! ERROR =: Error getting 'rec_load_all' from cloudflare.com\n"
43        return 1
44}
45echo "$__ANSWER" > $__TMP       # save data to tmp-file
46
47#based on the sample output on cloudflare.com homepage we need to do some cleanup
48sed -i 's/^[ \t]*//;s/[ \t]*$//' $__TMP # remove invisible chars at beginning and end of lines
49sed -i '/^-$/d' $__TMP                  # remove lines with "-" (dash)
50sed -i '/^$/d' $__TMP                   # remove empty lines
51
52# lets extract data
53json_load "$(cat $__TMP)"
54rm -f $__TMP                    # remove tmp-file because no longer needed
55__TMP=0                         # reuse variable as found indicator
56json_select "response"
57json_select "recs"
58json_select "objs"
59json_get_keys __KEYS
60for __KEY in $__KEYS; do
61        local _ZONE _NAME _DISPLAY _TYPE
62        json_select "$__KEY"
63        json_get_var _ZONE "zone_name"
64        json_get_var _NAME "name"
65        json_get_var _DISPLAY "display_name"
66        json_get_var _TYPE "type"
67        # verify all infos availible
68        if [ "$_ZONE" == "$__DOMAIN" -a "$_NAME" == "$domain" -a "$_DISPLAY" == "$__SUBDOM" ]; then
69                # we must verify IPv4 and IPv6 because there might be both for the same host
70                [ $use_ipv6 -eq 0 -a "$TYPE" == "A" ] || [ $use_ipv6 -eq 1 -a "$TYPE" == "AAAA" ] && {
71                        __TMP=1 # mark found
72                        break   # found leave for loop
73                }
74        fi
75        json_select ..
76done
77[ $__TMP -eq 0 ] && {
78        # we don't need to continue trying to update cloudflare because record to update does not exist
79        # user has to setup record first outside ddns-scripts
80        critical_error "No valid record found at your Cloudflare setup. Please create first!"
81}
82json_get_var __RECID "rec_id"   # last thing to do get rec_id
83json_cleanup                    # cleanup
84
85# build url according to cloudflare client api at https://www.cloudflare.com/docs/client-api.html
86# for "rec_edit" to update IP address
87__URL="https://www.cloudflare.com/api_json.html"        # https://www.cloudflare.com/api_json.html
88__URL="${__URL}?a=rec_edit"                             #  -d 'a=rec_edit'
89__URL="${__URL}&tkn=$password"                          #  -d 'tkn=8afbe6dea02407989af4dd4c97bb6e25'
90__URL="${__URL}&id=$__RECID"                            #  -d 'id=9001'
91__URL="${__URL}&email=$username"                        #  -d 'email=sample@example.com'
92__URL="${__URL}&z=$__DOMAIN"                            #  -d 'z=example.com'
93
94[ $use_ipv6 -eq 0 ] && __URL="${__URL}&type=A"          #  -d 'type=A'          (IPv4)
95[ $use_ipv6 -eq 1 ] && __URL="${__URL}&type=AAAA"       #  -d 'type=AAAA'       (IPv6)
96
97__URL="${__URL}&name=$__SUBDOM"                         #  -d 'name=sub'
98__URL="${__URL}&content=$__IP"                          #  -d 'content=1.2.3.4'
99__URL="${__URL}&service_mode=1"                         #  -d 'service_mode=1'
100__URL="${__URL}&ttl=1"                                  #  -d 'ttl=1'
101
102# lets do the update
103__do_transfer __ANSWER "$__URL"
104__ERR=$?
105[ $__ERR -gt 0 ] && {
106        verbose_echo "\n!!!!!!!!! ERROR =: Error sending update to cloudflare.com\n"
107        return 1
108}
109verbose_echo "   update send =: cloudflare.com answered\n$__ANSWER"
110return 0