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.

109 lines
2.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_west_cn_info='West.cn
  4. Site: West.cn
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_west_cn
  6. Options:
  7. WEST_Username API username
  8. WEST_Key API Key. Set at https://www.west.cn/manager/API/APIconfig.asp
  9. Issues: github.com/acmesh-official/acme.sh/issues/4894
  10. '
  11. REST_API="https://api.west.cn/API/v2"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_west_cn_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. WEST_Username="${WEST_Username:-$(_readaccountconf_mutable WEST_Username)}"
  18. WEST_Key="${WEST_Key:-$(_readaccountconf_mutable WEST_Key)}"
  19. if [ -z "$WEST_Username" ] || [ -z "$WEST_Key" ]; then
  20. WEST_Username=""
  21. WEST_Key=""
  22. _err "You don't specify west api key and username yet."
  23. _err "Please set you key and try again."
  24. return 1
  25. fi
  26. #save the api key and email to the account conf file.
  27. _saveaccountconf_mutable WEST_Username "$WEST_Username"
  28. _saveaccountconf_mutable WEST_Key "$WEST_Key"
  29. add_record "$fulldomain" "$txtvalue"
  30. }
  31. #Usage: rm _acme-challenge.www.domain.com
  32. dns_west_cn_rm() {
  33. fulldomain=$1
  34. txtvalue=$2
  35. WEST_Username="${WEST_Username:-$(_readaccountconf_mutable WEST_Username)}"
  36. WEST_Key="${WEST_Key:-$(_readaccountconf_mutable WEST_Key)}"
  37. if ! _rest POST "domain/dns/" "act=dnsrec.list&username=$WEST_Username&apikey=$WEST_Key&domain=$fulldomain&hostname=$fulldomain&record_type=TXT"; then
  38. _err "dnsrec.list error."
  39. return 1
  40. fi
  41. if _contains "$response" 'no records'; then
  42. _info "Don't need to remove."
  43. return 0
  44. fi
  45. record_id=$(echo "$response" | tr "{" "\n" | grep -- "$txtvalue" | grep '^"record_id"' | cut -d : -f 2 | cut -d ',' -f 1)
  46. _debug record_id "$record_id"
  47. if [ -z "$record_id" ]; then
  48. _err "Can not get record id."
  49. return 1
  50. fi
  51. if ! _rest POST "domain/dns/" "act=dnsrec.remove&username=$WEST_Username&apikey=$WEST_Key&domain=$fulldomain&hostname=$fulldomain&record_id=$record_id"; then
  52. _err "dnsrec.remove error."
  53. return 1
  54. fi
  55. _contains "$response" "success"
  56. }
  57. #add the txt record.
  58. #usage: add fulldomain txtvalue
  59. add_record() {
  60. fulldomain=$1
  61. txtvalue=$2
  62. _info "Adding record"
  63. if ! _rest POST "domain/dns/" "act=dnsrec.add&username=$WEST_Username&apikey=$WEST_Key&domain=$fulldomain&hostname=$fulldomain&record_type=TXT&record_value=$txtvalue"; then
  64. return 1
  65. fi
  66. _contains "$response" "success"
  67. }
  68. #Usage: method URI data
  69. _rest() {
  70. m="$1"
  71. ep="$2"
  72. data="$3"
  73. _debug "$ep"
  74. url="$REST_API/$ep"
  75. _debug url "$url"
  76. if [ "$m" = "GET" ]; then
  77. response="$(_get "$url" | tr -d '\r')"
  78. else
  79. _debug2 data "$data"
  80. response="$(_post "$data" "$url" | tr -d '\r')"
  81. fi
  82. if [ "$?" != "0" ]; then
  83. _err "error $ep"
  84. return 1
  85. fi
  86. _debug2 response "$response"
  87. return 0
  88. }