2017-03-14 12:24:09 +01:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2017-03-15 16:16:54 +01:00
|
|
|
# Author: Boyan Peychev <boyan at cloudns dot net>
|
|
|
|
# Repository: https://github.com/ClouDNS/acme.sh/
|
|
|
|
|
2017-03-14 12:24:09 +01:00
|
|
|
#CLOUDNS_AUTH_ID=XXXXX
|
2017-11-13 03:38:30 +01:00
|
|
|
#CLOUDNS_SUB_AUTH_ID=XXXXX
|
2017-03-14 12:24:09 +01:00
|
|
|
#CLOUDNS_AUTH_PASSWORD="YYYYYYYYY"
|
|
|
|
CLOUDNS_API="https://api.cloudns.net"
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
|
2017-03-14 13:25:50 +01:00
|
|
|
dns_cloudns_add() {
|
2017-10-01 10:31:38 +02:00
|
|
|
_info "Using cloudns"
|
|
|
|
|
|
|
|
if ! _dns_cloudns_init_check; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
zone="$(_dns_cloudns_get_zone_name "$1")"
|
|
|
|
if [ -z "$zone" ]; then
|
|
|
|
_err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
host="$(echo "$1" | sed "s/\.$zone\$//")"
|
|
|
|
record=$2
|
|
|
|
|
|
|
|
_debug zone "$zone"
|
|
|
|
_debug host "$host"
|
|
|
|
_debug record "$record"
|
|
|
|
|
2018-02-19 13:14:08 +01:00
|
|
|
_info "Adding the TXT record for $1"
|
|
|
|
_dns_cloudns_http_api_call "dns/add-record.json" "domain-name=$zone&record-type=TXT&host=$host&record=$record&ttl=60"
|
|
|
|
if ! _contains "$response" "\"status\":\"Success\""; then
|
|
|
|
_err "Record cannot be added."
|
|
|
|
return 1
|
2017-10-01 10:31:38 +02:00
|
|
|
fi
|
2018-02-19 13:14:08 +01:00
|
|
|
_info "Added."
|
2017-10-01 10:31:38 +02:00
|
|
|
|
|
|
|
return 0
|
2017-03-14 12:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
|
2017-03-14 13:25:50 +01:00
|
|
|
dns_cloudns_rm() {
|
2017-10-01 10:31:38 +02:00
|
|
|
_info "Using cloudns"
|
|
|
|
|
|
|
|
if ! _dns_cloudns_init_check; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$zone" ]; then
|
|
|
|
zone="$(_dns_cloudns_get_zone_name "$1")"
|
|
|
|
if [ -z "$zone" ]; then
|
|
|
|
_err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
host="$(echo "$1" | sed "s/\.$zone\$//")"
|
|
|
|
record=$2
|
|
|
|
|
2018-02-20 10:09:37 +01:00
|
|
|
_dns_cloudns_http_api_call "dns/records.json" "domain-name=$zone&host=$host&type=TXT"
|
|
|
|
if ! _contains "$response" "\"id\":"; then
|
|
|
|
return 1
|
|
|
|
fi
|
2017-10-01 10:31:38 +02:00
|
|
|
|
2018-02-20 10:22:06 +01:00
|
|
|
for i in $(echo "$response" | tr '{' "\n" | grep "$record"); do
|
|
|
|
record_id=$(echo "$i" | tr ',' "\n" | grep -E '^"id"' | sed -re 's/^\"id\"\:\"([0-9]+)\"$/\1/g')
|
2018-02-20 10:09:37 +01:00
|
|
|
|
2020-07-16 06:14:50 +02:00
|
|
|
if [ -n "$record_id" ]; then
|
2018-02-20 10:09:37 +01:00
|
|
|
_debug zone "$zone"
|
|
|
|
_debug host "$host"
|
|
|
|
_debug record "$record"
|
|
|
|
_debug record_id "$record_id"
|
|
|
|
|
|
|
|
_info "Deleting the TXT record for $1"
|
|
|
|
_dns_cloudns_http_api_call "dns/delete-record.json" "domain-name=$zone&record-id=$record_id"
|
|
|
|
|
|
|
|
if ! _contains "$response" "\"status\":\"Success\""; then
|
|
|
|
_err "The TXT record for $1 cannot be deleted."
|
|
|
|
else
|
|
|
|
_info "Deleted."
|
|
|
|
fi
|
2017-10-01 10:31:38 +02:00
|
|
|
fi
|
2018-02-20 10:09:37 +01:00
|
|
|
done
|
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
return 0
|
2017-03-14 12:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#################### Private functions below ##################################
|
2017-03-14 13:25:50 +01:00
|
|
|
_dns_cloudns_init_check() {
|
2020-07-16 06:14:50 +02:00
|
|
|
if [ -n "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
|
2017-10-01 10:31:38 +02:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
CLOUDNS_AUTH_ID="${CLOUDNS_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_AUTH_ID)}"
|
2017-11-01 06:35:15 +01:00
|
|
|
CLOUDNS_SUB_AUTH_ID="${CLOUDNS_SUB_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_SUB_AUTH_ID)}"
|
2017-10-01 10:31:38 +02:00
|
|
|
CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
|
2017-11-01 06:35:15 +01:00
|
|
|
if [ -z "$CLOUDNS_AUTH_ID$CLOUDNS_SUB_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
|
2017-10-01 10:31:38 +02:00
|
|
|
CLOUDNS_AUTH_ID=""
|
2017-11-01 06:35:15 +01:00
|
|
|
CLOUDNS_SUB_AUTH_ID=""
|
2017-10-01 10:31:38 +02:00
|
|
|
CLOUDNS_AUTH_PASSWORD=""
|
|
|
|
_err "You don't specify cloudns api id and password yet."
|
|
|
|
_err "Please create you id and password and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2017-11-01 06:35:15 +01:00
|
|
|
if [ -z "$CLOUDNS_AUTH_ID" ] && [ -z "$CLOUDNS_SUB_AUTH_ID" ]; then
|
|
|
|
_err "CLOUDNS_AUTH_ID or CLOUDNS_SUB_AUTH_ID is not configured"
|
2017-10-01 10:31:38 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
|
|
|
|
_err "CLOUDNS_AUTH_PASSWORD is not configured"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_dns_cloudns_http_api_call "dns/login.json" ""
|
|
|
|
|
|
|
|
if ! _contains "$response" "\"status\":\"Success\""; then
|
|
|
|
_err "Invalid CLOUDNS_AUTH_ID or CLOUDNS_AUTH_PASSWORD. Please check your login credentials."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2018-02-20 10:09:37 +01:00
|
|
|
# save the api id and password to the account conf file.
|
2017-10-01 10:31:38 +02:00
|
|
|
_saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
|
2017-11-01 06:35:15 +01:00
|
|
|
_saveaccountconf_mutable CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
|
2017-10-01 10:31:38 +02:00
|
|
|
_saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
|
|
|
|
|
|
|
|
CLOUDNS_INIT_CHECK_COMPLETED=1
|
|
|
|
|
|
|
|
return 0
|
2017-03-14 12:24:09 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 13:25:50 +01:00
|
|
|
_dns_cloudns_get_zone_name() {
|
2017-10-01 10:31:38 +02:00
|
|
|
i=2
|
|
|
|
while true; do
|
|
|
|
zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
if [ -z "$zoneForCheck" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
_debug zoneForCheck "$zoneForCheck"
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
_dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
if ! _contains "$response" "\"status\":\"Failed\""; then
|
|
|
|
echo "$zoneForCheck"
|
|
|
|
return 0
|
|
|
|
fi
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
i=$(_math "$i" + 1)
|
|
|
|
done
|
|
|
|
return 1
|
2017-03-14 12:24:09 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 13:25:50 +01:00
|
|
|
_dns_cloudns_http_api_call() {
|
2017-10-01 10:31:38 +02:00
|
|
|
method=$1
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
_debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
|
2017-11-01 06:35:15 +01:00
|
|
|
_debug CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
|
2017-10-01 10:31:38 +02:00
|
|
|
_debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2020-07-16 06:14:50 +02:00
|
|
|
if [ -n "$CLOUDNS_SUB_AUTH_ID" ]; then
|
2017-11-01 06:35:15 +01:00
|
|
|
auth_user="sub-auth-id=$CLOUDNS_SUB_AUTH_ID"
|
|
|
|
else
|
|
|
|
auth_user="auth-id=$CLOUDNS_AUTH_ID"
|
2017-11-13 03:38:30 +01:00
|
|
|
fi
|
2017-11-01 06:35:15 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
if [ -z "$2" ]; then
|
2017-11-01 06:35:15 +01:00
|
|
|
data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD"
|
2017-10-01 10:31:38 +02:00
|
|
|
else
|
2017-11-01 06:35:15 +01:00
|
|
|
data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
|
2017-10-01 10:31:38 +02:00
|
|
|
fi
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
response="$(_get "$CLOUDNS_API/$method?$data")"
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2018-02-20 10:09:37 +01:00
|
|
|
_debug response "$response"
|
2017-03-14 12:24:09 +01:00
|
|
|
|
2017-10-01 10:31:38 +02:00
|
|
|
return 0
|
2017-03-14 13:40:18 +01:00
|
|
|
}
|