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.

139 lines
3.1 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #AD_API_KEY="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #This is the Alwaysdata api wrapper for acme.sh
  5. AD_HOST="api.alwaysdata.com"
  6. AD_URL="https://$AD_API_KEY:@$AD_HOST"
  7. ######## Public functions #####################
  8. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_ad_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. if [ -z "$AD_API_KEY" ]; then
  13. AD_API_KEY=""
  14. _err "You didn't specify the AD api key yet."
  15. _err "Please create you key and try again."
  16. return 1
  17. fi
  18. _saveaccountconf AD_API_KEY "$AD_API_KEY"
  19. _debug "First detect the root zone"
  20. if ! _get_root "$fulldomain"; then
  21. _err "invalid domain"
  22. return 1
  23. fi
  24. _debug _domain_id "$_domain_id"
  25. _debug _sub_domain "$_sub_domain"
  26. _debug _domain "$_domain"
  27. _ad_tmpl_json="{\"domain\":$_domain_id,\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\"}"
  28. if ad_rest POST "record/" "" "$_ad_tmpl_json" && [ -z "$response" ]; then
  29. _info "txt record updated success."
  30. return 0
  31. fi
  32. return 1
  33. }
  34. #fulldomain txtvalue
  35. dns_ad_rm() {
  36. fulldomain=$1
  37. txtvalue=$2
  38. _debug "First detect the root zone"
  39. if ! _get_root "$fulldomain"; then
  40. _err "invalid domain"
  41. return 1
  42. fi
  43. _debug _domain_id "$_domain_id"
  44. _debug _sub_domain "$_sub_domain"
  45. _debug _domain "$_domain"
  46. if ad_rest DELETE "record/" "domain=$_domain_id&name=$_sub_domain" "" && [ -z "$response" ]; then
  47. _info "txt record deleted success."
  48. return 0
  49. fi
  50. _debug response "$response"
  51. return 1
  52. }
  53. #################### Private functions below ##################################
  54. _get_root() {
  55. domain=$1
  56. i=2
  57. p=1
  58. if ad_rest GET "domain/"; then
  59. while true; do
  60. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  61. if [ -z "$h" ]; then
  62. #not valid
  63. return 1
  64. fi
  65. if _contains "$response" "<name>$h</name>"; then
  66. hostedzone="$(echo "$response" | tr -d "\n" | sed 's/<object>/\n&/g' | _egrep_o "<object>.*<name>$h<.name>.*<.object>")"
  67. if [ -z "$hostedzone" ]; then
  68. _err "Error, can not get domain record."
  69. return 1
  70. fi
  71. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<id>.*<.id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  72. if [ "$_domain_id" ]; then
  73. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  74. _domain=$h
  75. return 0
  76. fi
  77. return 1
  78. fi
  79. p=$i
  80. i=$(_math "$i" + 1)
  81. done
  82. fi
  83. return 1
  84. }
  85. #method uri qstr data
  86. ad_rest() {
  87. mtd="$1"
  88. ep="$2"
  89. qsr="$3"
  90. data="$4"
  91. _debug mtd "$mtd"
  92. _debug ep "$ep"
  93. _debug qsr "$qsr"
  94. _debug data "$data"
  95. _H1="Accept: application/xml"
  96. url="$AD_URL/v1/$ep?$qsr"
  97. if [ "$mtd" = "GET" ]; then
  98. response="$(_get "$url")"
  99. elif [ "$mtd" = "DELETE" ]; then
  100. response="$(_delete "$url")"
  101. else
  102. response="$(_post "$data" "$url")"
  103. fi
  104. _ret="$?"
  105. if [ "$_ret" = "0" ]; then
  106. # Errors usually 404, otherwise just empty response. How to detect 404?
  107. if _contains "$response" "<ErrorResponse"; then
  108. _err "Response error:$response"
  109. return 1
  110. fi
  111. fi
  112. return "$_ret"
  113. }