-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_domains_and_update-dns-multiple.sh
More file actions
170 lines (142 loc) · 5.14 KB
/
Copy pathadd_domains_and_update-dns-multiple.sh
File metadata and controls
170 lines (142 loc) · 5.14 KB
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
#!/bin/bash
set -euo pipefail
check_requirements() {
local missing_tools=()
for tool in curl jq; do
if ! command -v "$tool" >/dev/null; then
missing_tools+=("$tool")
fi
done
if [ ${#missing_tools[@]} -ne 0 ]; then
local level="$1"
shift
printf "[%s] [%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$level" "Missing required tools: ${missing_tools[*]}"
exit 1
fi
if [ ! -f "redirect_domains.txt" ]; then
log "ERROR" "domains.txt file not found"
exit 1
fi
}
check_requirements
# Load environment variables
if [ -f .env ]; then
source .env
else
echo "Error: .env file not found" >&2
exit 1
fi
log() {
local level="$1"
shift
printf "[%s] [%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$level" "$*"
}
# Validate API token
log "INFO" "Validating API token..."
token_check=$(curl -s https://api.cloudflare.com/client/v4/user/tokens/verify \
-H "Authorization: Bearer $ACCOUNT_API_TOKEN")
if ! echo "$token_check" | jq -e '.success' >/dev/null; then
log "ERROR" "Invalid API token: $(echo "$token_check" | jq -r '.errors[]?.message')"
log "ERROR" "Please check your token permissions: Zone.Zone (Edit), Zone.DNS (Edit), Zone.Settings (Edit), Account.Account Settings (Read)"
exit 1
fi
log "INFO" "API token validated successfully"
# Validate required environment variables
required_vars=("ACCOUNT_ID" "ACCOUNT_API_TOKEN" "REDIRECT_LIST_NAME" "TARGET_URL" "ENABLE_QUICK_SCAN")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "Error: $var is not set" >&2
exit 1
fi
done
validate_domain() {
local domain="$1"
if [[ ! "$domain" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$ ]]; then
log "ERROR" "Invalid domain name format: $domain"
return 1
fi
}
create_dns_record() {
local type="$1"
local name="$2"
local content="$3"
local response
response=$(curl -s "https://api.cloudflare.com/client/v4/zones/$domain_id/dns_records" \
-H "Authorization: Bearer $ACCOUNT_API_TOKEN" \
-H 'Content-Type: application/json' \
-d @- <<EOF
{
"comment": "Redirection DNS Entry, for bulk rule forwarding with Cloudflare rules.",
"content": "$content",
"name": "$name",
"proxied": true,
"ttl": 3600,
"type": "$type"
}
EOF
)
if ! echo "$response" | jq -e '.success' >/dev/null; then
log "ERROR" "Error creating DNS record: $(echo "$response" | jq -r '.errors[]?.message')"
return 1
fi
}
perform_quick_scan() {
local domain="$1"
local domain_id="$2"
if [ "${ENABLE_QUICK_SCAN,,}" = "true" ]; then
log "INFO" "DNS quick scanning ${domain}"
scan_output=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$domain_id/dns_records/scan" \
-H "Authorization: Bearer $ACCOUNT_API_TOKEN")
if ! echo "$scan_output" | jq -e '.success' >/dev/null; then
log "ERROR" "Quick scan failed: $(echo "$scan_output" | jq -r '.errors[]?.message')"
return 1
fi
echo "$scan_output" | jq .
log "INFO" "Quick scan completed for ${domain}"
else
log "INFO" "Quick scan disabled - skipping for ${domain}"
fi
}
# Initialize array to store domains for bulk redirect
declare -a domains_to_be_added_to_bulk_list
for domain in $(cat redirect_domains.txt); do
log "INFO" "Processing domain: $domain"
validate_domain "$domain"
add_output=$(curl https://api.cloudflare.com/client/v4/zones \
--header "Authorization: Bearer $ACCOUNT_API_TOKEN" \
--header "Content-Type: application/json" \
--data "{
\"account\": {
\"id\": \"$ACCOUNT_ID\"
},
\"name\": \"$domain\",
\"type\": \"full\"
}")
if ! echo "$add_output" | jq -e '.success' >/dev/null; then
log "ERROR" "Failed to add zone for domain $domain"
continue
fi
echo "$add_output" | jq -r '[.result.name,.result.id,.result.name_servers[]] | @csv' >> domain_nameservers.csv
domain_id=$(echo "$add_output" | jq -r .result.id)
# Only add to bulk list if all DNS operations succeed
if create_dns_record "A" "@" "192.0.2.1" && \
create_dns_record "AAAA" "@" "100::" && \
create_dns_record "A" "www" "192.0.2.1" && \
create_dns_record "AAAA" "www" "100::"; then
perform_quick_scan "$domain" "$domain_id"
# Add domain to array for bulk redirect
domains_to_be_added_to_bulk_list+=("$domain")
log "INFO" "Domain $domain successfully processed and added to bulk list queue"
else
log "ERROR" "Failed to create all DNS records for $domain"
fi
printf "\n\n"
done
# Save domains to file for bulk redirect processing
if [ ${#domains_to_be_added_to_bulk_list[@]} -gt 0 ]; then
printf "%s\n" "${domains_to_be_added_to_bulk_list[@]}" > domains_for_bulk_redirect.txt
log "INFO" "Created domains_for_bulk_redirect.txt with ${#domains_to_be_added_to_bulk_list[@]} domains"
else
log "WARNING" "No domains were successfully processed for bulk redirect"
fi
log "INFO" "Name servers are saved in domain_nameservers.csv"