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.

146 lines
3.5 KiB

  1. #!/usr/bin/env sh
  2. # Author: @SBohomolov <noc@fornex.com>
  3. # Site: Fornex.com
  4. # Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_fornex
  5. # Bugs: https://github.com/acmesh-official/acme.sh/issues/5161
  6. FORNEX_API_URL="https://fornex.com/api/dns/domain"
  7. ######## Public functions #####################
  8. #Usage: dns_fornex_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_fornex_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. if ! _Fornex_API; then
  13. return 1
  14. fi
  15. domain=$(echo "$fulldomain" | sed 's/^\*\.//')
  16. if ! _get_domain_id "$domain"; then
  17. _err "Unable to determine domain ID"
  18. return 1
  19. else
  20. _debug _domain_id "$_domain_id"
  21. fi
  22. _info "Adding TXT record for $fulldomain"
  23. # Add the TXT record
  24. if ! _rest POST "$domain/entry_set/" "type=TXT&host=_acme-challenge&value=$txtvalue"; then
  25. _err "Failed to add TXT record"
  26. return 1
  27. fi
  28. _info "TXT record added successfully"
  29. return 0
  30. }
  31. dns_fornex_rm() {
  32. fulldomain=$1
  33. if ! _Fornex_API; then
  34. return 1
  35. fi
  36. domain=$(echo "$fulldomain" | sed 's/^\*\.//')
  37. if ! _get_domain_id "$domain"; then
  38. _err "Unable to determine domain ID"
  39. return 1
  40. else
  41. _debug _domain_id "$_domain_id"
  42. fi
  43. _info "Removing TXT records for domain: _acme-challenge.$domain"
  44. txt_ids=$(curl -X GET -H "Authorization: Api-Key $FORNEX_API_KEY" "https://fornex.com/api/dns/domain/$domain/entry_set/" | jq -r '.[] | select(.type == "TXT") | .id')
  45. if [ -z "$txt_ids" ]; then
  46. _info "No TXT records found for domain: _acme-challenge.$domain"
  47. return 0
  48. fi
  49. for txt_id in $txt_ids; do
  50. _info "Removing TXT record with ID: $txt_id"
  51. if ! _rest DELETE "$domain/entry_set/$txt_id"; then
  52. _err "Failed to remove TXT record with ID: $txt_id"
  53. else
  54. _info "TXT record with ID $txt_id removed successfully"
  55. fi
  56. done
  57. return 0
  58. }
  59. #################### Private functions below ##################################
  60. #_acme-challenge.www.domain.com
  61. #returns
  62. # _sub_domain=_acme-challenge.www
  63. # _domain=domain.com
  64. _get_domain_id() {
  65. domain=$1
  66. _debug "Getting domain ID for $domain"
  67. if echo "$domain" | grep -q "_acme-challenge"; then
  68. # If yes, remove "_acme-challenge" from the domain name
  69. domain=$(echo "$domain" | sed 's/_acme-challenge\.//')
  70. fi
  71. if ! _rest GET "$domain/entry_set/"; then
  72. _err "Failed to get domain ID for $domain"
  73. return 1
  74. fi
  75. _domain_id="$response"
  76. _debug "Domain ID for $domain is $_domain_id"
  77. return 0
  78. }
  79. _Fornex_API() {
  80. FORNEX_API_KEY="${FORNEX_API_KEY:-$(_readaccountconf_mutable FORNEX_API_KEY)}"
  81. if [ -z "$FORNEX_API_KEY" ]; then
  82. FORNEX_API_KEY=""
  83. _err "You didn't specify the Fornex API key yet."
  84. _err "Please create your key and try again."
  85. return 1
  86. fi
  87. _saveaccountconf_mutable FORNEX_API_KEY "$FORNEX_API_KEY"
  88. }
  89. #method method action data
  90. _rest() {
  91. m=$1
  92. ep="$2"
  93. data="$3"
  94. _debug "$ep"
  95. export _H1="Accept: application/json"
  96. export _H2="Authorization: Api-Key $FORNEX_API_KEY"
  97. if [ "$m" != "GET" ]; then
  98. _debug data "$data"
  99. url="$FORNEX_API_URL/$ep"
  100. echo "curl -X $m -H 'Authorization: Api-Key $FORNEX_API_KEY' -d '$data' \"$url\""
  101. response="$(_post "$data" "$url" "" "$m")"
  102. else
  103. echo "curl -X GET -H 'Authorization: Api-Key $FORNEX_API_KEY' $FORNEX_API_URL/$ep"
  104. response="$(_get "$FORNEX_API_URL/$ep" | _normalizeJson)"
  105. fi
  106. _ret="$?"
  107. if [ "$_ret" != "0" ]; then
  108. _err "error $ep"
  109. return 1
  110. fi
  111. _debug2 response "$response"
  112. return 0
  113. }