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.

105 lines
2.7 KiB

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