3 # bug reports to dev@1e.ca
6 #LUA_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
8 #LUA_Email="user@luadns.net"
10 LUA_Api
="https://api.luadns.com/v1"
11 LUA_auth
=$
(printf "%s" "$LUA_Email:$LUA_Key" | _base64
)
13 ######## Public functions #####################
15 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
20 if [ -z "$LUA_Key" ] ||
[ -z "$LUA_Email" ]; then
23 _err
"You don't specify luadns api key and email yet."
24 _err
"Please create you key and try again."
28 #save the api key and email to the account conf file.
29 _saveaccountconf LUA_Key
"$LUA_Key"
30 _saveaccountconf LUA_Email
"$LUA_Email"
32 _debug
"First detect the root zone"
33 if ! _get_root
"$fulldomain"; then
37 _debug _domain_id
"$_domain_id"
38 _debug _sub_domain
"$_sub_domain"
39 _debug _domain
"$_domain"
41 _debug
"Getting txt records"
42 _LUA_rest GET
"zones/${_domain_id}/records"
44 if ! _contains
"$response" "\"id\":"; then
49 count
=$
(printf "%s\n" "$response" | _egrep_o
"\"name\":\"$fulldomain.\",\"type\":\"TXT\"" |
wc -l |
tr -d " ")
51 if [ "$count" = "0" ]; then
53 if _LUA_rest POST
"zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
54 if _contains
"$response" "$fulldomain"; then
56 #todo: check if the record takes effect
59 _err
"Add txt record error."
63 _err
"Add txt record error."
65 _info
"Updating record"
66 record_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n
1 | cut
-d: -f2 | cut
-d, -f1)
67 _debug
"record_id" "$record_id"
69 _LUA_rest PUT
"zones/$_domain_id/records/$record_id" "{\"id\":$record_id,\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"zone_id\":$_domain_id,\"ttl\":120}"
70 if [ "$?" = "0" ] && _contains
"$response" "updated_at"; then
72 #todo: check if the record takes effect
85 _debug
"First detect the root zone"
86 if ! _get_root
"$fulldomain"; then
90 _debug _domain_id
"$_domain_id"
91 _debug _sub_domain
"$_sub_domain"
92 _debug _domain
"$_domain"
94 _debug
"Getting txt records"
95 _LUA_rest GET
"zones/${_domain_id}/records"
97 count
=$
(printf "%s\n" "$response" | _egrep_o
"\"name\":\"$fulldomain.\",\"type\":\"TXT\"" |
wc -l |
tr -d " ")
99 if [ "$count" = "0" ]; then
100 _info
"Don't need to remove."
102 record_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n
1 | cut
-d: -f2 | cut
-d, -f1)
103 _debug
"record_id" "$record_id"
104 if [ -z "$record_id" ]; then
105 _err
"Can not get record id to remove."
108 if ! _LUA_rest DELETE
"/zones/$_domain_id/records/$record_id"; then
109 _err
"Delete record error."
112 _contains
"$response" "$record_id"
116 #################### Private functions below ##################################
117 #_acme-challenge.www.domain.com
119 # _sub_domain=_acme-challenge.www
121 # _domain_id=sdjkglgdfewsdfg
126 if ! _LUA_rest GET
"zones"; then
130 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
137 if _contains
"$response" "\"name\":\"$h\""; then
138 _domain_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":[^,]*,\"name\":\"$h\"" | cut
-d : -f 2 | cut
-d , -f 1)
139 _debug _domain_id
"$_domain_id"
140 if [ "$_domain_id" ]; then
141 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
159 export _H1
="Accept: application/json"
160 export _H2
="Authorization: Basic $LUA_auth"
161 if [ "$m" != "GET" ]; then
163 response
="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
165 response
="$(_get "$LUA_Api/$ep")"
168 if [ "$?" != "0" ]; then
172 _debug2 response
"$response"