Ticket #12500: 20141109-update_cloudflare.sh

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