6eb36fa58cec7a59a505e7304e5341148a0f097d
[feed/packages.git] / net / adblock / files / adblock.sh
1 #!/bin/sh
2 # dns based ad/abuse domain blocking
3 # written by Dirk Brenken (dev@brenken.org)
4
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8
9 # set initial defaults
10 #
11 LC_ALL=C
12 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
13 adb_ver="3.0.2"
14 adb_sysver="unknown"
15 adb_enabled=0
16 adb_debug=0
17 adb_manmode=0
18 adb_forcesrt=0
19 adb_forcedns=0
20 adb_triggerdelay=0
21 adb_backup=0
22 adb_backupdir="/mnt"
23 adb_fetch="/usr/bin/wget"
24 adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
25 adb_dns="dnsmasq"
26 adb_dnsprefix="adb_list"
27 adb_dnsfile="${adb_dnsprefix}.overall"
28 adb_rtfile="/tmp/adb_runtime.json"
29 adb_action="${1:-"start"}"
30 adb_cnt=0
31 adb_rc=0
32
33 # f_envload: load adblock environment
34 #
35 f_envload()
36 {
37 local dns_up sys_call sys_desc sys_model sys_ver cnt=0
38
39 # get system information
40 #
41 sys_call="$(ubus -S call system board 2>/dev/null)"
42 if [ -n "${sys_call}" ]
43 then
44 sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
45 sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
46 sys_ver="$(cat /etc/turris-version 2>/dev/null)"
47 if [ -n "${sys_ver}" ]
48 then
49 sys_desc="${sys_desc}/${sys_ver}"
50 fi
51 adb_sysver="${sys_model}, ${sys_desc}"
52 fi
53
54 # source in system libraries
55 #
56 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
57 then
58 . "/lib/functions.sh"
59 . "/usr/share/libubox/jshn.sh"
60 else
61 f_log "error" "system libraries not found"
62 fi
63
64 # parse global section by callback
65 #
66 config_cb()
67 {
68 local type="${1}"
69 if [ "${type}" = "adblock" ]
70 then
71 option_cb()
72 {
73 local option="${1}"
74 local value="${2}"
75 eval "${option}=\"${value}\""
76 }
77 else
78 reset_cb
79 fi
80 }
81
82 # parse 'source' sections
83 #
84 parse_config()
85 {
86 local value opt section="${1}" options="enabled adb_src adb_src_rset adb_src_cat"
87 eval "adb_sources=\"${adb_sources} ${section}\""
88 for opt in ${options}
89 do
90 config_get value "${section}" "${opt}"
91 if [ -n "${value}" ]
92 then
93 eval "${opt}_${section}=\"${value}\""
94 fi
95 done
96 }
97
98 # load adblock config
99 #
100 config_load adblock
101 config_foreach parse_config source
102
103 # set/check dns backend environment
104 #
105 case "${adb_dns}" in
106 dnsmasq)
107 adb_dnsuser="${adb_dns}"
108 adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
109 adb_dnsformat="awk '{print \"local=/\"\$0\"/\"}'"
110 ;;
111 unbound)
112 adb_dnsuser="${adb_dns}"
113 adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}"
114 adb_dnsformat="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
115 ;;
116 named)
117 adb_dnsuser="bind"
118 adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}"
119 adb_dnsformat="awk '{print \"\"\$0\" IN CNAME .\n*.\"\$0\" IN CNAME .\"}'"
120 ;;
121 kresd)
122 adb_dnsuser="root"
123 adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}"
124 adb_dnsformat="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
125 adb_dnsheader="\$TTL 2h"$'\n'"@ IN SOA localhost. root.localhost. (2 6h 1h 1w 2h)"$'\n'" IN NS localhost."
126 ;;
127 dnscrypt-proxy)
128 adb_dnsuser="nobody"
129 adb_dnsdir="${adb_dnsdir:-"/tmp"}"
130 adb_dnsformat="awk '{print \$0}'"
131 ;;
132 esac
133
134 if [ -d "${adb_dnsdir}" ] && [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ]
135 then
136 > "${adb_dnsdir}/${adb_dnsfile}"
137 fi
138
139 case "${adb_action}" in
140 start|restart|reload)
141 > "${adb_rtfile}"
142 if [ "${adb_action}" = "start" ] && [ "${adb_trigger}" = "timed" ]
143 then
144 sleep ${adb_triggerdelay}
145 fi
146 ;;
147 esac
148
149 while [ ${cnt} -le 30 ]
150 do
151 dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running" 2>/dev/null)"
152 if [ "${dns_up}" = "true" ]
153 then
154 break
155 fi
156 sleep 1
157 cnt=$((cnt+1))
158 done
159
160 if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
161 then
162 f_log "error" "'${adb_dns}' not running, DNS backend not found"
163 fi
164
165 # force dns to local resolver
166 #
167 if [ ${adb_forcedns} -eq 1 ] && [ -z "$(uci -q get firewall.adblock_dns)" ]
168 then
169 uci -q set firewall.adblock_dns="redirect"
170 uci -q set firewall.adblock_dns.name="Adblock DNS"
171 uci -q set firewall.adblock_dns.src="lan"
172 uci -q set firewall.adblock_dns.proto="tcp udp"
173 uci -q set firewall.adblock_dns.src_dport="53"
174 uci -q set firewall.adblock_dns.dest_port="53"
175 uci -q set firewall.adblock_dns.target="DNAT"
176 elif [ ${adb_forcedns} -eq 0 ] && [ -n "$(uci -q get firewall.adblock_dns)" ]
177 then
178 uci -q delete firewall.adblock_dns
179 fi
180 if [ -n "$(uci -q changes firewall)" ]
181 then
182 uci -q commit firewall
183 if [ $(/etc/init.d/firewall enabled; printf "%u" ${?}) -eq 0 ]
184 then
185 /etc/init.d/firewall reload >/dev/null 2>&1
186 fi
187 fi
188 }
189
190 # f_envcheck: check/set environment prerequisites
191 #
192 f_envcheck()
193 {
194 local ssl_lib
195
196 # check 'enabled' option
197 #
198 if [ ${adb_enabled} -ne 1 ]
199 then
200 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
201 then
202 f_rmdns
203 f_dnsrestart
204 fi
205 f_jsnupdate
206 f_log "info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
207 exit 0
208 fi
209
210 # check fetch utility
211 #
212 ssl_lib="-"
213 if [ -x "${adb_fetch}" ]
214 then
215 if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
216 then
217 adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
218 elif [ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ] ||
219 ([ "$(readlink -fn "/bin/wget")" = "/bin/busybox" ] && [ "$(readlink -fn "${adb_fetch}")" != "/usr/bin/wget" ])
220 then
221 adb_fetch="/bin/busybox"
222 adb_fetchparm="-q -O"
223 else
224 ssl_lib="built-in"
225 fi
226 fi
227 if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin/wget")" = "/bin/uclient-fetch" ]
228 then
229 adb_fetch="/bin/uclient-fetch"
230 if [ -f "/lib/libustream-ssl.so" ]
231 then
232 adb_fetchparm="-q --timeout=10 --no-check-certificate -O"
233 ssl_lib="libustream-ssl"
234 else
235 adb_fetchparm="-q --timeout=10 -O"
236 fi
237 fi
238 if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
239 then
240 f_log "error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
241 fi
242 adb_fetchinfo="${adb_fetch##*/} (${ssl_lib})"
243
244 # create adblock temp file/directory
245 #
246 adb_tmpload="$(mktemp -tu)"
247 adb_tmpfile="$(mktemp -tu)"
248 adb_tmpdir="$(mktemp -p /tmp -d)"
249
250 # prepare whitelist entries
251 #
252 if [ -s "${adb_whitelist}" ] && [ -n "${adb_whitelist_rset}" ]
253 then
254 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
255 fi
256 }
257
258 # f_rmtemp: remove temporary files & directories
259 #
260 f_rmtemp()
261 {
262 if [ -d "${adb_tmpdir}" ]
263 then
264 rm -f "${adb_tmpload}"
265 rm -f "${adb_tmpfile}"
266 rm -rf "${adb_tmpdir}"
267 fi
268 }
269
270 # f_rmdns: remove dns related files & directories
271 #
272 f_rmdns()
273 {
274 if [ -n "${adb_dns}" ]
275 then
276 > "${adb_dnsdir}/${adb_dnsfile}"
277 > "${adb_rtfile}"
278 rm -f "${adb_dnsdir}/.${adb_dnsfile}"
279 rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
280 fi
281 }
282
283 # f_dnsrestart: restart the dns backend
284 #
285 f_dnsrestart()
286 {
287 local dns_up cnt=0
288
289 "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
290 while [ ${cnt} -le 10 ]
291 do
292 dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running")"
293 if [ "${dns_up}" = "true" ]
294 then
295 return 0
296 fi
297 cnt=$((cnt+1))
298 sleep 1
299 done
300 return 1
301 }
302
303 # f_list: backup/restore/remove block lists
304 #
305 f_list()
306 {
307 local mode="${1}" in_rc="${adb_rc}" cnt=0
308
309 case "${mode}" in
310 backup)
311 cnt="$(wc -l < "${adb_tmpfile}")"
312 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
313 then
314 gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
315 adb_rc=${?}
316 fi
317 ;;
318 restore)
319 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ] &&
320 [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
321 then
322 gunzip -cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
323 adb_rc=${?}
324 fi
325 ;;
326 remove)
327 if [ -d "${adb_backupdir}" ]
328 then
329 rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
330 fi
331 adb_rc=${?}
332 ;;
333 merge)
334 if [ -s "${adb_tmpfile}" ]
335 then
336 cat "${adb_tmpfile}" >> "${adb_tmpdir}/${adb_dnsfile}"
337 adb_rc=${?}
338 fi
339 ;;
340 format)
341 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
342 then
343 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpdir}/${adb_dnsfile}" | eval "${adb_dnsformat}" > "${adb_dnsdir}/${adb_dnsfile}"
344 else
345 eval "${adb_dnsformat}" "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
346 fi
347 if [ -n "${adb_dnsheader}" ]
348 then
349 printf '%s\n' "${adb_dnsheader}" | cat - "${adb_dnsdir}/${adb_dnsfile}" > "${adb_tmpdir}/${adb_dnsfile}"
350 cat "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
351 fi
352 adb_rc=${?}
353 ;;
354 esac
355 f_log "debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
356 }
357
358 # f_tldcompression: top level domain compression
359 #
360 f_tldcompression()
361 {
362 local source="${1}" temp="${adb_tmpload}"
363
364 awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev/null | sort -u > "${temp}"
365 awk '{if(NR==1){tld=$NF};while(getline){if($NF !~ tld"\\."){print tld;tld=$NF}}print tld}' "${temp}" 2>/dev/null > "${source}"
366 awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev/null > "${temp}"
367 sort -u "${temp}" > "${source}"
368 }
369
370 # f_switch: suspend/resume adblock processing
371 #
372 f_switch()
373 {
374 local source target status mode="${1}"
375
376 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && [ "${mode}" = "suspend" ]
377 then
378 source="${adb_dnsdir}/${adb_dnsfile}"
379 target="${adb_dnsdir}/.${adb_dnsfile}"
380 status="suspended"
381 elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ] && [ "${mode}" = "resume" ]
382 then
383 source="${adb_dnsdir}/.${adb_dnsfile}"
384 target="${adb_dnsdir}/${adb_dnsfile}"
385 status="resumed"
386 fi
387 if [ -n "${status}" ]
388 then
389 cat "${source}" > "${target}"
390 > "${source}"
391 f_dnsrestart
392 f_jsnupdate
393 f_log "info " "adblock processing ${status}"
394 fi
395 }
396
397 # f_query: query block list for certain (sub-)domains
398 #
399 f_query()
400 {
401 local search result cnt
402 local domain="${1}"
403 local tld="${domain#*.}"
404
405 if [ ! -s "${adb_dnsdir}/${adb_dnsfile}" ]
406 then
407 printf "%s\n" "::: no active block list found, please start / resume adblock first"
408 elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
409 then
410 printf "%s\n" "::: invalid domain input, please submit a single domain, e.g. 'doubleclick.net'"
411 else
412 cd "${adb_dnsdir}"
413 while [ "${domain}" != "${tld}" ]
414 do
415 search="${domain//./\.}"
416 if [ "${adb_dns}" = "dnsmasq" ] || [ "${adb_dns}" = "unbound" ]
417 then
418 result="$(awk -F '/|\"' "/[\/\"\.]${search}/{i++;{printf(\" + %s\n\",\$2)};if(i>9){exit}}" "${adb_dnsfile}")"
419 else
420 result="$(awk "/(^[^\*][a-z]*[\.]+${search}|^${search})/{i++;{printf(\" + %s\n\",\$1)};if(i>9){exit}}" "${adb_dnsfile}")"
421 fi
422 printf "%s\n" "::: max. ten results for domain '${domain}'"
423 printf "%s\n" "${result:-" - no match"}"
424 domain="${tld}"
425 tld="${domain#*.}"
426 done
427 fi
428 }
429
430 # f_jsnupdate: update runtime information
431 #
432 f_jsnupdate()
433 {
434 local status rundate="$(/bin/date "+%d.%m.%Y %H:%M:%S")"
435
436 if [ ${adb_rc} -gt 0 ]
437 then
438 status="error"
439 elif [ ${adb_enabled} -ne 1 ]
440 then
441 status="disabled"
442 elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ]
443 then
444 status="paused"
445 else
446 status="enabled"
447 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
448 then
449 if [ "${adb_dns}" = "named" ] || [ "${adb_dns}" = "kresd" ]
450 then
451 adb_cnt="$(( ( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ) / 2 ))"
452 else
453 adb_cnt="$(( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ))"
454 fi
455 fi
456 fi
457
458 if [ -z "${adb_fetchinfo}" ] && [ -s "${adb_rtfile}" ]
459 then
460 json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
461 json_select data
462 json_get_var adb_fetchinfo "fetch_utility"
463 fi
464
465 json_init
466 json_add_object "data"
467 json_add_string "adblock_status" "${status}"
468 json_add_string "adblock_version" "${adb_ver}"
469 json_add_string "blocked_domains" "${adb_cnt}"
470 json_add_string "fetch_utility" "${adb_fetchinfo}"
471 json_add_string "dns_backend" "${adb_dns} (${adb_dnsdir})"
472 json_add_string "last_rundate" "${rundate}"
473 json_add_string "system_release" "${adb_sysver}"
474 json_close_object
475 json_dump > "${adb_rtfile}"
476 }
477
478 # f_status: output runtime information
479 #
480 f_status()
481 {
482 local key keylist value
483
484 if [ -s "${adb_rtfile}" ]
485 then
486 printf "%s\n" "::: adblock runtime information"
487 json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
488 json_select data
489 json_get_keys keylist
490 for key in ${keylist}
491 do
492 json_get_var value "${key}"
493 printf " + %-15s : %s\n" "${key}" "${value}"
494 done
495 fi
496 }
497
498 # f_log: write to syslog, exit on error
499 #
500 f_log()
501 {
502 local class="${1}" log_msg="${2}"
503
504 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
505 then
506 logger -t "adblock-[${adb_ver}] ${class}" "${log_msg}"
507 if [ "${class}" = "error" ]
508 then
509 logger -t "adblock-[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
510 f_rmtemp
511 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
512 then
513 f_rmdns
514 f_dnsrestart
515 fi
516 adb_rc=1
517 f_jsnupdate
518 exit 1
519 fi
520 fi
521 }
522
523 # main function for block list processing
524 #
525 f_main()
526 {
527 local src_name src_rset shalla_archive enabled url hash_old hash_new
528 local mem_total="$(awk '/^MemTotal/ {print int($2/1000)}' "/proc/meminfo")"
529
530 f_log "info " "start adblock processing ..."
531 f_log "debug" "action: ${adb_action}, manual_mode:${adb_manmode}, backup: ${adb_backup}, dns: ${adb_dns}, fetch: ${adb_fetchinfo}, mem_total: ${mem_total}, force_srt/_dns: ${adb_forcesrt}/${adb_forcedns}"
532 > "${adb_rtfile}"
533 for src_name in ${adb_sources}
534 do
535 eval "enabled=\"\${enabled_${src_name}}\""
536 eval "url=\"\${adb_src_${src_name}}\""
537 eval "src_rset=\"\${adb_src_rset_${src_name}}\""
538 > "${adb_tmpload}"
539 > "${adb_tmpfile}"
540 adb_rc=4
541
542 # basic pre-checks
543 #
544 f_log "debug" "name: ${src_name}, enabled: ${enabled}, url: ${url}, rset: ${src_rset}"
545 if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
546 then
547 f_list remove
548 continue
549 fi
550
551 # manual / backup mode
552 #
553 if [ ${adb_manmode} -eq 1 ] && [ "${adb_action}" = "start" ] && [ "${src_name}" != "blacklist" ]
554 then
555 f_list restore
556 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
557 then
558 f_list merge
559 continue
560 fi
561 fi
562
563 # download block list
564 #
565 if [ "${src_name}" = "blacklist" ] && [ -s "${url}" ]
566 then
567 cat "${url}" > "${adb_tmpload}"
568 adb_rc=${?}
569 elif [ "${src_name}" = "shalla" ]
570 then
571 shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
572 "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}" 2>/dev/null
573 adb_rc=${?}
574 if [ ${adb_rc} -eq 0 ]
575 then
576 for category in ${adb_src_cat_shalla}
577 do
578 tar -xOzf "${shalla_archive}" "BL/${category}/domains" >> "${adb_tmpload}"
579 adb_rc=${?}
580 if [ ${adb_rc} -ne 0 ]
581 then
582 break
583 fi
584 done
585 fi
586 rm -f "${shalla_archive}"
587 rm -rf "${adb_tmpdir}/BL"
588 else
589 "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}" 2>/dev/null
590 adb_rc=${?}
591 fi
592
593 # check download result and prepare list output (incl. tld compression, list backup & restore)
594 #
595 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
596 then
597 awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
598 if [ -s "${adb_tmpfile}" ]
599 then
600 f_tldcompression "${adb_tmpfile}"
601 if [ "${src_name}" != "blacklist" ]
602 then
603 f_list backup
604 fi
605 else
606 f_list restore
607 fi
608 else
609 f_list restore
610 fi
611
612 # list merge
613 #
614 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
615 then
616 f_list merge
617 if [ ${adb_rc} -ne 0 ]
618 then
619 f_list remove
620 fi
621 else
622 f_list remove
623 fi
624 done
625
626 # hash preparation, whitelist removal and overall sort
627 #
628 if [ -f "${adb_dnsdir}/${adb_dnsfile}" ]
629 then
630 hash_old="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
631 fi
632 if [ -s "${adb_tmpdir}/${adb_dnsfile}" ]
633 then
634 if [ ${mem_total} -ge 64 ] || [ ${adb_forcesrt} -eq 1 ]
635 then
636 f_tldcompression "${adb_tmpdir}/${adb_dnsfile}"
637 fi
638 f_list format
639 else
640 > "${adb_dnsdir}/${adb_dnsfile}"
641 fi
642 chown "${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
643 f_rmtemp
644
645 # conditional restart of the dns backend and runtime information export
646 #
647 hash_new="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
648 if [ -z "${hash_old}" ] || [ -z "${hash_new}" ] || [ "${hash_old}" != "${hash_new}" ]
649 then
650 f_dnsrestart
651 fi
652 if [ ${?} -eq 0 ]
653 then
654 f_jsnupdate "${adb_cnt}"
655 f_log "info " "block list with overall ${adb_cnt} domains loaded successfully (${adb_sysver})"
656 else
657 f_log "error" "dns backend restart with active block list failed"
658 fi
659 }
660
661 # handle different adblock actions
662 #
663 f_envload
664 case "${adb_action}" in
665 stop)
666 f_rmtemp
667 f_rmdns
668 f_dnsrestart
669 ;;
670 restart)
671 f_rmtemp
672 f_rmdns
673 f_envcheck
674 f_main
675 ;;
676 suspend)
677 f_switch suspend
678 ;;
679 resume)
680 f_switch resume
681 ;;
682 query)
683 f_query "${2}"
684 ;;
685 status)
686 f_status
687 ;;
688 *)
689 f_envcheck
690 f_main
691 ;;
692 esac
693 exit 0