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