-
Notifications
You must be signed in to change notification settings - Fork 1
/
unhBlock
executable file
·301 lines (252 loc) · 7.25 KB
/
unhBlock
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
#!/bin/sh
# based on unboundbl.sh maintained by alec armbruster (github.com/alectrocute)
# refactor by carlos hernandez (git.io/hrndz) for unbound
# list of urls containing list of domains seving ads and malware
PROGRAM="unhBlock"
UNHBLOCK_CONF="unhBlock.conf"
UNHBLOCK_CONF_DST="/etc/unbound/unbound.conf.d/$UNHBLOCK_CONF"
STATS=false
RELOAD=false
BLACKHOLE="0.0.0.0"
BLACKHOLE_RECORD_TYPE="A"
download_url() {
url="$1"
target_file="$2"
printf " Attempting to download %.185s (via curl).\n" "$url"
if curl --output /dev/null --silent --head --fail "$url"; then
curl -s "$url" >> "$target_file"
cnt=$((cnt+1))
printf " ^ Downloaded %.35s ...successfully.\n" "$url"
else
printf " * Error trying to download %.35s ...\n" "$url"
failed_cnt=$((failed_cnt+1))
fi
}
download_urls_in_list() {
urllist="$1"
target_file="$2"
list_name="$(basename "$urllist")"
printf "\n# Overview\n"
printf " ^ %s URLs to fetchs: %s\n" "$list_name" "$urllist"
# init counter for debugging purposes
cnt=0
failed_cnt=0
printf "# Downloading URLs in %s..." "$list_name"
while read -r url; do
download_url "$url" "$target_file"
done < "$urllist"
printf "\n# Done downloading external URLs\n"
# sort all the lists and remove any whitelist items!
printf " ^ %d %s fetches failed." $failed_cnt "$list_name"
printf " ^ %d %s(s) will be parsed...\n\n" $cnt "$list_name"
}
rm_cruft() {
target_file="$1"
printf " ^ Removing comments, empty lines, + fixing lineendings\n"
perl -i -ne \
'
$line = $_;
$line =~ s/^\s+//g;
$line =~ s/^#.*//g;
$line =~ s/#.*$//g;
$line =~ s/ +$//g;
$line =~ s/\r\n/\n/g;
if ($line ne "\n") {
print lc($line);
}
' "${target_file}"
}
parse_domain_from_hostfile_records() {
target_file="$1"
printf " ^ Parse domains from hostfile like records\n"
perl \
-i -pe \
'
s/(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|^::[A-z0-9_.:]+)\s+([^#\s]+)/$2/g;
' "${target_file}"
}
rm_duplicates() {
target_file="$1"
tmp_file=$(mktemp)
printf " ^ Removing duplicates\n"
sort "${target_file}" | uniq > "${tmp_file}"
mv "${tmp_file}" "${target_file}"
}
rm_whitelist_from_blocklist() {
whitelist="$1"
blocklist="$2"
results=$(mktemp)
printf "\n# Removing whitelist from blocklist\n"
grep -Fvxf "${whitelist}" "${blocklist}" > "${results}"
mv "${results}" "${blocklist}"
}
format_as_unbound_conf() {
target_file="$1"
printf "\n# Formatting records as unbound local data\n"
blackhole=$BLACKHOLE record_type=$BLACKHOLE_RECORD_TYPE \
perl -i -ne \
'
$cnt++;
if ($cnt == 1) {
printf("server:\n")
};
($domain) = split(" ", $_, -1);
printf("local-zone: \"%s\" redirect\n", $domain);
printf("local-data: \"%s %s %s\"\n", $domain, $ENV{record_type}, $ENV{blackhole});
' "${target_file}"
}
filter_domains() {
list="$1"
printf "\n# Filtering domains from blocklist\n"
rm_cruft "$list"
parse_domain_from_hostfile_records "$list"
rm_duplicates "$list"
}
check_unbound_conf() {
conf="$1"
unbound-checkconf "$conf" > /dev/null 2>&1 || \
(printf "Unbound blocklist conf contained errors." && exit 1)
}
place_unboundBL_conf() {
blocklist="$1"
mv "$blocklist" "$UNHBLOCK_CONF_DST"
}
reload_unbound() {
! $RELOAD && return
if unbound-control status > /dev/null 2>&1 ; then
unbound-control reload
fi
}
# create unbound conf
create_and_install_unbound_conf() {
printf "[Starting unHblock update]"
blocklist="$(mktemp)"
download_urls_in_list "$blacklist_sources" "$blocklist"
filter_domains "$blocklist"
if [ -n "$whitelist_sources" ]; then
whitelist="$(mktemp)"
download_urls_in_list "$whitelist_sources" "$whitelist"
filter_domains "$whitelist"
rm_whitelist_from_blocklist "$whitelist" "$blocklist"
fi
format_as_unbound_conf "$blocklist"
check_unbound_conf "$blocklist"
place_unboundBL_conf "$blocklist"
printf "+ %s update complete!\n\n" "$PROGRAM"
}
# to be expanded in the future, stats() or -stats displays
# the amount of domains on the included blocklist.
show_stats() {
! $STATS && return
domains_total=$(grep -Ec "^local-data" "$UNHBLOCK_CONF_DST")
printf "\n# Blocked stats\n"
printf " ^ %s domains are being blocked.\n\n" "$domains_total"
exit 0
}
# displays usage settings for manual usage, if desired
show_usage() {
printf "\nUsage: %s [-s] -u [-w whitelist] blaclist\n\n" "$PROGRAM"
printf " -h, --help Display usage instructions\n"
printf " -b, --blackhole Blackhole destination (0.0.0.0).\n"
printf " -r, --record Blackhole record type (A).\n"
printf " -d, --dest System path to place/source Unbound foramtted blocklist(s).\n"
printf " -s, --stats Display basic statistics of blocklist(s).\n"
printf " -w, --whitelist file Whitelist sources to omit form blocklist.\n\n"
}
check_arguments() {
$STATS && return
if [ -z "$blacklist_sources" ] || [ ! -f "$blacklist_sources" ]; then
printf "Missing blocklist file.\n"
exit 1
fi
if [ -n "$whitelist_sources" ]; then
[ -f "$whitelist_sources" ] || \
(printf "File containing whitelist sources not found.\n" \
&& exit 1)
fi
}
parse_arguments() {
# shell script functionality
while getopts ":w:b:t:d:hrs-:" opt; do
case $opt in
w)
whitelist_sources="$OPTARG"
;;
b)
BLACKHOLE="$OPTARG"
;;
t)
BLACKHOLE_RECORD_TYPE="$OPTARG"
;;
d)
UNHBLOCK_CONF_DST="$OPTARG"
;;
r)
RELOAD=true
;;
s)
STATS=true
;;
h)
show_usage >&2
exit
;;
- )
case $OPTARG in
whitelist)
whitelist_sources="$OPTARG"
;;
blackhole)
BLACKHOLE="$OPTARG"
;;
type)
BLACKHOLE_RECORD_TYPE="$OPTARG"
;;
dest)
UNHBLOCK_CONF_DST="$OPTARG"
;;
reload)
RELOAD=true
;;
stats)
STATS=true
;;
help)
show_usage >&2
exit
;;
*)
echo "Invalid option: --$OPTARG" >&2
exit 2
;;
esac
;;
:)
echo 'Missing argument!' >&2
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 2
;;
esac
done
shift $((OPTIND-1))
blacklist_sources="$1"
check_arguments
}
check_sys_prereqs() {
command -v unbound-checkconf > /dev/null || \
(echo "Missing unbound-checkconf" && exit 1)
}
main() {
check_sys_prereqs
parse_arguments "$@"
show_stats
create_and_install_unbound_conf
reload_unbound
}
# Support sourcing
if [ "$(basename -- "$0")" = "$PROGRAM" ]; then
main "$@"
fi