4 #CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
6 #CF_Email="xxxx@sss.com"
8 CF_Api
="https://api.cloudflare.com/client/v4"
10 ######## Public functions #####################
12 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
17 CF_Key
="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
18 CF_Email
="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
19 if [ -z "$CF_Key" ] ||
[ -z "$CF_Email" ]; then
22 _err
"You didn't specify a cloudflare api key and email yet."
23 _err
"Please create the key and try again."
27 if ! _contains
"$CF_Email" "@"; then
28 _err
"It seems that the CF_Email=$CF_Email is not a valid email address."
29 _err
"Please check and retry."
33 #save the api key and email to the account conf file.
34 _saveaccountconf_mutable CF_Key
"$CF_Key"
35 _saveaccountconf_mutable CF_Email
"$CF_Email"
37 _debug
"First detect the root zone"
38 if ! _get_root
"$fulldomain"; then
42 _debug _domain_id
"$_domain_id"
43 _debug _sub_domain
"$_sub_domain"
44 _debug _domain
"$_domain"
46 _debug
"Getting txt records"
47 _cf_rest GET
"zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
49 if ! printf "%s" "$response" |
grep \"success
\":true
>/dev
/null
; then
54 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
55 # we can not use updating anymore.
56 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
57 # _debug count "$count"
58 # if [ "$count" = "0" ]; then
60 if _cf_rest POST
"zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
61 if printf -- "%s" "$response" |
grep "$fulldomain" >/dev
/null
; then
65 _err
"Add txt record error."
69 _err
"Add txt record error."
72 # _info "Updating record"
73 # record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
74 # _debug "record_id" "$record_id"
76 # _cf_rest PUT "zones/$_domain_id/dns_records/$record_id" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"zone_id\":\"$_domain_id\",\"zone_name\":\"$_domain\"}"
77 # if [ "$?" = "0" ]; then
92 CF_Key
="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
93 CF_Email
="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
94 if [ -z "$CF_Key" ] ||
[ -z "$CF_Email" ]; then
97 _err
"You didn't specify a cloudflare api key and email yet."
98 _err
"Please create the key and try again."
102 _debug
"First detect the root zone"
103 if ! _get_root
"$fulldomain"; then
104 _err
"invalid domain"
107 _debug _domain_id
"$_domain_id"
108 _debug _sub_domain
"$_sub_domain"
109 _debug _domain
"$_domain"
111 _debug
"Getting txt records"
112 _cf_rest GET
"zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
114 if ! printf "%s" "$response" |
grep \"success
\":true
>/dev
/null
; then
119 count
=$
(printf "%s\n" "$response" | _egrep_o
"\"count\":[^,]*" | cut
-d : -f 2)
120 _debug count
"$count"
121 if [ "$count" = "0" ]; then
122 _info
"Don't need to remove."
124 record_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":\"[^\"]*\"" | cut
-d : -f 2 |
tr -d \" |
head -n 1)
125 _debug
"record_id" "$record_id"
126 if [ -z "$record_id" ]; then
127 _err
"Can not get record id to remove."
130 if ! _cf_rest DELETE
"zones/$_domain_id/dns_records/$record_id"; then
131 _err
"Delete record error."
134 _contains
"$response" '"success":true'
139 #################### Private functions below ##################################
140 #_acme-challenge.www.domain.com
142 # _sub_domain=_acme-challenge.www
144 # _domain_id=sdjkglgdfewsdfg
150 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
157 if ! _cf_rest GET
"zones?name=$h"; then
161 if _contains
"$response" "\"name\":\"$h\"" >/dev
/null
; then
162 _domain_id
=$
(printf "%s\n" "$response" | _egrep_o
"\[.\"id\":\"[^\"]*\"" |
head -n 1 | cut
-d : -f 2 |
tr -d \")
163 if [ "$_domain_id" ]; then
164 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
182 export _H1
="X-Auth-Email: $CF_Email"
183 export _H2
="X-Auth-Key: $CF_Key"
184 export _H3
="Content-Type: application/json"
186 if [ "$m" != "GET" ]; then
188 response
="$(_post "$data" "$CF_Api/$ep" "" "$m")"
190 response
="$(_get "$CF_Api/$ep")"
193 if [ "$?" != "0" ]; then
197 _debug2 response
"$response"