-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook.sh
executable file
·174 lines (145 loc) · 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
_ARGS=(${@})
TRAPPED=false
function safety() {
echo "*** TRAPPED CTRL-C! ***"
if ! $TRAPPED; then
TRAPPED=true
if [[ "${_ARGS[0]}" == "deploy_challenge" ]]; then
echo "*** PERFORMING DNS CLEANUP ***"
clean_challenge ${_ARGS[*]:1}
fi
exit 1
fi
}
trap safety INT
BASEPATH="$(dirname $0)"
function split_domain() {
if [[ -z "${1/*.*.*/}" ]]; then
domain="$(expr match ${1} '.*\.\(.*\..*\)')"
subdomain="${1%.*.*}"
else
domain="${1}"
subdomain=""
fi
}
function get_cflogin() {
# You can use this if you are using multiple CF accounts:
# if [ "${1}" == "domain.one" ]; then
# CF_EMAIL="account@one.com"
# CF_KEY="k3y0n3"
# else
# CF_EMAIL="account@two.com"
# CF_KEY="k3ytw0"
# fi
:
}
function get_zone() {
get_cflogin "${1}"
zone=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=${1}" \
-H "X-Auth-Email: ${CF_EMAIL}" \
-H "X-Auth-Key: ${CF_KEY}" \
-H "Content-Type: application/json" \
| grep -Po '(?<="id":")[^"]*' | head -1)
if [ -z "${zone}" ]; then
echo "[ZONE] Could not get zone identifier for ${domain}" >&2
exit 1
fi
}
function deploy_challenge() {
split_domain "${1}"
get_zone "${domain}"
echo "[CHALLENGE] ${1} CF Zone ID: ${zone}"
idomain=1
itoken=3
while [ -n "${!idomain}" ]; do
echo "[CHALLENGE] ${!idomain}"
split_domain "${!idomain}"
if [ -z "${subdomain}" ]; then
key="${domain}"
else
key="${subdomain}.${domain}"
fi
resp=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records" \
-H "X-Auth-Email: ${CF_EMAIL}" \
-H "X-Auth-Key: ${CF_KEY}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"TXT\",\"name\":\"_acme-challenge.${key}\",\"content\":\"${!itoken}\",\"ttl\":1}")
if [[ "${resp}" != *"\"success\":true"* ]]; then
echo "[CHALLENGE] Error occured, check output below:" >&2
echo "$resp" >&2
exit 1
fi
idomain=$((idomain + 3))
itoken=$((itoken + 3))
done
echo "[CHALLENGE] Waiting for DNS to update..."
sleep 5
idomain=1
itoken=3
while [ -n "${!idomain}" ]; do
digresult="$(dig +short TXT _acme-challenge.${!idomain} | tr -d \")"
while [ "${digresult}" != "${!itoken}" ]; do
echo "[CHALLENGE] Waiting for DNS: ${!idomain} -> \"${digresult}\" != \"${!itoken}\""
sleep 3
digresult="$(dig +short TXT _acme-challenge.${!idomain} | tr -d \")"
done
idomain=$((idomain + 3))
itoken=$((itoken + 3))
done
}
function clean_challenge() {
split_domain "${1}"
get_zone "${domain}"
echo "[CLEAN] ${1} CF Zone ID: ${zone}"
idomain=1
itoken=3
while [ -n "${!idomain}" ]; do
echo "[CLEAN] ${!idomain}"
split_domain "${!idomain}"
if [ -z "${subdomain}" ]; then
key="${domain}"
else
key="${subdomain}.${domain}"
fi
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records?type=TXT&name=_acme-challenge.${key}&content=${!itoken}" \
-H "X-Auth-Email: ${CF_EMAIL}" \
-H "X-Auth-Key: ${CF_KEY}" \
-H "Content-Type: application/json" \
| grep -Po '(?<="id":")[^"]*')
if [ -z "$record" ]; then
echo "[CLEAN] Could not find TXT record _acme-challenge.${key} in zone ${zone}" >&2
echo "[CLEAN] No need to clean up then..." >&2
shift 3
continue
fi
resp=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records/${record}" \
-H "X-Auth-Email: ${CF_EMAIL}" \
-H "X-Auth-Key: ${CF_KEY}" \
-H "Content-Type: application/json")
if [[ "${resp}" != *"\"success\":true"* ]]; then
echo "[CLEAN] Error occured, check output below:" >&2
echo "$resp" >&2
exit 1
fi
idomain=$((idomain + 3))
itoken=$((itoken + 3))
done
}
function deploy_cert() {
domain="${1}"
privkey="${2}"
cert="${4}"
if [[ -f $BASEPATH/deploy.sh ]]; then
echo "[DEPLOY] ./deploy.sh ${domain} ${privkey} ${cert}"
$BASEPATH/deploy.sh ${domain} ${privkey} ${cert}
fi
}
if [[ "${1}" = "deploy_challenge" ]]; then
deploy_challenge ${@:2}
elif [[ "${1}" = "clean_challenge" ]]; then
clean_challenge ${@:2}
elif [[ "${1}" = "deploy_cert" ]]; then
deploy_cert ${@:2}
fi
exit 0