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.

147 lines
3.4 KiB

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