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
"You can get yours from here https://dash.cloudflare.com/profile."
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 _contains
"$response" "$txtvalue"; then
64 elif _contains
"$response" "The record already exists"; then
65 _info
"Already exists, OK"
68 _err
"Add txt record error."
72 _err
"Add txt record error."
75 # _info "Updating record"
76 # record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
77 # _debug "record_id" "$record_id"
79 # _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\"}"
80 # if [ "$?" = "0" ]; then
95 CF_Key
="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
96 CF_Email
="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
97 if [ -z "$CF_Key" ] ||
[ -z "$CF_Email" ]; then
100 _err
"You didn't specify a Cloudflare api key and email yet."
101 _err
"You can get yours from here https://dash.cloudflare.com/profile."
105 _debug
"First detect the root zone"
106 if ! _get_root
"$fulldomain"; then
107 _err
"invalid domain"
110 _debug _domain_id
"$_domain_id"
111 _debug _sub_domain
"$_sub_domain"
112 _debug _domain
"$_domain"
114 _debug
"Getting txt records"
115 _cf_rest GET
"zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
117 if ! printf "%s" "$response" |
grep \"success
\":true
>/dev
/null
; then
122 count
=$
(printf "%s\n" "$response" | _egrep_o
"\"count\":[^,]*" | cut
-d : -f 2)
123 _debug count
"$count"
124 if [ "$count" = "0" ]; then
125 _info
"Don't need to remove."
127 record_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":\"[^\"]*\"" | cut
-d : -f 2 |
tr -d \" |
head -n 1)
128 _debug
"record_id" "$record_id"
129 if [ -z "$record_id" ]; then
130 _err
"Can not get record id to remove."
133 if ! _cf_rest DELETE
"zones/$_domain_id/dns_records/$record_id"; then
134 _err
"Delete record error."
137 _contains
"$response" '"success":true'
142 #################### Private functions below ##################################
143 #_acme-challenge.www.domain.com
145 # _sub_domain=_acme-challenge.www
147 # _domain_id=sdjkglgdfewsdfg
153 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
160 if ! _cf_rest GET
"zones?name=$h"; then
164 if _contains
"$response" "\"name\":\"$h\"" || _contains
"$response" '"total_count":1'; then
165 _domain_id
=$
(echo "$response" | _egrep_o
"\[.\"id\":\"[^\"]*\"" | _head_n
1 | cut
-d : -f 2 |
tr -d \")
166 if [ "$_domain_id" ]; then
167 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
185 email_trimmed
=$
(echo $CF_Email |
tr -d '"')
186 key_trimmed
=$
(echo $CF_Key |
tr -d '"')
188 export _H1
="X-Auth-Email: $email_trimmed"
189 export _H2
="X-Auth-Key: $key_trimmed"
190 export _H3
="Content-Type: application/json"
192 if [ "$m" != "GET" ]; then
194 response
="$(_post "$data" "$CF_Api/$ep" "" "$m")"
196 response
="$(_get "$CF_Api/$ep")"
199 if [ "$?" != "0" ]; then
203 _debug2 response
"$response"