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.

106 lines
2.7 KiB

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