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.

120 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. #
  3. # REGRU_API_Username="test"
  4. #
  5. # REGRU_API_Password="test"
  6. #
  7. REGRU_API_URL="https://api.reg.ru/api/regru2"
  8. ######## Public functions #####################
  9. dns_regru_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. REGRU_API_Username="${REGRU_API_Username:-$(_readaccountconf_mutable REGRU_API_Username)}"
  13. REGRU_API_Password="${REGRU_API_Password:-$(_readaccountconf_mutable REGRU_API_Password)}"
  14. if [ -z "$REGRU_API_Username" ] || [ -z "$REGRU_API_Password" ]; then
  15. REGRU_API_Username=""
  16. REGRU_API_Password=""
  17. _err "You don't specify regru password or username."
  18. return 1
  19. fi
  20. _saveaccountconf_mutable REGRU_API_Username "$REGRU_API_Username"
  21. _saveaccountconf_mutable REGRU_API_Password "$REGRU_API_Password"
  22. _debug "First detect the root zone"
  23. if ! _get_root "$fulldomain"; then
  24. _err "invalid domain"
  25. return 1
  26. fi
  27. _debug _domain "$_domain"
  28. _info "Adding TXT record to ${fulldomain}"
  29. _regru_rest POST "zone/add_txt" "input_data={%22username%22:%22${REGRU_API_Username}%22,%22password%22:%22${REGRU_API_Password}%22,%22domains%22:[{%22dname%22:%22${_domain}%22}],%22subdomain%22:%22_acme-challenge%22,%22text%22:%22${txtvalue}%22,%22output_content_type%22:%22plain%22}&input_format=json"
  30. if ! _contains "${response}" 'error'; then
  31. return 0
  32. fi
  33. _err "Could not create resource record, check logs"
  34. _err "${response}"
  35. return 1
  36. }
  37. dns_regru_rm() {
  38. fulldomain=$1
  39. txtvalue=$2
  40. REGRU_API_Username="${REGRU_API_Username:-$(_readaccountconf_mutable REGRU_API_Username)}"
  41. REGRU_API_Password="${REGRU_API_Password:-$(_readaccountconf_mutable REGRU_API_Password)}"
  42. if [ -z "$REGRU_API_Username" ] || [ -z "$REGRU_API_Password" ]; then
  43. REGRU_API_Username=""
  44. REGRU_API_Password=""
  45. _err "You don't specify regru password or username."
  46. return 1
  47. fi
  48. _debug "First detect the root zone"
  49. if ! _get_root "$fulldomain"; then
  50. _err "invalid domain"
  51. return 1
  52. fi
  53. _debug _domain "$_domain"
  54. _info "Deleting resource record $fulldomain"
  55. _regru_rest POST "zone/remove_record" "input_data={%22username%22:%22${REGRU_API_Username}%22,%22password%22:%22${REGRU_API_Password}%22,%22domains%22:[{%22dname%22:%22${_domain}%22}],%22subdomain%22:%22_acme-challenge%22,%22content%22:%22${txtvalue}%22,%22record_type%22:%22TXT%22,%22output_content_type%22:%22plain%22}&input_format=json"
  56. if ! _contains "${response}" 'error'; then
  57. return 0
  58. fi
  59. _err "Could not delete resource record, check logs"
  60. _err "${response}"
  61. return 1
  62. }
  63. #################### Private functions below ##################################
  64. #_acme-challenge.www.domain.com
  65. #returns
  66. # _domain=domain.com
  67. _get_root() {
  68. domain=$1
  69. _regru_rest POST "service/get_list" "username=${REGRU_API_Username}&password=${REGRU_API_Password}&output_format=xml&servtype=domain"
  70. domains_list=$(echo "${response}" | grep dname | sed -r "s/.*dname=\"([^\"]+)\".*/\\1/g")
  71. for ITEM in ${domains_list}
  72. do
  73. case "${domain}" in
  74. *${ITEM}*)
  75. _domain=${ITEM}
  76. _debug _domain "${_domain}"
  77. return 0
  78. ;;
  79. esac
  80. done
  81. return 1
  82. }
  83. #returns
  84. # response
  85. _regru_rest() {
  86. m=$1
  87. ep="$2"
  88. data="$3"
  89. _debug "$ep"
  90. export _H1="Content-Type: application/x-www-form-urlencoded"
  91. if [ "$m" != "GET" ]; then
  92. _debug data "$data"
  93. response="$(_post "$data" "$REGRU_API_URL/$ep" "" "$m")"
  94. else
  95. response="$(_get "$REGRU_API_URL/$ep?$data")"
  96. fi
  97. _debug response "${response}"
  98. return 0
  99. }