-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfcontrol.sh
executable file
·457 lines (413 loc) · 13.1 KB
/
cfcontrol.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/bin/bash
## var declaration
declare proxy
declare output
declare zoneid
fullstring=( "$@" )
echo "${fullstring[*]}"
function addSites () {
while IFS= read -r site; do
addSite
done < <(grep -v '^ *#' < "$importlist")
}
function addSubDomainAs () {
while IFS= read -r subdomain; do
addSubDomainA
done < <(grep -v '^ *#' < "$importlist")
}
function addSubDomainCs () {
while IFS= read -r subdomain; do
addSubDomainC
done < <(grep -v '^ *#' < "$importlist")
}
function addSite () {
output=$(curl -s -X POST \
-H "X-Auth-Key: $CF_API_KEY" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "Content-Type: application/json" "https://api.cloudflare.com/client/v4/zones" \
-d "{\
\"account\":{\
\"id\":\"$ACCOUNT_ID\"
},\
\"name\":\"$site\",\
\"jump_start\":true\
}"
)
results "$output"
}
addSubDomainA () {
local subdomain="$1"
local site="$2"
local ip="$3"
echo "IP: $ip"
echo "Sub-Domain: $subdomain"
echo "Site: ${site}"
getZone "$site"
echo "Zone ID: ${zoneid}"
output=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${zoneid}/dns_records" \
-H "X-Auth-Email: ${CF_API_EMAIL}" \
-H "X-Auth-Key: ${CF_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"type":"A",
"name":"'$subdomain'",
"content":"'$ip'",
"ttl":3600,
"priority":10,
"proxied":'$proxy'
}'
)
thisrecord="arecord"
output=$(echo "$output")
results "$output"
}
function addSubDomainC () {
local subdomain="$1"
local site="$2"
local cnamerecord="$3"
getZone "$site"
local url="https://api.cloudflare.com/client/v4/zones/${zoneid}/dns_records"
output=$(curl -s -X POST "$url" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type":"CNAME",
"name":"'"$subdomain"'",
"content":"'"$cnamerecord"'",
"ttl":3600,
"priority":10,
"proxied":'"$proxy"'
}'
)
thisrecord="cnamerecord"
results "$output"
}
function listDnsRecords() {
local domain="$2"
local record_type="$1"
getZone "$domain"
local url="https://api.cloudflare.com/client/v4/zones/${zoneid}/dns_records"
if [[ $debug -eq 1 ]]; then
echo "DEBUG listDnsRecords:"
echo "Zone ID: $zoneid"
fi
if [[ -n "$record_type" ]]; then
url="${url}"
fi
output=$(curl -s -X GET "$url" \
-H "X-Auth-Email: ${CF_API_EMAIL}" \
-H "X-Auth-Key: ${CF_API_KEY}" \
-H "Content-Type: application/json")
# Parse JSON and convert to table
echo "ID | TYPE | NAME | CONTENT | TTL | PROXIED"
echo "-------------------------------------------"
echo "$output" | jq -r '.result[] | "\(.id) | \(.type) | \(.name) | \(.content) | \(.ttl) | \(.proxied)"'
echo ""
echo ""
echo "${output}"
}
proxyFlag() {
if [[ "${fullstring[*]}" =~ "-proxy" ]]; then
proxy=true
else
proxy=false
fi
echo "${fullstring[*]}"
echo "Proxy: ${proxy}"
}
formatting() {
## Formatting and styles
#BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BOLD=$(tput bold)
RESET=$(tput sgr0)
}
getZone() {
site=$1
url="https://api.cloudflare.com/client/v4/zones?name=${site}&status=active&account.id=${ACCOUNT_ID}&page=1&per_page=100&order=status&direction=desc&match=all"
output=$(curl -s -X GET "$url" \
-H "X-Auth-Email: ${CF_API_EMAIL}" \
-H "X-Auth-Key: ${CF_API_KEY}" \
-H "Content-Type: application/json")
zoneid=$(echo "$output" | jq '.result[].id' | tr -d '"')
echo "ZONE ID: ${zoneid}"
if [[ $debug -eq 1 ]]; then
echo DEBUG getZone:
echo "Raw output:"
echo "$output"
fi
}
results() {
output="${*}"
echo "$output"
success=$( [[ "$output" =~ "\"success\":true" ]] && echo "true" || echo "false" )
proxyresult=$( [[ "$output" =~ "\"proxied\":true" ]] && echo "true" || echo "false" )
if [[ $success == "true" ]]; then
echo "The command completed successfully"
else
echo "The command did not complete successfully"
fi
if [[ $proxyresult == "true" ]]; then
echo "${GREEN}Proxy enabled${WHITE}"
fi
if [[ "$thisrecord" == "arecord" || "$thisrecord" == "cnamerecord" ]]; then
echo "${subdomain} now points to ${arecord}"
fi
}
sourceEnv() {
## Cloudflare Control Script for the management of domains using Cloudflare's API
if [[ -e "${HOME}/.cfcontrol/.env" ]]; then
# shellcheck source=./.env
echo "Using ${HOME}/.cfcontrol/.env"
source "${HOME}/.cfcontrol/.env"
elif [[ -e "${PWD}/.env" ]]; then
echo "Using ${PWD}/.cfcontrol/.env"
source "${PWD}/.env"
else
echo "Did not find a .env file in ~/.config/cfcontrol/ or your current working directory."
echo "Using defaults"
fi
}
sourceSecrets() {
local secretsfile="${SECRETS_FILE:-$HOME/.cfcontrol/.secrets}"
local found=0
fullstring=( "$@" )
# Check for -email and -apikey flags
for ((i=0; i<${#fullstring[@]}; i++)); do
if [[ "${fullstring[i]}" == "-email" && $((i+1)) -lt ${#fullstring[@]} ]]; then
CF_API_EMAIL="${fullstring[i+1]}"
elif [[ "${fullstring[i]}" == "-apikey" && $((i+1)) -lt ${#fullstring[@]} ]]; then
CF_API_KEY="${fullstring[i+1]}"
elif [[ "${fullstring[i]}" == "-secrets" && $((i+1)) -lt ${#fullstring[@]} ]]; then
secretsfile="${fullstring[i+1]}"
fi
done
# If email and API key are not set, read from secrets file
if [[ -z "$CF_API_KEY" || -z "$CF_API_EMAIL" ]]; then
if [[ -e "$secretsfile" ]]; then
while read -r LINE; do
if [[ $LINE != '#'* ]] && [[ $LINE == *'='* ]]; then
export "$LINE"
[[ $LINE == CF_API_KEY=* ]] && CF_API_KEY="${LINE#*=}"
[[ $LINE == CF_API_EMAIL=* ]] && CF_API_EMAIL="${LINE#*=}"
fi
done < "$secretsfile"
found=1
fi
fi
# Prompt for credentials if still not set
if [[ -z "$CF_API_KEY" || -z "$CF_API_EMAIL" ]]; then
echo "No valid .secrets file found. Please add a .secrets file to ~/.config/cfcontrol/ or your current working directory."
read -p "Would you like to enter your credentials now? (y/n): " choice
if [[ "$choice" == "y" ]]; then
read -p "Enter CF_API_KEY: " CF_API_KEY
read -p "Enter CF_API_EMAIL: " CF_API_EMAIL
mkdir -p "${HOME}/.cfcontrol"
echo "CF_API_KEY=$CF_API_KEY" > "${HOME}/.cfcontrol/.secrets"
echo "CF_API_EMAIL=$CF_API_EMAIL" >> "${HOME}/.cfcontrol/.secrets"
export CF_API_KEY
export CF_API_EMAIL
echo "Credentials saved to ${HOME}/.cfcontrol/.secrets and exported."
else
echo "Exiting script."
exit 1
fi
fi
echo "CF_API_KEY and CF_API_EMAIL are set."
if [[ $debug -eq 1 ]]; then
echo "sourceSecrets:"
echo "CF_API_KEY=${CF_API_KEY}"
echo "CF_API_EMAIL=${CF_API_EMAIL}"
fi
}
# Example usage:
# sourceSecrets /path/to/your/secretsfile
testFunction() {
sourceSecrets
echo "fullstring: ${fullstring[@]}"
echo "uncomment test values as required"
# echo "Subdomain: $subdomain"
echo "Site Domain: $sitedomain to $arecord"
echo "API Key: $CF_API_KEY"
echo "API Email: $CF_API_EMAIL"
echo "Zone ID: ${zoneid}"
}
function headerGraphic() {
printf "
%s
_ _ __ _
| | | |/ _| |
___| | ___ _ _ __| | |_| | __ _ _ __ ___
/ __| |/ _ \| | | |/ _\` | _| |/ _\` | \'__/ _ \\
| (__| | (_) | |_| | (_| | | | | (_| | | | __/
\___|_|\___/ \__,_|\__,_|_| |_|\__,_|_| \___|
%s=============================================
%sC O N T R O L %s/%s * %s\%s S C R I P T%s
" "$MAGENTA" "$RED" "$WHITE" "$RED" "$BLUE" "$RED" "$WHITE" "$RESET"
}
function usage() {
headerGraphic
printf "
You need to specify parameters and arguments to use this script.
Add an A record to a pre-existing site:
%s cfcontrol %s-A%s subdomain.sitedomain.co.uk%s 122.222.211.212%s [flags]%s
Add a CNAME record to a pre-existing site:
%s cfcontrol %s-C%s subdomain.sitedomain.co.uk %starget.domain.com%s [flags]%s
Create a new site:
%s cfcontrol %s-S%s mynewsite.com%s [optional flags]%s
You can bulk import any record type by using a .txt or .csv file after the -A or -C specifier:
%s cfcontrol %s-A%s /home/subdomains.txt%s [flags]%s
Return a list of recrods:
%s cfcontrol %s-L%s sites
%s cfcontrol %s-L%s dns%s <domain> %s[optional flags]%s
Additional features and debugging can be enabled by appending flags to the end of your command:
%s -proxy%s Toggles Cloudflare Proxy (works with A and C records only)
%s -overwrite%s Overwrite (modify) record if it already exists
%s -debug%s Enable debugging to the console
%s -email%s <email>%s Enter your Cloudflare email credential as part of the command
%s -apikey%s <apikey>%s Enter your Cloudflare API key as part of the command
%s -secrets%s <file>%s Specify path of file containing CF_API_EMAIL and CF_API_KEY
%s" "$MAGENTA" "$RESET" "$RED" "$CYAN" "$WHITE" "$RESET" \
"$MAGENTA" "$RESET" "$RED" "$CYAN" "$WHITE" "$RESET" \
"$MAGENTA" "$RESET" "$RED" "$WHITE" "$RESET" \
"$MAGENTA" "$RESET" "$RED" "$WHITE" "$RESET" \
"$MAGENTA" "$RESET" "$RED" "$CYAN" "$RESET" \
"$WHITE" "$RESET" \
"$WHITE" "$RESET" \
"$WHITE" "$RESET" \
"$WHITE" "$CYAN" "$RESET" \
"$WHITE" "$CYAN" "$RESET"
}
function handleARecord() {
headerGraphic
echo "A RECORD"
if [[ "$2" =~ "*.txt" || "$2" =~ "*.csv" ]]; then
local file="$2"
local bulk=1
else
local fqdn="$2"
local bulk=0
fi
if [[ $bulk -eq 1 ]]; then
echo "Bulk file import specified."
addSubDomainAs "$file"
else
local ip="$3"
local subdomain=$(echo "$fqdn" | sed 's/\([a-zA-Z0-9_\-]\)\..*/\1/')
local site=$(echo "$fqdn" | cut -d . -f 2-)
echo "Single record specified."
proxyFlag
addSubDomainA "$subdomain" "$site" "$ip"
fi
}
function handleCRecord() {
local fqdn="$2"
local cnamerecord="$3"
local subdomain=$(echo "$fqdn" | sed 's/\([a-zA-Z0-9_\-]\)\..*/\1/')
local site=$(echo "$fqdn" | cut -d . -f 2-)
headerGraphic
echo "CNAME RECORD"
if [[ $debug -eq 1 ]]; then
echo "DEBUG handleCRecord:"
echo "cnamerecord=$cnamerecord"
echo "subdomain=$subdomain"
echo "site=$site"
fi
if [[ $2 =~ "txt" || $2 =~ "csv" ]]; then
importlist="$2"
echo "Bulk file import specified."
addSubDomainCs
else
echo "Single record specified."
proxyFlag
addSubDomainC "$subdomain" "$site" "$cnamerecord"
fi
}
function handleSite() {
local site="$2"
headerGraphic
echo "SITE"
if [[ $3 =~ "txt" || $3 =~ "csv" ]]; then
importlist="$2"
echo "Bulk file import specified."
addSites
else
echo "Single record specified."
addSite
fi
}
handleListDnsRecords() {
local domain="$3"
local record_type="$2"
headerGraphic
echo "LIST DNS RECORDS"
if [[ $debug -eq 1 ]]; then
echo "DEBUG:"
echo "domain=$domain"
echo "record_type=$record_type"
fi
listDnsRecords "$record_type" "$domain"
}
function handleListSites() {
headerGraphic
echo "LIST SITES"
# Add your logic to list sites here
}
function detectDebugFlag() {
debug=0
if [[ " ${fullstring[*]} " =~ " -debug " ]]; then
debug=1
echo "DEBUGGING IS ON"
fi
echo "detectDebugFlag ran"
echo debug="$debug"
echo fullstring="${fullstring[*]}"
}
formatting
sourceEnv
sourceSecrets "${fullstring[@]}"
detectDebugFlag "${fullstring[@]}"
case "$1" in
-A)
handleARecord "${fullstring[@]}"
exit 0
;;
-C)
handleCRecord "${fullstring[@]}"
exit 0
;;
-S)
handleSite "${fullstring[@]}"
exit 0
;;
-L)
case "$2" in
dns)
handleListDnsRecords "${fullstring[@]}"
;;
site)
handleListSites "${fullstring[@]}"
;;
*)
echo "Invalid list type. Use 'dns' or 'site'."
usage
;;
esac
exit 0
;;
-test)
testFunction "${fullstring[@]}"
exit
;;
"" | * )
usage
;;
esac
exit