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.

222 lines
4.9 KiB

9 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 '<type>TXT</type>' | wc -l)
  86. record_id=$(printf "%s" "$response" | grep '^<id>' | tail -1 | cut -d '>' -f 2 | cut -d '<' -f 1)
  87. return 0
  88. else
  89. _err "get existing records error."
  90. return 1
  91. fi
  92. count=0
  93. }
  94. #add the txt record.
  95. #usage: root sub txtvalue
  96. add_record() {
  97. root=$1
  98. sub=$2
  99. txtvalue=$3
  100. fulldomain="$sub.$root"
  101. _info "Adding record"
  102. 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
  103. return 1
  104. fi
  105. if _contains "$response" "Action completed successful"; then
  106. return 0
  107. fi
  108. return 1 #error
  109. }
  110. #update the txt record
  111. #Usage: root sub txtvalue
  112. update_record() {
  113. root=$1
  114. sub=$2
  115. txtvalue=$3
  116. fulldomain="$sub.$root"
  117. _info "Updating record"
  118. 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
  119. return 1
  120. fi
  121. if _contains "$response" "Action completed successful"; then
  122. return 0
  123. fi
  124. return 1 #error
  125. }
  126. #################### Private functions bellow ##################################
  127. #_acme-challenge.www.domain.com
  128. #returns
  129. # _sub_domain=_acme-challenge.www
  130. # _domain=domain.com
  131. # _domain_id=sdjkglgdfewsdfg
  132. _get_root() {
  133. domain=$1
  134. i=2
  135. p=1
  136. while true; do
  137. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  138. if [ -z "$h" ]; then
  139. #not valid
  140. return 1
  141. fi
  142. if ! _rest POST "Domain.Info" "login_token=$DP_Id,$DP_Key&format=json&domain=$h"; then
  143. return 1
  144. fi
  145. if _contains "$response" "Action completed successful"; then
  146. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  147. _debug _domain_id "$_domain_id"
  148. if [ "$_domain_id" ]; then
  149. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  150. _debug _sub_domain "$_sub_domain"
  151. _domain="$h"
  152. _debug _domain "$_domain"
  153. return 0
  154. fi
  155. return 1
  156. fi
  157. p="$i"
  158. i=$(_math "$i" + 1)
  159. done
  160. return 1
  161. }
  162. #Usage: method URI data
  163. _rest() {
  164. m=$1
  165. ep="$2"
  166. data="$3"
  167. _debug "$ep"
  168. url="$REST_API/$ep"
  169. _debug url "$url"
  170. if [ "$data" ]; then
  171. _debug2 data "$data"
  172. response="$(_post "$data" "$url")"
  173. else
  174. response="$(_get "$url")"
  175. fi
  176. if [ "$?" != "0" ]; then
  177. _err "error $ep"
  178. return 1
  179. fi
  180. _debug2 response "$response"
  181. return 0
  182. }