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.

191 lines
5.1 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_gcore_info='Gcore.com
  4. Site: Gcore.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_gcore
  6. Options:
  7. GCORE_Key API Key
  8. Issues: github.com/acmesh-official/acme.sh/issues/4460
  9. '
  10. GCORE_Api="https://api.gcore.com/dns/v2"
  11. GCORE_Doc="https://api.gcore.com/docs/dns"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_gcore_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. GCORE_Key="${GCORE_Key:-$(_readaccountconf_mutable GCORE_Key)}"
  18. if [ -z "$GCORE_Key" ]; then
  19. GCORE_Key=""
  20. _err "You didn't specify a Gcore api key yet."
  21. _err "You can get yours from here $GCORE_Doc"
  22. return 1
  23. fi
  24. #save the api key to the account conf file.
  25. _saveaccountconf_mutable GCORE_Key "$GCORE_Key"
  26. _debug "First detect the zone name"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug _zone_name "$_zone_name"
  32. _debug _sub_domain "$_sub_domain"
  33. _debug _domain "$_domain"
  34. _debug "Getting txt records"
  35. _gcore_rest GET "zones/$_zone_name/$fulldomain/TXT"
  36. payload=""
  37. if echo "$response" | grep "record is not found" >/dev/null; then
  38. _info "Record doesn't exists"
  39. payload="{\"resource_records\":[{\"content\":[\"$txtvalue\"],\"enabled\":true}],\"ttl\":120}"
  40. elif echo "$response" | grep "$txtvalue" >/dev/null; then
  41. _info "Already exists, OK"
  42. return 0
  43. elif echo "$response" | tr -d " " | grep \"name\":\""$fulldomain"\",\"type\":\"TXT\" >/dev/null; then
  44. _info "Record with mismatch txtvalue, try update it"
  45. payload=$(echo "$response" | tr -d " " | sed 's/"updated_at":[0-9]\+,//g' | sed 's/"meta":{}}]}/"meta":{}},{"content":['\""$txtvalue"\"'],"enabled":true}]}/')
  46. fi
  47. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  48. # we can not use updating anymore.
  49. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  50. # _debug count "$count"
  51. # if [ "$count" = "0" ]; then
  52. _info "Adding record"
  53. if _gcore_rest PUT "zones/$_zone_name/$fulldomain/TXT" "$payload"; then
  54. if _contains "$response" "$txtvalue"; then
  55. _info "Added, OK"
  56. return 0
  57. elif _contains "$response" "rrset is already exists"; then
  58. _info "Already exists, OK"
  59. return 0
  60. else
  61. _err "Add txt record error."
  62. return 1
  63. fi
  64. fi
  65. _err "Add txt record error."
  66. return 1
  67. }
  68. #fulldomain txtvalue
  69. dns_gcore_rm() {
  70. fulldomain=$1
  71. txtvalue=$2
  72. GCORE_Key="${GCORE_Key:-$(_readaccountconf_mutable GCORE_Key)}"
  73. _debug "First detect the root zone"
  74. if ! _get_root "$fulldomain"; then
  75. _err "invalid domain"
  76. return 1
  77. fi
  78. _debug _zone_name "$_zone_name"
  79. _debug _sub_domain "$_sub_domain"
  80. _debug _domain "$_domain"
  81. _debug "Getting txt records"
  82. _gcore_rest GET "zones/$_zone_name/$fulldomain/TXT"
  83. if echo "$response" | grep "record is not found" >/dev/null; then
  84. _info "No such txt recrod"
  85. return 0
  86. fi
  87. if ! echo "$response" | tr -d " " | grep \"name\":\""$fulldomain"\",\"type\":\"TXT\" >/dev/null; then
  88. _err "Error: $response"
  89. return 1
  90. fi
  91. if ! echo "$response" | tr -d " " | grep \""$txtvalue"\" >/dev/null; then
  92. _info "No such txt recrod"
  93. return 0
  94. fi
  95. count="$(echo "$response" | grep -o "content" | wc -l)"
  96. if [ "$count" = "1" ]; then
  97. if ! _gcore_rest DELETE "zones/$_zone_name/$fulldomain/TXT"; then
  98. _err "Delete record error. $response"
  99. return 1
  100. fi
  101. return 0
  102. fi
  103. payload="$(echo "$response" | tr -d " " | sed 's/"updated_at":[0-9]\+,//g' | sed 's/{"id":[0-9]\+,"content":\["'"$txtvalue"'"\],"enabled":true,"meta":{}}//' | sed 's/\[,/\[/' | sed 's/,,/,/' | sed 's/,\]/\]/')"
  104. if ! _gcore_rest PUT "zones/$_zone_name/$fulldomain/TXT" "$payload"; then
  105. _err "Delete record error. $response"
  106. fi
  107. }
  108. #################### Private functions below ##################################
  109. #_acme-challenge.sub.domain.com
  110. #returns
  111. # _sub_domain=_acme-challenge.sub or _acme-challenge
  112. # _domain=domain.com
  113. # _zone_name=domain.com or sub.domain.com
  114. _get_root() {
  115. domain=$1
  116. i=1
  117. p=1
  118. while true; do
  119. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  120. _debug h "$h"
  121. if [ -z "$h" ]; then
  122. #not valid
  123. return 1
  124. fi
  125. if ! _gcore_rest GET "zones/$h"; then
  126. return 1
  127. fi
  128. if _contains "$response" "\"name\":\"$h\""; then
  129. _zone_name=$h
  130. if [ "$_zone_name" ]; then
  131. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  132. _domain=$h
  133. return 0
  134. fi
  135. return 1
  136. fi
  137. p=$i
  138. i=$(_math "$i" + 1)
  139. done
  140. return 1
  141. }
  142. _gcore_rest() {
  143. m=$1
  144. ep="$2"
  145. data="$3"
  146. _debug "$ep"
  147. key_trimmed=$(echo "$GCORE_Key" | tr -d '"')
  148. export _H1="Content-Type: application/json"
  149. export _H2="Authorization: APIKey $key_trimmed"
  150. if [ "$m" != "GET" ]; then
  151. _debug data "$data"
  152. response="$(_post "$data" "$GCORE_Api/$ep" "" "$m")"
  153. else
  154. response="$(_get "$GCORE_Api/$ep")"
  155. fi
  156. if [ "$?" != "0" ]; then
  157. _err "error $ep"
  158. return 1
  159. fi
  160. _debug2 response "$response"
  161. return 0
  162. }