]>
Commit | Line | Data |
---|---|---|
0a7c9364 | 1 | #!/usr/bin/env sh |
9773342a | 2 | |
3 | # Dnspod.cn Domain api | |
4 | # | |
5 | #DP_Id="1234" | |
6 | # | |
7 | #DP_Key="sADDsdasdgdsf" | |
8 | ||
f162ad19 | 9 | REST_API="https://dnsapi.cn" |
9773342a | 10 | |
9773342a | 11 | ######## Public functions ##################### |
12 | ||
13 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
eccec5f6 | 14 | dns_dp_add() { |
9773342a | 15 | fulldomain=$1 |
16 | txtvalue=$2 | |
439580b9 | 17 | |
a6b6e31c | 18 | DP_Id="${DP_Id:-$(_readaccountconf_mutable DP_Id)}" |
19 | DP_Key="${DP_Key:-$(_readaccountconf_mutable DP_Key)}" | |
4c2a3841 | 20 | if [ -z "$DP_Id" ] || [ -z "$DP_Key" ]; then |
422e5026 | 21 | DP_Id="" |
22 | DP_Key="" | |
65785b5f | 23 | _err "You don't specify dnspod api key and key id yet." |
9773342a | 24 | _err "Please create you key and try again." |
25 | return 1 | |
26 | fi | |
439580b9 | 27 | |
9773342a | 28 | #save the api key and email to the account conf file. |
a6b6e31c | 29 | _saveaccountconf_mutable DP_Id "$DP_Id" |
30 | _saveaccountconf_mutable DP_Key "$DP_Key" | |
439580b9 | 31 | |
9773342a | 32 | _debug "First detect the root zone" |
c7b16249 | 33 | if ! _get_root "$fulldomain"; then |
9773342a | 34 | _err "invalid domain" |
35 | return 1 | |
36 | fi | |
439580b9 | 37 | |
a6b6e31c | 38 | add_record "$_domain" "$_sub_domain" "$txtvalue" |
9773342a | 39 | |
9773342a | 40 | } |
41 | ||
f162ad19 | 42 | #fulldomain txtvalue |
5d6fd809 | 43 | dns_dp_rm() { |
44 | fulldomain=$1 | |
f162ad19 | 45 | txtvalue=$2 |
a6b6e31c | 46 | |
47 | DP_Id="${DP_Id:-$(_readaccountconf_mutable DP_Id)}" | |
48 | DP_Key="${DP_Key:-$(_readaccountconf_mutable DP_Key)}" | |
49 | ||
f162ad19 | 50 | _debug "First detect the root zone" |
51 | if ! _get_root "$fulldomain"; then | |
52 | _err "invalid domain" | |
53 | return 1 | |
54 | fi | |
55 | ||
20ba8202 | 56 | if ! _rest POST "Record.List" "login_token=$DP_Id,$DP_Key&format=json&lang=en&domain_id=$_domain_id&sub_domain=$_sub_domain"; then |
f162ad19 | 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 | ||
b9b2cd27 | 66 | record_id=$(echo "$response" | tr "{" "\n" | grep -- "$txtvalue" | grep '^"id"' | cut -d : -f 2 | cut -d '"' -f 2) |
f162ad19 | 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 | ||
20ba8202 | 73 | if ! _rest POST "Record.Remove" "login_token=$DP_Id,$DP_Key&format=json&lang=en&domain_id=$_domain_id&record_id=$record_id"; then |
f162ad19 | 74 | _err "Record.Remove error." |
75 | return 1 | |
76 | fi | |
77 | ||
20ba8202 | 78 | _contains "$response" "successful" |
5d6fd809 | 79 | |
80 | } | |
81 | ||
9773342a | 82 | #add the txt record. |
83 | #usage: root sub txtvalue | |
84 | add_record() { | |
85 | root=$1 | |
86 | sub=$2 | |
87 | txtvalue=$3 | |
c7b16249 | 88 | fulldomain="$sub.$root" |
439580b9 | 89 | |
9773342a | 90 | _info "Adding record" |
439580b9 | 91 | |
20ba8202 | 92 | if ! _rest POST "Record.Create" "login_token=$DP_Id,$DP_Key&format=json&lang=en&domain_id=$_domain_id&sub_domain=$_sub_domain&record_type=TXT&value=$txtvalue&record_line=默认"; then |
9773342a | 93 | return 1 |
94 | fi | |
439580b9 | 95 | |
20ba8202 | 96 | _contains "$response" "successful" || _contains "$response" "Domain record already exists" |
9773342a | 97 | } |
98 | ||
329174b6 | 99 | #################### Private functions below ################################## |
9773342a | 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 | |
c7b16249 | 109 | while true; do |
110 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
4c2a3841 | 111 | if [ -z "$h" ]; then |
9773342a | 112 | #not valid |
439580b9 | 113 | return 1 |
9773342a | 114 | fi |
439580b9 | 115 | |
20ba8202 | 116 | if ! _rest POST "Domain.Info" "login_token=$DP_Id,$DP_Key&format=json&lang=en&domain=$h"; then |
9773342a | 117 | return 1 |
118 | fi | |
439580b9 | 119 | |
20ba8202 | 120 | if _contains "$response" "successful"; then |
e440223b | 121 | _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \") |
9773342a | 122 | _debug _domain_id "$_domain_id" |
4c2a3841 | 123 | if [ "$_domain_id" ]; then |
c7b16249 | 124 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) |
125 | _debug _sub_domain "$_sub_domain" | |
126 | _domain="$h" | |
127 | _debug _domain "$_domain" | |
9773342a | 128 | return 0 |
129 | fi | |
130 | return 1 | |
131 | fi | |
c7b16249 | 132 | p="$i" |
133 | i=$(_math "$i" + 1) | |
9773342a | 134 | done |
135 | return 1 | |
136 | } | |
137 | ||
9773342a | 138 | #Usage: method URI data |
139 | _rest() { | |
671a6994 | 140 | m="$1" |
9773342a | 141 | ep="$2" |
a4270efa | 142 | data="$3" |
c7b16249 | 143 | _debug "$ep" |
9773342a | 144 | url="$REST_API/$ep" |
439580b9 | 145 | |
9773342a | 146 | _debug url "$url" |
439580b9 | 147 | |
671a6994 | 148 | if [ "$m" = "GET" ]; then |
149 | response="$(_get "$url" | tr -d '\r')" | |
150 | else | |
a63b05a9 | 151 | _debug2 data "$data" |
7d1c5fca | 152 | response="$(_post "$data" "$url" | tr -d '\r')" |
9773342a | 153 | fi |
439580b9 | 154 | |
4c2a3841 | 155 | if [ "$?" != "0" ]; then |
9773342a | 156 | _err "error $ep" |
157 | return 1 | |
158 | fi | |
a63b05a9 | 159 | _debug2 response "$response" |
9773342a | 160 | return 0 |
161 | } |