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 _DOMAIN_CF_ZONES_CACHE_NAME_
="$(echo "${CF_Email}_CF_ZONES_
" | tr '@.' '__')"
38 _cleardomainconf
"$_DOMAIN_CF_ZONES_CACHE_NAME_"
40 _debug
"First detect the root zone"
41 if ! _get_root
"$fulldomain"; then
45 _debug _domain_id
"$_domain_id"
46 _debug _sub_domain
"$_sub_domain"
47 _debug _domain
"$_domain"
49 _debug
"Getting txt records"
50 _cf_rest GET
"zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
52 if ! printf "%s" "$response" |
grep \"success
\":true
>/dev
/null
; then
57 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
58 # we can not use updating anymore.
59 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
60 # _debug count "$count"
61 # if [ "$count" = "0" ]; then
63 if _cf_rest POST
"zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
64 if _contains
"$response" "$fulldomain"; then
67 elif _contains
"$response" "The record already exists"; then
68 _info
"Already exists, OK"
71 _err
"Add txt record error."
75 _err
"Add txt record error."
78 # _info "Updating record"
79 # record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
80 # _debug "record_id" "$record_id"
82 # _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\"}"
83 # if [ "$?" = "0" ]; then
98 CF_Key
="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
99 CF_Email
="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
100 if [ -z "$CF_Key" ] ||
[ -z "$CF_Email" ]; then
103 _err
"You didn't specify a cloudflare api key and email yet."
104 _err
"Please create the key and try again."
108 _DOMAIN_CF_ZONES_CACHE_NAME_
="$(echo "${CF_Email}_CF_ZONES_
" | tr '@.' '__')"
110 _debug
"First detect the root zone"
111 if ! _get_root
"$fulldomain"; then
112 _cleardomainconf
"$_DOMAIN_CF_ZONES_CACHE_NAME_"
113 _err
"invalid domain"
116 _cleardomainconf
"$_DOMAIN_CF_ZONES_CACHE_NAME_"
118 _debug _domain_id
"$_domain_id"
119 _debug _sub_domain
"$_sub_domain"
120 _debug _domain
"$_domain"
122 _debug
"Getting txt records"
123 _cf_rest GET
"zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
125 if ! printf "%s" "$response" |
grep \"success
\":true
>/dev
/null
; then
130 count
=$
(printf "%s\n" "$response" | _egrep_o
"\"count\":[^,]*" | cut
-d : -f 2)
131 _debug count
"$count"
132 if [ "$count" = "0" ]; then
133 _info
"Don't need to remove."
135 record_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":\"[^\"]*\"" | cut
-d : -f 2 |
tr -d \" |
head -n 1)
136 _debug
"record_id" "$record_id"
137 if [ -z "$record_id" ]; then
138 _err
"Can not get record id to remove."
141 if ! _cf_rest DELETE
"zones/$_domain_id/dns_records/$record_id"; then
142 _err
"Delete record error."
145 _contains
"$response" '"success":true'
150 #################### Private functions below ##################################
151 #_acme-challenge.www.domain.com
153 # _sub_domain=_acme-challenge.www
155 # _domain_id=sdjkglgdfewsdfg
158 _cf_zones
="$(_readdomainconf "$_DOMAIN_CF_ZONES_CACHE_NAME_")"
159 _debug2
"_cf_zones" "$_cf_zones"
160 if [ -z "$_cf_zones" ]; then
161 _debug
"$_DOMAIN_CF_ZONES_CACHE_NAME_ is none, so get it."
162 if ! _cf_rest GET
"zones"; then
165 _cf_zones
="$response"
166 _savedomainconf
"$_DOMAIN_CF_ZONES_CACHE_NAME_" "$(echo "$_cf_zones" | _base64)"
168 _debug
"$_DOMAIN_CF_ZONES_CACHE_NAME_ found"
169 _cf_zones
="$(echo "$_cf_zones" | _dbase64)"
176 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
183 if _contains
"$_cf_zones" "\"name\":\"$h\"" >/dev
/null
; then
184 _domain_id
=$
(echo "$_cf_zones" |
tr '{' "\n" |
grep "\"name\":\"$h\"" | _egrep_o
"^\"id\":\"[^\"]*\"" | _head_n
1 | cut
-d : -f 2 |
tr -d \")
185 if [ "$_domain_id" ]; then
186 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
204 export _H1
="X-Auth-Email: $CF_Email"
205 export _H2
="X-Auth-Key: $CF_Key"
206 export _H3
="Content-Type: application/json"
208 if [ "$m" != "GET" ]; then
210 response
="$(_post "$data" "$CF_Api/$ep" "" "$m")"
212 response
="$(_get "$CF_Api/$ep")"
215 if [ "$?" != "0" ]; then
219 _debug2 response
"$response"