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.

123 lines
3.5 KiB

  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. # Sometimes cloudflare / google doesn't pick new dns recods fast enough.
  8. # You can add --dnssleep XX to params as workaround.
  9. ######## Public functions #####################
  10. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  11. dns_yandex_add() {
  12. local fulldomain="${1}"
  13. local txtvalue="${2}"
  14. _debug "Calling: dns_yandex_add() '${fulldomain}' '$txtvalue'"
  15. _PDD_credentials || return 1
  16. _PDD_get_domain "$fulldomain" || return 1
  17. _debug "Found suitable domain: $domain"
  18. _PDD_get_record_ids "${domain}" "${subdomain}" || return 1
  19. _debug "Record_ids: $record_ids"
  20. if [ ! -z "$record_ids" ]; then
  21. _err "Remove all existing $subdomain records from $domain"
  22. return 1
  23. fi
  24. local data="domain=${domain}&type=TXT&subdomain=${subdomain}&ttl=300&content=${txtvalue}"
  25. local uri="https://pddimp.yandex.ru/api2/admin/dns/add"
  26. local result="$(_post "${data}" "${uri}" | _normalizeJson)"
  27. _debug "Result: $result"
  28. if ! _contains "$result" '"success":"ok"'; then
  29. _err "Can't add $subdomain to $domain"
  30. return 1
  31. fi
  32. }
  33. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  34. dns_yandex_rm() {
  35. local fulldomain="${1}"
  36. _debug "Calling: dns_yandex_rm() '${fulldomain}'"
  37. _PDD_credentials || return 1
  38. _PDD_get_domain "$fulldomain" || return 1
  39. _debug "Found suitable domain: $domain"
  40. _PDD_get_record_ids "${domain}" "${subdomain}" || return 1
  41. _debug "Record_ids: $record_ids"
  42. for record_id in $record_ids; do
  43. local data="domain=${domain}&record_id=${record_id}"
  44. local uri="https://pddimp.yandex.ru/api2/admin/dns/del"
  45. local result="$(_post "${data}" "${uri}" | _normalizeJson)"
  46. _debug "Result: $result"
  47. if ! _contains "$result" '"success":"ok"'; then
  48. _info "Can't remove $subdomain from $domain"
  49. fi
  50. done
  51. }
  52. #################### Private functions below ##################################
  53. _PDD_get_domain() {
  54. local fulldomain=${1}
  55. local subdomain_start=1
  56. while true; do
  57. local domain_start=$(_math $subdomain_start + 1)
  58. domain=$(echo "$fulldomain" | cut -d . -f $domain_start-)
  59. subdomain=$(echo "$fulldomain" | cut -d . -f -$subdomain_start)
  60. _debug "Checking domain $domain"
  61. if [ -z "$domain" ]; then
  62. return 1
  63. fi
  64. local uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=$domain"
  65. local result="$(_get "${uri}" | _normalizeJson)"
  66. _debug "Result: $result"
  67. if _contains "$result" '"success":"ok"'; then
  68. return 0
  69. fi
  70. subdomain_start=$(_math $subdomain_start + 1)
  71. done
  72. }
  73. _PDD_credentials() {
  74. if [ -z "${PDD_Token}" ]; then
  75. PDD_Token=""
  76. _err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx"
  77. _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token"
  78. return 1
  79. else
  80. _saveaccountconf PDD_Token "${PDD_Token}"
  81. fi
  82. export _H1="PddToken: $PDD_Token"
  83. }
  84. _PDD_get_record_ids() {
  85. local domain="${1}"
  86. local subdomain="${2}"
  87. _debug "Check existing records for $subdomain"
  88. local uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${domain}"
  89. local result="$(_get "${uri}" | _normalizeJson)"
  90. _debug "Result: $result"
  91. if ! _contains "$result" '"success":"ok"'; then
  92. return 1
  93. fi
  94. record_ids=$(echo "$result" | _egrep_o "{[^{]*\"subdomain\":\"${subdomain}\"[^}]*}" | sed -n -e 's#.*"record_id": \([0-9]*\).*#\1#p')
  95. }