3 # Cloudxns.com Domain api
7 #CX_Secret="sADDsdasdgdsf"
9 CX_Api
="https://www.cloudxns.net/api2"
12 ######## Public functions #####################
14 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
19 if [ -z "$CX_Key" ] ||
[ -z "$CX_Secret" ]; then
22 _err
"You don't specify cloudxns.com api key or secret yet."
23 _err
"Please create you key and try again."
29 #save the api key and email to the account conf file.
30 _saveaccountconf CX_Key
"$CX_Key"
31 _saveaccountconf CX_Secret
"$CX_Secret"
33 _debug
"First detect the root zone"
34 if ! _get_root
"$fulldomain"; then
39 add_record
"$_domain" "$_sub_domain" "$txtvalue"
47 if _get_root
"$fulldomain"; then
49 existing_records
"$_domain" "$_sub_domain" "$txtvalue"
50 if [ "$record_id" ]; then
51 _rest DELETE
"record/$record_id/$_domain_id" "{}"
52 _info
"Deleted record ${fulldomain}"
58 #return if the sub record already exists.
59 #echos the existing records count.
60 # '0' means doesn't exist
62 _debug
"Getting txt records"
65 if ! _rest GET
"record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100"; then
69 seg
=$
(printf "%s\n" "$response" | _egrep_o
'"record_id":[^{]*host":"'"$_sub_domain"'"[^}]*\}')
71 if [ -z "$seg" ]; then
75 if printf "%s" "$response" |
grep '"type":"TXT"' >/dev
/null
; then
76 record_id
=$
(printf "%s\n" "$seg" | _egrep_o
'"record_id":"[^"]*"' | cut
-d : -f 2 |
tr -d \" | _head_n
1)
77 _debug record_id
"$record_id"
84 #usage: root sub txtvalue
89 fulldomain
="$sub.$root"
93 if ! _rest POST
"record" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
100 #################### Private functions below ##################################
101 #_acme-challenge.www.domain.com
103 # _sub_domain=_acme-challenge.www
105 # _domain_id=sdjkglgdfewsdfg
111 if ! _rest GET
"domain"; then
116 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
123 if _contains
"$response" "$h."; then
124 seg
=$
(printf "%s\n" "$response" | _egrep_o
'"id":[^{]*"'"$h"'."[^}]*}')
126 _domain_id
=$
(printf "%s\n" "$seg" | _egrep_o
"\"id\":\"[^\"]*\"" | cut
-d : -f 2 |
tr -d \")
127 _debug _domain_id
"$_domain_id"
128 if [ "$_domain_id" ]; then
129 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
130 _debug _sub_domain
"$_sub_domain"
132 _debug _domain
"$_domain"
143 #Usage: method URI data
151 cdate
=$
(date -u "+%Y-%m-%d %H:%M:%S UTC")
152 _debug cdate
"$cdate"
157 sec
="$CX_Key$url$data$cdate$CX_Secret"
159 hmac
=$
(printf "%s" "$sec" | _digest md5 hex
)
162 export _H1
="API-KEY: $CX_Key"
163 export _H2
="API-REQUEST-DATE: $cdate"
164 export _H3
="API-HMAC: $hmac"
165 export _H4
="Content-Type: application/json"
168 response
="$(_post "$data" "$url" "" "$m")"
170 response
="$(_get "$url")"
173 if [ "$?" != "0" ]; then
177 _debug2 response
"$response"
179 _contains
"$response" '"code":1'