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.

144 lines
3.3 KiB

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