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.

207 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. _err "You don't specify dnspod api key and key id yet."
  16. _err "Please create you key and try again."
  17. return 1
  18. fi
  19. REST_API=$DP_Api
  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
  41. dns_dp_rm() {
  42. fulldomain=$1
  43. }
  44. #usage: root sub
  45. #return if the sub record already exists.
  46. #echos the existing records count.
  47. # '0' means doesn't exist
  48. existing_records() {
  49. _debug "Getting txt records"
  50. root=$1
  51. sub=$2
  52. if ! _rest POST "Record.List" "login_token=$DP_Id,$DP_Key&domain_id=$_domain_id&sub_domain=$_sub_domain"; then
  53. return 1
  54. fi
  55. if printf "$response" | grep 'No records' ; then
  56. count=0;
  57. return 0
  58. fi
  59. if printf "$response" | grep "Action completed successful" >/dev/null ; then
  60. count=$(printf "$response" | grep '<type>TXT</type>' | wc -l)
  61. record_id=$(printf "$response" | grep '^<id>' | tail -1 | cut -d '>' -f 2 | cut -d '<' -f 1)
  62. return 0
  63. else
  64. _err "get existing records error."
  65. return 1
  66. fi
  67. count=0
  68. }
  69. #add the txt record.
  70. #usage: root sub txtvalue
  71. add_record() {
  72. root=$1
  73. sub=$2
  74. txtvalue=$3
  75. fulldomain=$sub.$root
  76. _info "Adding record"
  77. 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
  78. return 1
  79. fi
  80. if printf "$response" | grep "Action completed successful" ; then
  81. return 0
  82. fi
  83. return 1 #error
  84. }
  85. #update the txt record
  86. #Usage: root sub txtvalue
  87. update_record() {
  88. root=$1
  89. sub=$2
  90. txtvalue=$3
  91. fulldomain=$sub.$root
  92. _info "Updating record"
  93. 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
  94. return 1
  95. fi
  96. if printf "$response" | grep "Action completed successful" ; then
  97. return 0
  98. fi
  99. return 1 #error
  100. }
  101. #################### Private functions bellow ##################################
  102. #_acme-challenge.www.domain.com
  103. #returns
  104. # _sub_domain=_acme-challenge.www
  105. # _domain=domain.com
  106. # _domain_id=sdjkglgdfewsdfg
  107. _get_root() {
  108. domain=$1
  109. i=2
  110. p=1
  111. while [ '1' ]; do
  112. h=$(printf $domain | cut -d . -f $i-100)
  113. if [ -z "$h" ]; then
  114. #not valid
  115. return 1;
  116. fi
  117. if ! _rest POST "Domain.Info" "login_token=$DP_Id,$DP_Key&format=json&domain=$h"; then
  118. return 1
  119. fi
  120. if printf "$response" | grep "Action completed successful" >/dev/null ; 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 $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=$(expr $i + 1)
  134. done
  135. return 1
  136. }
  137. #Usage: method URI data
  138. _rest() {
  139. m=$1
  140. ep="$2"
  141. data="$3"
  142. _debug $ep
  143. url="$REST_API/$ep"
  144. _debug url "$url"
  145. if [ "$data" ]; then
  146. _debug2 data "$data"
  147. response="$(_post $data "$url")"
  148. else
  149. response="$(_get "$url")"
  150. fi
  151. if [ "$?" != "0" ]; then
  152. _err "error $ep"
  153. return 1
  154. fi
  155. _debug2 response "$response"
  156. return 0
  157. }