-
Notifications
You must be signed in to change notification settings - Fork 2
/
adblockrouter.sh
executable file
·132 lines (118 loc) · 3.74 KB
/
adblockrouter.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
#!/usr/bin/env bash
#
# Adblockrouter script to grab and sort a list of adservers and
# malware, suitable for use in a DNS server like dnsmasq.
#
# Copyright (C) 2014-2017 Simon Eisenmann <[email protected]>
#
# Adblockrouter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Adblockrouter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
set -e
FOLDER=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
BLACKLIST="$FOLDER/black.list"
WHITELIST="$FOLDER/white.list"
POSTSCRIPT="$FOLDER/post.sh"
MODE=dnsmasq
TARGET="$FOLDER/block.hosts"
function usage {
echo "Usage: $0 [-m dnsmasq|unbound] [-t <target>] [-bl <blacklist>] [-wl <whitelist>] [-s <post-script>]"
}
# Parse parameters
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-m|--mode)
MODE="$2"
shift
;;
-t|--target)
TARGET="$2"
shift
;;
-bl|--blacklist)
BLACKLIST="$2"
shift
;;
-wl|--whitelist)
WHITELIST="$2"
shift
;;
-s|--script)
POSTSCRIPT="$2"
shift
;;
-h|--help)
usage
exit 0
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
# Tmp.
TMPDIR=$(mktemp -d -t adblockrouter.XXXXXXXXXX)
function cleanup {
rm -rf "$TMPDIR"
}
trap cleanup EXIT
# Download and process the files needed to make the lists (add more, if you want).
echo '> Downloading hosts lists'
wget -qO- http://www.mvps.org/winhelp2002/hosts.txt| awk '/^0.0.0.0/' > $TMPDIR/block.build.list
wget -qO- http://www.malwaredomainlist.com/hostslist/hosts.txt|awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> $TMPDIR/block.build.list
wget -qO- "http://hosts-file.net/.\ad_servers.txt"|awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> $TMPDIR/block.build.list
wget -qO- --no-check-certificate "https://adaway.org/hosts.txt"|awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> $TMPDIR/block.build.list
wget -qO- "http://someonewhocares.org/hosts/hosts"|awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> $TMPDIR/block.build.list
# Add black list, if non-empty.
if [ -s "$BLACKLIST" ]
then
echo '> Adding blacklist'
awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >> $TMPDIR/block.build.list
fi
# Sort the download/black lists.
echo '> Sorting'
awk '{sub(/\r$/,"");print $1,$2}' $TMPDIR/block.build.list | sort -u > $TMPDIR/block.build.before
if [ -s "$WHITELIST" ]
then
# Filter according to whitelist matches.
echo '> Applying whitelist'
awk '/^[^#]/ {sub(/\r$/,"");print $1}' "$WHITELIST" | grep -vf - $TMPDIR/block.build.before > $TMPDIR/block.build
else
cat "$TMPDIR/block.build.before" > $TMPDIR/block.build
fi
echo "> Output mode: $MODE"
case "$MODE" in
dnsmasq)
# Add ipv6 support
sed -re 's/^(0\.0\.0\.0) (.*)$/\1 \2\n:: \2/g' $TMPDIR/block.build >$TMPDIR/block.hosts
;;
unbound)
sed -re 's/^(0\.0\.0\.0) (.*)$/local-data: \"\2 A \1\"\nlocal-data: \"\2 AAAA ::\"/g' $TMPDIR/block.build >$TMPDIR/block.hosts
;;
*)
echo "E: Unknown mode '$MODE'"
exit 1
;;
esac
# Write output.
cp -f $TMPDIR/block.hosts "${TARGET}.new"
mv -f "${TARGET}.new" "$TARGET"
# Notify.
if [ -x "$POSTSCRIPT" ]
then
echo "> Running post script"
("$POSTSCRIPT" "$TARGET" "$MODE")
fi
echo "> Done"
exit 0