forked from dibdot/DoH-IP-blocklists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoh-lookup.sh
executable file
·178 lines (169 loc) · 6.13 KB
/
doh-lookup.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
#!/bin/sh
# doh-lookup - retrieve IPv4/IPv6 addresses via dig from a given domain list
# and write the adjusted output to separate lists (IPv4/IPv6 addresses plus domains)
# Copyright (c) 2019-2023 Dirk Brenken ([email protected])
#
# This is free software, licensed under the GNU General Public License v3.
# disable (s)hellcheck in release
# shellcheck disable=all
# prepare environment
#
export LC_ALL=C
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
input1="./doh-domains_overall.txt"
input2="input2.txt"
input3="input3.txt"
upstream="8.8.8.8"
check_domains="google.com heise.de openwrt.org"
cache_domains="doh.dns.apple.com doh.dns.apple.com.v.aaplimg.com mask-api.icloud.com mask-h2.icloud.com mask.icloud.com dns.nextdns.io"
wc_tool="$(command -v wc)"
dig_tool="$(command -v dig)"
awk_tool="$(command -v awk)"
: >"./${input2}"
: >"./${input3}"
: >"./ipv4.tmp"
: >"./ipv6.tmp"
: >"./ipv4_cache.tmp"
: >"./ipv6_cache.tmp"
: >"./domains.tmp"
: >"./domains_abandoned.tmp"
# sanity pre-checks
#
if [ ! -x "${wc_tool}" ] || [ ! -x "${dig_tool}" ] || [ ! -x "${awk_tool}" ] || [ ! -s "${input1}" ] || [ -z "${upstream}" ]; then
printf "%s\n" "ERR: general pre-check failed"
exit 1
fi
for domain in ${check_domains}; do
out="$("${dig_tool}" "${domain}" A "${domain}" AAAA +noall +answer +time=5 +tries=1 2>/dev/null)"
if [ -z "${out}" ]; then
printf "%s\n" "ERR: domain pre-check failed"
exit 1
else
ips="$(printf "%s" "${out}" | "${awk_tool}" '/^.*[[:space:]]+IN[[:space:]]+A{1,4}[[:space:]]+/{printf "%s ",$NF}')"
if [ -z "${ips}" ]; then
printf "%s\n" "ERR: ip pre-check failed"
exit 1
fi
fi
done
# pre-fill cache domains
#
for domain in ${cache_domains}; do
"${awk_tool}" -v d="${domain}" '$0~d{print $0}' "./doh-ipv4.txt" >>"./ipv4_cache.tmp"
"${awk_tool}" -v d="${domain}" '$0~d{print $0}' "./doh-ipv6.txt" >>"./ipv6_cache.tmp"
done
# domain processing (first run)
#
cnt="0"
doh_start1="$(date "+%s")"
doh_cnt="$("${awk_tool}" 'END{printf "%d",NR}' "./${input1}" 2>/dev/null)"
printf "%s\n" "::: Start DOH-processing, overall domains: ${doh_cnt}"
while IFS= read -r domain; do
(
domain_ok="false"
out="$("${dig_tool}" "${domain}" A "${domain}" AAAA +noall +answer +time=5 +tries=1 2>/dev/null)"
if [ -n "${out}" ]; then
ips="$(printf "%s" "${out}" | "${awk_tool}" '/^.*[[:space:]]+IN[[:space:]]+A{1,4}[[:space:]]+/{printf "%s ",$NF}')"
if [ -n "${ips}" ]; then
for ip in ${ips}; do
if [ "${ip%%.*}" = "127" ] || [ "${ip%%.*}" = "0" ] || [ -z "${ip%%::*}" ]; then
continue
else
if ipcalc-ng -cs "${ip}"; then
domain_ok="true"
if [ "${ip##*:}" = "${ip}" ]; then
printf "%-20s%s\n" "${ip}" "# ${domain}" >>"./ipv4.tmp"
else
printf "%-40s%s\n" "${ip}" "# ${domain}" >>"./ipv6.tmp"
fi
fi
fi
done
else
printf "%s\n" "$domain" >>"./${input2}"
fi
fi
if [ "${domain_ok}" = "false" ]; then
printf "%s\n" "${domain}" >>./domains_abandoned.tmp
else
printf "%s\n" "${domain}" >>./domains.tmp
fi
) &
hold1="$((cnt % 512))"
hold2="$((cnt % 2048))"
[ "${hold1}" = "0" ] && sleep 3
[ "${hold2}" = "0" ] && wait
cnt="$((cnt + 1))"
done <"${input1}"
wait
error_cnt="$("${awk_tool}" 'END{printf "%d",NR}' "./${input2}" 2>/dev/null)"
doh_end="$(date "+%s")"
doh_duration="$(((doh_end - doh_start1) / 60))m $(((doh_end - doh_start1) % 60))s"
printf "%s\n" "::: First run, duration: ${doh_duration}, processed domains: ${cnt}, error domains: ${error_cnt}"
# domain processing (second run)
#
cnt="0"
doh_start2="$(date "+%s")"
while IFS= read -r domain; do
(
domain_ok="false"
out="$("${dig_tool}" "@${upstream}" "${domain}" A "${domain}" AAAA +noall +answer +time=5 +tries=1 2>/dev/null)"
if [ -n "${out}" ]; then
ips="$(printf "%s" "${out}" | "${awk_tool}" '/^.*[[:space:]]+IN[[:space:]]+A{1,4}[[:space:]]+/{printf "%s ",$NF}')"
if [ -n "${ips}" ]; then
for ip in ${ips}; do
if [ "${ip%%.*}" = "0" ] || [ -z "${ip%%::*}" ]; then
continue
else
if ipcalc-ng -cs "${ip}"; then
domain_ok="true"
if [ "${ip##*:}" = "${ip}" ]; then
printf "%-20s%s\n" "${ip}" "# ${domain}" >>"./ipv4.tmp"
else
printf "%-40s%s\n" "${ip}" "# ${domain}" >>"./ipv6.tmp"
fi
fi
fi
done
else
printf "%s\n" "$domain" >>"./${input3}"
fi
fi
if [ "${domain_ok}" = "false" ]; then
printf "%s\n" "${domain}" >>./domains_abandoned.tmp
else
printf "%s\n" "${domain}" >>./domains.tmp
fi
) &
hold1="$((cnt % 512))"
hold2="$((cnt % 2048))"
[ "${hold1}" = "0" ] && sleep 3
[ "${hold2}" = "0" ] && wait
cnt="$((cnt + 1))"
done <"${input2}"
wait
error_cnt="$("${awk_tool}" 'END{printf "%d",NR}' "./${input3}" 2>/dev/null)"
doh_end="$(date "+%s")"
doh_duration="$(((doh_end - doh_start2) / 60))m $(((doh_end - doh_start2) % 60))s"
printf "%s\n" "::: Second run, duration: ${doh_duration}, processed domains: ${cnt}, error domains: ${error_cnt}"
# sanity re-check
#
if [ ! -s "./ipv4.tmp" ] || [ ! -s "./ipv6.tmp" ] || [ ! -s "./domains.tmp" ] || [ ! -f "./domains_abandoned.tmp" ]; then
printf "%s\n" "ERR: general re-check failed"
exit 1
fi
# final sort/merge step
#
sort -b -u -n -t. -k1,1 -k2,2 -k3,3 -k4,4 "./ipv4_cache.tmp" "./ipv4.tmp" >"./doh-ipv4.txt"
sort -b -u -k1,1 "./ipv6_cache.tmp" "./ipv6.tmp" >"./doh-ipv6.txt"
sort -b -u "./domains.tmp" >"./doh-domains.txt"
sort -b -u "./domains_abandoned.tmp" >"./doh-domains_abandoned.txt"
cnt_cache_tmpv4="$("${awk_tool}" 'END{printf "%d",NR}' "./ipv4_cache.tmp" 2>/dev/null)"
cnt_cache_tmpv6="$("${awk_tool}" 'END{printf "%d",NR}' "./ipv6_cache.tmp" 2>/dev/null)"
cnt_tmpv4="$("${awk_tool}" 'END{printf "%d",NR}' "./ipv4.tmp" 2>/dev/null)"
cnt_tmpv6="$("${awk_tool}" 'END{printf "%d",NR}' "./ipv6.tmp" 2>/dev/null)"
cnt_ipv4="$("${awk_tool}" 'END{printf "%d",NR}' "./doh-ipv4.txt" 2>/dev/null)"
cnt_ipv6="$("${awk_tool}" 'END{printf "%d",NR}' "./doh-ipv6.txt" 2>/dev/null)"
doh_end="$(date "+%s")"
doh_duration="$(((doh_end - doh_start1) / 60))m $(((doh_end - doh_start1) % 60))s"
printf "%s\n" "::: Finished DOH-processing, duration: ${doh_duration}, cachev4/cachev6: ${cnt_cache_tmpv4}/${cnt_cache_tmpv6}, all/unique IPv4: ${cnt_tmpv4}/${cnt_ipv4}, all/unique IPv6: ${cnt_tmpv6}/${cnt_ipv6}"