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/dns/v0.1"
  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 "$_domain/entry_set/add/" "host=$fulldomain&type=TXT&value=$txtvalue&apikey=$FORNEX_API_KEY"; then
  28. _debug _response "$response"
  29. if _contains "$response" '"ok": true' || _contains "$response" 'Такая запись уже существует.'; then
  30. _info "Added, OK"
  31. return 0
  32. fi
  33. fi
  34. _err "Add txt record error."
  35. return 1
  36. }
  37. #Usage: dns_fornex_rm _acme-challenge.www.domain.com
  38. dns_fornex_rm() {
  39. fulldomain=$1
  40. txtvalue=$2
  41. if ! _Fornex_API; then
  42. return 1
  43. fi
  44. if ! _get_root "$fulldomain"; then
  45. _err "Unable to determine root domain"
  46. return 1
  47. else
  48. _debug _domain "$_domain"
  49. fi
  50. _debug "Getting txt records"
  51. _rest GET "$_domain/entry_set.json?apikey=$FORNEX_API_KEY"
  52. if ! _contains "$response" "$txtvalue"; then
  53. _err "Txt record not found"
  54. return 1
  55. fi
  56. _record_id="$(echo "$response" | _egrep_o "{[^{]*\"value\"*:*\"$txtvalue\"[^}]*}" | sed -n -e 's#.*"id": \([0-9]*\).*#\1#p')"
  57. _debug "_record_id" "$_record_id"
  58. if [ -z "$_record_id" ]; then
  59. _err "can not find _record_id"
  60. return 1
  61. fi
  62. if ! _rest POST "$_domain/entry_set/$_record_id/delete/" "apikey=$FORNEX_API_KEY"; then
  63. _err "Delete record error."
  64. return 1
  65. fi
  66. return 0
  67. }
  68. #################### Private functions below ##################################
  69. #_acme-challenge.www.domain.com
  70. #returns
  71. # _sub_domain=_acme-challenge.www
  72. # _domain=domain.com
  73. _get_root() {
  74. domain=$1
  75. i=1
  76. while true; do
  77. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  78. _debug h "$h"
  79. if [ -z "$h" ]; then
  80. #not valid
  81. return 1
  82. fi
  83. if ! _rest GET "domain_list.json?q=$h&apikey=$FORNEX_API_KEY"; then
  84. return 1
  85. fi
  86. if _contains "$response" "\"$h\"" >/dev/null; then
  87. _domain=$h
  88. return 0
  89. else
  90. _debug "$h not found"
  91. fi
  92. i=$(_math "$i" + 1)
  93. done
  94. return 1
  95. }
  96. _Fornex_API() {
  97. FORNEX_API_KEY="${FORNEX_API_KEY:-$(_readaccountconf_mutable FORNEX_API_KEY)}"
  98. if [ -z "$FORNEX_API_KEY" ]; then
  99. FORNEX_API_KEY=""
  100. _err "You didn't specify the Fornex API key yet."
  101. _err "Please create your key and try again."
  102. return 1
  103. fi
  104. _saveaccountconf_mutable FORNEX_API_KEY "$FORNEX_API_KEY"
  105. }
  106. #method method action data
  107. _rest() {
  108. m=$1
  109. ep="$2"
  110. data="$3"
  111. _debug "$ep"
  112. export _H1="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. }