acme.sh/dnsapi/dns_yandex.sh

122 lines
3.5 KiB
Bash
Raw Normal View History

2017-07-08 18:35:22 +00:00
#!/usr/bin/env sh
# Author: non7top@gmail.com
# 07 Jul 2017
# report bugs at https://github.com/non7top/acme.sh
# Values to export:
# export PDD_Token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2020-01-12 12:54:18 +00:00
# Sometimes cloudflare / google doesn't pick new dns records fast enough.
2020-01-12 12:29:09 +00:00
# You can add --dnssleep XX to params as workaround.
2017-07-08 18:35:22 +00:00
######## Public functions #####################
#Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_yandex_add() {
2020-01-12 12:54:18 +00:00
fulldomain="${1}"
txtvalue="${2}"
_debug "Calling: dns_yandex_add() '${fulldomain}' '${txtvalue}'"
2020-01-12 12:29:09 +00:00
2017-07-10 23:19:39 +00:00
_PDD_credentials || return 1
2017-07-08 18:35:22 +00:00
_PDD_get_domain || return 1
2020-01-12 12:29:09 +00:00
_debug "Found suitable domain: $domain"
_PDD_get_record_ids || return 1
2020-01-12 12:29:09 +00:00
_debug "Record_ids: $record_ids"
2020-07-16 04:18:46 +00:00
if [ -n "$record_ids" ]; then
_info "All existing $subdomain records from $domain will be removed at the very end."
2020-01-12 12:29:09 +00:00
fi
2020-01-12 12:54:18 +00:00
data="domain=${domain}&type=TXT&subdomain=${subdomain}&ttl=300&content=${txtvalue}"
uri="https://pddimp.yandex.ru/api2/admin/dns/add"
result="$(_post "${data}" "${uri}" | _normalizeJson)"
2020-01-12 12:29:09 +00:00
_debug "Result: $result"
if ! _contains "$result" '"success":"ok"'; then
if _contains "$result" '"success":"error"' && _contains "$result" '"error":"record_exists"'; then
_info "Record already exists."
else
_err "Can't add $subdomain to $domain."
return 1
fi
2020-01-12 12:29:09 +00:00
fi
2017-07-08 18:35:22 +00:00
}
#Usage: dns_myapi_rm _acme-challenge.www.domain.com
dns_yandex_rm() {
2020-01-12 12:54:18 +00:00
fulldomain="${1}"
2017-07-08 18:35:22 +00:00
_debug "Calling: dns_yandex_rm() '${fulldomain}'"
2020-01-12 12:29:09 +00:00
2017-07-10 23:19:39 +00:00
_PDD_credentials || return 1
2017-07-08 18:35:22 +00:00
2018-05-02 21:58:25 +00:00
_PDD_get_domain "$fulldomain" || return 1
2020-01-12 12:29:09 +00:00
_debug "Found suitable domain: $domain"
2017-07-12 00:51:48 +00:00
2020-01-12 12:29:09 +00:00
_PDD_get_record_ids "${domain}" "${subdomain}" || return 1
_debug "Record_ids: $record_ids"
2018-05-02 22:00:51 +00:00
2020-01-12 12:29:09 +00:00
for record_id in $record_ids; do
2020-01-12 12:54:18 +00:00
data="domain=${domain}&record_id=${record_id}"
uri="https://pddimp.yandex.ru/api2/admin/dns/del"
result="$(_post "${data}" "${uri}" | _normalizeJson)"
2020-01-12 12:29:09 +00:00
_debug "Result: $result"
if ! _contains "$result" '"success":"ok"'; then
_info "Can't remove $subdomain from $domain."
2020-01-12 12:29:09 +00:00
fi
2018-05-02 22:01:14 +00:00
done
2017-07-08 18:35:22 +00:00
}
#################### Private functions below ##################################
2017-07-12 00:51:48 +00:00
_PDD_get_domain() {
2020-01-12 12:54:18 +00:00
subdomain_start=1
2020-01-12 12:29:09 +00:00
while true; do
2020-01-12 12:54:18 +00:00
domain_start=$(_math $subdomain_start + 1)
domain=$(echo "$fulldomain" | cut -d . -f "$domain_start"-)
subdomain=$(echo "$fulldomain" | cut -d . -f -"$subdomain_start")
2017-07-12 00:51:48 +00:00
2020-01-12 12:29:09 +00:00
_debug "Checking domain $domain"
if [ -z "$domain" ]; then
return 1
fi
2020-01-12 12:54:18 +00:00
uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=$domain"
result="$(_get "${uri}" | _normalizeJson)"
2020-01-12 12:29:09 +00:00
_debug "Result: $result"
2017-07-12 00:51:48 +00:00
2020-01-12 12:29:09 +00:00
if _contains "$result" '"success":"ok"'; then
2020-01-12 13:07:49 +00:00
return 0
2020-01-12 12:29:09 +00:00
fi
subdomain_start=$(_math $subdomain_start + 1)
2017-07-12 00:51:48 +00:00
done
}
2017-07-08 18:35:22 +00:00
_PDD_credentials() {
if [ -z "${PDD_Token}" ]; then
PDD_Token=""
_err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx."
_err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token."
2017-07-08 18:35:22 +00:00
return 1
else
_saveaccountconf PDD_Token "${PDD_Token}"
fi
2020-01-12 12:29:09 +00:00
export _H1="PddToken: $PDD_Token"
2017-07-08 18:35:22 +00:00
}
2020-01-12 12:29:09 +00:00
_PDD_get_record_ids() {
_debug "Check existing records for $subdomain"
2017-07-12 00:51:48 +00:00
2020-01-12 12:54:18 +00:00
uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${domain}"
result="$(_get "${uri}" | _normalizeJson)"
2020-01-12 12:29:09 +00:00
_debug "Result: $result"
if ! _contains "$result" '"success":"ok"'; then
2020-01-12 13:07:49 +00:00
return 1
2020-01-12 12:29:09 +00:00
fi
2017-07-12 00:51:48 +00:00
2020-01-12 12:29:09 +00:00
record_ids=$(echo "$result" | _egrep_o "{[^{]*\"subdomain\":\"${subdomain}\"[^}]*}" | sed -n -e 's#.*"record_id": \([0-9]*\).*#\1#p')
2017-07-08 18:35:22 +00:00
}