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.

152 lines
3.3 KiB

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