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.

150 lines
3.1 KiB

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