]>
Commit | Line | Data |
---|---|---|
f37546e1 SM |
1 | #!/usr/bin/env sh |
2 | ||
3 | # bug reports to support+acmesh@misaka.io | |
4 | # based on dns_nsone.sh by dev@1e.ca | |
5 | ||
6 | # | |
7 | #Misaka_Key="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
8 | # | |
9 | ||
10 | Misaka_Api="https://dnsapi.misaka.io/dns" | |
11 | ||
12 | ######## Public functions ##################### | |
13 | ||
14 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
15 | dns_misaka_add() { | |
16 | fulldomain=$1 | |
17 | txtvalue=$2 | |
18 | ||
19 | if [ -z "$Misaka_Key" ]; then | |
20 | Misaka_Key="" | |
21 | _err "You didn't specify misaka.io dns api key yet." | |
22 | _err "Please create you key and try again." | |
23 | return 1 | |
24 | fi | |
25 | ||
26 | #save the api key and email to the account conf file. | |
27 | _saveaccountconf Misaka_Key "$Misaka_Key" | |
28 | ||
29 | _debug "checking root zone [$fulldomain]" | |
30 | if ! _get_root "$fulldomain"; then | |
31 | _err "invalid domain" | |
32 | return 1 | |
33 | fi | |
34 | _debug _sub_domain "$_sub_domain" | |
35 | _debug _domain "$_domain" | |
36 | ||
37 | _debug "Getting txt records" | |
38 | _misaka_rest GET "zones/${_domain}/recordsets?search=${_sub_domain}" | |
39 | ||
40 | if ! _contains "$response" "\"results\":"; then | |
41 | _err "Error" | |
42 | return 1 | |
43 | fi | |
44 | ||
45 | count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$_sub_domain\",[^{]*\"type\":\"TXT\"" | wc -l | tr -d " ") | |
46 | _debug count "$count" | |
47 | if [ "$count" = "0" ]; then | |
48 | _info "Adding record" | |
49 | ||
50 | if _misaka_rest PUT "zones/${_domain}/recordsets/${_sub_domain}/TXT" "{\"records\":[{\"value\":\"\\\"$txtvalue\\\"\"}],\"filters\":[],\"ttl\":1}"; then | |
51 | _debug response "$response" | |
52 | if _contains "$response" "$_sub_domain"; then | |
53 | _info "Added" | |
54 | return 0 | |
55 | else | |
56 | _err "Add txt record error." | |
57 | return 1 | |
58 | fi | |
59 | fi | |
60 | _err "Add txt record error." | |
61 | else | |
62 | _info "Updating record" | |
63 | ||
375b8dce | 64 | _misaka_rest POST "zones/${_domain}/recordsets/${_sub_domain}/TXT?append=true" "{\"records\": [{\"value\": \"\\\"$txtvalue\\\"\"}],\"ttl\":1}" |
f37546e1 SM |
65 | if [ "$?" = "0" ] && _contains "$response" "$_sub_domain"; then |
66 | _info "Updated!" | |
67 | #todo: check if the record takes effect | |
68 | return 0 | |
69 | fi | |
70 | _err "Update error" | |
71 | return 1 | |
72 | fi | |
73 | ||
74 | } | |
75 | ||
76 | #fulldomain | |
77 | dns_misaka_rm() { | |
78 | fulldomain=$1 | |
79 | txtvalue=$2 | |
80 | _debug "First detect the root zone" | |
81 | if ! _get_root "$fulldomain"; then | |
82 | _err "invalid domain" | |
83 | return 1 | |
84 | fi | |
85 | _debug _sub_domain "$_sub_domain" | |
86 | _debug _domain "$_domain" | |
87 | ||
88 | _debug "Getting txt records" | |
89 | _misaka_rest GET "zones/${_domain}/recordsets?search=${_sub_domain}" | |
90 | ||
91 | count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$_sub_domain\",[^{]*\"type\":\"TXT\"" | wc -l | tr -d " ") | |
92 | _debug count "$count" | |
93 | if [ "$count" = "0" ]; then | |
94 | _info "Don't need to remove." | |
95 | else | |
96 | if ! _misaka_rest DELETE "zones/${_domain}/recordsets/${_sub_domain}/TXT"; then | |
97 | _err "Delete record error." | |
98 | return 1 | |
99 | fi | |
100 | _contains "$response" "" | |
101 | fi | |
102 | } | |
103 | ||
104 | #################### Private functions below ################################## | |
105 | #_acme-challenge.www.domain.com | |
106 | #returns | |
107 | # _sub_domain=_acme-challenge.www | |
108 | # _domain=domain.com | |
109 | # _domain_id=sdjkglgdfewsdfg | |
110 | _get_root() { | |
111 | domain=$1 | |
112 | i=2 | |
113 | p=1 | |
114 | if ! _misaka_rest GET "zones?limit=1000"; then | |
115 | return 1 | |
116 | fi | |
117 | while true; do | |
118 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
119 | _debug h "$h" | |
120 | if [ -z "$h" ]; then | |
121 | #not valid | |
122 | return 1 | |
123 | fi | |
124 | ||
125 | if _contains "$response" "\"name\":\"$h\""; then | |
126 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
127 | _domain="$h" | |
128 | return 0 | |
129 | fi | |
130 | p=$i | |
131 | i=$(_math "$i" + 1) | |
132 | done | |
133 | return 1 | |
134 | } | |
135 | ||
136 | _misaka_rest() { | |
137 | m=$1 | |
138 | ep="$2" | |
139 | data="$3" | |
140 | _debug "$ep" | |
141 | ||
142 | export _H1="Content-Type: application/json" | |
143 | export _H2="User-Agent: acme.sh/$VER misaka-dns-acmesh/20191213" | |
144 | export _H3="Authorization: Token $Misaka_Key" | |
145 | ||
146 | if [ "$m" != "GET" ]; then | |
147 | _debug data "$data" | |
148 | response="$(_post "$data" "$Misaka_Api/$ep" "" "$m")" | |
149 | else | |
150 | response="$(_get "$Misaka_Api/$ep")" | |
151 | fi | |
152 | ||
153 | if [ "$?" != "0" ]; then | |
154 | _err "error $ep" | |
155 | return 1 | |
156 | fi | |
157 | _debug2 response "$response" | |
158 | return 0 | |
159 | } |