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.

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