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.

162 lines
4.5 KiB

2 months ago
2 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_veesp_info='veesp.com
  4. Site: veesp.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_veesp
  6. Options:
  7. VEESP_User Username
  8. VEESP_Password Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/3712
  10. Author: <stepan@plyask.in>
  11. '
  12. VEESP_Api="https://secure.veesp.com/api"
  13. ######## Public functions #####################
  14. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_veesp_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. VEESP_Password="${VEESP_Password:-$(_readaccountconf_mutable VEESP_Password)}"
  19. VEESP_User="${VEESP_User:-$(_readaccountconf_mutable VEESP_User)}"
  20. VEESP_auth=$(printf "%s" "$VEESP_User:$VEESP_Password" | _base64)
  21. if [ -z "$VEESP_Password" ] || [ -z "$VEESP_User" ]; then
  22. VEESP_Password=""
  23. VEESP_User=""
  24. _err "You don't specify veesp api key and email yet."
  25. _err "Please create you key and try again."
  26. return 1
  27. fi
  28. #save the api key and email to the account conf file.
  29. _saveaccountconf_mutable VEESP_Password "$VEESP_Password"
  30. _saveaccountconf_mutable VEESP_User "$VEESP_User"
  31. _debug "First detect the root zone"
  32. if ! _get_root "$fulldomain"; then
  33. _err "invalid domain"
  34. return 1
  35. fi
  36. _debug _domain_id "$_domain_id"
  37. _debug _sub_domain "$_sub_domain"
  38. _debug _domain "$_domain"
  39. _info "Adding record"
  40. if VEESP_rest POST "service/$_service_id/dns/$_domain_id/records" "{\"name\":\"$fulldomain\",\"ttl\":1,\"priority\":0,\"type\":\"TXT\",\"content\":\"$txtvalue\"}"; then
  41. if _contains "$response" "\"success\":true"; then
  42. _info "Added"
  43. #todo: check if the record takes effect
  44. return 0
  45. else
  46. _err "Add txt record error."
  47. return 1
  48. fi
  49. fi
  50. }
  51. # Usage: fulldomain txtvalue
  52. # Used to remove the txt record after validation
  53. dns_veesp_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. VEESP_Password="${VEESP_Password:-$(_readaccountconf_mutable VEESP_Password)}"
  57. VEESP_User="${VEESP_User:-$(_readaccountconf_mutable VEESP_User)}"
  58. VEESP_auth=$(printf "%s" "$VEESP_User:$VEESP_Password" | _base64)
  59. _debug "First detect the root zone"
  60. if ! _get_root "$fulldomain"; then
  61. _err "invalid domain"
  62. return 1
  63. fi
  64. _debug _domain_id "$_domain_id"
  65. _debug _sub_domain "$_sub_domain"
  66. _debug _domain "$_domain"
  67. _debug "Getting txt records"
  68. VEESP_rest GET "service/$_service_id/dns/$_domain_id"
  69. count=$(printf "%s\n" "$response" | _egrep_o "\"type\":\"TXT\",\"content\":\".\"$txtvalue.\"\"" | wc -l | tr -d " ")
  70. _debug count "$count"
  71. if [ "$count" = "0" ]; then
  72. _info "Don't need to remove."
  73. else
  74. record_id=$(printf "%s\n" "$response" | _egrep_o "{\"id\":[^}]*\"type\":\"TXT\",\"content\":\".\"$txtvalue.\"\"" | cut -d\" -f4)
  75. _debug "record_id" "$record_id"
  76. if [ -z "$record_id" ]; then
  77. _err "Can not get record id to remove."
  78. return 1
  79. fi
  80. if ! VEESP_rest DELETE "service/$_service_id/dns/$_domain_id/records/$record_id"; then
  81. _err "Delete record error."
  82. return 1
  83. fi
  84. _contains "$response" "\"success\":true"
  85. fi
  86. }
  87. #################### Private functions below ##################################
  88. #_acme-challenge.www.domain.com
  89. #returns
  90. # _sub_domain=_acme-challenge.www
  91. # _domain=domain.com
  92. # _domain_id=sdjkglgdfewsdfg
  93. _get_root() {
  94. domain=$1
  95. i=2
  96. p=1
  97. if ! VEESP_rest GET "dns"; then
  98. return 1
  99. fi
  100. while true; do
  101. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  102. _debug h "$h"
  103. if [ -z "$h" ]; then
  104. #not valid
  105. return 1
  106. fi
  107. if _contains "$response" "\"name\":\"$h\""; then
  108. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"domain_id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1 | cut -d '"' -f 2)
  109. _debug _domain_id "$_domain_id"
  110. _service_id=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$h\",\"service_id\":[^}]*" | cut -d : -f 3 | cut -d '"' -f 2)
  111. _debug _service_id "$_service_id"
  112. if [ "$_domain_id" ]; then
  113. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  114. _domain="$h"
  115. return 0
  116. fi
  117. return 1
  118. fi
  119. p=$i
  120. i=$(_math "$i" + 1)
  121. done
  122. return 1
  123. }
  124. VEESP_rest() {
  125. m=$1
  126. ep="$2"
  127. data="$3"
  128. _debug "$ep"
  129. export _H1="Accept: application/json"
  130. export _H2="Authorization: Basic $VEESP_auth"
  131. if [ "$m" != "GET" ]; then
  132. _debug data "$data"
  133. export _H3="Content-Type: application/json"
  134. response="$(_post "$data" "$VEESP_Api/$ep" "" "$m")"
  135. else
  136. response="$(_get "$VEESP_Api/$ep")"
  137. fi
  138. if [ "$?" != "0" ]; then
  139. _err "error $ep"
  140. return 1
  141. fi
  142. _debug2 response "$response"
  143. return 0
  144. }