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.

141 lines
3.3 KiB

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