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.

172 lines
4.4 KiB

  1. #!/usr/bin/env sh
  2. #######################################################
  3. #
  4. # easyDNS REST API for acme.sh by Neilpang based on dns_cf.sh
  5. #
  6. # Please note: # API is currently beta and subject to constant change
  7. # http://sandbox.rest.easydns.net:3000/
  8. #
  9. # Author: wurzelpanzer [wurzelpanzer@maximolider.net]
  10. # Report Bugs here: https://github.com/Neilpang/acme.sh/issues/2647
  11. #
  12. #################### Public functions #################
  13. #EASYDNS_Key="xxxxxxxxxxxxxxxxxxxxxxxx"
  14. #EASYDNS_Token="xxxxxxxxxxxxxxxxxxxxxxxx"
  15. EASYDNS_Api="https://rest.easydns.net"
  16. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_easydns_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. EASYDNS_Token="${EASYDNS_Token:-$(_readaccountconf_mutable EASYDNS_Token)}"
  21. EASYDNS_Key="${EASYDNS_Key:-$(_readaccountconf_mutable EASYDNS_Key)}"
  22. if [ -z "$EASYDNS_Token" ] || [ -z "$EASYDNS_Key" ]; then
  23. _err "You didn't specify an easydns.net token or api key. Please sign up at http://docs.sandbox.rest.easydns.net/beta_signup.php"
  24. return 1
  25. else
  26. _saveaccountconf_mutable EASYDNS_Token "$EASYDNS_Token"
  27. _saveaccountconf_mutable EASYDNS_Key "$EASYDNS_Key"
  28. fi
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _sub_domain "$_sub_domain"
  35. _debug _domain "$_domain"
  36. _debug "Getting txt records"
  37. _EASYDNS_rest GET "zones/records/all/${_domain}/search/${_sub_domain}"
  38. if ! printf "%s" "$response" | grep \"status\":200 >/dev/null; then
  39. _err "Error"
  40. return 1
  41. fi
  42. _info "Adding record"
  43. if _EASYDNS_rest PUT "zones/records/add/$_domain/TXT" "{\"host\":\"$_sub_domain\",\"rdata\":\"$txtvalue\"}"; then
  44. if _contains "$response" "\"status\":201"; then
  45. _info "Added, OK"
  46. return 0
  47. elif _contains "$response" "Record already exists"; then
  48. _info "Already exists, OK"
  49. return 0
  50. else
  51. _err "Add txt record error."
  52. return 1
  53. fi
  54. fi
  55. _err "Add txt record error."
  56. return 1
  57. }
  58. dns_easydns_rm() {
  59. fulldomain=$1
  60. txtvalue=$2
  61. EASYDNS_Token="${EASYDNS_Token:-$(_readaccountconf_mutable EASYDNS_Token)}"
  62. EASYDNS_Key="${EASYDNS_Key:-$(_readaccountconf_mutable EASYDNS_Key)}"
  63. _debug "First detect the root zone"
  64. if ! _get_root "$fulldomain"; then
  65. _err "invalid domain"
  66. return 1
  67. fi
  68. _debug _sub_domain "$_sub_domain"
  69. _debug _domain "$_domain"
  70. _debug "Getting txt records"
  71. _EASYDNS_rest GET "zones/records/all/${_domain}/search/${_sub_domain}"
  72. if ! printf "%s" "$response" | grep \"status\":200 >/dev/null; then
  73. _err "Error"
  74. return 1
  75. fi
  76. count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  77. _debug count "$count"
  78. if [ "$count" = "0" ]; then
  79. _info "Don't need to remove."
  80. else
  81. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | head -n 1)
  82. _debug "record_id" "$record_id"
  83. if [ -z "$record_id" ]; then
  84. _err "Can not get record id to remove."
  85. return 1
  86. fi
  87. if ! _EASYDNS_rest DELETE "zones/records/$_domain/$record_id"; then
  88. _err "Delete record error."
  89. return 1
  90. fi
  91. _contains "$response" "\"status\":200"
  92. fi
  93. }
  94. #################### Private functions below ##################################
  95. #_acme-challenge.www.domain.com
  96. #returns
  97. # _sub_domain=_acme-challenge.www
  98. # _domain=domain.com
  99. _get_root() {
  100. domain=$1
  101. i=1
  102. p=1
  103. while true; do
  104. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  105. _debug h "$h"
  106. if [ -z "$h" ]; then
  107. #not valid
  108. return 1
  109. fi
  110. if ! _EASYDNS_rest GET "zones/records/all/$h"; then
  111. return 1
  112. fi
  113. if _contains "$response" "\"status\":200"; then
  114. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  115. _domain=$h
  116. return 0
  117. fi
  118. p=$i
  119. i=$(_math "$i" + 1)
  120. done
  121. return 1
  122. }
  123. _EASYDNS_rest() {
  124. m=$1
  125. ep="$2"
  126. data="$3"
  127. _debug "$ep"
  128. basicauth=$(printf "%s" "$EASYDNS_Token":"$EASYDNS_Key" | _base64)
  129. export _H1="accept: application/json"
  130. if [ "$basicauth" ]; then
  131. export _H2="Authorization: Basic $basicauth"
  132. fi
  133. if [ "$m" != "GET" ]; then
  134. export _H3="Content-Type: application/json"
  135. _debug data "$data"
  136. response="$(_post "$data" "$EASYDNS_Api/$ep" "" "$m")"
  137. else
  138. response="$(_get "$EASYDNS_Api/$ep")"
  139. fi
  140. if [ "$?" != "0" ]; then
  141. _err "error $ep"
  142. return 1
  143. fi
  144. _debug2 response "$response"
  145. return 0
  146. }