1
+ #! /usr/bin/env bash
2
+ # FreeContributor: Enjoy a safe and faster web experience
3
+ # (c) 2016 by TBDS
4
+ # https://github.com/tbds/FreeContributor
5
+ #
6
+ # Simple script that pulls ad blocking host files from different providers
7
+ # and combines them to use as a dnsmasq resolver file.
8
+ #
9
+ # A, --address=/<domain>/ [domain/] <ipaddr>
10
+ # Specify an IP address to return for any host in the given
11
+ # domains. Queries in the domains are never forwarded and always
12
+ # replied to with the specified IP address which may be IPv4 or
13
+ # IPv6. To give both IPv4 and IPv6 addresses for a domain, use
14
+ # repeated -A flags. Note that /etc/hosts and DHCP leases over-
15
+ # ride this for individual names. A common use of this is to redi-
16
+ # rect the entire doubleclick.net domain to some friendly local
17
+ # web server to avoid banner ads. The domain specification works
18
+ # in the same was as for --server, with the additional facility
19
+ # that /#/ matches any domain. Thus --address=/#/1.2.3.4 will
20
+ # always return 1.2.3.4 for any query not answered from /etc/hosts
21
+ # or DHCP and not sent to an upstream nameserver by a more spe-
22
+ # cific --server directive."
23
+ #
24
+ # FreeContributor is free software: you can redistribute it and/or modify
25
+ # it under the terms of the GNU General Public License as published by
26
+ # the Free Software Foundation, either version 3 of the License, or
27
+ # (at your option) any later version.
28
+
29
+ # Dependencies:
30
+ # * curl
31
+ # * dnsmasq
32
+ # * GNU coreutils
33
+
34
+ # Declair variables
35
+ resolv=/etc/resolv.conf
36
+ dnsmasqdir=/etc/dnsmasq.d
37
+ dnsmasqconf=/etc/dnsmasq.conf
38
+ dnsmasqconfbak=/etc/dnsmasq.conf.bak
39
+
40
+ welcome (){
41
+ echo "
42
+ _____ ____ _ _ _ _
43
+ | ___| __ ___ ___ / ___|___ _ __ | |_ _ __(_) |__ _ _| |_ ___ _ __
44
+ | |_ | '__/ _ \/ _ \ | / _ \| '_ \| __| '__| | '_ \| | | | __/ _ \| '__|
45
+ | _|| | | __/ __/ |__| (_) | | | | |_| | | | |_) | |_| | || (_) | |
46
+ |_| |_| \___|\___|\____\___/|_| |_|\__|_| |_|_.__/ \__,_|\__\___/|_|
47
+
48
+
49
+ Enjoy a safe and faster web experience
50
+
51
+ FreeContributor - http://github.com/tbds/FreeContributor
52
+ Released under the GPLv3 license
53
+ (c) 2016 tbds and contributors
54
+
55
+ "
56
+ }
57
+
58
+ rootcheck (){
59
+ if [[ $UID -ne 0 ]]; then
60
+ echo " Please run this script as root"
61
+ exit 1
62
+ fi
63
+ }
64
+
65
+ dependencies (){
66
+ programs=( wget curl sed unzip 7z dnsmasq )
67
+ for prg in " ${programs[@]} "
68
+ do
69
+ type -P $prg & > /dev/null || { echo " Error: FreeConributor requires the program $prg ... Aborting." ; echo ; exit 192; }
70
+ done
71
+ }
72
+
73
+ backup (){
74
+ if [ ! -f " $dnsmasqconf " ] ; then
75
+ echo " Backing up your previous dnsmasq file"
76
+ sudo cp $dnsmasqconf $dnsmasqconfbak
77
+ fi
78
+ }
79
+
80
+ download_sources (){
81
+ # # See FilterLists for a comprehensive list of filter lists from all over the web
82
+ # # https://filterlists.com/
83
+
84
+ sources=(\
85
+ ' https://adaway.org/hosts.txt' \
86
+ ' http://winhelp2002.mvps.org/hosts.txt' \
87
+ ' http://hosts-file.net/.\ad_servers.txt' \
88
+ ' http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext' \
89
+ ' http://someonewhocares.org/hosts/hosts' \
90
+ ' http://sysctl.org/cameleon/hosts' \
91
+ ' http://securemecca.com/Downloads/hosts.txt' \
92
+ ' https://raw.githubusercontent.com/StevenBlack/hosts/master/data/StevenBlack/hosts' \
93
+ # 'https://hosts.neocities.org/' \
94
+ ' http://www.malwaredomainlist.com/hostslist/hosts.txt' \
95
+ ' http://malwaredomains.lehigh.edu/files/justdomains' \
96
+ ' http://adblock.gjtech.net/?format=hostfile' \
97
+ ' https://raw.githubusercontent.com/quidsup/notrack/master/trackers.txt'
98
+ # 'https://publicsuffix.org/list/effective_tld_names.dat' \
99
+ # 'http://jansal.googlecode.com/svn/trunk/adblock/hosts' \
100
+ # 'http://malwaredomains.lehigh.edu/files/justdomains' \
101
+ # 'http://cdn.files.trjlive.com/hosts/hosts-v8.txt' \
102
+ # 'https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt' \
103
+ # 'https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt' \
104
+ # 'https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist' \
105
+ # 'http://tcpdiag.dl.sourceforge.net/project/adzhosts/HOSTS.txt' \
106
+ # 'http://optimate.dl.sourceforge.net/project/adzhosts/HOSTS.txt' \
107
+ # 'http://mirror1.malwaredomains.com/files/justdomains' \
108
+ # 'https://raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt' \
109
+ # 'http://spam404bl.com/spam404scamlist.txt' \
110
+ # 'http://malwaredomains.lehigh.edu/files/domains.txt' \
111
+ # 'http://www.joewein.net/dl/bl/dom-bl.txt' \
112
+ # 'http://www.sa-blacklist.stearns.org/sa-blacklist/sa-blacklist.current' \
113
+ # 'https://easylist-downloads.adblockplus.org/malwaredomains_full.txt' \
114
+ # 'https://easylist-downloads.adblockplus.org/easyprivacy.txt' \
115
+ # 'https://easylist-downloads.adblockplus.org/easylist.txt' \
116
+ # 'https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt' \
117
+ # 'http://www.fanboy.co.nz/adblock/opera/urlfilter.ini' \
118
+ # 'http://www.fanboy.co.nz/adblock/fanboy-tracking.txt' \
119
+ )
120
+
121
+ for item in ${sources[*]}
122
+ do
123
+ echo " ---------------------"
124
+ echo " Downloading $item ..."
125
+ curl $item >> tmp || { echo -e " \nError downloading $item " ; exit 1; }
126
+ echo " ---------------------"
127
+ done
128
+
129
+ }
130
+
131
+ extract_domains (){
132
+ # clean this code with better regex
133
+ # https://blog.mister-muffin.de/2011/11/14/adblocking-with-a-hosts-file/
134
+
135
+ echo " Extract domains from lists"
136
+ # remove empty lines and comments
137
+ grep -Ev ' ^$' tmp | \
138
+ grep -o ' ^[^#]*' | \
139
+ # exclude locahost entries
140
+ grep -v " localhost" | \
141
+ # remove 127.0.0.1 and 0.0.0.0
142
+ sed ' s/127.0.0.1//' | \
143
+ sed ' s/0.0.0.0//' | \
144
+ # remove tab and spaces in the begining
145
+ sed -e ' s/^[ \t]*//' | \
146
+ # remove ^M
147
+ sed ' s/\r//g' | grep -Ev ' ^$' > domains-extracted
148
+
149
+ echo domains extracted; wc -l domains-extracted
150
+ }
151
+
152
+ dnsmasq-conf (){
153
+ cat domains-extracted | sort | uniq | \
154
+ awk ' {print "address=/"$1"/"}' > dnsmasq-domains.conf
155
+ echo domains dnsmasq-domains.conf; wc -l dnsmasq-domains.conf
156
+ }
157
+
158
+ finish (){
159
+ echo " Done"
160
+ }
161
+
162
+
163
+
164
+ # welcome
165
+ # rootcheck
166
+ # dependencies
167
+ # backup
168
+ download_sources
169
+ extract_domains
170
+ dnsmasq-conf
171
+ finish
0 commit comments