dns_anx.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env sh
  2. ### Anexia CloudDNS acme.sh hook
  3. #ANX_Token="xxxx"
  4. ANX_API='https://engine.anexia-it.com/api/clouddns/v1'
  5. ######## Public functions #####################
  6. dns_anx_add() {
  7. fulldomain=$1
  8. txtvalue=$2
  9. _info "Using ANX CDNS API"
  10. ANX_Token="${ANX_Token:-$(_readaccountconf_mutable ANX_Token)}"
  11. _debug fulldomain "$fulldomain"
  12. _debug txtvalue "$txtvalue"
  13. if [ "$ANX_Token" ]; then
  14. _saveaccountconf_mutable ANX_Token "$ANX_Token"
  15. else
  16. _err "You didn't specify a ANEXIA Engine API token."
  17. return 1
  18. fi
  19. _debug "First detect the root zone"
  20. if ! _get_root "$fulldomain"; then
  21. _err "invalid domain"
  22. return 1
  23. fi
  24. # Always add records, wildcard need two records with the same name
  25. _anx_rest POST "zone.json/${_domain}/records" "{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"rdata\":\"$txtvalue\"}"
  26. if _contains "$response" "$txtvalue"; then
  27. return 0
  28. else
  29. return 1
  30. fi
  31. }
  32. dns_anx_rm() {
  33. fulldomain=$1
  34. txtvalue=$2
  35. _info "Using ANX CDNS API"
  36. ANX_Token="${ANX_Token:-$(_readaccountconf_mutable ANX_Token)}"
  37. _debug fulldomain "$fulldomain"
  38. _debug txtvalue "$txtvalue"
  39. _debug "First detect the root zone"
  40. if ! _get_root "$fulldomain"; then
  41. _err "invalid domain"
  42. return 1
  43. fi
  44. _get_record_id
  45. if _is_uuid "$_record_id"; then
  46. if ! _anx_rest DELETE "zone.json/${_domain}/records/$_record_id"; then
  47. _err "Delete record"
  48. return 1
  49. fi
  50. else
  51. _info "No record found."
  52. fi
  53. echo "$response" | tr -d " " | grep \"status\":\"OK\" >/dev/null
  54. }
  55. #################### Private functions below ##################################
  56. _is_uuid() {
  57. pattern='^\{?[A-Z0-9a-z]{8}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{12}\}?$'
  58. if echo "$1" | _egrep_o "$pattern" >/dev/null; then
  59. return 0
  60. fi
  61. return 1
  62. }
  63. _get_record_id() {
  64. _debug subdomain "$_sub_domain"
  65. _debug domain "$_domain"
  66. if _anx_rest GET "zone.json/${_domain}/records?name=$_sub_domain&type=TXT"; then
  67. _debug response "$response"
  68. if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
  69. _record_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"identifier\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  70. else
  71. _record_id=''
  72. fi
  73. else
  74. _err "Search existing record"
  75. fi
  76. }
  77. _anx_rest() {
  78. m=$1
  79. ep="$2"
  80. data="$3"
  81. _debug "$ep"
  82. export _H1="Content-Type: application/json"
  83. export _H2="Authorization: Token $ANX_Token"
  84. if [ "$m" != "GET" ]; then
  85. _debug data "$data"
  86. response="$(_post "$data" "${ANX_API}/$ep" "" "$m")"
  87. else
  88. response="$(_get "${ANX_API}/$ep")"
  89. fi
  90. # shellcheck disable=SC2181
  91. if [ "$?" != "0" ]; then
  92. _err "error $ep"
  93. return 1
  94. fi
  95. _debug response "$response"
  96. return 0
  97. }
  98. #_acme-challenge.www.domain.com
  99. #returns
  100. # _sub_domain=_acme-challenge.www
  101. # _domain=domain.com
  102. _get_root() {
  103. domain=$1
  104. i=1
  105. p=1
  106. while true; do
  107. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  108. _debug h "$h"
  109. if [ -z "$h" ]; then
  110. #not valid
  111. return 1
  112. fi
  113. # Does a zone with that name exist?
  114. _anx_rest GET "zone.json/$h"
  115. # shellcheck disable=SC2154
  116. if [ "$code" -ne 200 ]; then
  117. continue
  118. fi
  119. if _contains "$response" "\"name\":\"$h\""; then
  120. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  121. _domain=$h
  122. return 0
  123. fi
  124. p=$i
  125. i=$(_math "$i" + 1)
  126. done
  127. return 1
  128. }