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.

109 lines
3.3 KiB

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" || return 1
  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. _PDD_get_domain "$fulldomain" || return 1
  29. _debug "Found suitable domain in pdd: $curDomain"
  30. record_id=$(pdd_get_record_id "${fulldomain}")
  31. _debug "Result: $record_id"
  32. for rec_i in $record_id; do
  33. curUri="https://pddimp.yandex.ru/api2/admin/dns/del"
  34. curData="domain=${curDomain}&record_id=${rec_i}"
  35. curResult="$(_post "${curData}" "${curUri}")"
  36. _debug "Result: $curResult"
  37. done
  38. }
  39. #################### Private functions below ##################################
  40. _PDD_get_domain() {
  41. fulldomain="${1}"
  42. __page=1
  43. __last=0
  44. while [ $__last -eq 0 ]; do
  45. uri1="https://pddimp.yandex.ru/api2/admin/domain/domains?page=${__page}&on_page=20"
  46. res1="$(_get "$uri1" | _normalizeJson)"
  47. _debug2 "res1" "$res1"
  48. __found="$(echo "$res1" | sed -n -e 's#.* "found": \([^,]*\),.*#\1#p')"
  49. _debug "found: $__found results on page"
  50. if [ "0$__found" -lt 20 ]; then
  51. _debug "last page: $__page"
  52. __last=1
  53. fi
  54. __all_domains="$__all_domains $(echo "$res1" | tr "," "\n" | grep '"name"' | cut -d: -f2 | sed -e 's@"@@g')"
  55. __page=$(_math $__page + 1)
  56. done
  57. k=2
  58. while [ $k -lt 10 ]; do
  59. __t=$(echo "$fulldomain" | cut -d . -f $k-100)
  60. _debug "finding zone for domain $__t"
  61. for d in $__all_domains; do
  62. if [ "$d" = "$__t" ]; then
  63. p=$(_math $k - 1)
  64. curSubdomain="$(echo "$fulldomain" | cut -d . -f "1-$p")"
  65. curDomain="$__t"
  66. return 0
  67. fi
  68. done
  69. k=$(_math $k + 1)
  70. done
  71. _err "No suitable domain found in your account"
  72. return 1
  73. }
  74. _PDD_credentials() {
  75. if [ -z "${PDD_Token}" ]; then
  76. PDD_Token=""
  77. _err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx"
  78. _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token"
  79. return 1
  80. else
  81. _saveaccountconf PDD_Token "${PDD_Token}"
  82. fi
  83. }
  84. pdd_get_record_id() {
  85. fulldomain="${1}"
  86. _PDD_get_domain "$fulldomain"
  87. _debug "Found suitable domain in pdd: $curDomain"
  88. curUri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${curDomain}"
  89. curResult="$(_get "${curUri}" | _normalizeJson)"
  90. _debug "Result: $curResult"
  91. echo "$curResult" | _egrep_o "{[^{]*\"content\":[^{]*\"subdomain\":\"${curSubdomain}\"" | sed -n -e 's#.* "record_id": \(.*\),[^,]*#\1#p'
  92. }