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.

148 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/{/\
  78. &/g')"
  79. while true; do
  80. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  81. _debug h "$h"
  82. if [ -z "$h" ]; then
  83. #not valid
  84. return 1
  85. fi
  86. hostedzone="$(echo "$response" | _egrep_o "{.*\"name\":\s*\"$h\".*}")"
  87. if [ "$hostedzone" ]; then
  88. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  89. if [ "$_domain_id" ]; then
  90. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  91. _domain=$h
  92. return 0
  93. fi
  94. return 1
  95. fi
  96. p=$i
  97. i=$(_math "$i" + 1)
  98. done
  99. fi
  100. return 1
  101. }
  102. #method uri qstr data
  103. _ad_rest() {
  104. mtd="$1"
  105. ep="$2"
  106. data="$3"
  107. _debug mtd "$mtd"
  108. _debug ep "$ep"
  109. export _H1="Accept: application/json"
  110. export _H2="Content-Type: application/json"
  111. if [ "$mtd" != "GET" ]; then
  112. # both POST and DELETE.
  113. _debug data "$data"
  114. response="$(_post "$data" "$AD_API_URL/$ep" "" "$mtd")"
  115. else
  116. response="$(_get "$AD_API_URL/$ep")"
  117. fi
  118. if [ "$?" != "0" ]; then
  119. _err "error $ep"
  120. return 1
  121. fi
  122. _debug2 response "$response"
  123. return 0
  124. }