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.

163 lines
3.8 KiB

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