2 # dns based ad/abuse domain blocking
3 # Copyright (c) 2015-2022 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
6 # disable (s)hellcheck in release
7 # shellcheck disable=all
12 export PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
31 adb_dnsprefix
="adb_list"
32 adb_locallist
="blacklist whitelist iplist"
34 adb_backupdir
="${adb_tmpbase}/adblock-Backup"
35 adb_reportdir
="${adb_tmpbase}/adblock-Report"
37 adb_pidfile
="/var/run/adblock.pid"
38 adb_blacklist
="/etc/adblock/adblock.blacklist"
39 adb_whitelist
="/etc/adblock/adblock.whitelist"
40 adb_mailservice
="/etc/adblock/adblock.mail"
41 adb_dnsfile
="${adb_dnsprefix}.overall"
42 adb_dnsjail
="${adb_dnsprefix}.jail"
43 adb_srcarc
="/etc/adblock/adblock.sources.gz"
44 adb_srcfile
="${adb_tmpbase}/adb_sources.json"
45 adb_rtfile
="${adb_tmpbase}/adb_runtime.json"
46 adb_loggercmd
="$(command -v logger)"
47 adb_dumpcmd
="$(command -v tcpdump)"
48 adb_lookupcmd
="$(command -v nslookup)"
58 adb_lookupdomain
="example.com"
59 adb_action
="${1:-"start"}"
64 # load & check adblock environment
67 local bg_pid iface port ports cpu core
69 adb_sysver
="$(ubus -S call system board 2>/dev/null | jsonfilter -q -e '@.model' -e '@.release.description' |
70 "${adb_awk}" 'BEGIN{RS="";FS="\n"}{printf "%s
, %s
",$1,$2}')"
71 adb_memory
="$("${adb_awk}" '/^MemTotal|^MemFree|^MemAvailable/{ORS="/"; print int($2/1000)}' "/proc
/meminfo
" 2>/dev/null |
72 "${adb_awk}" '{print substr($0,1,length($0)-1)}')"
76 cpu
="$(grep -c '^processor' /proc/cpuinfo 2>/dev/null)"
77 core
="$(grep -cm1 '^core id' /proc/cpuinfo 2>/dev/null)"
78 [ "${cpu}" = "0" ] && cpu
="1"
79 [ "${core}" = "0" ] && core
="1"
80 adb_cores
="$((cpu * core))"
82 if [ "${adb_action}" != "report" ]; then
87 if [ "${adb_enabled}" = "0" ]; then
92 f_log
"info" "adblock is currently disabled, please set the config option 'adb_enabled' to '1' to use this service"
96 if [ "${adb_report}" = "1" ] && [ ! -x "${adb_dumpcmd}" ]; then
97 f_log
"info" "Please install the package 'tcpdump' or 'tcpdump-mini' to use the reporting feature"
98 elif [ "${adb_report}" = "0" ] && [ "${adb_action}" = "report" ]; then
99 f_log
"info" "Please enable the 'DNS Report' option to use the reporting feature"
103 bg_pid
="$(pgrep -f "^
${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awk}" '{ORS=" "; print $1}')"
104 if [ -x "${adb_dumpcmd}" ] && { [ "${adb_report}" = "0" ] || { [ -n "${bg_pid}" ] && { [ "${adb_action}" = "stop" ] || [ "${adb_action}" = "restart" ]; }; }; }; then
105 if [ -n "${bg_pid}" ]; then
106 kill -HUP "${bg_pid}" 2>/dev/null
107 while kill -0 "${bg_pid}" 2>/dev/null; do
114 if [ -x "${adb_dumpcmd}" ] && [ "${adb_report}" = "1" ] && [ -z "${bg_pid}" ] && [ "${adb_action}" != "report" ] && [ "${adb_action}" != "stop" ]; then
115 for port in ${adb_replisten}; do
116 [ -z "${ports}" ] && ports="port ${port}" || ports="${ports} or port ${port}"
118 if [ -z "${adb_repiface}" ]; then
119 network_get_device iface "lan"
120 [ -z "${iface}" ] && network_get_physdev iface "lan"
121 [ -n "${iface}" ] && adb_repiface="${iface}"
122 [ -n "${adb_repiface}" ] && { uci_set adblock global adb_repiface "${adb_repiface}"; f_uci "adblock"; }
124 if [ -n "${adb_reportdir}" ] && [ ! -d "${adb_reportdir}" ]; then
125 mkdir -p "${adb_reportdir}"
126 f_log "info" "report directory '${adb_reportdir}' created"
128 if [ -n "${adb_repiface}" ] && [ -d "${adb_reportdir}" ]; then
129 ("${adb_dumpcmd}" -nn -p -s0 -l -i ${adb_repiface} ${ports} -C${adb_repchunksize} -W${adb_repchunkcnt} -w "${adb_reportdir}/adb_report.pcap" >/dev/null 2>&1 &)
130 bg_pid="$(pgrep -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awk}" '{ORS=" "; print $1}')"
132 f_log "info
" "Please
set the name of the reporting network device
'adb_repiface' manually
"
137 # check & set environment
140 adb_starttime="$
(date "+%s")"
141 f_log "info
" "adblock instance started
::: action
: ${adb_action}, priority: ${adb_nice:-"0"}, pid: ${$}"
146 if [ "${adb_dnsflush}" = "1" ] || [ "${adb_memory##*/}" -lt "64" ]; then
147 printf "%b
" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
151 if [ ! -r "${adb_srcfile}" ]; then
152 if [ -r "${adb_srcarc}" ]; then
153 zcat "${adb_srcarc}" >"${adb_srcfile}"
155 f_log "err
" "adblock
source archive not found
"
158 if [ -r "${adb_srcfile}" ] && [ "${adb_action}" != "report
" ]; then
160 json_load_file "${adb_srcfile}"
162 f_log "err
" "adblock
source file not found
"
166 # load adblock config
169 local cnt="0" cnt_max="10"
171 [ ! -r "/etc
/config
/adblock
" ] && f_log "err
" "no valid adblock config found
, please re-install the package via opkg with the
'--force-reinstall --force-maintainer' options
"
177 eval "${option}=\"${value}\""
182 if [ "${option}" = "adb_sources
" ]; then
183 eval "${option}=\"$(printf "%s" "${adb_sources}") ${value}\""
184 elif [ "${option}" = "adb_eng_sources
" ]; then
185 eval "${option}=\"$(printf "%s" "${adb_eng_sources}") ${value}\""
186 elif [ "${option}" = "adb_stb_sources
" ]; then
187 eval "${option}=\"$(printf "%s" "${adb_stb_sources}") ${value}\""
188 elif [ "${option}" = "adb_utc_sources
" ]; then
189 eval "${option}=\"$(printf "%s" "${adb_utc_sources}") ${value}\""
190 elif [ "${option}" = "adb_denyip
" ]; then
191 eval "${option}=\"$(printf "%s" "${adb_denyip}") ${value}\""
192 elif [ "${option}" = "adb_allowip
" ]; then
193 eval "${option}=\"$(printf "%s" "${adb_allowip}") ${value}\""
194 elif [ "${option}" = "adb_safesearchlist
" ]; then
195 eval "${option}=\"$(printf "%s" "${adb_safesearchlist}") ${value}\""
196 elif [ "${option}" = "adb_zonelist
" ]; then
197 eval "${option}=\"$(printf "%s" "${adb_zonelist}") ${value}\""
198 elif [ "${option}" = "adb_portlist
" ]; then
199 eval "${option}=\"$(printf "%s" "${adb_portlist}") ${value}\""
205 if [ -z "${adb_fetchutil}" ] || [ -z "${adb_dns}" ]; then
206 while [ -z "${adb_packages}" ] && [ "${cnt}" -le "${cnt_max}" ]; do
207 adb_packages="$
(opkg list-installed
2>/dev
/null
)"
211 [ -z "${adb_packages}" ] && f_log "err
" "local opkg package repository is not available
, please
set 'adb_fetchutil' and
'adb_dns' manually
"
215 # status helper function
218 local result input="${1}"
220 if [ "${input}" = "1" ]; then
225 printf "%s
" "${result}"
228 # load dns backend config
231 local util utils dns_up cnt="0"
233 if [ -z "${adb_dns}" ]; then
234 utils="knot-resolver
bind unbound dnsmasq raw
"
235 for util in ${utils}; do
236 if [ "${util}" = "raw" ] || printf "%s" "${adb_packages}" | grep -q "^${util}"; then
237 if [ "${util}" = "knot-resolver
" ]; then
239 elif [ "${util}" = "bind" ]; then
242 if [ "${util}" = "raw
" ] || [ -x "$
(command -v "${util}")" ]; then
244 uci_set adblock global adb_dns "${util}"
250 elif [ "${adb_dns}" != "raw
" ] && [ ! -x "$
(command -v "${adb_dns}")" ]; then
254 if [ -n "${adb_dns}" ]; then
258 adb_dnsinstance="${adb_dnsinstance:-"0"}"
259 adb_dnsuser="${adb_dnsuser:-"dnsmasq"}"
260 adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
261 adb_dnsheader="${adb_dnsheader:-""}"
262 adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"local=/\"\$0\"/\"}'"}"
263 adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"local=/\"\$0\"/#\"}'"}"
264 adb_dnssafesearch
="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{print \"address=/\"\$0\"/\"item\"\"}'"}"
265 adb_dnsstop="${adb_dnsstop:-"address=/#/"}"
268 adb_dnscachecmd="$
(command -v unbound-control ||
printf "%s" "-")"
269 adb_dnsinstance="${adb_dnsinstance:-"0"}"
270 adb_dnsuser="${adb_dnsuser:-"unbound"}"
271 adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}"
272 adb_dnsheader="${adb_dnsheader:-""}"
273 adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"local-zone: \\042\"\$0\"\\042 always_nxdomain\"}'"}"
274 adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"local-zone: \\042\"\$0\"\\042 always_transparent\"}'"}"
275 adb_dnssafesearch
="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{type=\"AAAA\";if(match(item,/^([0-9]{1,3}\.){3}[0-9]{1,3}$/)){type=\"A\"}}{print \"local-data: \\042\"\$0\" \"type\" \"item\"\\042\"}'"}"
276 adb_dnsstop="${adb_dnsstop:-"local-zone: \".\" always_nxdomain"}"
279 adb_dnscachecmd="$
(command -v rndc ||
printf "%s" "-")"
280 adb_dnsinstance="${adb_dnsinstance:-"0"}"
281 adb_dnsuser="${adb_dnsuser:-"bind"}"
282 adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}"
283 adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n IN NS localhost.\n"}"
284 adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}"
285 adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}"
286 adb_dnsdenyip
="${adb_dnsdenyip:-"${adb_awk} '{print \"\"\$0\".rpz-client-ip CNAME .\"}'"}"
287 adb_dnsallowip="${adb_dnsallowip:-"${adb_awk} '{print \"\"\$0\".rpz-client-ip CNAME rpz-passthru.\"}'"}"
288 adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{print \"\"\$0\" CNAME \"item\".\\n*.\"\$0\" CNAME \"item\".\"}'"}"
289 adb_dnsstop
="${adb_dnsstop:-"* CNAME ."}"
293 adb_dnsinstance
="${adb_dnsinstance:-"0"}"
294 adb_dnsuser
="${adb_dnsuser:-"root"}"
295 adb_dnsdir
="${adb_dnsdir:-"/etc/kresd"}"
296 adb_dnsheader
="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n"}"
297 adb_dnsdeny
="${adb_dnsdeny:-"${adb_awk} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}"
298 adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}"
299 adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{type=\"AAAA\";if(match(item,/^([0-9]{1,3}\.){3}[0-9]{1,3}$/)){type=\"A\"}}{print \"\"\$0\" \"type\" \"item\"\"}'"}"
300 adb_dnsstop
="${adb_dnsstop:-"* CNAME ."}"
304 adb_dnsinstance
="${adb_dnsinstance:-"0"}"
305 adb_dnsuser
="${adb_dnsuser:-"root"}"
306 adb_dnsdir
="${adb_dnsdir:-"/tmp"}"
307 adb_dnsheader
="${adb_dnsheader:-""}"
308 adb_dnsdeny
="${adb_dnsdeny:-"0"}"
309 adb_dnsallow
="${adb_dnsallow:-"1"}"
310 adb_dnssafesearch
="${adb_dnssafesearch:-"0"}"
311 adb_dnsstop
="${adb_dnsstop:-"0"}"
316 if [ "${adb_dns}" != "raw" ] && { [ -z "${adb_dns}" ] || [ ! -x "$(command -v "${adb_dns}")" ]; }; then
317 f_log "err
" "dns backend not found
, please
set 'adb_dns' manually
"
320 if [ "${adb_dns}" != "raw" ] && { [ "${adb_dnsdir}" = "${adb_tmpbase}" ] || [ "${adb_dnsdir}" = "${adb_backupdir}" ] || [ "${adb_dnsdir}" = "${adb_reportdir}" ]; }; then
321 f_log
"err" "dns directory '${adb_dnsdir}' has been misconfigured, it must not point to the 'adb_tmpbase', 'adb_backupdir', 'adb_reportdir'"
324 if [ "${adb_action}" = "start" ] && [ -z "${adb_trigger}" ]; then
325 sleep ${adb_triggerdelay}
328 if [ "${adb_dns}" != "raw" ] && [ "${adb_action}" != "stop" ]; then
329 while [ "${cnt}" -le 30 ]; do
330 dns_up
="$(ubus -S call service list "{\"name
\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@
[\"${adb_dns}\"].instances.
*.running
" 2>/dev/null)"
331 if [ "${dns_up}" = "true" ]; then
339 if [ "${adb_action}" != "stop" ]; then
340 if [ -n "${adb_dnsdir}" ] && [ ! -d "${adb_dnsdir}" ]; then
341 if mkdir
-p "${adb_dnsdir}"; then
342 f_log
"info" "dns backend directory '${adb_dnsdir}' created"
344 f_log
"err" "dns backend directory '${adb_dnsdir}' could not be created"
347 [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ] && printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
349 if [ "${dns_up}" != "true" ]; then
351 f_log
"err" "dns backend '${adb_dns}' not running or executable"
355 if [ "${adb_backup}" = "1" ] && [ -n "${adb_backupdir}" ] && [ ! -d "${adb_backupdir}" ]; then
356 if mkdir
-p "${adb_backupdir}"; then
357 f_log
"info" "backup directory '${adb_backupdir}' created"
359 f_log
"err" "backup directory '${adb_backupdir}' could not be created"
363 if [ -n "${adb_jaildir}" ] && [ ! -d "${adb_jaildir}" ]; then
364 if mkdir
-p "${adb_jaildir}"; then
365 f_log
"info" "jail directory '${adb_jaildir}' created"
367 f_log
"err" "jail directory '${adb_jaildir}' could not be created"
371 f_log
"debug" "f_dns ::: dns: ${adb_dns}, dns_dir: ${adb_dnsdir}, dns_file: ${adb_dnsfile}, dns_user: ${adb_dnsuser}, dns_instance: ${adb_dnsinstance}, backup: ${adb_backup}, backup_dir: ${adb_backupdir}, jail_dir: ${adb_jaildir}"
377 local util utils insecure cnt
="0"
379 if [ -z "${adb_fetchutil}" ]; then
380 utils
="aria2c curl wget uclient-fetch"
381 for util
in ${utils}; do
382 if { [ "${util}" = "uclient-fetch" ] && printf "%s" "${adb_packages}" | grep -q "^libustream-"; } ||
383 { [ "${util}" = "wget" ] && printf "%s" "${adb_packages}" | grep -q "^wget -"; } ||
384 [ "${util}" = "curl" ] ||
[ "${util}" = "aria2c" ]; then
385 if [ -x "$(command -v "${util}")" ]; then
386 adb_fetchutil
="${util}"
387 uci_set adblock global adb_fetchutil
"${util}"
393 elif [ ! -x "$(command -v "${adb_fetchutil}")" ]; then
396 case "${adb_fetchutil}" in
398 [ "${adb_fetchinsecure}" = "1" ] && insecure
="--check-certificate=false"
399 adb_fetchparm
="${adb_fetchparm:-"${insecure} --timeout=20 --allow-overwrite=true --auto-file-renaming=false --log-level=warn --dir=/ -o"}"
402 [ "${adb_fetchinsecure}" = "1" ] && insecure
="--insecure"
403 adb_fetchparm
="${adb_fetchparm:-"${insecure} --connect-timeout 20 --silent --show-error --location -o"}"
406 [ "${adb_fetchinsecure}" = "1" ] && insecure
="--no-check-certificate"
407 adb_fetchparm
="${adb_fetchparm:-"${insecure} --timeout=20 -O"}"
410 [ "${adb_fetchinsecure}" = "1" ] && insecure
="--no-check-certificate"
411 adb_fetchparm
="${adb_fetchparm:-"${insecure} --no-cache --no-cookies --max-redirect=0 --timeout=20 -O"}"
414 if [ -n "${adb_fetchutil}" ] && [ -n "${adb_fetchparm}" ]; then
415 adb_fetchutil
="$(command -v "${adb_fetchutil}")"
417 f_log
"err" "download utility with SSL support not found, please install 'uclient-fetch' with a 'libustream-*' variant or another download utility like 'wget', 'curl' or 'aria2'"
419 f_log
"debug" "f_fetch ::: fetch_util: ${adb_fetchutil:-"-"}, fetch_parm: ${adb_fetchparm:-"-"}"
422 # create temporary files, directories and set dependent options
425 if [ -d "${adb_tmpbase}" ]; then
426 adb_tmpdir
="$(mktemp -p "${adb_tmpbase}" -d)"
427 adb_tmpload
="$(mktemp -p "${adb_tmpdir}" -tu)"
428 adb_tmpfile
="$(mktemp -p "${adb_tmpdir}" -tu)"
429 adb_srtopts
="--temporary-directory=${adb_tmpdir} --compress-program=gzip --parallel=${adb_cores}"
431 f_log
"err" "the temp base directory '${adb_tmpbase}' does not exist/is not mounted yet, please create the directory or raise the 'adb_triggerdelay' to defer the adblock start"
433 [ ! -s "${adb_pidfile}" ] && printf "%s" "${$}" >"${adb_pidfile}"
434 f_log
"debug" "f_temp ::: tmp_base: ${adb_tmpbase:-"-"}, tmp_dir: ${adb_tmpdir:-"-"}, sort_options: ${adb_srtopts}, pid_file: ${adb_pidfile:-"-"}"
437 # remove temporary files and directories
440 [ -d "${adb_tmpdir}" ] && rm -rf "${adb_tmpdir}"
441 rm -f "${adb_srcfile}"
443 f_log
"debug" "f_rmtemp ::: tmp_dir: ${adb_tmpdir:-"-"}, src_file: ${adb_srcfile:-"-"}, pid_file: ${adb_pidfile:-"-"}"
446 # remove dns related files
451 status
="$(ubus -S call service list '{"name
":"adblock
"}' 2>/dev/null | jsonfilter -l1 -e '@["adblock
"].instances.*.running' 2>/dev/null)"
452 if [ "${adb_dns}" = "raw" ] || { [ -n "${adb_dns}" ] && [ -n "${status}" ]; }; then
454 [ "${adb_backup}" = "1" ] && rm -f "${adb_backupdir}/${adb_dnsprefix}".*.gz
455 printf "%b
" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
459 f_log "debug
" "f_rmdns
::: dns
: ${adb_dns}, status: ${status:-"-"}, dns_dir: ${adb_dnsdir}, dns_file: ${adb_dnsfile}, rt_file: ${adb_rtfile}, backup_dir: ${adb_backupdir:-"-"}"
465 local change config="${1}"
467 if [ -n "${config}" ]; then
468 change="$
(uci
-q changes
"${config}" | "${adb_awk}" '{ORS=" "; print $0}')"
469 if [ -n "${change}" ]; then
470 uci_commit
"${config}"
473 "/etc/init.d/firewall" reload
>/dev
/null
2>&1
476 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
479 "/etc/init.d/${adb_dns}" reload
>/dev
/null
2>&1
483 f_log
"debug" "f_uci ::: config: ${config}, change: ${change}"
490 local file mode
="${1}" name
="${2}"
495 [ -s "${adb_tmpdir}/tmp.add.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.add.${name}")"
498 [ -s "${adb_tmpfile}.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpfile}.${name}")"
501 [ -s "${adb_tmpdir}/tmp.raw.${name}" ] && { adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.raw.${name}")"; rm -f "${adb_tmpdir}/tmp.raw.${name}"; }
504 [ -s "${adb_tmpdir}/tmp.safesearch.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.safesearch.${name}")"
507 [ -s "${adb_tmpdir}/${adb_dnsfile}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/${adb_dnsfile}")"
509 "download
" | "backup
" | "restore
")
510 [ -s "${src_tmpfile}" ] && adb_cnt="$
(wc -l 2>/dev
/null
<"${src_tmpfile}")"
513 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]; then
514 adb_cnt="$
(wc -l 2>/dev
/null
<"${adb_dnsdir}/${adb_dnsfile}")"
515 if [ -s "${adb_tmpdir}/tmp.add.whitelist
" ]; then
516 adb_cnt="$
((adb_cnt
- $
(wc -l 2>/dev
/null
<"${adb_tmpdir}/tmp.add.whitelist")))"
518 for file in "${adb_tmpdir}/tmp.safesearch
".*; do
519 if [ -r "${file}" ]; then
520 adb_cnt="$
((adb_cnt
- $
(wc -l 2>/dev
/null
<"${file}")))"
523 [ -n "${adb_dnsheader}" ] && adb_cnt="$
(((adb_cnt
- $
(printf "%b" "${adb_dnsheader}" |
grep -c "^")) / 2))"
529 # set external config options
532 local config config_dir config_file section zone port fwcfg
537 config_dir="$
(uci_get dhcp
"@dnsmasq[${adb_dnsinstance}]" confdir |
grep -Fo "${adb_dnsdir}")"
538 if [ "${adb_enabled}" = "1" ] && [ -z "${config_dir}" ]; then
539 uci_set dhcp "@dnsmasq
[${adb_dnsinstance}]" confdir "${adb_dnsdir}" 2>/dev/null
544 config_file="$
(uci_get resolver kresd rpz_file |
grep -Fo "${adb_dnsdir}/${adb_dnsfile}")"
545 if [ "${adb_enabled}" = "1" ] && [ -z "${config_file}" ]; then
546 uci -q add_list resolver.kresd.rpz_file="${adb_dnsdir}/${adb_dnsfile}"
547 elif [ "${adb_enabled}" = "0" ] && [ -n "${config_file}" ]; then
548 uci -q del_list resolver.kresd.rpz_file="${adb_dnsdir}/${adb_dnsfile}"
555 fwcfg="$
(uci
-qNX show
"${config}" | "${adb_awk}" 'BEGIN{FS="[.=]"};/adblock_/{if(zone==$2){next}else{ORS=" ";zone=$2;print zone}}')"
556 if [ "${adb_enabled}" = "1" ] && [ "${adb_forcedns}" = "1" ] &&
557 /etc
/init.d
/firewall enabled
; then
558 for zone
in ${adb_zonelist}; do
559 for port
in ${adb_portlist}; do
560 if ! printf "%s" "${fwcfg}" | grep -q "adblock_${zone}${port}[ |\$]"; then
562 set firewall."adblock_${zone}${port}"="redirect"
563 set firewall."adblock_${zone}${port}".name="Adblock DNS (${zone}, ${port})"
564 set firewall."adblock_${zone}${port}".src="${zone}"
565 set firewall."adblock_${zone}${port}".proto="tcp udp"
566 set firewall."adblock_${zone}${port}".src_dport="${port}"
567 set firewall."adblock_${zone}${port}".dest_port="${port}"
568 set firewall."adblock_${zone}${port}".target="DNAT"
571 fwcfg
="${fwcfg/adblock_${zone}${port}[ |\$]/}"
574 fwcfg
="${fwcfg#"${fwcfg%%[![:space:]]*}"}"
575 fwcfg
="${fwcfg%"${fwcfg##*[![:space:]]}"}"
577 if [ "${adb_enabled}" = "0" ] || [ "${adb_forcedns}" = "0" ] || [ -n "${fwcfg}" ]; then
578 for section
in ${fwcfg}; do
579 uci_remove firewall
"${section}"
585 # restart dns backend
588 local dns_service dns_up dns_pid restart_rc cnt
="0" out_rc
="4" in_rc
="${1:-0}"
590 if [ "${adb_dns}" = "raw" ]; then
593 if [ "${in_rc}" = "0" ] && [ "${adb_dnsflush}" = "0" ]; then
596 if [ -x "${adb_dnscachecmd}" ] && [ -d "${adb_tmpdir}" ] && [ -f "${adb_dnsdir}/unbound.conf" ]; then
597 "${adb_dnscachecmd}" -c "${adb_dnsdir}/unbound.conf" dump_cache >"${adb_tmpdir}/adb_cache.dump" 2>/dev
/null
599 "/etc/init.d/${adb_dns}" restart
>/dev
/null
2>&1
603 if [ -x "${adb_dnscachecmd}" ] && [ -f "/etc/bind/rndc.conf" ]; then
604 "${adb_dnscachecmd}" -c "/etc/bind/rndc.conf" reload
>/dev
/null
2>&1
607 if [ -z "${restart_rc}" ] || { [ -n "${restart_rc}" ] && [ "${restart_rc}" != "0" ]; }; then
608 "/etc
/init.d
/${adb_dns}" restart >/dev/null 2>&1
613 "/etc
/init.d
/${adb_dns}" restart >/dev/null 2>&1
618 if [ -z "${restart_rc}" ]; then
619 "/etc
/init.d
/${adb_dns}" restart >/dev/null 2>&1
623 if [ "${restart_rc}" = "0" ]; then
624 while [ "${cnt}" -le "${adb_dnstimeout}" ]; do
625 dns_service="$
(ubus
-S call service list
"{\"name\":\"${adb_dns}\"}")"
626 dns_up="$
(printf "%s" "${dns_service}" | jsonfilter
-l1 -e "@[\"${adb_dns}\"].instances.*.running")"
627 dns_pid="$
(printf "%s" "${dns_service}" | jsonfilter
-l1 -e "@[\"${adb_dns}\"].instances.*.pid")"
628 if [ "${dns_up}" = "true" ] && [ -n "${dns_pid}" ] && ! ls "/proc/${dns_pid}/fd/${adb_dnsdir}/${adb_dnsfile}" >/dev/null 2>&1; then
629 if [ -x "${adb_lookupcmd}" ] && [ "${adb_lookupdomain}" != "false
" ]; then
630 if "${adb_lookupcmd}" "${adb_lookupdomain}" >/dev/null 2>&1; then
635 sleep ${adb_dnstimeout}
636 cnt=${adb_dnstimeout}
644 if [ "${out_rc}" = "0" ] && [ "${adb_dns}" = "unbound
" ]; then
645 if [ -x "${adb_dnscachecmd}" ] && [ -d "${adb_tmpdir}" ] && [ -s "${adb_tmpdir}/adb_cache.dump
" ]; then
646 "${adb_dnscachecmd}" -c "${adb_dnsdir}/unbound.conf" load_cache <"${adb_tmpdir}/adb_cache.dump
" >/dev/null 2>&1
651 f_log "debug
" "f_dnsup
::: dns
: ${adb_dns}, cache_cmd: ${adb_dnscachecmd:-"-"}, lookup_cmd: ${adb_lookupcmd:-"-"}, lookup_domain: ${adb_lookupdomain:-"-"}, restart_rc: ${restart_rc:-"-"}, dns_flush: ${adb_dnsflush}, dns_timeout: ${adb_dnstimeout}, dns_cnt: ${cnt}, in_rc: ${in_rc}, out_rc: ${out_rc}"
655 # backup/restore/remove blocklists
658 local hold file rset item array safe_url safe_ips safe_cname safe_domains ip out_rc mode="${1}" src_name="${2:-"${src_name}"}" in_rc="${src_rc:-0}" cnt ffiles="-maxdepth 1 -name ${adb_dnsprefix}.
*.gz
"
663 if [ "${adb_dns}" = "named
" ]; then
664 rset="BEGIN
{FS
=\"[.
:]\";pfx
=\"32\"}{if(match
(\
$0,/:/))pfx
=\"128\"}{printf \"%s.
\",pfx
;for(seg
=NF
;seg
>=1;seg--
)if(seg
==1)printf \"%s
\n\",\
$seg;else if(\
$seg>=0)printf \"%s.
\",\
$seg; else printf \"%s.
\",\"zz
\"}"
665 if [ -n "${adb_allowip}" ]; then
666 : >"${adb_tmpdir}/tmp.raw.
${src_name}"
667 for ip in ${adb_allowip}; do
668 printf "%s
" "${ip}" | "${adb_awk}" "${rset}" >>"${adb_tmpdir}/tmp.raw.${src_name}"
670 eval "${adb_dnsallowip}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.add.${src_name}"
673 if [ -n "${adb_denyip}" ] && { [ -z "${out_rc}" ] || [ "${out_rc}" = "0" ]; }; then
674 : >"${adb_tmpdir}/tmp.raw.${src_name}"
675 for ip
in ${adb_denyip}; do
676 printf "%s" "${ip}" | "${adb_awk}" "${rset}" >>"${adb_tmpdir}/tmp.raw.${src_name}"
678 eval "${adb_dnsdenyip}" "${adb_tmpdir}/tmp.raw.${src_name}" >>"${adb_tmpdir}/tmp.add.${src_name}"
681 rm -f "${adb_tmpdir}/tmp.raw.${src_name}"
684 "blacklist" |
"whitelist")
686 if [ "${src_name}" = "blacklist" ] && [ -f "${adb_blacklist}" ]; then
687 rset
="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}"
688 "${adb_awk}" "${rset}" "${adb_blacklist}" |
689 "${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${adb_tmpdir}/tmp.raw.${src_name}"
690 "${adb_sort}" ${adb_srtopts} -u "${adb_tmpdir}/tmp.raw.${src_name}" 2>/dev/null >"${adb_tmpfile}.${src_name}"
692 rm -f "${adb_tmpdir}/tmp.raw.${src_name}"
693 elif [ "${src_name}" = "whitelist" ] && [ -f "${adb_whitelist}" ]; then
694 rset
="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}"
695 "${adb_awk}" "${rset}" "${adb_whitelist}" >"${adb_tmpdir}/tmp.raw.${src_name}"
697 if [ "${out_rc}" = "0" ]; then
698 rset
="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{gsub(\"\\\\.\",\"\\\\.\",\$1);print tolower(\"^(|.*\\\\.)\"\$1\"$\")}"
699 "${adb_awk}" "${rset}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.rem.${src_name}"
701 if [ "${out_rc}" = "0" ] && [ "${adb_dnsallow}" != "1" ]; then
702 eval "${adb_dnsallow}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.add.${src_name}"
704 if [ "${out_rc}" = "0" ] && [ "${adb_jail}" = "1" ] && [ "${adb_dnsstop}" != "0" ]; then
705 : >"${adb_jaildir}/${adb_dnsjail}"
706 [ -n "${adb_dnsheader}" ] && printf "%b" "${adb_dnsheader}" >>"${adb_jaildir}/${adb_dnsjail}"
707 cat "${adb_tmpdir}/tmp.add.${src_name}" >>"${adb_jaildir}/${adb_dnsjail}"
708 printf "%s\n" "${adb_dnsstop}" >>"${adb_jaildir}/${adb_dnsjail}"
715 case "${src_name}" in
717 rset
="/^\\.([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{printf \"%s\n%s\n\",tolower(\"www\"\$1),tolower(substr(\$1,2,length(\$1)))}"
718 safe_url
="https://www.google.com/supported_domains"
719 safe_cname
="forcesafesearch.google.com"
720 safe_domains
="${adb_tmpdir}/tmp.load.safesearch.${src_name}"
721 if [ "${adb_backup}" = "1" ] && [ -s "${adb_backupdir}/safesearch.${src_name}.gz" ]; then
722 zcat
"${adb_backupdir}/safesearch.${src_name}.gz" >"${safe_domains}"
725 "${adb_fetchutil}" ${adb_fetchparm} "${safe_domains}" "${safe_url}" 2>/dev
/null
727 if [ "${adb_backup}" = "1" ] && [ "${out_rc}" = "0" ]; then
728 gzip -cf "${safe_domains}" >"${adb_backupdir}/safesearch.${src_name}.gz"
732 if [ "${out_rc}" = "0" ]; then
733 if [ -x "${adb_lookupcmd}" ]; then
734 safe_ips
="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
735 [ -n "${safe_ips}" ] && "${adb_awk}" "${rset}" "${safe_domains}" >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
741 safe_cname="strict.bing.com"
742 safe_domains="www.bing.com"
743 if [ -x "${adb_lookupcmd}" ]; then
744 safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
745 [ -n "${safe_ips}" ] && printf "%s\n" ${safe_domains} >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
750 safe_cname="safe.duckduckgo.com
"
751 safe_domains="duckduckgo.com
"
752 if [ -x "${adb_lookupcmd}" ]; then
753 safe_ips="$
("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
754 [ -n "${safe_ips}" ] && printf "%s\n" ${safe_domains} >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
759 safe_cname
="safesearch.pixabay.com"
760 safe_domains
="pixabay.com"
761 if [ -x "${adb_lookupcmd}" ]; then
762 safe_ips
="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
763 [ -n "${safe_ips}" ] && printf "%s\n" ${safe_domains} >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
768 safe_cname="familysearch.yandex.ru"
769 safe_domains="ya.ru yandex.ru yandex.com yandex.com.tr yandex.ua yandex.by yandex.ee yandex.lt yandex.lv yandex.md yandex.uz yandex.tm yandex.tj yandex.az yandex.kz"
770 if [ -x "${adb_lookupcmd}" ]; then
771 safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
772 [ -n "${safe_ips}" ] && printf "%s\n" ${safe_domains} >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
777 if [ "${adb_safesearchmod}" = "0" ]; then
778 safe_cname="restrict.youtube.com
"
780 safe_cname="restrictmoderate.youtube.com
"
782 safe_domains="www.youtube.com m.youtube.com youtubei.googleapis.com youtube.googleapis.com www.youtube-nocookie.com
"
783 if [ -x "${adb_lookupcmd}" ]; then
784 safe_ips="$
("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
785 [ -n "${safe_ips}" ] && printf "%s\n" ${safe_domains} >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
790 if [ "${out_rc}" = "0" ] && [ -s "${adb_tmpdir}/tmp.raw.safesearch.${src_name}" ]; then
791 : >"${adb_tmpdir}/tmp.safesearch.${src_name}"
792 [ "${adb_dns}" = "named" ] && array="${safe_cname}" || array="${safe_ips}"
793 for item
in ${array}; do
794 if ! eval "${adb_dnssafesearch}" "${adb_tmpdir}/tmp.raw.safesearch.${src_name}" >>"${adb_tmpdir}/tmp.safesearch.${src_name}"; then
795 rm -f "${adb_tmpdir}/tmp.safesearch.${src_name}"
800 rm -f "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
805 gzip -cf "${src_tmpfile}" >"${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
810 if [ -n "${src_name}" ] && [ -s "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]; then
811 zcat
"${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" >"${src_tmpfile}"
813 elif [ -z "${src_name}" ]; then
815 for file in "${adb_backupdir}/${adb_dnsprefix}".
*.gz
; do
816 if [ -r "${file}" ]; then
819 zcat
"${file}" >"${adb_tmpfile}.${name}" &
820 hold
="$((cnt % adb_cores))"
821 if [ "${hold}" = "0" ]; then
832 if [ "${adb_action}" != "start" ] && [ "${adb_action}" != "resume" ] && [ -n "${src_name}" ] && [ "${out_rc}" != "0" ]; then
833 adb_sources
="${adb_sources/${src_name}}"
837 [ "${adb_backup}" = "1" ] && rm "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" 2>/dev
/null
839 adb_sources
="${adb_sources/${src_name}}"
842 if [ "${adb_backup}" = "1" ]; then
843 for src_name
in ${adb_sources}; do
844 ffiles
="${ffiles} -a ! -name ${adb_dnsprefix}.${src_name}.gz"
846 if [ "${adb_safesearch}" = "1" ] && [ "${adb_dnssafesearch}" != "0" ]; then
847 ffiles
="${ffiles} -a ! -name safesearch.google.gz"
849 find "${adb_backupdir}" ${ffiles} -print0 2>/dev
/null |
xargs -0 rm 2>/dev
/null
852 "${adb_sort}" ${adb_srtopts} -mu "${adb_tmpfile}".* 2>/dev/null >"${adb_tmpdir}/${adb_dnsfile}"
854 rm -f "${adb_tmpfile}".
*
858 { [ -n "${adb_dnsheader}" ] && printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"; } || : >"${adb_dnsdir}/${adb_dnsfile}"
859 [ -s "${adb_tmpdir}/tmp.add.iplist" ] && cat "${adb_tmpdir}/tmp.add.iplist" >>"${adb_dnsdir}/${adb_dnsfile}"
860 [ -s "${adb_tmpdir}/tmp.add.whitelist" ] && cat "${adb_tmpdir}/tmp.add.whitelist" >>"${adb_dnsdir}/${adb_dnsfile}"
861 for file in "${adb_tmpdir}/tmp.safesearch".
*; do
862 [ -r "${file}" ] && cat "${file}" >>"${adb_dnsdir}/${adb_dnsfile}"
864 { [ "${adb_dnsdeny}" != "0" ] && eval "${adb_dnsdeny}" "${adb_tmpdir}/${adb_dnsfile}" >>"${adb_dnsdir}/${adb_dnsfile}"; } || mv "${adb_tmpdir}/${adb_dnsfile}" "${adb_dnsdir}/${adb_dnsfile}"
868 f_count
"${mode}" "${src_name}"
869 out_rc
="${out_rc:-"${in_rc}"}"
870 f_log
"debug" "f_list ::: name: ${src_name:-"-"}, mode: ${mode}, cnt: ${adb_cnt}, in_rc: ${in_rc}, out_rc: ${out_rc}"
874 # top level domain compression
877 local cnt cnt_tld
source="${1}" temp_tld
="${1}.tld"
879 if "${adb_awk}" '{if(NR==1){tld=$NF};while(getline){if(index($NF,tld".")==0){print tld;tld=$NF}}print tld}' "${source}" |
880 "${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${temp_tld}"; then
881 mv -f "${temp_tld}" "${source}"
882 cnt_tld
="$(wc -l 2>/dev/null <"${source}")"
886 f_log
"debug" "f_tld ::: source: ${source}, cnt: ${adb_cnt:-"-"}, cnt_tld: ${cnt_tld:-"-"}"
889 # suspend/resume adblock processing
892 local status entry
done="false" mode
="${1}"
895 json_load_file
"${adb_rtfile}" >/dev
/null
2>&1
896 json_select
"data" >/dev
/null
2>&1
897 json_get_var status
"adblock_status"
898 if [ "${mode}" = "suspend" ] && [ "${status}" = "enabled" ]; then
900 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
901 if [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then
902 printf "%b" "${adb_dnsheader}" >"${adb_jaildir}/${adb_dnsjail}"
903 elif [ -f "${adb_dnsdir}/${adb_dnsjail}" ]; then
904 rm -f "${adb_dnsdir}/${adb_dnsjail}"
908 elif [ "${mode}" = "resume" ] && [ "${status}" = "paused" ]; then
913 if [ "${done}" = "true" ]; then
914 [ "${mode}" = "suspend" ] && f_dnsup
916 f_log
"info" "${mode} adblock processing"
921 # query blocklist for certain (sub-)domains
924 local search result prefix suffix field query_start query_end query_timeout
=30 domain
="${1}" tld
="${1#*.}"
926 if [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]; then
927 printf "%s\n" "::: invalid input, please submit a single (sub-)domain :::"
937 suffix
="(always_nxdomain)"
941 prefix
="[^\\*].*[\\.]"
946 prefix
="[^\\*].*[\\.]"
956 query_start
="$(date "+%s
")"
957 while [ "${domain}" != "${tld}" ]; do
958 search
="${domain//[+*~%\$&\"\']/}"
959 search
="${search//./\\.}"
960 result
="$("${adb_awk}" -F '/|\"|\t| ' "/^(${search}|${prefix}+${search}.*${suffix})$/{i++;if(i<=9){printf \" + %s\n\",\$${field}}else if(i==10){printf \" + %s\n\",\"[...]\";exit}}" "${adb_dnsdir}/${adb_dnsfile}")"
961 printf "%s\n%s\n%s\n" ":::" "::: domain '${domain}' in active blocklist" ":::"
962 printf "%s\n\n" "${result:-" - no match"}"
966 if [ "${adb_backup}" = "1" ] && [ -d "${adb_backupdir}" ]; then
967 search
="${1//[+*~%\$&\"\']/}"
968 search
="${search//./\\.}"
969 printf "%s\n%s\n%s\n" ":::" "::: domain '${1}' in backups and black-/whitelist" ":::"
970 for file in "${adb_backupdir}/${adb_dnsprefix}".*.gz "${adb_blacklist}" "${adb_whitelist}"; do
972 if [ "${suffix}" = "gz" ]; then
973 zcat
"${file}" 2>/dev
/null |
974 "${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' | "${adb_awk}" -v f="${file##*/}" "BEGIN{rc=1};/^($search|.*\\.${search})$/{i++;if(i<=3){printf \" + %-30s%s\n\",f,\$1;rc=0}else if(i==4){printf \" + %-30s%s\n\",f,\"[...]\"}};END{exit rc}"
976 "${adb_awk}" -v f="${file##*/}" "BEGIN{rc=1};/^($search|.*\\.${search})$/{i++;if(i<=3){printf \" + %-30s%s\n\",f,\$1;rc=0}else if(i==4){printf \" + %-30s%s\n\",f,\"[...]\"}};END{exit rc}" "${file}"
978 if [ "${?}" = "0" ]; then
980 query_end
="$(date "+%s
")"
981 if [ "$((query_end - query_start))" -gt "${query_timeout}" ]; then
982 printf "%s\n\n" " - [...]"
987 [ "${result}" != "true" ] && printf "%s\n\n" " - no match"
992 # update runtime information
995 local entry sources runtime utils bg_pid status
="${1:-"enabled"}"
997 adb_memory
="$("${adb_awk}" '/^MemTotal|^MemFree|^MemAvailable/{ORS="/"; print int($2/1000)}' "/proc
/meminfo
" 2>/dev/null |
998 "${adb_awk}" '{print substr($0,1,length($0)-1)}')"
1001 "enabled" |
"error")
1002 adb_endtime
="$(date "+%s
")"
1003 if [ "$(((adb_endtime - adb_starttime) / 60))" -lt 60 ]; then
1004 runtime
="${adb_action}, $(((adb_endtime - adb_starttime) / 60))m $(((adb_endtime - adb_starttime) % 60))s, ${adb_memory:-0}, $(date -Iseconds)"
1006 runtime
="${adb_action}, n/a, ${adb_memory:-0}, $(date -Iseconds)"
1008 [ "${status}" = "error" ] && adb_cnt
="0"
1018 if json_load_file
"${adb_rtfile}" >/dev
/null
2>&1; then
1019 utils
="download: $(readlink -fn "${adb_fetchutil}"), sort: $(readlink -fn "${adb_sort}"), awk: $(readlink -fn "${adb_awk}")"
1020 [ -z "${adb_cnt}" ] && { json_get_var adb_cnt "blocked_domains"; adb_cnt="${adb_cnt%% *}"; }
1021 [ -z "${runtime}" ] && json_get_var runtime "last_run
"
1023 if [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then
1025 sources="restrictive_jail
"
1027 sources="$
(printf "%s\n" ${adb_sources} | "${adb_sort}" | "${adb_awk}" '{ORS=" ";print $0}')"
1032 json_load_file "${adb_rtfile}" >/dev/null 2>&1
1034 json_add_string "adblock_status" "${status:-"enabled"}"
1035 json_add_string "adblock_version" "${adb_ver}"
1036 json_add_string "blocked_domains" "${adb_cnt:-0}"
1037 json_add_array "active_sources"
1038 for entry in ${sources}; do
1040 json_add_string "source" "${entry}"
1044 json_add_string "dns_backend" "${adb_dns:-"-"} (${adb_dnscachecmd##*/}), ${adb_dnsdir:-"-"}"
1045 json_add_string "run_utils" "${utils:-"-"}"
1046 json_add_string "run_ifaces" "trigger: ${adb_trigger:-"-"}, report: ${adb_repiface:-"-"}"
1047 json_add_string "run_directories" "base: ${adb_tmpbase}, backup: ${adb_backupdir}, report: ${adb_reportdir}, jail: ${adb_jaildir}"
1048 json_add_string "run_flags" "backup: $(f_char ${adb_backup}), flush: $(f_char ${adb_dnsflush}), force: $(f_char ${adb_forcedns}), search: $(f_char ${adb_safesearch}), report: $(f_char ${adb_report}), mail: $(f_char ${adb_mail}), jail: $(f_char ${adb_jail})"
1049 json_add_string "last_run" "${runtime:-"-"}"
1050 json_add_string "system" "${adb_sysver}"
1051 json_dump >"${adb_rtfile}"
1053 if [ "${adb_mail}" = "1" ] && [ -x "${adb_mailservice}" ] &&
1054 { [ "${status}" = "error" ] || { [ "${status}" = "enabled" ] && [ "${adb_cnt}" -le "${adb_mailcnt}" ]; }; }; then
1055 ("${adb_mailservice}" "${adb_ver}" >/dev/null 2>&1) &
1058 f_log "debug" "f_jsnup ::: status: ${status:-"-"}, cnt: ${adb_cnt}, mail: ${adb_mail}, mail_service: ${adb_mailservice}, mail_cnt: ${adb_mailcnt}, mail_pid: ${bg_pid:-"-"}"
1064 local class="${1}" log_msg="${2}"
1066 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${adb_debug}" = "1" ]; }; then
1067 [ -x "${adb_loggercmd}" ] && "${adb_loggercmd}" -p "${class}" -t "adblock-${adb_ver}[${$}]" "${log_msg}" || \
1068 printf "%s %s %s\n" "${class}" "adblock-${adb_ver}[${$}]" "${log_msg}"
1069 if [ "${class}" = "err" ]; then
1077 # main function for blocklist processing
1080 local src_tmpload src_tmpfile src_name src_rset src_url src_log src_arc src_cat src_item src_list src_entries src_suffix src_rc entry cnt
1082 f_log "debug" "f_main ::: memory: ${adb_memory:-0}, cores: ${adb_cores}, safe_search: ${adb_safesearch}, force_dns: ${adb_forcedns}, awk: ${adb_awk}"
1084 # white- and blacklist preparation
1086 for entry in ${adb_locallist}; do
1087 (f_list "${entry}" "${entry}") &
1090 if [ "${adb_dns}" != "raw" ] && [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then
1091 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
1092 chown "${adb_dnsuser}" "${adb_jaildir}/${adb_dnsjail}" 2>/dev/null
1094 if [ "${adb_action}" != "resume" ]; then
1097 f_log "info" "restrictive jail mode enabled successfully (${adb_sysver})"
1099 f_log "err" "dns backend restart in jail mode failed"
1103 elif [ -f "${adb_dnsdir}/${adb_dnsjail}" ]; then
1104 rm -f "${adb_dnsdir}/${adb_dnsjail}"
1108 # safe search preparation
1110 if [ "${adb_safesearch}" = "1" ] && [ "${adb_dnssafesearch}" != "0" ]; then
1111 [ -z "${adb_safesearchlist}" ] && adb_safesearchlist="google bing duckduckgo pixabay yandex youtube"
1113 for entry in ${adb_safesearchlist}; do
1114 (f_list safesearch "${entry}") &
1115 hold="$((cnt % adb_cores))"
1116 [ "${hold}" = "0" ] && wait
1125 for src_name in ${adb_sources}; do
1126 if ! json_select "${src_name}" >/dev/null 2>&1; then
1127 adb_sources="${adb_sources/${src_name}/}"
1130 json_get_var src_url "url" >/dev/null 2>&1
1131 json_get_var src_rset "rule" >/dev/null 2>&1
1133 src_tmpcat="${adb_tmpload}.${src_name}.cat"
1134 src_tmpload="${adb_tmpload}.${src_name}.load"
1135 src_tmpsort="${adb_tmpload}.${src_name}.sort"
1136 src_tmpfile="${adb_tmpfile}.${src_name}"
1141 if [ -z "${src_url}" ] || [ -z "${src_rset}" ]; then
1148 if [ "${adb_backup}" = "1" ] && { [ "${adb_action}" = "start" ] || [ "${adb_action}" = "resume" ]; }; then
1149 if f_list restore && [ -s "${src_tmpfile}" ]; then
1154 # download queue processing
1156 unset src_cat src_entries
1157 if [ "${src_name}" = "utcapitole" ] && [ -n "${adb_utc_sources}" ]; then
1158 src_cat="${adb_utc_sources}"
1159 if [ -n "${src_cat}" ]; then
1161 src_arc="${adb_tmpdir}/${src_url##*/}"
1162 src_log="$("${adb_fetchutil}" ${adb_fetchparm} "${src_arc}" "${src_url}" 2>&1)"
1164 if [ "${src_rc}" = "0" ] && [ -s "${src_arc}" ]; then
1165 src_suffix="$(eval printf "%s" \"\$\{adb_src_suffix_${src_name}:-\"domains\"\}\")"
1166 src_list="$(tar -tzf "${src_arc}" 2>/dev/null)"
1167 for src_item in ${src_cat}; do
1168 src_entries="${src_entries} $(printf "%s" "${src_list}" | grep -E "${src_item}/${src_suffix}$")"
1170 if [ -n "${src_entries}" ]; then
1171 tar -xOzf "${src_arc}" ${src_entries} 2>/dev/null >"${src_tmpload}"
1176 src_log="$(printf "%s" "${src_log}" | "${adb_awk}" '{ORS=" ";print $0}')"
1177 f_log "info
" "download of
'${src_name}' failed, url: ${src_url}, rule: ${src_rset:-"-"}, categories: ${src_cat:-"-"}, rc: ${src_rc}, log: ${src_log:-"-"}"
1179 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpload}" ]; then
1180 if [ -s "${adb_tmpdir}/tmp.rem.whitelist" ]; then
1181 "${adb_awk}" "${src_rset}" "${src_tmpload}" | sed "s/\r//g" |
1182 grep -Evf "${adb_tmpdir}/tmp.rem.whitelist" | "${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${src_tmpsort}"
1184 "${adb_awk}" "${src_rset}" "${src_tmpload}" |
sed "s/\r//g" |
1185 "${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${src_tmpsort}"
1188 "${adb_sort}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null >"${src_tmpfile}"
1191 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpfile}" ]; then
1193 [ "${adb_backup}" = "1" ] && f_list backup
1194 elif [ "${adb_backup}" = "1" ] && [ "${adb_action}" != "start" ]; then
1195 f_log
"info" "archive preparation of '${src_name}' failed, categories: ${src_cat:-"-"}, entries: ${src_entries}, rc: ${src_rc}"
1197 rm -f "${src_tmpfile}"
1199 elif [ "${adb_backup}" = "1" ] && [ "${adb_action}" != "start" ]; then
1200 f_log
"info" "archive extraction of '${src_name}' failed, categories: ${src_cat:-"-"}, entries: ${src_entries}, rc: ${src_rc}"
1206 if [ "${src_name}" = "energized" ] && [ -n "${adb_eng_sources}" ]; then
1207 src_cat
="${adb_eng_sources}"
1208 elif [ "${src_name}" = "stevenblack" ] && [ -n "${adb_stb_sources}" ]; then
1209 src_cat
="${adb_stb_sources}"
1210 elif { [ "${src_name}" = "energized" ] && [ -z "${adb_eng_sources}" ]; } ||
1211 { [ "${src_name}" = "stevenblack" ] && [ -z "${adb_stb_sources}" ]; }; then
1215 for suffix
in ${src_cat:-${src_url}}; do
1216 if [ "${src_url}" != "${suffix}" ]; then
1217 src_log
="$("${adb_fetchutil}" ${adb_fetchparm} "${src_tmpcat}" "${src_url}${suffix}" 2>&1)"
1219 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpcat}" ]; then
1220 cat "${src_tmpcat}" >>"${src_tmpload}"
1224 src_log
="$("${adb_fetchutil}" ${adb_fetchparm} "${src_tmpload}" "${src_url}" 2>&1)"
1228 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpload}" ]; then
1229 if [ -s "${adb_tmpdir}/tmp.rem.whitelist" ]; then
1230 "${adb_awk}" "${src_rset}" "${src_tmpload}" |
sed "s/\r//g" |
1231 grep -Evf "${adb_tmpdir}/tmp.rem.whitelist" | "${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.
",$f;print $1}' >"${src_tmpsort}"
1233 "${adb_awk}" "${src_rset}" "${src_tmpload}" | sed "s
/\r//g
" |
1234 "${adb_awk}" 'BEGIN{FS=".
"}{for(f=NF;f>1;f--)printf "%s.
",$f;print $1}' >"${src_tmpsort}"
1237 "${adb_sort}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null >"${src_tmpfile}"
1240 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpfile}" ]; then
1242 [ "${adb_backup}" = "1" ] && f_list backup
1243 elif [ "${adb_backup}" = "1" ] && [ "${adb_action}" != "start
" ]; then
1244 f_log "info
" "preparation of
'${src_name}' failed
, rc
: ${src_rc}"
1246 rm -f "${src_tmpfile}"
1249 src_log="$
(printf "%s" "${src_log}" | "${adb_awk}" '{ORS=" ";print $0}')"
1250 f_log
"info" "download of '${src_name}' failed, url: ${src_url}, rule: ${src_rset:-"-"}, categories: ${src_cat:-"-"}, rc: ${src_rc}, log: ${src_log:-"-"}"
1251 [ "${adb_backup}" = "1" ] && [ "${adb_action}" != "start" ] && f_list restore
1255 hold
="$((cnt % adb_cores))"
1256 [ "${hold}" = "0" ] && wait
1261 # tld compression and dns restart
1263 if f_list merge
&& [ -s "${adb_tmpdir}/${adb_dnsfile}" ]; then
1264 f_tld
"${adb_tmpdir}/${adb_dnsfile}"
1267 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
1269 chown
"${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev
/null
1271 [ "${adb_action}" != "resume" ] && f_jsnup
"enabled"
1272 f_log
"info" "blocklist with overall ${adb_cnt} blocked domains loaded successfully (${adb_sysver})"
1274 f_log
"err" "dns backend restart with adblock blocklist failed"
1279 # trace dns queries via tcpdump and prepare a report
1282 local report_raw report_txt content status total start end start_date start_time end_date end_time blocked percent top_list top array item index hold ports value key key_list cnt
="0" resolve
="-nn" action
="${1}" count="${2:-"50"}" search="${3:-"+"}"
1284 report_raw
="${adb_reportdir}/adb_report.raw"
1285 report_srt
="${adb_reportdir}/adb_report.srt"
1286 report_jsn
="${adb_reportdir}/adb_report.json"
1287 report_txt
="${adb_reportdir}/adb_mailreport.txt"
1291 if [ "${action}" != "json" ]; then
1296 [ "${adb_represolve}" = "1" ] && resolve
=""
1297 for file in "${adb_reportdir}/adb_report.pcap"*; do
1299 "${adb_dumpcmd}" "${resolve}" -tttt -r "${file}" 2>/dev
/null |
1300 "${adb_awk}" -v cnt
="${cnt}" '!/\.lan\. |PTR\? | SOA\? /&&/ A[\? ]+|NXDomain|0\.0\.0\.0/{a=$1;b=substr($2,0,8);c=$4;sub(/\.[0-9]+$/,"",c);gsub(/[^[:alnum:]\.:-]/,"",c);d=cnt $7;sub(/\*$/,"",d);
1301 e=$(NF-1);sub(/[0-9]\/[0-9]\/[0-9]|0\.0\.0\.0/,"NX",e);sub(/\.$/,"",e);sub(/([0-9]{1,3}\.){3}[0-9]{1,3}/,"OK",e);gsub(/[^[:alnum:]\.-]/,"",e);if(e==""){e="err"};printf "%s\t%s\t%s\t%s\t%s\n",d,e,a,b,c}' >>"${report_raw}"
1303 hold
="$((cnt % adb_cores))"
1304 [ "${hold}" = "0" ] && wait
1308 if [ -s "${report_raw}" ]; then
1309 "${adb_sort}" ${adb_srtopts} -k1 -k3 -k4 -k5 -k1 -ur "${report_raw}" |
1310 "${adb_awk}" '{currA=($1+0);currB=$1;currC=substr($1,length($1),1);if(reqA==currB){reqA=0;printf "%s\t%s\n",d,$2}else if(currC=="+"){reqA=currA;d=$3"\t"$4"\t"$5"\t"$2}}' |
1311 "${adb_sort}" ${adb_srtopts} -k1 -k2 -k3 -k4 -ur >"${report_srt}"
1312 rm -f "${report_raw}"
1315 if [ -s "${report_srt}" ]; then
1316 start
="$("${adb_awk}" 'END{printf "%s_
%s
",$1,$2}' "${report_srt}")"
1317 end
="$("${adb_awk}" 'NR==1{printf "%s_
%s
",$1,$2}' "${report_srt}")"
1318 total
="$(wc -l <"${report_srt}")"
1319 blocked
="$("${adb_awk}" '{if($5=="NX
")cnt++}END{printf "%s
",cnt}' "${report_srt}")"
1320 percent
="$("${adb_awk}" -v t="${total}" -v b="${blocked}" 'BEGIN{printf "%.2f%s",b/t*100,"%"}')"
1324 printf "\t%s\n" "\"start_date\": \"${start%_*}\", "
1325 printf "\t%s\n" "\"start_time\": \"${start#*_}\", "
1326 printf "\t%s\n" "\"end_date\": \"${end%_*}\", "
1327 printf "\t%s\n" "\"end_time\": \"${end#*_}\", "
1328 printf "\t%s\n" "\"total\": \"${total}\", "
1329 printf "\t%s\n" "\"blocked\": \"${blocked}\", "
1330 printf "\t%s\n" "\"percent\": \"${percent}\", "
1332 top_list="top_clients top_domains top_blocked"
1333 for top in ${top_list}; do
1334 printf "\t%s" "\"${top}\": [ " >>"${report_jsn}"
1337 "${adb_awk}" '{print $3}' "${report_srt}" | "${adb_sort}" ${adb_srtopts} | uniq -c |
1338 "${adb_sort}" ${adb_srtopts} -nr |
1339 "${adb_awk}" '{ORS
=" ";if(NR
==1)printf "\n\t\t{\n\t\t\t\"count\": \"%s\",\n\t\t\t\"address\": \"%s\"\n\t\t}",$1,$2; else if(NR
<10)printf ",\n\t\t{\n\t\t\t\"count\": \"%s\",\n\t\t\t\"address\": \"%s\"\n\t\t}",$1,$2}' >>"${report_jsn}"
1342 "${adb_awk}" '{if($5!="NX")print $4}' "${report_srt}" | "${adb_sort}" ${adb_srtopts} | uniq -c |
1343 "${adb_sort}" ${adb_srtopts} -nr |
1344 "${adb_awk}" '{ORS
=" ";if(NR
==1)printf "\n\t\t{\n\t\t\t\"count\": \"%s\",\n\t\t\t\"address\": \"%s\"\n\t\t}",$1,$2; else if(NR
<10)printf ",\n\t\t{\n\t\t\t\"count\": \"%s\",\n\t\t\t\"address\": \"%s\"\n\t\t}",$1,$2}' >>"${report_jsn}"
1347 "${adb_awk}" '{if($5=="NX")print
$4}' "${report_srt}" |
1348 "${adb_sort}" ${adb_srtopts} | uniq -c | "${adb_sort}" ${adb_srtopts} -nr |
1349 "${adb_awk}" '{ORS
=" ";if(NR
==1)printf "\n\t\t{\n\t\t\t\"count\": \"%s\",\n\t\t\t\"address\": \"%s\"\n\t\t}",$1,$2; else if(NR
<10)printf ",\n\t\t{\n\t\t\t\"count\": \"%s\",\n\t\t\t\"address\": \"%s\"\n\t\t}",$1,$2}' >>"${report_jsn}"
1352 printf "\n\t%s\n" "]," >>"${report_jsn}"
1354 search="${search//./\\.}"
1355 search="${search//[+*~%\$&\"\' ]/}"
1356 "${adb_awk}" "BEGIN{i=0;printf \"\t\\\"requests\\\": [\n\"}/(${search})/{i++;if(i==1)printf \"\n\t\t{\n\t\t\t\\\"date\\\": \\\"%s\\\",\n\t\t\t\\\"time\\\": \\\"%s\\\",\n\t\t\t\\\"client\\\": \\\"%s\\\",\n\t\t\t\\\"domain\\\": \\\"%s\\\",\n\t\t\t\\\"rc\\\": \\\"%s\\\"\n\t\t}\",\$1,\$2,\$3,\$4,\$5;else if(i<=${count})printf \",\n\t\t{\n\t\t\t\\\"date\\\": \\\"%s\\\",\n\t\t\t\\\"time\\\": \\\"%s\\\",\n\t\t\t\\\"client\\\": \\\"%s\\\",\n\t\t\t\\\"domain\\\": \\\"%s\\\",\n\t\t\t\\\"rc\\\": \\\"%s\\\"\n\t\t}\",\$1,\$2,\$3,\$4,\$5}END{printf \"\n\t]\n}\n\"}" "${adb_reportdir}/adb_report.srt" >>"${report_jsn}"
1357 rm -f "${report_srt}"
1361 # output preparation
1363 if [ -s "${report_jsn}" ] && { [ "${action}" = "cli" ] || [ "${action}" = "mail" ]; }; then
1364 printf "%s\n%s\n%s\n" ":::" "::: Adblock DNS-Query Report" ":::" >>"${report_txt}"
1366 json_load_file "${report_jsn}"
1367 json_get_keys key_list
1368 for key in ${key_list}; do
1369 json_get_var value "${key}"
1370 eval "${key}=\"${value}\""
1372 printf " + %s\n + %s\n" "Start ::: ${start_date}, ${start_time}" "End ::: ${end_date}, ${end_time}" >>"${report_txt}"
1373 printf " + %s\n + %s %s\n" "Total ::: ${total}" "Blocked ::: ${blocked}" "(${percent})" >>"${report_txt}"
1374 top_list="top_clients top_domains top_blocked requests"
1375 for top in ${top_list}; do
1378 item="::: Top 10 Clients"
1381 item="::: Top 10 Domains"
1384 item="::: Top 10 Blocked Domains"
1387 if json_get_type status "${top}" && [ "${top}" != "requests" ] && [ "${status}" = "array" ]; then
1388 printf "%s\n%s\n%s\n" ":::" "${item}" ":::" >>"${report_txt}"
1389 json_select "${top}"
1392 while json_get_type status "${index}" && [ "${status}" = "object" ]; do
1393 json_get_values item "${index}"
1394 printf " + %-9s::: %s\n" ${item} >>"${report_txt}"
1395 index="$((index + 1))"
1397 elif json_get_type status "${top}" && [ "${top}" = "requests" ] && [ "${status}" = "array" ]; then
1398 printf "%s\n%s\n%s\n" ":::" "::: Latest DNS Queries" ":::" >>"${report_txt}"
1399 printf "%-15s%-15s%-45s%-80s%s\n" "Date" "Time" "Client" "Domain" "Answer" >>"${report_txt}"
1400 json_select "${top}"
1402 while json_get_type status "${index}" && [ "${status}" = "object" ]; do
1403 json_get_values item "${index}"
1404 printf "%-15s%-15s%-45s%-80s%s\n" ${item} >>"${report_txt}"
1405 index="$((index + 1))"
1410 content="$(cat "${report_txt}" 2>/dev/null)"
1411 rm -f "${report_txt}"
1416 if [ "${action}" = "cli" ]; then
1417 printf "%s\n" "${content}"
1418 elif [ "${action}" = "json" ]; then
1420 elif [ "${action}" = "mail" ] && [ "${adb_mail}" = "1" ] && [ -x "${adb_mailservice}" ]; then
1421 ("${adb_mailservice}" "${adb_ver}" "${content}" >/dev/null 2>&1) &
1424 f_log "debug" "f_report ::: action: ${action}, count: ${count}, search: ${search}, dump_util: ${adb_dumpcmd}, rep_dir: ${adb_reportdir}, rep_iface: ${adb_repiface:-"-"}, rep_listen: ${adb_replisten}, rep_chunksize: ${adb_repchunksize}, rep_chunkcnt: ${adb_repchunkcnt}, rep_resolve: ${adb_represolve}"
1427 # source required system libraries
1429 if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then
1430 . "/lib/functions.sh"
1431 . "/lib/functions/network.sh"
1432 . "/usr/share/libubox/jshn.sh"
1434 f_log "err" "system libraries not found"
1439 adb_awk="$(command -v gawk)"
1440 if [ ! -x "${adb_awk}" ]; then
1441 adb_awk="$(command -v awk)"
1442 [ ! -x "${adb_awk}" ] && f_log "err" "awk not found or not executable"
1447 adb_sort="$(command -v sort)"
1448 if [ ! -x "${adb_sort}" ] || ! "${adb_sort}" --version 2>/dev/null | grep -q "coreutils"; then
1449 f_log "err" "coreutils sort not found or not executable"
1452 # handle different adblock actions
1455 case "${adb_action}" in
1465 [ "${adb_dns}" != "raw" ] && f_switch suspend
1468 [ "${adb_dns}" != "raw" ] && f_switch resume
1471 f_report "${2}" "${3}" "${4}"