You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.0 KiB

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