-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhblock
executable file
·74 lines (63 loc) · 1.94 KB
/
hblock
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
#!/usr/bin/env bash
#
# Simple ad- and malware-blocking-list builder for dnsmasq.
#
# Inspired by hostblock (http://gaenserich.github.com/hostsblock)
(($#)) &&
exec -- sudo -- "$(type -P "${BASH_SOURCE[0]}")"
typeset -x \
LC_ALL=C \
hblock_cachedir=${TMPDIR:-/tmp}/hostfiles \
hblock_tmpdir=${TMPDIR:-/tmp}/hblock \
hblock_white=/home/user1/etc/hblock/white.txt
#hostfile_regular=/etc/hosts.regular
hblock_hlist=/home/user1/etc/hblock/hlist.txt
hblock_black=/home/user1/etc/hblock/black.txt
hostfile_block=/etc/hosts.block
function __hblock_do {
typeset outfile=${1//http:\/\//}
outfile=${outfile//[\/\%\&\+\?\=\\]/\.}
exec -- wget --no-verbose \
--connect-timeout=3 \
--content-on-error \
--dns-timeout=3 \
--ignore-length \
--inet4-only \
--no-cache \
--no-cookies \
--no-http-keep-alive \
--read-timeout=3 \
--tries=1 \
-O - \
"$1" |
exec -- tee "$hblock_cachedir/$outfile.dirty" |
exec -- sed -rn '
/^(0.0.0.0|127.0.0.1)/ {
s/^127.0.0.1/0.0.0.0/;
s/[[:space:]][[:space:]]*/ /g;
s/[[:space:]]#.*$//;
s/[[:space:]]*$//;
s/^$//;
p
}
' |
exec -- grep -E -v -f "$hblock_white" >"$hblock_tmpdir/$outfile.clean"
}
typeset -xf __hblock_do
trap "exec -- rm -fr -- '$hblock_tmpdir'; exit 0" INT TERM QUIT EXIT
command -- mkdir -p -- "$hblock_cachedir" "$hblock_tmpdir"
## Do the thing.
command -- xargs \
-a "$hblock_hlist" \
-n 1 \
-P 0 \
bash -c '__hblock_do "$@"' --
command -- cat -- "$hblock_black" <(
echo
echo
exec -- sort -u -- "$hblock_tmpdir/"*.clean
) >"$hostfile_block"
command -- chmod 644 "$hostfile_block" /etc/resolv-dnsmasq.conf
command -- rm -f /var/run/dnsmasq/resolv.conf /run/dnsmasq/resolv.conf
exec -- systemctl restart dnsmasq
# vim: set ft=zsh :