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.

161 lines
3.6 KiB

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