]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_dpi.sh
Updated with latest changes from Neilpang/dev
[mirror_acme.sh.git] / dnsapi / dns_dpi.sh
1 #!/usr/bin/env sh
2
3 # Dnspod.com Domain api
4 #
5 #DPI_Id="1234"
6 #
7 #DPI_Key="sADDsdasdgdsf"
8
9 REST_API="https://api.dnspod.com"
10
11 ######## Public functions #####################
12
13 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
14 dns_dpi_add() {
15 fulldomain=$1
16 txtvalue=$2
17
18 DPI_Id="${DPI_Id:-$(_readaccountconf_mutable DPI_Id)}"
19 DPI_Key="${DPI_Key:-$(_readaccountconf_mutable DPI_Key)}"
20 if [ -z "$DPI_Id" ] || [ -z "$DPI_Key" ]; then
21 DPI_Id=""
22 DPI_Key=""
23 _err "You don't specify dnspod api key and key id yet."
24 _err "Please create you key and try again."
25 return 1
26 fi
27
28 #save the api key and email to the account conf file.
29 _saveaccountconf_mutable DPI_Id "$DPI_Id"
30 _saveaccountconf_mutable DPI_Key "$DPI_Key"
31
32 _debug "First detect the root zone"
33 if ! _get_root "$fulldomain"; then
34 _err "invalid domain"
35 return 1
36 fi
37
38 add_record "$_domain" "$_sub_domain" "$txtvalue"
39
40 }
41
42 #fulldomain txtvalue
43 dns_dpi_rm() {
44 fulldomain=$1
45 txtvalue=$2
46
47 DPI_Id="${DPI_Id:-$(_readaccountconf_mutable DPI_Id)}"
48 DPI_Key="${DPI_Key:-$(_readaccountconf_mutable DPI_Key)}"
49
50 _debug "First detect the root zone"
51 if ! _get_root "$fulldomain"; then
52 _err "invalid domain"
53 return 1
54 fi
55
56 if ! _rest POST "Record.List" "user_token=$DPI_Id,$DPI_Key&format=json&domain_id=$_domain_id&sub_domain=$_sub_domain"; then
57 _err "Record.Lis error."
58 return 1
59 fi
60
61 if _contains "$response" 'No records'; then
62 _info "Don't need to remove."
63 return 0
64 fi
65
66 record_id=$(echo "$response" | _egrep_o '{[^{]*"value":"'"$txtvalue"'"' | cut -d , -f 1 | cut -d : -f 2 | tr -d \")
67 _debug record_id "$record_id"
68 if [ -z "$record_id" ]; then
69 _err "Can not get record id."
70 return 1
71 fi
72
73 if ! _rest POST "Record.Remove" "user_token=$DPI_Id,$DPI_Key&format=json&domain_id=$_domain_id&record_id=$record_id"; then
74 _err "Record.Remove error."
75 return 1
76 fi
77
78 _contains "$response" "Action completed successful"
79
80 }
81
82 #add the txt record.
83 #usage: root sub txtvalue
84 add_record() {
85 root=$1
86 sub=$2
87 txtvalue=$3
88 fulldomain="$sub.$root"
89
90 _info "Adding record"
91
92 if ! _rest POST "Record.Create" "user_token=$DPI_Id,$DPI_Key&format=json&domain_id=$_domain_id&sub_domain=$_sub_domain&record_type=TXT&value=$txtvalue&record_line=default"; then
93 return 1
94 fi
95
96 _contains "$response" "Action completed successful" || _contains "$response" "Domain record already exists"
97 }
98
99 #################### Private functions below ##################################
100 #_acme-challenge.www.domain.com
101 #returns
102 # _sub_domain=_acme-challenge.www
103 # _domain=domain.com
104 # _domain_id=sdjkglgdfewsdfg
105 _get_root() {
106 domain=$1
107 i=2
108 p=1
109 while true; do
110 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
111 if [ -z "$h" ]; then
112 #not valid
113 return 1
114 fi
115
116 if ! _rest POST "Domain.Info" "user_token=$DPI_Id,$DPI_Key&format=json&domain=$h"; then
117 return 1
118 fi
119
120 if _contains "$response" "Action completed successful"; then
121 _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
122 _debug _domain_id "$_domain_id"
123 if [ "$_domain_id" ]; then
124 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
125 _debug _sub_domain "$_sub_domain"
126 _domain="$h"
127 _debug _domain "$_domain"
128 return 0
129 fi
130 return 1
131 fi
132 p="$i"
133 i=$(_math "$i" + 1)
134 done
135 return 1
136 }
137
138 #Usage: method URI data
139 _rest() {
140 m="$1"
141 ep="$2"
142 data="$3"
143 _debug "$ep"
144 url="$REST_API/$ep"
145
146 _debug url "$url"
147
148 if [ "$m" = "GET" ]; then
149 response="$(_get "$url" | tr -d '\r')"
150 else
151 _debug2 data "$data"
152 response="$(_post "$data" "$url" | tr -d '\r')"
153 fi
154
155 if [ "$?" != "0" ]; then
156 _err "error $ep"
157 return 1
158 fi
159 _debug2 response "$response"
160 return 0
161 }