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.1 KiB

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