Browse Source

Merge pull request #1946 from dibdot/master

adblock: uci support, dynamic uhttpd instance support plus various fixes
Ted Hess 9 years ago
parent
commit
25c6b0818f

+ 11
- 7
net/adblock/Makefile View File

@@ -7,7 +7,7 @@
7 7
 include $(TOPDIR)/rules.mk
8 8
 
9 9
 PKG_NAME:=adblock
10
-PKG_VERSION:=0.11.0
10
+PKG_VERSION:=0.20.0
11 11
 PKG_RELEASE:=1
12 12
 PKG_LICENSE:=GPL-3.0+
13 13
 PKG_MAINTAINER:=Dirk Brenken <dirk@brenken.org>
@@ -40,7 +40,7 @@ The script supports the following domain blacklist sites:
40 40
 endef
41 41
 
42 42
 define Package/$(PKG_NAME)/conffiles
43
-/etc/adblock/adblock.conf
43
+/etc/config/adblock
44 44
 /etc/adblock/adblock.whitelist
45 45
 /etc/adblock/adblock.blacklist
46 46
 endef
@@ -56,15 +56,19 @@ endef
56 56
 
57 57
 define Package/$(PKG_NAME)/install
58 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/
59
+	$(INSTALL_BIN) ./files/adblock-update.sh $(1)/usr/bin/
60
+	$(INSTALL_DATA) ./files/adblock-helper.sh $(1)/usr/bin/
61
+
62
+	$(INSTALL_DIR) $(1)/etc/config
63
+	$(INSTALL_CONF) ./files/adblock.conf $(1)/etc/config/adblock
61 64
 
62 65
 	$(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/
66
+	$(INSTALL_CONF) ./files/adblock.blacklist $(1)/etc/adblock/
67
+	$(INSTALL_CONF) ./files/adblock.whitelist $(1)/etc/adblock/
68
+	$(INSTALL_CONF) ./files/README.md $(1)/etc/adblock/
65 69
 
66 70
 	$(INSTALL_DIR) $(1)/etc/adblock/samples
67
-	$(INSTALL_CONF) ./files/etc/adblock/samples/*.sample $(1)/etc/adblock/samples/
71
+	$(INSTALL_CONF) ./files/samples/*.sample $(1)/etc/adblock/samples/
68 72
 
69 73
 	$(INSTALL_DIR) $(1)/www/adblock
70 74
 	$(INSTALL_DATA) ./files/www/adblock/* $(1)/www/adblock/

net/adblock/files/etc/adblock/README.md → net/adblock/files/README.md View File

@@ -5,7 +5,7 @@ A lot of people already use adblocker plugins within their desktop browsers,
5 5
 but what if you are using your (smart) phone, tablet, watch or any other wlan gadget...  
6 6
 ...getting rid of annoying ads, trackers and other abuse sites (like facebook ;-) is simple: block them with your router.  
7 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  
8
+When the dns server on your router receives dns requests, you’ll sort out queries that ask for the [A] resource records of ad servers  
9 9
 and return the local ip address of your router and the internal web server delivers a transparent pixel instead.
10 10
 
11 11
 ## Main Features
@@ -21,34 +21,35 @@ and return the local ip address of your router and the internal web server deliv
21 21
     * [winhelp2002.mvps.org](http://winhelp2002.mvps.org)
22 22
 * blocklist parsing by fast & flexible regex rulesets
23 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)
24
+* separate dynamic adblock network interface
25
+* separate dynamic uhttpd instance as pixel server
26 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
27
+* optional: adblock updates only on pre-defined wan interfaces
28 28
 * optional: domain query logging as a background service to easily identify free and already blocked domains
29 29
 * optional: ntp time sync
30 30
 * optional: status & error logging (req. ntp time sync)
31 31
 
32 32
 ## Prerequisites
33
-* [openwrt](https://openwrt.org) (tested only with trunk > r47025), CC should also work (please adjust *min_release* accordingly)
33
+* [openwrt](https://openwrt.org) (tested only with trunk > r47025), CC should also work
34 34
 * additional software packages:
35 35
     * curl
36 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)
37
+    * busybox find with *-mtime* support for logfile housekeeping (enabled by default with r47362, will be disabled if not found)
38 38
 * optional: mounted usb stick or any other storage device to overcome limited memory resources on embedded router devices
39 39
 * the above dependencies will be checked during package installation & script runtime, please check *logread -e "adblock"* for errors
40 40
 
41 41
 ## Usage
42 42
 * select & install adblock package (*opkg install adblock*)
43
-* configure /etc/adblock/adblock.conf to your needs
43
+* configure /etc/config/adblock to your needs
44 44
 * start /usr/bin/adblock-update.sh and check *logread -e "adblock"* for errors
45 45
 
46 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*.
47
+* all sample configuration files stored in */etc/adblock/samples*.
48
+* to enable/disable additional domain query logging set the dnsmasq option *logqueries* accordingly, see *dhcp.config.sample*.
49
+* for script autostart by rc.local and /tmp resizing on the fly see *rc.local.sample*.
50
+* for scheduled call of *adblock-update.sh* see *root.crontab.sample*.
51
+* to redirect/force all dns queries to your router see *firwall.user.sample*.
52
+* for further dnsmasq tweaks see *dnsmasq.conf.sample*.
52 53
 
53 54
 Have fun!  
54 55
 Dirk  

+ 566
- 0
net/adblock/files/adblock-helper.sh View File

@@ -0,0 +1,566 @@
1
+#!/bin/sh
2
+##############################################
3
+# function library used by adblock-update.sh #
4
+# written by Dirk Brenken (dirk@brenken.org) #
5
+##############################################
6
+
7
+#####################################
8
+# f_envload: load adblock environment
9
+#
10
+f_envload()
11
+{
12
+    # source in openwrt function library
13
+    #
14
+    if [ -r "/lib/functions.sh" ]
15
+    then
16
+        . /lib/functions.sh
17
+    else
18
+        /usr/bin/logger -t "adblock[${pid}]" "error: openwrt function library not found"
19
+        f_deltemp
20
+        exit 10
21
+    fi
22
+
23
+    # source in openwrt json helpers library
24
+    #
25
+    if [ -r "/usr/share/libubox/jshn.sh" ]
26
+    then
27
+        . "/usr/share/libubox/jshn.sh"
28
+    else
29
+        /usr/bin/logger -t "adblock[${pid}]" "error: openwrt json helpers library not found"
30
+        f_deltemp
31
+        exit 15
32
+    fi
33
+
34
+    # get list with all installed openwrt packages
35
+    #
36
+    pkg_list="$(opkg list-installed 2>/dev/null)"
37
+    if [ -z "${pkg_list}" ]
38
+    then
39
+        /usr/bin/logger -t "adblock[${pid}]" "error: empty openwrt package list"
40
+        f_deltemp
41
+        exit 20
42
+    fi
43
+}
44
+
45
+######################################################
46
+# f_envparse: parse adblock config and set environment
47
+#
48
+f_envparse()
49
+{
50
+    # function to read/set global options by callback,
51
+    # prepare list items and build option list for all others
52
+    #
53
+    config_cb()
54
+    {
55
+        local type="${1}"
56
+        local name="${2}"
57
+        if [ "${type}" = "adblock" ]
58
+        then
59
+            option_cb()
60
+            {
61
+                local option="${1}"
62
+                local value="${2}"
63
+                eval "${option}=\"${value}\""
64
+            }
65
+        else
66
+            option_cb()
67
+            {
68
+                local option="${1}"
69
+                local value="${2}"
70
+                local opt_out="$(printf "${option}" | sed -n '/.*_ITEM[0-9]$/p; /.*_LENGTH$/p; /enabled/p')"
71
+                if [ -z "${opt_out}" ]
72
+                then
73
+                    all_options="${all_options} ${option}"
74
+                fi
75
+            }
76
+            list_cb()
77
+            {
78
+                local list="${1}"
79
+                local value="${2}"
80
+                if [ "${list}" = "adb_wanlist" ]
81
+                then
82
+                    adb_wandev="${adb_wandev} ${value}"
83
+                elif [ "${list}" = "adb_ntplist" ]
84
+                then
85
+                    adb_ntpsrv="${adb_ntpsrv} ${value}"
86
+                elif [ "${list}" = "adb_catlist" ]
87
+                then
88
+                    adb_cat_shalla="${adb_cat_shalla} ${value}"
89
+                fi
90
+            }
91
+        fi
92
+    }
93
+
94
+    # function to iterate through option list, read/set all options in "enabled" sections
95
+    #
96
+    parse_config()
97
+    {
98
+        local config="${1}"
99
+        config_get switch "${config}" "enabled"
100
+        if [ "${switch}" = "1" ]
101
+        then
102
+            for option in ${all_options}
103
+            do
104
+                config_get value "${config}" "${option}"
105
+                if [ -n "${value}" ]
106
+                then
107
+                    local opt_src="$(printf "${option}" | sed -n '/^adb_src_[a-z0-9]*$/p')"
108
+                    if [ -n "${opt_src}" ]
109
+                    then
110
+                        adb_sources="${adb_sources} ${value}"
111
+                    else
112
+                        eval "${option}=\"${value}\""
113
+                    fi
114
+                fi
115
+            done
116
+        elif [ "${config}" = "wancheck" ]
117
+        then
118
+           unset adb_wandev
119
+        elif [ "${config}" = "ntpcheck" ]
120
+        then
121
+           unset adb_ntpsrv
122
+        elif [ "${config}" = "shalla" ]
123
+        then
124
+           unset adb_cat_shalla
125
+        fi
126
+    }
127
+
128
+    # load adblock config and start parsing functions
129
+    #
130
+    config_load adblock
131
+    config_foreach parse_config service
132
+    config_foreach parse_config source
133
+
134
+    # set temp variables and counter
135
+    #
136
+    adb_tmpfile="$(mktemp -tu)"
137
+    adb_tmpdir="$(mktemp -d)"
138
+    cnt=0
139
+    max_cnt=30
140
+    max_time=60
141
+
142
+    # set adblock source ruleset definitions
143
+    #
144
+    rset_start="sed -r 's/[[:space:]]|[\[!#/:;_].*|[0-9\.]*localhost//g; s/[\^#/:;_\.\t ]*$//g'"
145
+    rset_end="sed '/^[#/:;_\s]*$/d'"
146
+    rset_default="${rset_start} | ${rset_end}"
147
+    rset_yoyo="${rset_start} | sed 's/,/\n/g' | ${rset_end}"
148
+    rset_shalla="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}$//g' | ${rset_end}"
149
+    rset_spam404="${rset_start} | sed 's/^\|\|//g' | ${rset_end}"
150
+    rset_winhelp="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
151
+
152
+    # set adblock/dnsmasq destination file and format
153
+    #
154
+    adb_dnsfile="/tmp/dnsmasq.d/adlist.conf"
155
+    adb_dnsformat="sed 's/^/address=\//;s/$/\/'${adb_ip}'/'"
156
+}
157
+
158
+#############################################
159
+# f_envcheck: check environment prerequisites
160
+#
161
+f_envcheck()
162
+{
163
+    # check adblock network device configuration
164
+    #
165
+    if [ ! -d "/sys/class/net/${adb_dev}" ]
166
+    then
167
+        /usr/bin/logger -t "adblock[${pid}]" "error: invalid adblock network device input (${adb_dev})"
168
+        f_deltemp
169
+        exit 25
170
+    fi
171
+
172
+    # check adblock network interface configuration
173
+    #
174
+    check_if="$(printf "${adb_if}" | sed -n '/[^_0-9A-Za-z]/p')"
175
+    banned_if="$(printf "${adb_if}" | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p')"
176
+    if [ -n "${check_if}" ] || [ -n "${banned_if}" ]
177
+    then
178
+        /usr/bin/logger -t "adblock[${pid}]" "error: invalid adblock network interface input (${adb_if})"
179
+        f_deltemp
180
+        exit 30
181
+    fi
182
+
183
+    # check adblock ip address configuration
184
+    #
185
+    check_ip="$(printf "${adb_ip}" | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p')"
186
+    if [ -z "${check_ip}" ]
187
+    then
188
+        /usr/bin/logger -t "adblock[${pid}]" "error: invalid adblock ip address input (${adb_ip})"
189
+        f_deltemp
190
+        exit 35
191
+    fi
192
+
193
+    # check adblock blacklist/whitelist configuration
194
+    #
195
+    if [ ! -r "${adb_blacklist}" ]
196
+    then
197
+        /usr/bin/logger -t "adblock[${pid}]" "error: adblock blacklist not found"
198
+        f_deltemp
199
+        exit 40
200
+    elif [ ! -r "${adb_whitelist}" ]
201
+    then
202
+        /usr/bin/logger -t "adblock[${pid}]" "error: adblock whitelist not found"
203
+        f_deltemp
204
+        exit 45
205
+    fi
206
+
207
+    # check wan update configuration
208
+    #
209
+    if [ -n "${adb_wandev}" ]
210
+    then
211
+        wan_ok="true"
212
+    else
213
+        wan_ok="false"
214
+        /usr/bin/logger -t "adblock[${pid}]" "info: wan update check will be disabled"
215
+    fi
216
+
217
+    # check ntp sync configuration
218
+    #
219
+    if [ -n "${adb_ntpsrv}" ]
220
+    then
221
+        ntp_ok="true"
222
+    else
223
+        ntp_ok="false"
224
+        /usr/bin/logger -t "adblock[${pid}]" "info: ntp time sync will be disabled"
225
+    fi
226
+
227
+    # check backup configuration
228
+    #
229
+    adb_backupdir="${adb_backupfile%/*}"
230
+    if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
231
+    then
232
+        backup_ok="true"
233
+        adb_mounts="${adb_backupdir} ${adb_tmpdir}"
234
+    else
235
+        backup_ok="false"
236
+        /usr/bin/logger -t "adblock[${pid}]" "info: backup/restore will be disabled"
237
+    fi
238
+
239
+    # check error log configuration
240
+    #
241
+    adb_logdir="${adb_logfile%/*}"
242
+    if [ -n "${adb_logfile}" ] && [ "${adb_logfile}" = "/dev/stdout" ]
243
+    then
244
+        log_ok="true"
245
+        adb_logfile="/proc/self/fd/1"
246
+    elif [ -n "${adb_logdir}" ] && [ -d "${adb_logdir}" ] && [ "${ntp_ok}" = "true" ]
247
+    then
248
+        log_ok="true"
249
+        adb_mounts="${adb_mounts} ${adb_logdir}"
250
+    else
251
+        log_ok="false"
252
+        adb_logfile="/dev/null"
253
+        /usr/bin/logger -t "adblock[${pid}]" "info: error logging will be disabled"
254
+    fi
255
+
256
+    # check dns query log configuration
257
+    #
258
+    adb_querydir="${adb_queryfile%/*}"
259
+    if [ -n "${adb_querydir}" ] && [ -d "${adb_querydir}" ]
260
+    then
261
+        # check find capabilities
262
+        #
263
+        check="$(find --help 2>&1 | grep "mtime")"
264
+        if [ -z "${check}" ]
265
+        then
266
+            query_ok="false"
267
+            /usr/bin/logger -t "adblock[${pid}]" "info: busybox without 'find/mtime' support (min. r47362), dns query logging will be disabled"
268
+        else
269
+            query_ok="true"
270
+            query_name="${adb_queryfile##*/}"
271
+            query_ip="${adb_ip//./\\.}"
272
+            query_pid="/var/run/query.pid"
273
+            adb_mounts="${adb_mounts} ${adb_querydir}"
274
+        fi
275
+    else
276
+        query_ok="false"
277
+        /usr/bin/logger -t "adblock[${pid}]" "info: dns query logging will be disabled"
278
+    fi
279
+
280
+    # check mount points & space requirements
281
+    #
282
+    adb_mounts="${adb_mounts} ${adb_tmpdir}"
283
+    for mp in ${adb_mounts}
284
+    do
285
+        df "${mp}" 2>/dev/null |\
286
+        tail -n1 |\
287
+        while read filesystem overall used available scrap
288
+        do
289
+            av_space="${available}"
290
+            if [ $((av_space)) -eq 0 ]
291
+            then
292
+                /usr/bin/logger -t "adblock[${pid}]" "error: no space left on device/not mounted (${mp})"
293
+                exit 50
294
+            elif [ $((av_space)) -lt $((adb_minspace)) ]
295
+            then
296
+                /usr/bin/logger -t "adblock[${pid}]" "error: not enough space on device (${mp})"
297
+                exit 55
298
+            fi
299
+        done
300
+        # subshell return code handling
301
+        #
302
+        rc=$?
303
+        if [ $((rc)) -ne 0 ]
304
+        then
305
+            f_deltemp
306
+            exit ${rc}
307
+        fi
308
+    done
309
+
310
+    # check curl package dependency
311
+    #
312
+    check="$(printf "${pkg_list}" | grep "^curl")"
313
+    if [ -z "${check}" ]
314
+    then
315
+        /usr/bin/logger -t "adblock[${pid}]" "error: curl package not found"
316
+        f_deltemp
317
+        exit 60
318
+    fi
319
+
320
+    # check wget package dependency
321
+    #
322
+    check="$(printf "${pkg_list}" | grep "^wget")"
323
+    if [ -z "${check}" ]
324
+    then
325
+        /usr/bin/logger -t "adblock[${pid}]" "error: wget package not found"
326
+        f_deltemp
327
+        exit 65
328
+    fi
329
+
330
+    # check dynamic/volatile adblock network interface configuration
331
+    #
332
+    rc="$(ifstatus "${adb_if}" >/dev/null 2>&1; printf $?)"
333
+    if [ $((rc)) -ne 0 ]
334
+    then
335
+        json_init
336
+        json_add_string name "${adb_if}"
337
+        json_add_string ifname "${adb_dev}"
338
+        json_add_string proto "static"
339
+        json_add_array ipaddr
340
+        json_add_string "" "${adb_ip}"
341
+        json_close_array
342
+        json_close_object
343
+        ubus call network add_dynamic "$(json_dump)"
344
+        /usr/bin/logger -t "adblock[${pid}]" "info: created new dynamic/volatile network interface (${adb_if}, ${adb_ip})"
345
+    fi
346
+
347
+    # check dynamic/volatile adblock uhttpd instance configuration
348
+    #
349
+    rc="$(ps | grep "[u]httpd.*\-r ${adb_if}" >/dev/null 2>&1; printf $?)"
350
+    if [ $((rc)) -ne 0 ]
351
+    then
352
+        uhttpd -h "/www/adblock" -r "${adb_if}" -E "/adblock.html" -p "${adb_ip}:80"
353
+        /usr/bin/logger -t "adblock[${pid}]" "info: created new dynamic/volatile uhttpd instance (${adb_if}, ${adb_ip})"
354
+    fi
355
+}
356
+
357
+###################################################
358
+# f_deltemp: delete temporary files and directories
359
+#
360
+f_deltemp()
361
+{
362
+    if [ -f "${adb_tmpfile}" ]
363
+    then
364
+       rm -f "${adb_tmpfile}" 2>/dev/null
365
+    fi
366
+    if [ -d "${adb_tmpdir}" ]
367
+    then
368
+       rm -rf "${adb_tmpdir}" 2>/dev/null
369
+    fi
370
+}
371
+
372
+################################################################
373
+# f_remove: remove temporary files, start and maintain query log
374
+#
375
+f_remove()
376
+{
377
+    # delete temporary files and directories
378
+    #
379
+    f_deltemp
380
+
381
+    # kill existing domain query log background process,
382
+    # housekeeping and start of a new process on daily basis
383
+    #
384
+    if [ "${query_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
385
+    then
386
+        query_date="$(date "+%Y%m%d")"
387
+        if [ -s "${query_pid}" ] && [ ! -f "${adb_queryfile}.${query_date}" ]
388
+        then
389
+            kill -9 $(< "${query_pid}") 2>/dev/null
390
+            > "${query_pid}"
391
+            find "${adb_backupdir}" -maxdepth 1 -type f -mtime +${adb_queryhistory} -name "${query_name}.*" -exec rm -f {} \; 2>/dev/null
392
+            /usr/bin/logger -t "adblock[${pid}]" "info: kill old query log background process and do logfile housekeeping"
393
+        fi
394
+        if [ ! -s "${query_pid}" ]
395
+        then
396
+            ( logread -f 2>/dev/null & printf -n "$!" > "${query_pid}" ) | egrep -o "(query\[A\].*)|([a-z0-9\.\-]* is ${query_ip}$)" >> "${adb_queryfile}.${query_date}" &
397
+            /usr/bin/logger -t "adblock[${pid}]" "info: start new domain query log background process"
398
+        fi
399
+    fi
400
+
401
+    # final log entry
402
+    #
403
+    /usr/bin/logger -t "adblock[${pid}]" "info: domain adblock processing finished (${adb_version})"
404
+}
405
+
406
+#####################################################
407
+# f_restore: if available, restore last adlist backup
408
+#
409
+f_restore()
410
+{
411
+    if [ -z "${restore_msg}" ]
412
+    then
413
+        restore_msg="unknown"
414
+    fi
415
+
416
+    if [ "${backup_ok}" = "true" ] && [ -f "${adb_backupfile}" ]
417
+    then
418
+        cp -f "${adb_backupfile}" "${adb_dnsfile}" 2>/dev/null
419
+        /usr/bin/logger -t "adblock[${pid}]" "error: ${restore_msg}, adlist backup restored"
420
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, adlist backup restored" >> "${adb_logfile}"
421
+    else
422
+        > "${adb_dnsfile}"
423
+        /usr/bin/logger -t "adblock[${pid}]" "error: ${restore_msg}, empty adlist generated"
424
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, empty adlist generated" >> "${adb_logfile}"
425
+    fi
426
+
427
+    # restart dnsmasq
428
+    #
429
+    /etc/init.d/dnsmasq restart >/dev/null 2>&1
430
+
431
+    # remove files and exit
432
+    #
433
+    f_remove
434
+    exit 100
435
+}
436
+
437
+#######################################################
438
+# f_wancheck: check for usable adblock update interface
439
+#
440
+f_wancheck()
441
+{
442
+    if [ "${wan_ok}" = "true" ]
443
+    then
444
+        # wait for wan update interface(s)
445
+        #
446
+        while [ $((cnt)) -le $((max_cnt)) ]
447
+        do
448
+            for dev in ${adb_wandev}
449
+            do
450
+                if [ -d "/sys/class/net/${dev}" ]
451
+                then
452
+                    dev_out=$(< /sys/class/net/${dev}/operstate 2>/dev/null)
453
+                    if [ "${dev_out}" = "up" ]
454
+                    then
455
+                        /usr/bin/logger -t "adblock[${pid}]" "info: get wan/update interface: ${dev}, after ${cnt} loops"
456
+                        break 2
457
+                    fi
458
+                fi
459
+                if [ $((cnt)) -eq $((max_cnt)) ]
460
+                then
461
+                    /usr/bin/logger -t "adblock[${pid}]" "error: no wan/update interface(s) found (${adb_wandev})"
462
+                    printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: no wan/update interface(s) found (${adb_wandev})" >> "${adb_logfile}"
463
+                    restore_msg="no wan/update interface(s)"
464
+                    f_restore
465
+                fi
466
+            done
467
+            sleep 1
468
+            cnt=$((cnt + 1))
469
+        done
470
+    fi
471
+}
472
+
473
+#####################################
474
+# f_ntpcheck: check/get ntp time sync
475
+#
476
+f_ntpcheck()
477
+{
478
+    if [ "${ntp_ok}" = "true" ]
479
+    then
480
+        # prepare ntp server pool
481
+        #
482
+        unset ntp_pool
483
+        for srv in ${adb_ntpsrv}
484
+        do
485
+            ntp_pool="${ntp_pool} -p ${srv}"
486
+        done
487
+
488
+        # wait for ntp time sync
489
+        #
490
+        while [ $((cnt)) -le $((max_cnt)) ]
491
+        do
492
+            /usr/sbin/ntpd -nq ${ntp_pool} >/dev/null 2>&1
493
+            rc=$?
494
+            if [ $((rc)) -eq 0 ]
495
+            then
496
+                /usr/bin/logger -t "adblock[${pid}]" "info: get ntp time sync (${adb_ntpsrv}), after ${cnt} loops"
497
+                break
498
+            fi
499
+            if [ $((cnt)) -eq $((max_cnt)) ]
500
+            then
501
+                ntp_ok="false"
502
+                /usr/bin/logger -t "adblock[${pid}]" "error: ntp time sync failed (${adb_ntpsrv})"
503
+                printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ntp time sync failed (${adb_ntpsrv})" >> "${adb_logfile}"
504
+                restore_msg="time sync failed"
505
+                f_restore
506
+            fi
507
+            sleep 1
508
+            cnt=$((cnt + 1))
509
+        done
510
+    fi
511
+}
512
+
513
+#################################################################
514
+# f_dnscheck: dnsmasq health check with newly generated blocklist
515
+#
516
+f_dnscheck()
517
+{
518
+    # check 1: dnsmasq startup
519
+    #
520
+    dns_status="$(logread -l 20 -e "dnsmasq" -e "FAILED to start up")"
521
+    if [ -z "${dns_status}" ]
522
+    then
523
+        # check 2: nslookup probe
524
+        #
525
+        dns_status="$(nslookup "${adb_domain}" 2>/dev/null | grep "${adb_ip}")"
526
+        if [ -z "${dns_status}" ]
527
+        then
528
+            # create backup of new block list only, if both checks are OK and backup enabled
529
+            #
530
+            if [ "${backup_ok}" = "true" ]
531
+            then
532
+                cp -f "${adb_dnsfile}" "${adb_backupfile}" 2>/dev/null
533
+                /usr/bin/logger -t "adblock[${pid}]" "info: new block list with ${adb_count} domains loaded, backup generated"
534
+            else
535
+                /usr/bin/logger -t "adblock[${pid}]" "info: new block list with ${adb_count} domains loaded, no backup"
536
+            fi
537
+        else
538
+            restore_msg="nslookup probe failed"
539
+            f_restore
540
+        fi
541
+    else
542
+            restore_msg="dnsmasq probe failed"
543
+            f_restore
544
+    fi
545
+}
546
+
547
+##########################################################
548
+# f_footer: write footer with a few statistics to dns file
549
+#
550
+f_footer()
551
+{
552
+    adb_count="$(wc -l < "${adb_dnsfile}")"
553
+    printf "%s\n" "###################################################" >> "${adb_dnsfile}"
554
+    printf "%s\n" "# last adblock file update: $(date +"%d.%m.%Y - %T")" >> "${adb_dnsfile}"
555
+    printf "%s\n" "# ${0##*/} (${adb_version}) - ${adb_count} ad/abuse domains blocked" >> "${adb_dnsfile}"
556
+    printf "%s\n" "# domain blacklist sources:" >> "${adb_dnsfile}"
557
+    for src in ${adb_sources}
558
+    do
559
+        url="${src//\&ruleset=*/}"
560
+        printf "%s\n" "# ${url}" >> "${adb_dnsfile}"
561
+    done
562
+    printf "%s\n" "###################################################" >> "${adb_dnsfile}"
563
+    printf "%s\n" "# domain whitelist source:" >> "${adb_dnsfile}"
564
+    printf "%s\n" "# ${adb_whitelist}" >> "${adb_dnsfile}"
565
+    printf "%s\n" "###################################################" >> "${adb_dnsfile}"
566
+}

net/adblock/files/usr/bin/adblock-update.sh → net/adblock/files/adblock-update.sh View File

@@ -25,38 +25,21 @@
25 25
 
26 26
 # set script version
27 27
 #
28
-script_ver="0.11.0"
28
+adb_version="0.20.0"
29 29
 
30 30
 # get current pid and script directory
31 31
 #
32 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
33
+adb_scriptdir="${0%/*}"
50 34
 
51 35
 # source in adblock function library
52 36
 #
53
-if [ -r "${script_dir}/adblock-helper.sh" ]
37
+if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
54 38
 then
55
-    . "${script_dir}/adblock-helper.sh"
39
+    . "${adb_scriptdir}/adblock-helper.sh"
56 40
 else
57
-    /usr/bin/logger -t "adblock[${pid}]" "adblock function library not found"
58
-    rm -rf "${tmp_dir}" 2>/dev/null
59
-    exit 210
41
+    /usr/bin/logger -t "adblock[${pid}]" "error: adblock function library not found"
42
+    exit 200
60 43
 fi
61 44
 
62 45
 ################
@@ -69,7 +52,15 @@ trap "restore_msg='trap error'; f_restore" 1 2 3 10 11 15
69 52
 
70 53
 # start logging
71 54
 #
72
-/usr/bin/logger -t "adblock[${pid}]" "domain adblock processing started (${script_ver})"
55
+/usr/bin/logger -t "adblock[${pid}]" "info: domain adblock processing started (${adb_version})"
56
+
57
+# load environment
58
+#
59
+f_envload
60
+
61
+# parse environment
62
+#
63
+f_envparse
73 64
 
74 65
 # check environment
75 66
 #
@@ -83,32 +74,36 @@ f_wancheck
83 74
 #
84 75
 f_ntpcheck
85 76
 
86
-# download shallalist archive
77
+# check/start shallalist (pre-)processing
87 78
 #
88
-if [ "${shalla_ok}" = "true" ]
79
+if [ -n "${adb_arc_shalla}" ]
89 80
 then
90
-    curl --insecure --max-time "${max_time}" "${shalla_url}" -o "${shalla_archive}" 2>/dev/null
81
+    # download shallalist archive
82
+    #
83
+    shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
84
+    shalla_file="${adb_tmpdir}/shallalist.txt"
85
+    curl --insecure --max-time "${max_time}" "${adb_arc_shalla}" -o "${shalla_archive}" 2>/dev/null
91 86
     rc=$?
92 87
     if [ $((rc)) -eq 0 ]
93 88
     then
94
-        /usr/bin/logger -t "adblock[${pid}]" "shallalist archive download finished"
89
+        /usr/bin/logger -t "adblock[${pid}]" "info: shallalist archive download finished"
95 90
     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}"
91
+        /usr/bin/logger -t "adblock[${pid}]" "error: shallalist archive download failed (${adb_arc_shalla})"
92
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist archive download failed (${adb_arc_shalla})" >> "${adb_logfile}"
98 93
         restore_msg="archive download failed"
99 94
         f_restore
100 95
     fi
101 96
 
102 97
     # extract shallalist archive
103 98
     #
104
-    tar -xzf "${shalla_archive}" -C "${tmp_dir}" 2>/dev/null
99
+    tar -xzf "${shalla_archive}" -C "${adb_tmpdir}" 2>/dev/null
105 100
     rc=$?
106 101
     if [ $((rc)) -eq 0 ]
107 102
     then
108
-        /usr/bin/logger -t "adblock[${pid}]" "shallalist archive extraction finished"
103
+        /usr/bin/logger -t "adblock[${pid}]" "info: shallalist archive extraction finished"
109 104
     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}"
105
+        /usr/bin/logger -t "adblock[${pid}]" "error: shallalist archive extraction failed"
106
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist archive extraction failed" >> "${adb_logfile}"
112 107
         restore_msg="archive extraction failed"
113 108
         f_restore
114 109
     fi
@@ -116,11 +111,11 @@ then
116 111
     # merge selected shallalist categories
117 112
     #
118 113
     > "${shalla_file}"
119
-    for category in ${shalla_cat}
114
+    for category in ${adb_cat_shalla}
120 115
     do
121
-        if [ -f "${tmp_dir}/BL/${category}/domains" ]
116
+        if [ -f "${adb_tmpdir}/BL/${category}/domains" ]
122 117
         then
123
-            cat "${tmp_dir}/BL/${category}/domains" >> "${shalla_file}" 2>/dev/null
118
+            cat "${adb_tmpdir}/BL/${category}/domains" >> "${shalla_file}" 2>/dev/null
124 119
             rc=$?
125 120
         else
126 121
             rc=220
@@ -135,23 +130,25 @@ then
135 130
     #
136 131
     if [ $((rc)) -eq 0 ]
137 132
     then
138
-        /usr/bin/logger -t "adblock[${pid}]" "shallalist (pre-)processing finished (${shalla_cat})"
133
+        adb_sources="${adb_sources} file:///${shalla_file}&ruleset=rset_shalla"
134
+        /usr/bin/logger -t "adblock[${pid}]" "info: shallalist (pre-)processing finished (${adb_cat_shalla})"
139 135
     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}"
136
+        /usr/bin/logger -t "adblock[${pid}]" "error: shallalist (pre-)processing failed (${rc}, ${adb_cat_shalla})"
137
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: shallalist (pre-)processing failed (${rc}, ${adb_cat_shalla})" >> "${adb_logfile}"
142 138
         restore_msg="shallalist merge failed"
143 139
         f_restore
144 140
     fi
145 141
 fi
146 142
 
147
-# loop through domain source list,
143
+# loop through active adblock domain sources,
148 144
 # prepare output and store all extracted domains in temp file
149 145
 #
150
-for src in ${adb_source}
146
+adb_sources="${adb_sources} file://${adb_blacklist}&ruleset=rset_default"
147
+for src in ${adb_sources}
151 148
 do
152 149
     # download selected adblock sources
153 150
     #
154
-    url="$(printf "${src}" | sed 's/\(\&ruleset=.*\)//g')"
151
+    url="${src//\&ruleset=*/}"
155 152
     check_url="$(printf "${url}" | sed -n '/^https:/p')"
156 153
     if [ -n "${check_url}" ]
157 154
     then
@@ -166,16 +163,16 @@ do
166 163
     #
167 164
     if [ $((rc)) -eq 0 ] && [ -n "${tmp_var}" ]
168 165
     then
169
-        eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$rset_/g')"
166
+        eval "$(printf "${src}" | sed 's/\(.*\&ruleset=\)/ruleset=\$/g')"
170 167
         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)"
168
+        adb_count="$(printf "%s\n" "${tmp_var}" | eval "${ruleset}" | tee -a "${adb_tmpfile}" | wc -l)"
169
+        /usr/bin/logger -t "adblock[${pid}]" "info: source download finished (${url}, ${adb_count} entries)"
173 170
     elif [ $((rc)) -eq 0 ] && [ -z "${tmp_var}" ]
174 171
     then
175
-        /usr/bin/logger -t "adblock[${pid}]" "empty source download finished (${url})"
172
+        /usr/bin/logger -t "adblock[${pid}]" "info: empty source download finished (${url})"
176 173
     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}"
174
+        /usr/bin/logger -t "adblock[${pid}]" "error: source download failed (${url})"
175
+        printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: source download failed (${url})" >> "${adb_logfile}"
179 176
         restore_msg="download failed"
180 177
         f_restore
181 178
     fi
@@ -183,12 +180,12 @@ done
183 180
 
184 181
 # create empty destination file
185 182
 #
186
-> "${dns_file}"
183
+> "${adb_dnsfile}"
187 184
 
188 185
 # rewrite ad/abuse domain information to dns file,
189 186
 # remove duplicates and whitelist entries
190 187
 #
191
-grep -vxf "${adb_whitelist}" < "${tmp_file}" | eval "${dns_format}" | sort -u 2>/dev/null >> "${dns_file}"
188
+grep -vxf "${adb_whitelist}" < "${adb_tmpfile}" | eval "${adb_dnsformat}" | sort -u 2>/dev/null >> "${adb_dnsfile}"
192 189
 
193 190
 # write dns file footer
194 191
 #

net/adblock/files/etc/adblock/adblock.blacklist → net/adblock/files/adblock.blacklist View File


+ 76
- 0
net/adblock/files/adblock.conf View File

@@ -0,0 +1,76 @@
1
+# adblock configuration
2
+#
3
+config adblock "global"
4
+	option adb_ip "192.168.2.1"
5
+	option adb_dev "eth0"
6
+	option adb_if "adblock"
7
+	option adb_domain "heise.de"
8
+	option adb_minspace "100000"
9
+	option adb_blacklist "/etc/adblock/adblock.blacklist"
10
+	option adb_whitelist "/etc/adblock/adblock.whitelist"
11
+
12
+config service "wancheck"
13
+	option enabled "0"
14
+	list adb_wanlist "wan"
15
+
16
+config service "ntpcheck"
17
+	option enabled "0"
18
+	list adb_ntplist "0.pool.ntp.org"
19
+	list adb_ntplist "1.pool.ntp.org"
20
+	list adb_ntplist "2.pool.ntp.org"
21
+	list adb_ntplist "3.pool.ntp.org"
22
+
23
+config service "backup"
24
+	option enabled "0"
25
+	option adb_backupfile "/tmp/adlist.backup"
26
+
27
+config service "errorlog"
28
+	option enabled "0"
29
+	option adb_logfile "/tmp/error.log"
30
+
31
+config service "querylog"
32
+	option enabled "0"
33
+	option adb_queryfile "/tmp/query.log"
34
+	option adb_queryhistory "1"
35
+
36
+config source "yoyo"
37
+	option enabled "1"
38
+	option adb_src_yoyo "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=0&mimetype=plaintext&ruleset=rset_yoyo"
39
+
40
+config source "malware"
41
+	option enabled "0"
42
+	option adb_src_malware "http://mirror1.malwaredomains.com/files/justdomains&ruleset=rset_default"
43
+
44
+config source "zeus"
45
+	option enabled "0"
46
+	option adb_src_zeus "https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist&ruleset=rset_default"
47
+
48
+config source "feodo"
49
+	option enabled "0"
50
+	option adb_src_feodo "https://feodotracker.abuse.ch/blocklist/?download=domainblocklist&ruleset=rset_default"
51
+
52
+config source "palevo"
53
+	option enabled "0"
54
+	option adb_src_palevo "https://palevotracker.abuse.ch/blocklists.php?download=domainblocklist&ruleset=rset_default"
55
+
56
+config source "dshield"
57
+	option enabled "0"
58
+	option adb_src_dshield "http://www.dshield.org/feeds/suspiciousdomains_Low.txt&ruleset=rset_default"
59
+
60
+config source "spam404"
61
+	option enabled "0"
62
+	option adb_src_spam404 "http://spam404bl.com/spam404scamlist.txt&ruleset=rset_spam404"
63
+
64
+config source "winhelp"
65
+	option enabled "0"
66
+	option adb_src_winhelp "http://winhelp2002.mvps.org/hosts.txt&ruleset=rset_winhelp"
67
+
68
+config source "shalla"
69
+	option enabled "0"
70
+	option adb_arc_shalla "http://www.shallalist.de/Downloads/shallalist.tar.gz"
71
+	list adb_catlist "adv"
72
+	list adb_catlist "costtraps"
73
+	list adb_catlist "downloads"
74
+	list adb_catlist "spyware"
75
+	list adb_catlist "tracker"
76
+	list adb_catlist "warez"

net/adblock/files/etc/adblock/adblock.whitelist → net/adblock/files/adblock.whitelist View File


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

@@ -1,118 +0,0 @@
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}'/'"

+ 76
- 0
net/adblock/files/samples/adblock.conf.sample View File

@@ -0,0 +1,76 @@
1
+# adblock configuration
2
+#
3
+config adblock "global"
4
+	option adb_ip "192.168.2.1"
5
+	option adb_dev "eth0"
6
+	option adb_if "adblock"
7
+	option adb_domain "heise.de"
8
+	option adb_minspace "100000"
9
+	option adb_blacklist "/etc/adblock/adblock.blacklist"
10
+	option adb_whitelist "/etc/adblock/adblock.whitelist"
11
+
12
+config service "wancheck"
13
+	option enabled "0"
14
+	list adb_wanlist "wan"
15
+
16
+config service "ntpcheck"
17
+	option enabled "0"
18
+	list adb_ntplist "0.pool.ntp.org"
19
+	list adb_ntplist "1.pool.ntp.org"
20
+	list adb_ntplist "2.pool.ntp.org"
21
+	list adb_ntplist "3.pool.ntp.org"
22
+
23
+config service "backup"
24
+	option enabled "0"
25
+	option adb_backupfile "/tmp/adlist.backup"
26
+
27
+config service "errorlog"
28
+	option enabled "0"
29
+	option adb_logfile "/tmp/error.log"
30
+
31
+config service "querylog"
32
+	option enabled "0"
33
+	option adb_queryfile "/tmp/query.log"
34
+	option adb_queryhistory "1"
35
+
36
+config source "yoyo"
37
+	option enabled "1"
38
+	option adb_src_yoyo "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=0&mimetype=plaintext&ruleset=rset_yoyo"
39
+
40
+config source "malware"
41
+	option enabled "0"
42
+	option adb_src_malware "http://mirror1.malwaredomains.com/files/justdomains&ruleset=rset_default"
43
+
44
+config source "zeus"
45
+	option enabled "0"
46
+	option adb_src_zeus "https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist&ruleset=rset_default"
47
+
48
+config source "feodo"
49
+	option enabled "0"
50
+	option adb_src_feodo "https://feodotracker.abuse.ch/blocklist/?download=domainblocklist&ruleset=rset_default"
51
+
52
+config source "palevo"
53
+	option enabled "0"
54
+	option adb_src_palevo "https://palevotracker.abuse.ch/blocklists.php?download=domainblocklist&ruleset=rset_default"
55
+
56
+config source "dshield"
57
+	option enabled "0"
58
+	option adb_src_dshield "http://www.dshield.org/feeds/suspiciousdomains_Low.txt&ruleset=rset_default"
59
+
60
+config source "spam404"
61
+	option enabled "0"
62
+	option adb_src_spam404 "http://spam404bl.com/spam404scamlist.txt&ruleset=rset_spam404"
63
+
64
+config source "winhelp"
65
+	option enabled "0"
66
+	option adb_src_winhelp "http://winhelp2002.mvps.org/hosts.txt&ruleset=rset_winhelp"
67
+
68
+config source "shalla"
69
+	option enabled "0"
70
+	option adb_arc_shalla "http://www.shallalist.de/Downloads/shallalist.tar.gz"
71
+	list adb_catlist "adv"
72
+	list adb_catlist "costtraps"
73
+	list adb_catlist "downloads"
74
+	list adb_catlist "spyware"
75
+	list adb_catlist "tracker"
76
+	list adb_catlist "warez"

net/adblock/files/etc/adblock/samples/dhcp.config.sample → net/adblock/files/samples/dhcp.config.sample View File


net/adblock/files/etc/adblock/samples/dnsmasq.conf.sample → net/adblock/files/samples/dnsmasq.conf.sample View File


net/adblock/files/etc/adblock/samples/firewall.user.sample → net/adblock/files/samples/firewall.user.sample View File


net/adblock/files/etc/adblock/samples/rc.local.sample → net/adblock/files/samples/rc.local.sample View File


net/adblock/files/etc/adblock/samples/root.crontab.sample → net/adblock/files/samples/root.crontab.sample View File


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

@@ -1,434 +0,0 @@
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
-}