]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env sh | |
2 | ||
3 | ####################################################### | |
4 | # | |
5 | # easyDNS REST API for acme.sh by Neilpang based on dns_cf.sh | |
6 | # | |
7 | # API Documentation: https://sandbox.rest.easydns.net:3001/ | |
8 | # | |
9 | # Author: wurzelpanzer [wurzelpanzer@maximolider.net] | |
10 | # Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/2647 | |
11 | # | |
12 | #################### Public functions ################# | |
13 | ||
14 | #EASYDNS_Key="xxxxxxxxxxxxxxxxxxxxxxxx" | |
15 | #EASYDNS_Token="xxxxxxxxxxxxxxxxxxxxxxxx" | |
16 | EASYDNS_Api="https://rest.easydns.net" | |
17 | ||
18 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
19 | dns_easydns_add() { | |
20 | fulldomain=$1 | |
21 | txtvalue=$2 | |
22 | ||
23 | EASYDNS_Token="${EASYDNS_Token:-$(_readaccountconf_mutable EASYDNS_Token)}" | |
24 | EASYDNS_Key="${EASYDNS_Key:-$(_readaccountconf_mutable EASYDNS_Key)}" | |
25 | ||
26 | if [ -z "$EASYDNS_Token" ] || [ -z "$EASYDNS_Key" ]; then | |
27 | _err "You didn't specify an easydns.net token or api key. Signup at https://cp.easydns.com/manage/security/api/signup.php" | |
28 | return 1 | |
29 | else | |
30 | _saveaccountconf_mutable EASYDNS_Token "$EASYDNS_Token" | |
31 | _saveaccountconf_mutable EASYDNS_Key "$EASYDNS_Key" | |
32 | fi | |
33 | ||
34 | _debug "First detect the root zone" | |
35 | if ! _get_root "$fulldomain"; then | |
36 | _err "invalid domain" | |
37 | return 1 | |
38 | fi | |
39 | _debug _sub_domain "$_sub_domain" | |
40 | _debug _domain "$_domain" | |
41 | ||
42 | _debug "Getting txt records" | |
43 | _EASYDNS_rest GET "zones/records/all/${_domain}/search/${_sub_domain}" | |
44 | ||
45 | if ! printf "%s" "$response" | grep \"status\":200 >/dev/null; then | |
46 | _err "Error" | |
47 | return 1 | |
48 | fi | |
49 | ||
50 | _info "Adding record" | |
51 | if _EASYDNS_rest PUT "zones/records/add/$_domain/TXT" "{\"host\":\"$_sub_domain\",\"rdata\":\"$txtvalue\"}"; then | |
52 | if _contains "$response" "\"status\":201"; then | |
53 | _info "Added, OK" | |
54 | return 0 | |
55 | elif _contains "$response" "Record already exists"; then | |
56 | _info "Already exists, OK" | |
57 | return 0 | |
58 | else | |
59 | _err "Add txt record error." | |
60 | return 1 | |
61 | fi | |
62 | fi | |
63 | _err "Add txt record error." | |
64 | return 1 | |
65 | ||
66 | } | |
67 | ||
68 | dns_easydns_rm() { | |
69 | fulldomain=$1 | |
70 | txtvalue=$2 | |
71 | ||
72 | EASYDNS_Token="${EASYDNS_Token:-$(_readaccountconf_mutable EASYDNS_Token)}" | |
73 | EASYDNS_Key="${EASYDNS_Key:-$(_readaccountconf_mutable EASYDNS_Key)}" | |
74 | ||
75 | _debug "First detect the root zone" | |
76 | if ! _get_root "$fulldomain"; then | |
77 | _err "invalid domain" | |
78 | return 1 | |
79 | fi | |
80 | _debug _sub_domain "$_sub_domain" | |
81 | _debug _domain "$_domain" | |
82 | ||
83 | _debug "Getting txt records" | |
84 | _EASYDNS_rest GET "zones/records/all/${_domain}/search/${_sub_domain}" | |
85 | ||
86 | if ! printf "%s" "$response" | grep \"status\":200 >/dev/null; then | |
87 | _err "Error" | |
88 | return 1 | |
89 | fi | |
90 | ||
91 | count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2) | |
92 | _debug count "$count" | |
93 | if [ "$count" = "0" ]; then | |
94 | _info "Don't need to remove." | |
95 | else | |
96 | record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1) | |
97 | _debug "record_id" "$record_id" | |
98 | if [ -z "$record_id" ]; then | |
99 | _err "Can not get record id to remove." | |
100 | return 1 | |
101 | fi | |
102 | if ! _EASYDNS_rest DELETE "zones/records/$_domain/$record_id"; then | |
103 | _err "Delete record error." | |
104 | return 1 | |
105 | fi | |
106 | _contains "$response" "\"status\":200" | |
107 | fi | |
108 | ||
109 | } | |
110 | ||
111 | #################### Private functions below ################################## | |
112 | #_acme-challenge.www.domain.com | |
113 | #returns | |
114 | # _sub_domain=_acme-challenge.www | |
115 | # _domain=domain.com | |
116 | _get_root() { | |
117 | domain=$1 | |
118 | i=1 | |
119 | p=1 | |
120 | while true; do | |
121 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
122 | _debug h "$h" | |
123 | if [ -z "$h" ]; then | |
124 | #not valid | |
125 | return 1 | |
126 | fi | |
127 | ||
128 | if ! _EASYDNS_rest GET "zones/records/all/$h"; then | |
129 | return 1 | |
130 | fi | |
131 | ||
132 | if _contains "$response" "\"status\":200"; then | |
133 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
134 | _domain=$h | |
135 | return 0 | |
136 | fi | |
137 | ||
138 | p=$i | |
139 | i=$(_math "$i" + 1) | |
140 | done | |
141 | return 1 | |
142 | } | |
143 | ||
144 | _EASYDNS_rest() { | |
145 | m=$1 | |
146 | ep="$2" | |
147 | data="$3" | |
148 | _debug "$ep" | |
149 | ||
150 | basicauth=$(printf "%s" "$EASYDNS_Token":"$EASYDNS_Key" | _base64) | |
151 | ||
152 | export _H1="accept: application/json" | |
153 | if [ "$basicauth" ]; then | |
154 | export _H2="Authorization: Basic $basicauth" | |
155 | fi | |
156 | ||
157 | if [ "$m" != "GET" ]; then | |
158 | export _H3="Content-Type: application/json" | |
159 | _debug data "$data" | |
160 | response="$(_post "$data" "$EASYDNS_Api/$ep" "" "$m")" | |
161 | else | |
162 | response="$(_get "$EASYDNS_Api/$ep")" | |
163 | fi | |
164 | ||
165 | if [ "$?" != "0" ]; then | |
166 | _err "error $ep" | |
167 | return 1 | |
168 | fi | |
169 | _debug2 response "$response" | |
170 | return 0 | |
171 | } |