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.

106 lines
3.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. # Author: non7top@gmail.com
  3. # 07 Jul 2017
  4. # report bugs at https://github.com/non7top/acme.sh
  5. # Values to export:
  6. # export PDD_Token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  7. ######## Public functions #####################
  8. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_yandex_add() {
  10. fulldomain="${1}"
  11. txtvalue="${2}"
  12. _debug "Calling: dns_yandex_add() '${fulldomain}' '${txtvalue}'"
  13. _PDD_credentials || return 1
  14. export _H1="PddToken: $PDD_Token"
  15. _PDD_get_domain "$fulldomain"
  16. _debug "Found suitable domain in pdd: $curDomain"
  17. curData="domain=${curDomain}&type=TXT&subdomain=${curSubdomain}&ttl=360&content=${txtvalue}"
  18. curUri="https://pddimp.yandex.ru/api2/admin/dns/add"
  19. curResult="$(_post "${curData}" "${curUri}")"
  20. _debug "Result: $curResult"
  21. }
  22. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  23. dns_yandex_rm() {
  24. fulldomain="${1}"
  25. _debug "Calling: dns_yandex_rm() '${fulldomain}'"
  26. _PDD_credentials || return 1
  27. export _H1="PddToken: $PDD_Token"
  28. record_id=$(pdd_get_record_id "${fulldomain}")
  29. _debug "Result: $record_id"
  30. _PDD_get_domain "$fulldomain"
  31. _debug "Found suitable domain in pdd: $curDomain"
  32. curUri="https://pddimp.yandex.ru/api2/admin/dns/del"
  33. curData="domain=${curDomain}&record_id=${record_id}"
  34. curResult="$(_post "${curData}" "${curUri}")"
  35. _debug "Result: $curResult"
  36. }
  37. #################### Private functions below ##################################
  38. _PDD_get_domain() {
  39. fulldomain="${1}"
  40. __page=1
  41. __last=0
  42. while [ $__last -eq 0 ]; do
  43. uri1="https://pddimp.yandex.ru/api2/admin/domain/domains?page=${__page}&on_page=20"
  44. res1=$(_get "$uri1" | _normalizeJson)
  45. #_debug "$res1"
  46. __found=$(echo "$res1" | sed -n -e 's#.* "found": \([^,]*\),.*#\1#p')
  47. _debug "found: $__found results on page"
  48. if [ "$__found" -lt 20 ]; then
  49. _debug "last page: $__page"
  50. __last=1
  51. fi
  52. __all_domains="$__all_domains $(echo "$res1" | tr "," "\n" | grep '"name"' | cut -d: -f2 | sed -e 's@"@@g')"
  53. __page=$(_math $__page + 1)
  54. done
  55. k=2
  56. while [ $k -lt 10 ]; do
  57. __t=$(echo "$fulldomain" | cut -d . -f $k-100)
  58. _debug "finding zone for domain $__t"
  59. for d in $__all_domains; do
  60. if [ "$d" = "$__t" ]; then
  61. p=$(_math $k - 1)
  62. curSubdomain="$(echo "$fulldomain" | cut -d . -f "1-$p")"
  63. curDomain="$__t"
  64. return 0
  65. fi
  66. done
  67. k=$(_math $k + 1)
  68. done
  69. _err "No suitable domain found in your account"
  70. return 1
  71. }
  72. _PDD_credentials() {
  73. if [ -z "${PDD_Token}" ]; then
  74. PDD_Token=""
  75. _err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx"
  76. _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token"
  77. return 1
  78. else
  79. _saveaccountconf PDD_Token "${PDD_Token}"
  80. fi
  81. }
  82. pdd_get_record_id() {
  83. fulldomain="${1}"
  84. _PDD_get_domain "$fulldomain"
  85. _debug "Found suitable domain in pdd: $curDomain"
  86. curUri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${curDomain}"
  87. curResult="$(_get "${curUri}" | _normalizeJson)"
  88. _debug "Result: $curResult"
  89. echo "$curResult" | _egrep_o "{[^{]*\"content\":[^{]*\"subdomain\":\"${curSubdomain}\"" | sed -n -e 's#.* "record_id": \(.*\),[^,]*#\1#p'
  90. }