This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hook.sh
executable file
·88 lines (74 loc) · 2.4 KB
/
hook.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
set -euo pipefail
export LC_ALL=C
get_delegated_domain() {
local domain="${1}"
while test "${domain#*.}" != "${domain}"; do
if host -t NS "${domain}" | grep -i "cloudns.net" &> /dev/null; then
echo "${domain}"
return 0
else
domain="${domain#*.}"
fi
done
return 1
}
get_prefix() {
local domain="$(get_delegated_domain ${1})"
test -z "${domain}" && return 1
test "${domain}" = "${1}" && return 0
echo "${1%*.${domain}}"
}
do_request() {
test -z "${CLOUDNS_AUTH_ID}" && return 1
test -z "${CLOUDNS_AUTH_PASSWORD}" && return 1
local args="auth-id=${CLOUDNS_AUTH_ID}&auth-password=${CLOUDNS_AUTH_PASSWORD}&${2}"
curl \
--silent \
"https://api.cloudns.net${1}?${args}"
}
deploy_challenge() {
echo " + cloudns hook executing: deploy_challenge"
local prefix="$(get_prefix ${1})" domain="$(get_delegated_domain ${1})"
test -z "${domain}" && return 1
echo " + creating TXT record for ${1}"
do_request \
/dns/add-record.json \
"domain-name=${domain}&record-type=TXT&host=_acme-challenge${prefix:+.${prefix}}&record=${2}&ttl=60" \
| grep -i success &> /dev/null
echo " + waiting for propagation ..."
sleep 5
while ! do_request /dns/is-updated.json "domain-name=${domain}" | grep -i true &> /dev/null; do
echo " + waiting for propagation ..."
sleep 30
done
}
clean_challenge() {
echo " + cloudns hook executing: clean_challenge"
local prefix="$(get_prefix ${1})" domain="$(get_delegated_domain ${1})"
test -z "${domain}" && return 1
echo " + retrieving TXT record for ${1}"
local txt_id=$(
do_request \
/dns/records.json \
"domain-name=${domain}" \
| jq -r \
"to_entries | map(.value) | .[] | select(.type == \"TXT\" and .host == \"_acme-challenge${prefix:+.${prefix}}\" and .record == \"${2}\") | .id"
)
test -z "${txt_id}" && return 1
echo " + cleaning TXT record for ${1}"
for record in ${txt_id}; do
do_request \
/dns/delete-record.json \
"domain-name=${domain}&record-id=${record}" \
| grep -i success &> /dev/null
done
}
case "${1:-}" in
deploy_challenge)
deploy_challenge "${2}" "${4}"
;;
clean_challenge)
clean_challenge "${2}" "${4}"
;;
esac