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.

171 lines
4.3 KiB

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