dns_dp.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. DP_Id="${DP_Id:-$(_readaccountconf_mutable DP_Id)}"
  14. DP_Key="${DP_Key:-$(_readaccountconf_mutable DP_Key)}"
  15. if [ -z "$DP_Id" ] || [ -z "$DP_Key" ]; then
  16. DP_Id=""
  17. DP_Key=""
  18. _err "You don't specify dnspod api key and key id yet."
  19. _err "Please create you key and try again."
  20. return 1
  21. fi
  22. #save the api key and email to the account conf file.
  23. _saveaccountconf_mutable DP_Id "$DP_Id"
  24. _saveaccountconf_mutable 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. add_record "$_domain" "$_sub_domain" "$txtvalue"
  31. }
  32. #fulldomain txtvalue
  33. dns_dp_rm() {
  34. fulldomain=$1
  35. txtvalue=$2
  36. DP_Id="${DP_Id:-$(_readaccountconf_mutable DP_Id)}"
  37. DP_Key="${DP_Key:-$(_readaccountconf_mutable DP_Key)}"
  38. _debug "First detect the root zone"
  39. if ! _get_root "$fulldomain"; then
  40. _err "invalid domain"
  41. return 1
  42. fi
  43. if ! _rest POST "Record.List" "login_token=$DP_Id,$DP_Key&format=json&domain_id=$_domain_id&sub_domain=$_sub_domain"; then
  44. _err "Record.Lis error."
  45. return 1
  46. fi
  47. if _contains "$response" 'No records'; then
  48. _info "Don't need to remove."
  49. return 0
  50. fi
  51. record_id=$(echo "$response" | tr "{" "\n" | grep "$txtvalue" | grep '^"id"' | cut -d : -f 2 | cut -d '"' -f 2)
  52. _debug record_id "$record_id"
  53. if [ -z "$record_id" ]; then
  54. _err "Can not get record id."
  55. return 1
  56. fi
  57. if ! _rest POST "Record.Remove" "login_token=$DP_Id,$DP_Key&format=json&domain_id=$_domain_id&record_id=$record_id"; then
  58. _err "Record.Remove error."
  59. return 1
  60. fi
  61. _contains "$response" "Action completed successful"
  62. }
  63. #add the txt record.
  64. #usage: root sub txtvalue
  65. add_record() {
  66. root=$1
  67. sub=$2
  68. txtvalue=$3
  69. fulldomain="$sub.$root"
  70. _info "Adding record"
  71. 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
  72. return 1
  73. fi
  74. _contains "$response" "Action completed successful" || _contains "$response" "Domain record already exists"
  75. }
  76. #################### Private functions below ##################################
  77. #_acme-challenge.www.domain.com
  78. #returns
  79. # _sub_domain=_acme-challenge.www
  80. # _domain=domain.com
  81. # _domain_id=sdjkglgdfewsdfg
  82. _get_root() {
  83. domain=$1
  84. i=2
  85. p=1
  86. while true; do
  87. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  88. if [ -z "$h" ]; then
  89. #not valid
  90. return 1
  91. fi
  92. if ! _rest POST "Domain.Info" "login_token=$DP_Id,$DP_Key&format=json&domain=$h"; then
  93. return 1
  94. fi
  95. if _contains "$response" "Action completed successful"; then
  96. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
  97. _debug _domain_id "$_domain_id"
  98. if [ "$_domain_id" ]; then
  99. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  100. _debug _sub_domain "$_sub_domain"
  101. _domain="$h"
  102. _debug _domain "$_domain"
  103. return 0
  104. fi
  105. return 1
  106. fi
  107. p="$i"
  108. i=$(_math "$i" + 1)
  109. done
  110. return 1
  111. }
  112. #Usage: method URI data
  113. _rest() {
  114. m="$1"
  115. ep="$2"
  116. data="$3"
  117. _debug "$ep"
  118. url="$REST_API/$ep"
  119. _debug url "$url"
  120. if [ "$m" = "GET" ]; then
  121. response="$(_get "$url" | tr -d '\r')"
  122. else
  123. _debug2 data "$data"
  124. response="$(_post "$data" "$url" | tr -d '\r')"
  125. fi
  126. if [ "$?" != "0" ]; then
  127. _err "error $ep"
  128. return 1
  129. fi
  130. _debug2 response "$response"
  131. return 0
  132. }