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.

165 lines
3.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_selectel_info='Selectel.com
  4. Domains: Selectel.ru
  5. Site: Selectel.com
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_selectel
  7. Options:
  8. SL_Key API Key
  9. '
  10. SL_Api="https://api.selectel.ru/domains/v1"
  11. ######## Public functions #####################
  12. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_selectel_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. SL_Key="${SL_Key:-$(_readaccountconf_mutable SL_Key)}"
  17. if [ -z "$SL_Key" ]; then
  18. SL_Key=""
  19. _err "You don't specify selectel.ru api key yet."
  20. _err "Please create you key and try again."
  21. return 1
  22. fi
  23. #save the api key to the account conf file.
  24. _saveaccountconf_mutable SL_Key "$SL_Key"
  25. _debug "First detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "invalid domain"
  28. return 1
  29. fi
  30. _debug _domain_id "$_domain_id"
  31. _debug _sub_domain "$_sub_domain"
  32. _debug _domain "$_domain"
  33. _info "Adding record"
  34. if _sl_rest POST "/$_domain_id/records/" "{\"type\": \"TXT\", \"ttl\": 60, \"name\": \"$fulldomain\", \"content\": \"$txtvalue\"}"; then
  35. if _contains "$response" "$txtvalue" || _contains "$response" "record_already_exists"; then
  36. _info "Added, OK"
  37. return 0
  38. fi
  39. fi
  40. _err "Add txt record error."
  41. return 1
  42. }
  43. #fulldomain txtvalue
  44. dns_selectel_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. SL_Key="${SL_Key:-$(_readaccountconf_mutable SL_Key)}"
  48. if [ -z "$SL_Key" ]; then
  49. SL_Key=""
  50. _err "You don't specify slectel api key yet."
  51. _err "Please create you key and try again."
  52. return 1
  53. fi
  54. _debug "First detect the root zone"
  55. if ! _get_root "$fulldomain"; then
  56. _err "invalid domain"
  57. return 1
  58. fi
  59. _debug _domain_id "$_domain_id"
  60. _debug _sub_domain "$_sub_domain"
  61. _debug _domain "$_domain"
  62. _debug "Getting txt records"
  63. _sl_rest GET "/${_domain_id}/records/"
  64. if ! _contains "$response" "$txtvalue"; then
  65. _err "Txt record not found"
  66. return 1
  67. fi
  68. _record_seg="$(echo "$response" | _egrep_o "[^{]*\"content\" *: *\"$txtvalue\"[^}]*}")"
  69. _debug2 "_record_seg" "$_record_seg"
  70. if [ -z "$_record_seg" ]; then
  71. _err "can not find _record_seg"
  72. return 1
  73. fi
  74. _record_id="$(echo "$_record_seg" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2)"
  75. _debug2 "_record_id" "$_record_id"
  76. if [ -z "$_record_id" ]; then
  77. _err "can not find _record_id"
  78. return 1
  79. fi
  80. if ! _sl_rest DELETE "/$_domain_id/records/$_record_id"; then
  81. _err "Delete record error."
  82. return 1
  83. fi
  84. return 0
  85. }
  86. #################### Private functions below ##################################
  87. #_acme-challenge.www.domain.com
  88. #returns
  89. # _sub_domain=_acme-challenge.www
  90. # _domain=domain.com
  91. # _domain_id=sdjkglgdfewsdfg
  92. _get_root() {
  93. domain=$1
  94. if ! _sl_rest GET "/"; then
  95. return 1
  96. fi
  97. i=2
  98. p=1
  99. while true; do
  100. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  101. _debug h "$h"
  102. if [ -z "$h" ]; then
  103. #not valid
  104. return 1
  105. fi
  106. if _contains "$response" "\"name\" *: *\"$h\","; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  108. _domain=$h
  109. _debug "Getting domain id for $h"
  110. if ! _sl_rest GET "/$h"; then
  111. return 1
  112. fi
  113. _domain_id="$(echo "$response" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\":" | cut -d : -f 2)"
  114. return 0
  115. fi
  116. p=$i
  117. i=$(_math "$i" + 1)
  118. done
  119. return 1
  120. }
  121. _sl_rest() {
  122. m=$1
  123. ep="$2"
  124. data="$3"
  125. _debug "$ep"
  126. export _H1="X-Token: $SL_Key"
  127. export _H2="Content-Type: application/json"
  128. if [ "$m" != "GET" ]; then
  129. _debug data "$data"
  130. response="$(_post "$data" "$SL_Api/$ep" "" "$m")"
  131. else
  132. response="$(_get "$SL_Api/$ep")"
  133. fi
  134. if [ "$?" != "0" ]; then
  135. _err "error $ep"
  136. return 1
  137. fi
  138. _debug2 response "$response"
  139. return 0
  140. }