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.

187 lines
4.7 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. ## install jq ##
  7. # Check the operating system
  8. if [ "$(uname)" = "Darwin" ]; then
  9. # macOS - install jq using Homebrew
  10. if ! command -v brew >/dev/null 2>&1; then
  11. echo "Error: Homebrew is not installed. Please install Homebrew first." >&2
  12. exit 1
  13. fi
  14. brew install jq
  15. elif [ -f "/etc/redhat-release" ] || [ -f "/etc/centos-release" ] || [ -f "/etc/fedora-release" ]; then
  16. # RedHat/CentOS/Fedora - install jq using yum or dnf
  17. if command -v dnf >/dev/null 2>&1; then
  18. dnf install -y jq
  19. elif command -v yum >/dev/null 2>&1; then
  20. yum install -y jq
  21. else
  22. echo "Error: Neither yum nor dnf package manager found." >&2
  23. exit 1
  24. fi
  25. elif [ -f "/etc/lsb-release" ] || [ -f "/etc/debian_version" ]; then
  26. # Debian/Ubuntu - install jq using apt
  27. if command -v apt >/dev/null 2>&1; then
  28. apt update
  29. apt install -y jq
  30. else
  31. echo "Error: apt package manager not found." >&2
  32. exit 1
  33. fi
  34. else
  35. echo "Error: Unsupported operating system." >&2
  36. exit 1
  37. fi
  38. # jq installed successfully
  39. echo "jq installed successfully."
  40. #######################################################
  41. FORNEX_API_URL="https://fornex.com/api/dns/domain"
  42. ######## Public functions ###########################
  43. # Usage: dns_fornex_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  44. dns_fornex_add() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. if ! _Fornex_API; then
  48. return 1
  49. fi
  50. domain=$(echo "$fulldomain" | sed 's/^\*\.//')
  51. if ! _get_domain_id "$domain"; then
  52. _err "Unable to determine domain ID"
  53. return 1
  54. else
  55. _debug _domain_id "$_domain_id"
  56. fi
  57. _info "Adding TXT record for $fulldomain"
  58. # Add the TXT record
  59. if ! _rest POST "$domain/entry_set/" "type=TXT&host=_acme-challenge&value=$txtvalue"; then
  60. _err "Failed to add TXT record"
  61. return 1
  62. fi
  63. _info "TXT record added successfully"
  64. return 0
  65. }
  66. dns_fornex_rm() {
  67. fulldomain=$1
  68. if ! _Fornex_API; then
  69. return 1
  70. fi
  71. domain=$(echo "$fulldomain" | sed 's/^\*\.//')
  72. if ! _get_domain_id "$domain"; then
  73. _err "Unable to determine domain ID"
  74. return 1
  75. else
  76. _debug _domain_id "$_domain_id"
  77. fi
  78. _info "Removing TXT records for domain: _acme-challenge.$domain"
  79. response=$(curl -X GET -H "Authorization: Api-Key $FORNEX_API_KEY" "https://fornex.com/api/dns/domain/$domain/entry_set/")
  80. # Extract TXT record IDs using jq
  81. txt_ids=$(echo "$response" | jq -r '.[] | select(.type == "TXT") | .id')
  82. if [ -z "$txt_ids" ]; then
  83. _info "No TXT records found for domain: _acme-challenge.$domain"
  84. return 0
  85. fi
  86. for txt_id in $txt_ids; do
  87. _info "Removing TXT record with ID: $txt_id"
  88. if ! curl -X DELETE -H "Authorization: Api-Key $FORNEX_API_KEY" "https://fornex.com/api/dns/domain/$domain/entry_set/$txt_id/"; then
  89. _err "Failed to remove TXT record with ID: $txt_id"
  90. else
  91. _info "TXT record with ID $txt_id removed successfully"
  92. fi
  93. done
  94. return 0
  95. }
  96. #################### Private functions below ##################################
  97. # _acme-challenge.www.domain.com
  98. # returns
  99. # _sub_domain=_acme-challenge.www
  100. # _domain=domain.com
  101. _get_domain_id() {
  102. domain=$1
  103. _debug "Getting domain ID for $domain"
  104. if echo "$domain" | grep -q "_acme-challenge"; then
  105. # If yes, remove "_acme-challenge" from the domain name
  106. domain=$(echo "$domain" | sed 's/_acme-challenge\.//')
  107. fi
  108. if ! _rest GET "$domain/entry_set/"; then
  109. _err "Failed to get domain ID for $domain"
  110. return 1
  111. fi
  112. _domain_id="$response"
  113. _debug "Domain ID for $domain is $_domain_id"
  114. return 0
  115. }
  116. _Fornex_API() {
  117. FORNEX_API_KEY="${FORNEX_API_KEY:-$(_readaccountconf_mutable FORNEX_API_KEY)}"
  118. if [ -z "$FORNEX_API_KEY" ]; then
  119. FORNEX_API_KEY=""
  120. _err "You didn't specify the Fornex API key yet."
  121. _err "Please create your key and try again."
  122. return 1
  123. fi
  124. _saveaccountconf_mutable FORNEX_API_KEY "$FORNEX_API_KEY"
  125. }
  126. # method method action data
  127. _rest() {
  128. m=$1
  129. ep="$2"
  130. data="$3"
  131. _debug "$ep"
  132. export _H1="Accept: application/json"
  133. export _H2="Authorization: Api-Key $FORNEX_API_KEY"
  134. if [ "$m" != "GET" ]; then
  135. _debug data "$data"
  136. url="$FORNEX_API_URL/$ep"
  137. response=$(curl -X "$m" -H "Authorization: Api-Key $FORNEX_API_KEY" -d "$data" "$url")
  138. else
  139. url="$FORNEX_API_URL/$ep"
  140. response=$(curl -X GET -H "Authorization: Api-Key $FORNEX_API_KEY" "$url")
  141. fi
  142. _ret="$?"
  143. if [ "$_ret" != "0" ]; then
  144. _err "error $ep"
  145. return 1
  146. fi
  147. _debug2 response "$response"
  148. return 0
  149. }