]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_dp.sh
Merge pull request #375 from Neilpang/dev
[mirror_acme.sh.git] / dnsapi / dns_dp.sh
1 #!/usr/bin/env sh
2
3 # Dnspod.cn Domain api
4 #
5 #DP_Id="1234"
6 #
7 #DP_Key="sADDsdasdgdsf"
8
9 DP_Api="https://dnsapi.cn"
10
11 #REST_API
12 ######## Public functions #####################
13
14 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
15 dns_dp_add() {
16 fulldomain=$1
17 txtvalue=$2
18
19 if [ -z "$DP_Id" ] || [ -z "$DP_Key" ]; then
20 _err "You don't specify dnspod api key and key id yet."
21 _err "Please create you key and try again."
22 return 1
23 fi
24
25 REST_API=$DP_Api
26
27 #save the api key and email to the account conf file.
28 _saveaccountconf DP_Id "$DP_Id"
29 _saveaccountconf DP_Key "$DP_Key"
30
31 _debug "First detect the root zone"
32 if ! _get_root $fulldomain; then
33 _err "invalid domain"
34 return 1
35 fi
36
37 existing_records $_domain $_sub_domain
38 _debug count "$count"
39 if [ "$?" != "0" ]; then
40 _err "Error get existing records."
41 return 1
42 fi
43
44 if [ "$count" = "0" ]; then
45 add_record $_domain $_sub_domain $txtvalue
46 else
47 update_record $_domain $_sub_domain $txtvalue
48 fi
49 }
50
51 #fulldomain
52 dns_dp_rm() {
53 fulldomain=$1
54
55 }
56
57 #usage: root sub
58 #return if the sub record already exists.
59 #echos the existing records count.
60 # '0' means doesn't exist
61 existing_records() {
62 _debug "Getting txt records"
63 root=$1
64 sub=$2
65
66 if ! _rest POST "Record.List" "login_token=$DP_Id,$DP_Key&domain_id=$_domain_id&sub_domain=$_sub_domain"; then
67 return 1
68 fi
69
70 if printf "$response" | grep 'No records'; then
71 count=0
72 return 0
73 fi
74
75 if printf "$response" | grep "Action completed successful" >/dev/null; then
76 count=$(printf "$response" | grep '<type>TXT</type>' | wc -l)
77 record_id=$(printf "$response" | grep '^<id>' | tail -1 | cut -d '>' -f 2 | cut -d '<' -f 1)
78 return 0
79 else
80 _err "get existing records error."
81 return 1
82 fi
83
84 count=0
85 }
86
87 #add the txt record.
88 #usage: root sub txtvalue
89 add_record() {
90 root=$1
91 sub=$2
92 txtvalue=$3
93 fulldomain=$sub.$root
94
95 _info "Adding record"
96
97 if ! _rest POST "Record.Create" "login_token=$DP_Id,$DP_Key&format=json&domain_id=$_domain_id&sub_domain=$_sub_domain&record_type=TXT&value=$txtvalue&record_line=默认"; then
98 return 1
99 fi
100
101 if printf "$response" | grep "Action completed successful"; then
102
103 return 0
104 fi
105
106 return 1 #error
107 }
108
109 #update the txt record
110 #Usage: root sub txtvalue
111 update_record() {
112 root=$1
113 sub=$2
114 txtvalue=$3
115 fulldomain=$sub.$root
116
117 _info "Updating record"
118
119 if ! _rest POST "Record.Modify" "login_token=$DP_Id,$DP_Key&format=json&domain_id=$_domain_id&sub_domain=$_sub_domain&record_type=TXT&value=$txtvalue&record_line=默认&record_id=$record_id"; then
120 return 1
121 fi
122
123 if printf "$response" | grep "Action completed successful"; then
124
125 return 0
126 fi
127
128 return 1 #error
129 }
130
131 #################### Private functions bellow ##################################
132 #_acme-challenge.www.domain.com
133 #returns
134 # _sub_domain=_acme-challenge.www
135 # _domain=domain.com
136 # _domain_id=sdjkglgdfewsdfg
137 _get_root() {
138 domain=$1
139 i=2
140 p=1
141 while [ '1' ]; do
142 h=$(printf $domain | cut -d . -f $i-100)
143 if [ -z "$h" ]; then
144 #not valid
145 return 1
146 fi
147
148 if ! _rest POST "Domain.Info" "login_token=$DP_Id,$DP_Key&format=json&domain=$h"; then
149 return 1
150 fi
151
152 if printf "$response" | grep "Action completed successful" >/dev/null; then
153 _domain_id=$(printf "%s\n" "$response" | _egrep_o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \")
154 _debug _domain_id "$_domain_id"
155 if [ "$_domain_id" ]; then
156 _sub_domain=$(printf $domain | cut -d . -f 1-$p)
157 _debug _sub_domain $_sub_domain
158 _domain=$h
159 _debug _domain $_domain
160 return 0
161 fi
162 return 1
163 fi
164 p=$i
165 i=$(expr $i + 1)
166 done
167 return 1
168 }
169
170 #Usage: method URI data
171 _rest() {
172 m=$1
173 ep="$2"
174 data="$3"
175 _debug $ep
176 url="$REST_API/$ep"
177
178 _debug url "$url"
179
180 if [ "$data" ]; then
181 _debug2 data "$data"
182 response="$(_post $data "$url")"
183 else
184 response="$(_get "$url")"
185 fi
186
187 if [ "$?" != "0" ]; then
188 _err "error $ep"
189 return 1
190 fi
191 _debug2 response "$response"
192 return 0
193 }