Browse Source

powerful adblock script to block ad/abuse domains

Signed-off-by: dirk <dirk@brenken.org>
dirk 9 years ago
parent
commit
10bd039c37

+ 73
- 0
net/adblock/Makefile View File

@@ -0,0 +1,73 @@
1
+#
2
+# Copyright (C) 2015 OpenWrt.org
3
+#
4
+# This is free software, licensed under the GNU General Public License v3.
5
+#
6
+
7
+include $(TOPDIR)/rules.mk
8
+
9
+PKG_NAME:=adblock
10
+PKG_VERSION:=0.11.0
11
+PKG_RELEASE:=1
12
+PKG_LICENSE:=GPL-3.0+
13
+PKG_MAINTAINER:=Dirk Brenken <dirk@brenken.org>
14
+
15
+include $(INCLUDE_DIR)/package.mk
16
+
17
+define Package/$(PKG_NAME)
18
+	SECTION:=net
19
+	CATEGORY:=Network
20
+	TITLE:=powerful adblock script to block ad/abuse domains
21
+	DEPENDS:=+curl +wget
22
+	PKGARCH:=all
23
+endef
24
+
25
+define Package/$(PKG_NAME)/description
26
+powerful adblock script to block ad/abuse domains
27
+When the dns server on your router receives dns requests, we will sort out queries that ask for the [A] resource records of ad servers
28
+and return the local ip address of your router and the internal web server delivers a transparent pixel instead.
29
+The script supports the following domain blacklist sites:
30
+  http://pgl.yoyo.org/adservers
31
+  http://malwaredomains.com
32
+  https://zeustracker.abuse.ch
33
+  https://feodotracker.abuse.ch
34
+  https://palevotracker.abuse.ch
35
+  http://dshield.org
36
+  http://www.shallalist.de
37
+  http://www.spam404.com
38
+  http://winhelp2002.mvps.org
39
+
40
+endef
41
+
42
+define Package/$(PKG_NAME)/conffiles
43
+/etc/adblock/adblock.conf
44
+/etc/adblock/adblock.whitelist
45
+/etc/adblock/adblock.blacklist
46
+endef
47
+
48
+define Build/Prepare
49
+endef
50
+
51
+define Build/Configure
52
+endef
53
+
54
+define Build/Compile
55
+endef
56
+
57
+define Package/$(PKG_NAME)/install
58
+	$(INSTALL_DIR) $(1)/usr/bin
59
+	$(INSTALL_BIN) ./files/usr/bin/adblock-update.sh $(1)/usr/bin/
60
+	$(INSTALL_DATA) ./files/usr/bin/adblock-helper.sh $(1)/usr/bin/
61
+
62
+	$(INSTALL_DIR) $(1)/etc/adblock
63
+	$(INSTALL_CONF) ./files/etc/adblock/adblock.* $(1)/etc/adblock/
64
+	$(INSTALL_CONF) ./files/etc/adblock/README.md $(1)/etc/adblock/
65
+
66
+	$(INSTALL_DIR) $(1)/etc/adblock/samples
67
+	$(INSTALL_CONF) ./files/etc/adblock/samples/*.sample $(1)/etc/adblock/samples/
68
+
69
+	$(INSTALL_DIR) $(1)/www/adblock
70
+	$(INSTALL_DATA) ./files/www/adblock/* $(1)/www/adblock/
71
+endef
72
+
73
+$(eval $(call BuildPackage,$(PKG_NAME)))

+ 54
- 0
net/adblock/files/etc/adblock/README.md View File

@@ -0,0 +1,54 @@
1
+# adblock script for openwrt
2
+
3
+## Description
4
+A lot of people already use adblocker plugins within their desktop browsers,  
5
+but what if you are using your (smart) phone, tablet, watch or any other wlan gadget...  
6
+...getting rid of annoying ads, trackers and other abuse sites (like facebook ;-) is simple: block them with your router.  
7
+
8
+When the dns server on your router receives dns requests, we’ll sort out queries that ask for the [A] resource records of ad servers  
9
+and return the local ip address of your router and the internal web server delivers a transparent pixel instead.
10
+
11
+## Main Features
12
+* support of the following domain blacklist sites (free for private usage, for commercial use please check their individual licenses):
13
+    * [pgl.yoyo.org](http://pgl.yoyo.org/adservers)
14
+    * [malwaredomains.com](http://malwaredomains.com)
15
+    * [zeustracker.abuse.ch](https://zeustracker.abuse.ch)
16
+    * [feodotracker.abuse.ch](https://feodotracker.abuse.ch)
17
+    * [palevotracker.abuse.ch](https://palevotracker.abuse.ch)
18
+    * [dshield.org](http://dshield.org)
19
+    * [shallalist.de](http://www.shallalist.de) (tested with the categories "adv" "costtraps" "downloads" "spyware" "tracker" "warez")
20
+    * [spam404.com](http://www.spam404.com)
21
+    * [winhelp2002.mvps.org](http://winhelp2002.mvps.org)
22
+* blocklist parsing by fast & flexible regex rulesets
23
+* additional white- and blacklist support for manual overrides
24
+* separate adblock loopback network interface (auto-install)
25
+* separate uhttpd instance as pixel server (auto-install)
26
+* optional: quality checks and a powerful backup/restore handling to ensure a reliable dnsmasq service
27
+* optional: adblock updates only on pre-defined interfaces
28
+* optional: domain query logging as a background service to easily identify free and already blocked domains
29
+* optional: ntp time sync
30
+* optional: status & error logging (req. ntp time sync)
31
+
32
+## Prerequisites
33
+* [openwrt](https://openwrt.org) (tested only with trunk > r47025), CC should also work (please adjust *min_release* accordingly)
34
+* additional software packages:
35
+    * curl
36
+    * wget (due to an openwrt bug still needed for certain https requests - see ticket #19621)
37
+    * busybox find with *-mtime* support (needed only for query logging/housekeeping, will be disabled if not found)
38
+* optional: mounted usb stick or any other storage device to overcome limited memory resources on embedded router devices
39
+* the above dependencies will be checked during package installation & script runtime, please check *logread -e "adblock"* for errors
40
+
41
+## Usage
42
+* select & install adblock package (*opkg install adblock*)
43
+* configure /etc/adblock/adblock.conf to your needs
44
+* start /usr/bin/adblock-update.sh and check *logread -e "adblock"* for errors
45
+
46
+## Distributed samples
47
+* to enable/disable additional domain query logging set the dnsmasq option *logqueries* accordingly, see */etc/adblock/samples/dhcp.config.sample*.
48
+* for script autostart via rc.local and /tmp resizing on the fly see */etc/adblock/samples/rc.local.sample*.
49
+* for scheduled call of *adblock-update.sh* see */etc/adblock/samples/root.crontab.sample*.
50
+* to redirect/force all dns queries to your router see */etc/adblock/samples/firwall.user.sample*.
51
+* for further dnsmasq tweaks see */etc/adblock/samples/dnsmasq.conf.sample*.
52
+
53
+Have fun!  
54
+Dirk  

+ 1
- 0
net/adblock/files/etc/adblock/adblock.blacklist View File

@@ -0,0 +1 @@
1
+bild.de

+ 118
- 0
net/adblock/files/etc/adblock/adblock.conf View File

@@ -0,0 +1,118 @@
1
+###################################################
2
+# adblock configuration used by adblock-update.sh #
3
+# written by Dirk Brenken (dirk@brenken.org)      #
4
+###################################################
5
+
6
+# adblock ip address and device information
7
+# adb_ip  => ip address of the local adblock loopback interface/uhttpd instance
8
+# adb_dev => name of the physical adblock network device
9
+# adb_if  => name of the adblock network interface and uhttpd instance
10
+#
11
+adb_ip="192.168.2.1"
12
+adb_dev="eth0"
13
+adb_if="adblock"
14
+
15
+# backup file and default restore message
16
+# backup_dir  => full path to directory for logging and backups, an empty entry disables this feature
17
+# backup_file => full path to backup file
18
+# restore_msg => default restore message
19
+#
20
+backup_dir=""
21
+backup_file="${backup_dir}/adlist.backup"
22
+restore_msg="unknown error"
23
+
24
+# wan/update interface(s)
25
+# wan_dev => space separated list of devices that are allowed for adblock updates (check /sys/class/net/<dev>),
26
+#            an empty entry disables this feature
27
+#
28
+wan_dev=""
29
+
30
+# local adblock black & white list for manual overrides
31
+# adb_blacklist => full path to domain blacklist file
32
+# adb_whitelist => full path to domain whitelist file
33
+#
34
+adb_blacklist="/etc/adblock/adblock.blacklist"
35
+adb_whitelist="/etc/adblock/adblock.whitelist"
36
+
37
+# error logfile
38
+# log_file => full path to additional logfile for error logging,
39
+#             "/dev/stdout" redirect errors to console, an empty entry disables this feature
40
+#             this feature requires ntp time sync too (see below)
41
+#
42
+log_file="${backup_dir}/error.log"
43
+
44
+# domain query logfile
45
+# query_file    => full path to additional domain query logfile,
46
+#                  an empty entry or an empty backup_dir entry disables this feature
47
+# query_history => delete query logfiles older than n days (req. busybox find with mtime support)
48
+#
49
+query_file="${backup_dir}/query.log"
50
+query_history=1
51
+query_name="$(printf "${query_file}" | sed 's/.*\///g')"
52
+query_ip="$(printf "${adb_ip}" | sed 's/\./\\./g')"
53
+query_pid="/var/run/query.pid"
54
+
55
+# ntp time server pool
56
+# ntp_srv => space separated list of ntp serverpools, an empty entry disables this feature
57
+#
58
+ntp_srv="ntp0.fau.de ntp1.fau.de ntp2.fau.de ntp3.fau.de"
59
+
60
+# domain for nslookup probe
61
+# check_domain => check the newly generated domain blocklist with this domain
62
+#
63
+check_domain="heise.de"
64
+
65
+# set startup counter and download timeout defaults
66
+# max_cnt  => wait n seconds/loops for wan & ntp check in sum
67
+# max_time => wait n seconds for every source download
68
+#
69
+cnt=0
70
+max_cnt=30
71
+max_time=60
72
+
73
+# minimum values for environment checks
74
+# min_release => minimum required openwrt release number
75
+# min_space   => minimum required space for backups & logfiles (in kbyte)
76
+#
77
+min_release=47025
78
+min_space=100000
79
+
80
+# shallalist url, categories and local naming
81
+# shalla_cat => space separated list of categories to use from shallalist archive
82
+#
83
+shalla_cat="adv costtraps downloads spyware tracker warez"
84
+shalla_url="http://www.shallalist.de/Downloads/shallalist.tar.gz"
85
+shalla_archive="${tmp_dir}/shallalist.tar.gz"
86
+shalla_file="${tmp_dir}/shallalist.txt"
87
+
88
+# remote and local domain block list sources
89
+# adb_source => comment out sources you don't want to use
90
+#
91
+unset adb_source
92
+adb_source="${adb_source} http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=0&mimetype=plaintext&ruleset=yoyo"
93
+#adb_source="${adb_source} http://mirror1.malwaredomains.com/files/justdomains&ruleset=default"
94
+#adb_source="${adb_source} https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist&ruleset=default"
95
+#adb_source="${adb_source} https://feodotracker.abuse.ch/blocklist/?download=domainblocklist&ruleset=default"
96
+#adb_source="${adb_source} https://palevotracker.abuse.ch/blocklists.php?download=domainblocklist&ruleset=default"
97
+#adb_source="${adb_source} http://www.dshield.org/feeds/suspiciousdomains_Low.txt&ruleset=default"
98
+#adb_source="${adb_source} file:///${shalla_file}&ruleset=shalla"
99
+#adb_source="${adb_source} http://spam404bl.com/spam404scamlist.txt&ruleset=spam404"
100
+#adb_source="${adb_source} http://winhelp2002.mvps.org/hosts.txt&ruleset=winhelp"
101
+adb_source="${adb_source} file://${adb_blacklist}&ruleset=default"
102
+
103
+# adblock source ruleset definitions
104
+# note: please keep the default settings below
105
+#
106
+rset_start="sed -r 's/[[:space:]]|[\[!#/:;_].*|[0-9\.]*localhost//g; s/[\^#/:;_\.\t ]*$//g'"
107
+rset_end="sed '/^[#/:;_\s]*$/d'"
108
+rset_default="${rset_start} | ${rset_end}"
109
+rset_yoyo="${rset_start} | sed 's/,/\n/g' | ${rset_end}"
110
+rset_shalla="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}$//g' | ${rset_end}"
111
+rset_spam404="${rset_start} | sed 's/^\|\|//g' | ${rset_end}"
112
+rset_winhelp="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
113
+
114
+# dnsmasq destination file and format
115
+# note: please keep the default settings below
116
+#
117
+dns_file="/tmp/dnsmasq.d/adlist.conf"
118
+dns_format="sed 's/^/address=\//;s/$/\/'${adb_ip}'/'"

+ 4
- 0
net/adblock/files/etc/adblock/adblock.whitelist View File

@@ -0,0 +1,4 @@
1
+downloads.openwrt.org
2
+ftp.de.debian.org
3
+download.eclipse.org
4
+dl.sourceforge.net

+ 7
- 0
net/adblock/files/etc/adblock/samples/dhcp.config.sample View File

@@ -0,0 +1,7 @@
1
+# tweaks for dnsmasq
2
+# configuration found in /etc/config/dhcp
3
+#
4
+config dnsmasq
5
+    option cachesize '1000'
6
+    option filterwin2k '0'
7
+    option logqueries '1'

+ 5
- 0
net/adblock/files/etc/adblock/samples/dnsmasq.conf.sample View File

@@ -0,0 +1,5 @@
1
+# tell DHCP clients to not ask for proxy information
2
+# some clients - like Win7 - will constantly ask if not told "No!"
3
+# configuration found in /etc/dnsmasq
4
+#
5
+dhcp-option=252,"\n"

+ 5
- 0
net/adblock/files/etc/adblock/samples/firewall.user.sample View File

@@ -0,0 +1,5 @@
1
+# redirect/force all dns queries to port 53 of your router
2
+# configuration found in /etc/firewall.user
3
+#
4
+iptables -t nat -I PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53
5
+iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53

+ 26
- 0
net/adblock/files/etc/adblock/samples/rc.local.sample View File

@@ -0,0 +1,26 @@
1
+# sample startup script
2
+# configuration found in /etc/rc.local
3
+#
4
+
5
+# start logging
6
+#
7
+/usr/bin/logger -t rc.local "start rc.local processing"
8
+
9
+# set home directory
10
+#
11
+export HOME=/root
12
+
13
+# resize /tmp partition to 256 MB
14
+#
15
+/usr/bin/logger -t rc.local "resize /tmp partition to 256 MB"
16
+mount tmpfs /tmp -t tmpfs -o remount,nosuid,nodev,noatime,size=256M
17
+
18
+# start adblock script
19
+#
20
+/usr/bin/logger -t rc.local "start adblock script"
21
+/usr/bin/adblock-update.sh >/dev/null 2>&1
22
+
23
+# write log and exit
24
+#
25
+/usr/bin/logger -t rc.local "finish rc.local processing"
26
+exit 0

+ 8
- 0
net/adblock/files/etc/adblock/samples/root.crontab.sample View File

@@ -0,0 +1,8 @@
1
+# sample crontab script
2
+# configuration found in /etc/crontabs/root
3
+#
4
+
5
+# start adblock script twice a day
6
+#
7
+0 06 * * *    /usr/bin/adblock-update.sh &
8
+0 22 * * *    /usr/bin/adblock-update.sh &

+ 434
- 0
net/adblock/files/usr/bin/adblock-helper.sh View File

@@ -0,0 +1,434 @@
1
+##############################################
2
+# function library used by adblock-update.sh #
3
+# written by Dirk Brenken (dirk@brenken.org) #
4
+##############################################
5
+
6
+#############################################
7
+# f_envcheck: check environment prerequisites
8
+#
9
+f_envcheck()
10
+{
11
+    # source in json helpers library
12
+    #
13
+    if [ -r "/usr/share/libubox/jshn.sh" ]
14
+    then
15
+        . "/usr/share/libubox/jshn.sh"
16
+    else
17
+        /usr/bin/logger -t "adblock[${pid}]" "json helpers library not found"
18
+        f_deltemp
19
+        exit 10
20
+    fi
21
+
22
+    # check adblock network device configuration
23
+    #
24
+    if [ ! -d "/sys/class/net/${adb_dev}" ]
25
+    then
26
+        /usr/bin/logger -t "adblock[${pid}]" "invalid adblock network device input (${adb_dev})"
27
+        f_deltemp
28
+        exit 15
29
+    fi
30
+
31
+    # check adblock network interface configuration
32
+    #
33
+    check_if="$(printf "${adb_if}" | sed -n '/[^_0-9A-Za-z]/p')"
34
+    banned_if="$(printf "${adb_if}" | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p')"
35
+    if [ -n "${check_if}" ] || [ -n "${banned_if}" ]
36
+    then
37
+        /usr/bin/logger -t "adblock[${pid}]" "invalid adblock network interface input (${adb_if})"
38
+        f_deltemp
39
+        exit 20
40
+    fi
41
+
42
+    # check adblock ip address configuration
43
+    #
44
+    check_ip="$(printf "${adb_ip}" | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p')"
45
+    if [ -z "${check_ip}" ]
46
+    then
47
+        /usr/bin/logger -t "adblock[${pid}]" "invalid adblock ip address input (${adb_ip})"
48
+        f_deltemp
49
+        exit 25
50
+    fi
51
+
52
+    # check adblock blacklist/whitelist configuration
53
+    #
54
+    if [ ! -r "${adb_blacklist}" ] || [ ! -r "${adb_whitelist}" ]
55
+    then
56
+        /usr/bin/logger -t "adblock[${pid}]" "adblock blacklist or whitelist not found"
57
+        f_deltemp
58
+        exit 30
59
+    fi
60
+
61
+    # check wan update configuration
62
+    #
63
+    if [ -n "${wan_dev}" ]
64
+    then
65
+        wan_ok="true"
66
+    else
67
+        wan_ok="false"
68
+        /usr/bin/logger -t "adblock[${pid}]" "wan update check will be disabled"
69
+    fi
70
+
71
+    # check ntp sync configuration
72
+    #
73
+    if [ -n "${ntp_srv}" ]
74
+    then
75
+        ntp_ok="true"
76
+    else
77
+        ntp_ok="false"
78
+        /usr/bin/logger -t "adblock[${pid}]" "ntp time sync will be disabled"
79
+    fi
80
+
81
+    # check backup configuration
82
+    #
83
+    if [ -n "${backup_dir}" ] && [ -d "${backup_dir}" ]
84
+    then
85
+        backup_ok="true"
86
+        mounts="${backup_dir} ${tmp_dir}"
87
+    else
88
+        backup_ok="false"
89
+        mounts="${tmp_dir}"
90
+        /usr/bin/logger -t "adblock[${pid}]" "backup/restore will be disabled"
91
+    fi
92
+
93
+    # check error log configuration
94
+    #
95
+    if [ "${log_file}" = "/dev/stdout" ]
96
+    then
97
+        log_ok="true"
98
+        log_file="/proc/self/fd/1"
99
+    elif [ -n "${log_file}" ] && [ "${backup_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
100
+    then
101
+        log_ok="true"
102
+    else
103
+        log_ok="false"
104
+        log_file="/dev/null"
105
+        /usr/bin/logger -t "adblock[${pid}]" "error logging will be disabled"
106
+    fi
107
+
108
+    # check dns query log configuration
109
+    #
110
+    if [ -n "${query_file}" ] && [ "${backup_ok}" = "true" ]
111
+    then
112
+        # check find capabilities
113
+        #
114
+        base="$(find --help 2>&1 | grep "mtime")"
115
+        if [[ -z "${base}" ]]
116
+        then
117
+            query_ok="false"
118
+            /usr/bin/logger -t "adblock[${pid}]" "no 'find/mtime' support, dns query logging will be disabled"
119
+        else
120
+            query_ok="true"
121
+        fi
122
+    else
123
+        query_ok="false"
124
+        /usr/bin/logger -t "adblock[${pid}]" "dns query logging will be disabled"
125
+    fi
126
+
127
+    # check shallalist configuration
128
+    #
129
+    check_shalla="$(printf "${adb_source}" | sed -n '/.*shallalist.txt.*/p')"
130
+    if [ -n "${check_shalla}" ]
131
+    then
132
+        shalla_ok="true"
133
+    else
134
+        shalla_ok="false"
135
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist processing will be disabled"
136
+    fi
137
+
138
+    # check mount points & space requirements
139
+    #
140
+    for mp in ${mounts}
141
+    do
142
+        df "${mp}" 2>/dev/null |\
143
+        tail -n1 |\
144
+        while read filesystem overall used available scrap
145
+        do
146
+            av_space="${available}"
147
+            if [ $((av_space)) -eq 0 ]
148
+            then
149
+                /usr/bin/logger -t "adblock[${pid}]" "no space left on device, not mounted (${mp})"
150
+                exit 35
151
+            elif [ $((av_space)) -lt $((min_space)) ]
152
+            then
153
+                /usr/bin/logger -t "adblock[${pid}]" "not enough space on device (${mp})"
154
+                exit 40
155
+            fi
156
+        done
157
+        # subshell return code handling
158
+        #
159
+        rc=$?
160
+        if [ $((rc)) -ne 0 ]
161
+        then
162
+            f_deltemp
163
+            exit ${rc}
164
+        fi
165
+    done
166
+
167
+    # get list with all installed packages
168
+    #
169
+    pkg_list="$(opkg list-installed 2>/dev/null)"
170
+
171
+    # check openwrt release
172
+    #
173
+    base="$(printf "${pkg_list}" | grep "^base-files" | sed 's/\(.*r\)//g')"
174
+    if [ $((base)) -lt $((min_release)) ]
175
+    then
176
+        /usr/bin/logger -t "adblock[${pid}]" "openwrt (r${wrt_release}) seems to be too old"
177
+        f_deltemp
178
+        exit 45
179
+    fi
180
+
181
+    # check curl package dependency
182
+    #
183
+    base="$(printf "${pkg_list}" | grep "^curl")"
184
+    if [ -z "${base}" ]
185
+    then
186
+        /usr/bin/logger -t "adblock[${pid}]" "curl package not found"
187
+        f_deltemp
188
+        exit 50
189
+    fi
190
+
191
+    # check wget package dependency
192
+    #
193
+    base="$(printf "${pkg_list}" | grep "^wget")"
194
+    if [ -z "${base}" ]
195
+    then
196
+        /usr/bin/logger -t "adblock[${pid}]" "wget package not found"
197
+        f_deltemp
198
+        exit 55
199
+    fi
200
+
201
+    # check dynamic/volatile adblock network interface configuration
202
+    #
203
+    rc="$(ifstatus "${adb_if}" >/dev/null 2>&1; printf $?)"
204
+    if [ $((rc)) -ne 0 ]
205
+    then
206
+        json_init
207
+        json_add_string name "${adb_if}"
208
+        json_add_string ifname "${adb_dev}"
209
+        json_add_string proto "static"
210
+        json_add_array ipaddr
211
+        json_add_string "" "${adb_ip}"
212
+        json_close_array
213
+        json_close_object
214
+        ubus call network add_dynamic "$(json_dump)"
215
+        /usr/bin/logger -t "adblock[${pid}]" "created new dynamic/volatile network interface (${adb_if}, ${adb_ip})"
216
+    fi
217
+
218
+    # check adblock uhttpd instance configuration
219
+    #
220
+    if [ -z "$(uci -q get uhttpd.${adb_if} 2>/dev/null)" ]
221
+    then
222
+        uci -q set uhttpd.${adb_if}="uhttpd"
223
+        uci -q set uhttpd.${adb_if}.listen_http="${adb_ip}:80"
224
+        uci -q set uhttpd.${adb_if}.home="/www/adblock"
225
+        uci -q set uhttpd.${adb_if}.error_page="/adblock.html"
226
+        uci -q commit uhttpd
227
+        /etc/init.d/uhttpd reload
228
+        /usr/bin/logger -t "adblock[${pid}]" "created new uhttpd instance (${adb_if}, ${adb_ip}) in /etc/config/uhttpd"
229
+    fi
230
+}
231
+
232
+###################################################
233
+# f_deltemp: delete temporary files and directories
234
+f_deltemp()
235
+{
236
+    if [ -f "${tmp_file}" ]
237
+    then
238
+       rm -f "${tmp_file}" 2>/dev/null
239
+    fi
240
+    if [ -d "${tmp_dir}" ]
241
+    then
242
+       rm -rf "${tmp_dir}" 2>/dev/null
243
+    fi
244
+}
245
+
246
+################################################################
247
+# f_remove: remove temporary files, start and maintain query log
248
+#
249
+f_remove()
250
+{
251
+    # delete temporary files and directories
252
+    #
253
+    f_deltemp
254
+
255
+    # kill existing domain query log background process,
256
+    # housekeeping and start of a new process on daily basis
257
+    #
258
+    if [ "${query_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
259
+    then
260
+        query_date="$(date "+%Y%m%d")"
261
+        if [ -s "${query_pid}" ] && [ ! -f "${query_file}.${query_date}" ]
262
+        then
263
+            kill -9 $(< "${query_pid}") 2>/dev/null
264
+            > "${query_pid}"
265
+            find "${backup_dir}" -maxdepth 1 -type f -mtime +${query_history} -name "${query_name}.*" -exec rm -f {} \; 2>/dev/null
266
+            /usr/bin/logger -t "adblock[${pid}]" "kill old query log background process and do logfile housekeeping"
267
+        fi
268
+        if [ ! -s "${query_pid}" ]
269
+        then
270
+            ( logread -f 2>/dev/null & printf -n "$!" > "${query_pid}" ) | egrep -o "(query\[A\].*)|([a-z0-9\.\-]* is ${query_ip}$)" >> "${query_file}.${query_date}" &
271
+            /usr/bin/logger -t "adblock[${pid}]" "start new domain query log background process"
272
+        fi
273
+    fi
274
+
275
+    # final log entry
276
+    #
277
+    /usr/bin/logger -t "adblock[${pid}]" "domain adblock processing finished (${script_ver})"
278
+}
279
+
280
+#####################################################
281
+# f_restore: if available, restore last adlist backup
282
+#
283
+f_restore()
284
+{
285
+    if [ "${backup_ok}" = "true" ] && [ -f "${backup_file}" ]
286
+    then
287
+        cp -f "${backup_file}" "${dns_file}" 2>/dev/null
288
+        /usr/bin/logger -t "adblock[${pid}]" "${restore_msg}, adlist backup restored"
289
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, adlist backup restored" >> "${log_file}"
290
+    else
291
+        > "${dns_file}"
292
+        /usr/bin/logger -t "adblock[${pid}]" "${restore_msg}, empty adlist generated"
293
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, empty adlist generated" >> "${log_file}"
294
+    fi
295
+    # restart dnsmasq
296
+    #
297
+    /etc/init.d/dnsmasq restart >/dev/null 2>&1
298
+
299
+    # remove files and exit
300
+    #
301
+    f_remove
302
+    exit 100
303
+}
304
+
305
+#######################################################
306
+# f_wancheck: check for usable adblock update interface
307
+#
308
+f_wancheck()
309
+{
310
+    if [ "${wan_ok}" = "true" ]
311
+    then
312
+        # wait for wan update interface(s)
313
+        #
314
+        while [ $((cnt)) -le $((max_cnt)) ]
315
+        do
316
+            for dev in ${wan_dev}
317
+            do
318
+                dev_out=$(< /sys/class/net/${dev}/operstate 2>/dev/null)
319
+                if [[ "${dev_out}" = "up" ]]
320
+                then
321
+                    /usr/bin/logger -t "adblock[${pid}]" "get wan/update interface: ${dev}, after ${cnt} loops"
322
+                    break 2
323
+                elif [ $((cnt)) -eq $((max_cnt)) ]
324
+                then
325
+                    /usr/bin/logger -t "adblock[${pid}]" "no wan/update interface(s) found (${wan_dev})"
326
+                    printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: no wan/update interface(s) found (${wan_dev})" >> "${log_file}"
327
+                    restore_msg="no wan/update interface(s)"
328
+                    f_restore
329
+                fi
330
+            done
331
+            sleep 1
332
+            cnt=$((cnt + 1))
333
+        done
334
+    fi
335
+}
336
+
337
+#####################################
338
+# f_ntpcheck: check/get ntp time sync
339
+#
340
+f_ntpcheck()
341
+{
342
+    if [ "${ntp_ok}" = "true" ]
343
+    then
344
+        # prepare ntp server pool
345
+        #
346
+        unset ntp_pool
347
+        for srv in ${ntp_srv}
348
+        do
349
+            ntp_pool="${ntp_pool} -p ${srv}"
350
+        done
351
+
352
+        # wait for ntp time sync
353
+        #
354
+        while [ $((cnt)) -le $((max_cnt)) ]
355
+        do
356
+            /usr/sbin/ntpd -nq ${ntp_pool} >/dev/null 2>&1
357
+            rc=$?
358
+            if [ $((rc)) -eq 0 ]
359
+            then
360
+                /usr/bin/logger -t "adblock[${pid}]" "get ntp time sync (${ntp_srv}), after ${cnt} loops"
361
+                break
362
+            elif [ $((cnt)) -eq $((max_cnt)) ]
363
+            then
364
+                ntp_ok="false"
365
+                /usr/bin/logger -t "adblock[${pid}]" "ntp time sync failed (${ntp_srv})"
366
+                printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ntp time sync failed (${ntp_srv})" >> "${log_file}"
367
+                restore_msg="time sync failed"
368
+                f_restore
369
+            fi
370
+            sleep 1
371
+            cnt=$((cnt + 1))
372
+        done
373
+    fi
374
+}
375
+
376
+#################################################################
377
+# f_dnscheck: dnsmasq health check with newly generated blocklist
378
+#
379
+f_dnscheck()
380
+{
381
+    # check 1: dnsmasq startup
382
+    #
383
+    dns_status="$(logread -l 20 -e "dnsmasq" -e "FAILED to start up")"
384
+    if [ -z "${dns_status}" ]
385
+    then
386
+        # check 2: nslookup probe
387
+        #
388
+        dns_status="$(nslookup "${check_domain}" 2>/dev/null | grep "${adb_ip}")"
389
+        if [ -z "${dns_status}" ]
390
+        then
391
+            # create backup of new block list only, if both checks are OK and backup enabled
392
+            #
393
+            if [ "${backup_ok}" = "true" ]
394
+            then
395
+                cp -f "${dns_file}" "${backup_file}" 2>/dev/null
396
+                /usr/bin/logger -t "adblock[${pid}]" "new block list with ${adb_count} domains loaded, backup generated"
397
+            else
398
+                /usr/bin/logger -t "adblock[${pid}]" "new block list with ${adb_count} domains loaded"
399
+            fi
400
+        else
401
+            restore_msg="nslookup probe failed"
402
+            f_restore
403
+        fi
404
+    else
405
+            restore_msg="dnsmasq probe failed"
406
+            f_restore
407
+    fi
408
+}
409
+
410
+##########################################################
411
+# f_footer: write footer with a few statistics to dns file
412
+#
413
+f_footer()
414
+{
415
+    # count result of merged domain entries
416
+    #
417
+    adb_count="$(wc -l < "${dns_file}")"
418
+
419
+    # write file footer with timestamp and merged ad count sum
420
+    #
421
+    printf "%s\n" "###################################################" >> "${dns_file}"
422
+    printf "%s\n" "# last adblock file update: $(date +"%d.%m.%Y - %T")" >> "${dns_file}"
423
+    printf "%s\n" "# ${0##*/} (${script_ver}) - ${adb_count} ad/abuse domains blocked" >> "${dns_file}"
424
+    printf "%s\n" "# domain blacklist sources:" >> "${dns_file}"
425
+    for src in ${adb_source}
426
+    do
427
+        url="$(printf "${src}" | sed 's/\(\&ruleset=.*\)//g')"
428
+        printf "%s\n" "# ${url}" >> "${dns_file}"
429
+    done
430
+    printf "%s\n" "###################################################" >> "${dns_file}"
431
+    printf "%s\n" "# domain whitelist source:" >> "${dns_file}"
432
+    printf "%s\n" "# ${adb_whitelist}" >> "${dns_file}"
433
+    printf "%s\n" "###################################################" >> "${dns_file}"
434
+}

+ 209
- 0
net/adblock/files/usr/bin/adblock-update.sh View File

@@ -0,0 +1,209 @@
1
+#!/bin/sh
2
+#######################################################
3
+# ad/abuse domain blocking script for dnsmasq/openwrt #
4
+# written by Dirk Brenken (dirk@brenken.org)          #
5
+#######################################################
6
+
7
+# LICENSE
8
+# ========
9
+# This program is free software: you can redistribute it and/or modify
10
+# it under the terms of the GNU General Public License as published by
11
+# the Free Software Foundation, either version 3 of the License, or
12
+# (at your option) any later version.
13
+#
14
+# This program is distributed in the hope that it will be useful,
15
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+# GNU General Public License for more details.
18
+#
19
+# You should have received a copy of the GNU General Public License
20
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
+
22
+###############
23
+# environment #
24
+###############
25
+
26
+# set script version
27
+#
28
+script_ver="0.11.0"
29
+
30
+# get current pid and script directory
31
+#
32
+pid=$$
33
+script_dir="$(printf "${0}" | sed 's/\(.*\)\/.*/\1/')"
34
+
35
+# set temp variables
36
+#
37
+tmp_file="$(mktemp -tu)"
38
+tmp_dir="$(mktemp -d)"
39
+
40
+# source in adblock configuration
41
+#
42
+if [ -r "/etc/adblock/adblock.conf" ]
43
+then
44
+    . "/etc/adblock/adblock.conf"
45
+else
46
+    /usr/bin/logger -t "adblock[${pid}]" "adblock configuration not found"
47
+    rm -rf "${tmp_dir}" 2>/dev/null
48
+    exit 200
49
+fi
50
+
51
+# source in adblock function library
52
+#
53
+if [ -r "${script_dir}/adblock-helper.sh" ]
54
+then
55
+    . "${script_dir}/adblock-helper.sh"
56
+else
57
+    /usr/bin/logger -t "adblock[${pid}]" "adblock function library not found"
58
+    rm -rf "${tmp_dir}" 2>/dev/null
59
+    exit 210
60
+fi
61
+
62
+################
63
+# main program #
64
+################
65
+
66
+# call restore function on trap signals (HUP, INT, QUIT, BUS, SEGV, TERM)
67
+#
68
+trap "restore_msg='trap error'; f_restore" 1 2 3 10 11 15
69
+
70
+# start logging
71
+#
72
+/usr/bin/logger -t "adblock[${pid}]" "domain adblock processing started (${script_ver})"
73
+
74
+# check environment
75
+#
76
+f_envcheck
77
+
78
+# check wan update interface(s)
79
+#
80
+f_wancheck
81
+
82
+# check for ntp time sync
83
+#
84
+f_ntpcheck
85
+
86
+# download shallalist archive
87
+#
88
+if [ "${shalla_ok}" = "true" ]
89
+then
90
+    curl --insecure --max-time "${max_time}" "${shalla_url}" -o "${shalla_archive}" 2>/dev/null
91
+    rc=$?
92
+    if [ $((rc)) -eq 0 ]
93
+    then
94
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist archive download finished"
95
+    else
96
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist archive download failed (${shalla_url})"
97
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist archive download failed (${shalla_url})" >> "${log_file}"
98
+        restore_msg="archive download failed"
99
+        f_restore
100
+    fi
101
+
102
+    # extract shallalist archive
103
+    #
104
+    tar -xzf "${shalla_archive}" -C "${tmp_dir}" 2>/dev/null
105
+    rc=$?
106
+    if [ $((rc)) -eq 0 ]
107
+    then
108
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist archive extraction finished"
109
+    else
110
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist archive extraction failed"
111
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist archive extraction failed" >> "${log_file}"
112
+        restore_msg="archive extraction failed"
113
+        f_restore
114
+    fi
115
+
116
+    # merge selected shallalist categories
117
+    #
118
+    > "${shalla_file}"
119
+    for category in ${shalla_cat}
120
+    do
121
+        if [ -f "${tmp_dir}/BL/${category}/domains" ]
122
+        then
123
+            cat "${tmp_dir}/BL/${category}/domains" >> "${shalla_file}" 2>/dev/null
124
+            rc=$?
125
+        else
126
+            rc=220
127
+        fi
128
+        if [ $((rc)) -ne 0 ]
129
+        then
130
+            break
131
+        fi
132
+    done
133
+
134
+    # finish shallalist (pre-)processing
135
+    #
136
+    if [ $((rc)) -eq 0 ]
137
+    then
138
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist (pre-)processing finished (${shalla_cat})"
139
+    else
140
+        /usr/bin/logger -t "adblock[${pid}]" "shallalist category merge failed (${rc}, ${shalla_cat})"
141
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist category merge failed (${rc}, ${shalla_cat})" >> "${log_file}"
142
+        restore_msg="shallalist merge failed"
143
+        f_restore
144
+    fi
145
+fi
146
+
147
+# loop through domain source list,
148
+# prepare output and store all extracted domains in temp file
149
+#
150
+for src in ${adb_source}
151
+do
152
+    # download selected adblock sources
153
+    #
154
+    url="$(printf "${src}" | sed 's/\(\&ruleset=.*\)//g')"
155
+    check_url="$(printf "${url}" | sed -n '/^https:/p')"
156
+    if [ -n "${check_url}" ]
157
+    then
158
+        tmp_var="$(wget --timeout="${max_time}" --tries=1 --output-document=- "${url}" 2>/dev/null)"
159
+        rc=$?
160
+    else
161
+        tmp_var="$(curl --insecure --max-time "${max_time}" "${url}" 2>/dev/null)"
162
+        rc=$?
163
+    fi
164
+
165
+    # check download result and prepare domain output by regex patterns
166
+    #
167
+    if [ $((rc)) -eq 0 ] && [ -n "${tmp_var}" ]
168
+    then
169
+        eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$rset_/g')"
170
+        tmp_var="$(printf "%s\n" "${tmp_var}" |  tr '[[:upper:]]' '[[:lower:]]')"
171
+        adb_count="$(printf "%s\n" "${tmp_var}" | eval "${ruleset}" | tee -a "${tmp_file}" | wc -l)"
172
+        /usr/bin/logger -t "adblock[${pid}]" "source download finished (${url}, ${adb_count} entries)"
173
+    elif [ $((rc)) -eq 0 ] && [ -z "${tmp_var}" ]
174
+    then
175
+        /usr/bin/logger -t "adblock[${pid}]" "empty source download finished (${url})"
176
+    else
177
+        /usr/bin/logger -t "adblock[${pid}]" "source download failed (${url})"
178
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: source download failed (${url})" >> "${log_file}"
179
+        restore_msg="download failed"
180
+        f_restore
181
+    fi
182
+done
183
+
184
+# create empty destination file
185
+#
186
+> "${dns_file}"
187
+
188
+# rewrite ad/abuse domain information to dns file,
189
+# remove duplicates and whitelist entries
190
+#
191
+grep -vxf "${adb_whitelist}" < "${tmp_file}" | eval "${dns_format}" | sort -u 2>/dev/null >> "${dns_file}"
192
+
193
+# write dns file footer
194
+#
195
+f_footer
196
+
197
+# restart dnsmasq with newly generated block list
198
+#
199
+/etc/init.d/dnsmasq restart >/dev/null 2>&1
200
+sleep 2
201
+
202
+# dnsmasq health check
203
+#
204
+f_dnscheck
205
+
206
+# remove files and exit
207
+#
208
+f_remove
209
+exit 0

+ 6
- 0
net/adblock/files/www/adblock/adblock.html View File

@@ -0,0 +1,6 @@
1
+<html>
2
+    <head></head>
3
+    <body>
4
+	<img src="/adblock.png" border=0 alt=""></img>
5
+    </body>
6
+</html>

BIN
net/adblock/files/www/adblock/adblock.png View File


+ 6
- 0
net/adblock/files/www/adblock/index.html View File

@@ -0,0 +1,6 @@
1
+<html>
2
+    <head></head>
3
+    <body>
4
+	<img src="/adblock.png" border=0 alt=""></img>
5
+    </body>
6
+</html>