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.

195 lines
4.1 KiB

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