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.

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