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.

153 lines
3.3 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_fornex_info='Fornex.com
  4. Site: Fornex.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_fornex
  6. Options:
  7. FORNEX_API_KEY API Key
  8. Issues: github.com/acmesh-official/acme.sh/issues/3998
  9. Author: Timur Umarov <inbox@tumarov.com>
  10. '
  11. FORNEX_API_URL="https://fornex.com/api"
  12. ######## Public functions #####################
  13. #Usage: dns_fornex_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_fornex_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if ! _Fornex_API; then
  18. return 1
  19. fi
  20. if ! _get_root "$fulldomain"; then
  21. _err "Unable to determine root domain"
  22. return 1
  23. else
  24. _debug _domain "$_domain"
  25. fi
  26. _info "Adding record"
  27. if _rest POST "dns/domain/$_domain/entry_set/" "{\"host\" : \"${fulldomain}\" , \"type\" : \"TXT\" , \"value\" : \"${txtvalue}\" , \"ttl\" : null}"; then
  28. _debug _response "$response"
  29. _info "Added, OK"
  30. return 0
  31. fi
  32. _err "Add txt record error."
  33. return 1
  34. }
  35. #Usage: dns_fornex_rm _acme-challenge.www.domain.com
  36. dns_fornex_rm() {
  37. fulldomain=$1
  38. txtvalue=$2
  39. if ! _Fornex_API; then
  40. return 1
  41. fi
  42. if ! _get_root "$fulldomain"; then
  43. _err "Unable to determine root domain"
  44. return 1
  45. else
  46. _debug _domain "$_domain"
  47. fi
  48. _debug "Getting txt records"
  49. _rest GET "dns/domain/$_domain/entry_set?type=TXT&q=$fulldomain"
  50. if ! _contains "$response" "$txtvalue"; then
  51. _err "Txt record not found"
  52. return 1
  53. fi
  54. _record_id="$(echo "$response" | _egrep_o "\{[^\{]*\"value\"*:*\"$txtvalue\"[^\}]*\}" | sed -n -e 's#.*"id":\([0-9]*\).*#\1#p')"
  55. _debug "_record_id" "$_record_id"
  56. if [ -z "$_record_id" ]; then
  57. _err "can not find _record_id
  58. return 1
  59. fi
  60. if ! _rest DELETE "dns/domain/$_domain/entry_set/$_record_id/"; then
  61. _err "Delete record error."
  62. return 1
  63. fi
  64. return 0
  65. }
  66. #################### Private functions below ##################################
  67. #_acme-challenge.www.domain.com
  68. #returns
  69. # _sub_domain=_acme-challenge.www
  70. # _domain=domain.com
  71. _get_root() {
  72. domain=$1
  73. i=1
  74. while true; do
  75. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  76. _debug h "$h"
  77. if [ -z "$h" ]; then
  78. #not valid
  79. return 1
  80. fi
  81. if ! _rest GET "dns/domain/"; then
  82. return 1
  83. fi
  84. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  85. _domain=$h
  86. return 0
  87. else
  88. _debug "$h not found"
  89. fi
  90. i=$(_math "$i" + 1)
  91. done
  92. return 1
  93. }
  94. _Fornex_API() {
  95. FORNEX_API_KEY="${FORNEX_API_KEY:-$(_readaccountconf_mutable FORNEX_API_KEY)}"
  96. if [ -z "$FORNEX_API_KEY" ]; then
  97. FORNEX_API_KEY=""
  98. _err "You didn't specify the Fornex API key yet."
  99. _err "Please create your key and try again."
  100. return 1
  101. fi
  102. _saveaccountconf_mutable FORNEX_API_KEY "$FORNEX_API_KEY"
  103. }
  104. #method method action data
  105. _rest() {
  106. m=$1
  107. ep="$2"
  108. data="$3"
  109. _debug "$ep"
  110. export _H1="Authorization: Api-Key $FORNEX_API_KEY"
  111. export _H2="Content-Type: application/json"
  112. export _H3="Accept: application/json"
  113. if [ "$m" != "GET" ]; then
  114. _debug data "$data"
  115. response="$(_post "$data" "$FORNEX_API_URL/$ep" "" "$m")"
  116. else
  117. response="$(_get "$FORNEX_API_URL/$ep" | _normalizeJson)"
  118. fi
  119. _ret="$?"
  120. if [ "$_ret" != "0" ]; then
  121. _err "error $ep"
  122. return 1
  123. fi
  124. _debug2 response "$response"
  125. return 0
  126. }