adblock: update 4.1.4-2
[feed/packages.git] / net / adblock / files / adblock.sh
1 #!/bin/sh
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.
5
6 # disable (s)hellcheck in release
7 # shellcheck disable=all
8
9 # set initial defaults
10 #
11 export LC_ALL=C
12 export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
13
14 adb_ver="4.1.4"
15 adb_enabled="0"
16 adb_debug="0"
17 adb_forcedns="0"
18 adb_dnsflush="0"
19 adb_dnstimeout="20"
20 adb_safesearch="0"
21 adb_safesearchlist=""
22 adb_safesearchmod="0"
23 adb_report="0"
24 adb_trigger=""
25 adb_triggerdelay="0"
26 adb_backup="1"
27 adb_mail="0"
28 adb_mailcnt="0"
29 adb_jail="0"
30 adb_dns=""
31 adb_dnsprefix="adb_list"
32 adb_locallist="blacklist whitelist iplist"
33 adb_tmpbase="/tmp"
34 adb_backupdir="${adb_tmpbase}/adblock-Backup"
35 adb_reportdir="${adb_tmpbase}/adblock-Report"
36 adb_jaildir="/tmp"
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)"
49 adb_fetchutil=""
50 adb_fetchinsecure=""
51 adb_zonelist=""
52 adb_portlist=""
53 adb_repiface=""
54 adb_replisten="53"
55 adb_repchunkcnt="5"
56 adb_repchunksize="1"
57 adb_represolve="0"
58 adb_lookupdomain="example.com"
59 adb_action="${1:-"start"}"
60 adb_packages=""
61 adb_sources=""
62 adb_cnt=""
63
64 # load & check adblock environment
65 #
66 f_load() {
67 local bg_pid iface port ports cpu core
68
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)}')"
73
74 f_conf
75
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))"
81
82 if [ "${adb_action}" != "report" ]; then
83 f_dns
84 f_fetch
85 fi
86
87 if [ "${adb_enabled}" = "0" ]; then
88 f_extconf
89 f_temp
90 f_rmdns
91 f_jsnup "disabled"
92 f_log "info" "adblock is currently disabled, please set the config option 'adb_enabled' to '1' to use this service"
93 exit 0
94 fi
95
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"
100 exit 0
101 fi
102
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
108 sleep 1
109 done
110 unset bg_pid
111 fi
112 fi
113
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}"
117 done
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"; }
123 fi
124 if [ -n "${adb_reportdir}" ] && [ ! -d "${adb_reportdir}" ]; then
125 mkdir -p "${adb_reportdir}"
126 f_log "info" "report directory '${adb_reportdir}' created"
127 fi
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}')"
131 else
132 f_log "info" "Please set the name of the reporting network device 'adb_repiface' manually"
133 fi
134 fi
135 }
136
137 # check & set environment
138 #
139 f_env() {
140 adb_starttime="$(date "+%s")"
141 f_log "info" "adblock instance started ::: action: ${adb_action}, priority: ${adb_nice:-"0"}, pid: ${$}"
142 f_jsnup "running"
143 f_extconf
144 f_temp
145
146 if [ "${adb_dnsflush}" = "1" ] || [ "${adb_memory##*/}" -lt "64" ]; then
147 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
148 f_dnsup
149 fi
150
151 if [ ! -r "${adb_srcfile}" ]; then
152 if [ -r "${adb_srcarc}" ]; then
153 zcat "${adb_srcarc}" >"${adb_srcfile}"
154 else
155 f_log "err" "adblock source archive not found"
156 fi
157 fi
158 if [ -r "${adb_srcfile}" ] && [ "${adb_action}" != "report" ]; then
159 json_init
160 json_load_file "${adb_srcfile}"
161 else
162 f_log "err" "adblock source file not found"
163 fi
164 }
165
166 # load adblock config
167 #
168 f_conf() {
169 local cnt="0" cnt_max="10"
170
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"
172
173 config_cb() {
174 option_cb() {
175 local option="${1}"
176 local value="${2}"
177 eval "${option}=\"${value}\""
178 }
179 list_cb() {
180 local option="${1}"
181 local value="${2}"
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}\""
200 fi
201 }
202 }
203 config_load adblock
204
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)"
208 cnt="$((cnt + 1))"
209 sleep 1
210 done
211 [ -z "${adb_packages}" ] && f_log "err" "local opkg package repository is not available, please set 'adb_fetchutil' and 'adb_dns' manually"
212 fi
213 }
214
215 # status helper function
216 #
217 f_char() {
218 local result input="${1}"
219
220 if [ "${input}" = "1" ]; then
221 result="✔"
222 else
223 result="✘"
224 fi
225 printf "%s" "${result}"
226 }
227
228 # load dns backend config
229 #
230 f_dns() {
231 local util utils dns_up cnt="0"
232
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
238 util="kresd"
239 elif [ "${util}" = "bind" ]; then
240 util="named"
241 fi
242 if [ "${util}" = "raw" ] || [ -x "$(command -v "${util}")" ]; then
243 adb_dns="${util}"
244 uci_set adblock global adb_dns "${util}"
245 f_uci "adblock"
246 break
247 fi
248 fi
249 done
250 elif [ "${adb_dns}" != "raw" ] && [ ! -x "$(command -v "${adb_dns}")" ]; then
251 unset adb_dns
252 fi
253
254 if [ -n "${adb_dns}" ]; then
255 case "${adb_dns}" in
256 "dnsmasq")
257 adb_dnscachecmd="-"
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=/#/"}"
266 ;;
267 "unbound")
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"}"
277 ;;
278 "named")
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 ."}"
290 ;;
291 "kresd")
292 adb_dnscachecmd="-"
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 ."}"
301 ;;
302 "raw")
303 adb_dnscachecmd="-"
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"}"
312 ;;
313 esac
314 fi
315
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"
318 fi
319
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'"
322 fi
323
324 if [ "${adb_action}" = "start" ] && [ -z "${adb_trigger}" ]; then
325 sleep ${adb_triggerdelay}
326 fi
327
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
332 break
333 fi
334 sleep 1
335 cnt="$((cnt + 1))"
336 done
337 fi
338
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"
343 else
344 f_log "err" "dns backend directory '${adb_dnsdir}' could not be created"
345 fi
346 fi
347 [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ] && printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
348
349 if [ "${dns_up}" != "true" ]; then
350 if ! f_dnsup 4; then
351 f_log "err" "dns backend '${adb_dns}' not running or executable"
352 fi
353 fi
354
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"
358 else
359 f_log "err" "backup directory '${adb_backupdir}' could not be created"
360 fi
361 fi
362
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"
366 else
367 f_log "err" "jail directory '${adb_jaildir}' could not be created"
368 fi
369 fi
370 fi
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}"
372 }
373
374 # load fetch utility
375 #
376 f_fetch() {
377 local util utils insecure cnt="0"
378
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}"
388 f_uci "adblock"
389 break
390 fi
391 fi
392 done
393 elif [ ! -x "$(command -v "${adb_fetchutil}")" ]; then
394 unset adb_fetchutil
395 fi
396 case "${adb_fetchutil}" in
397 "aria2c")
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"}"
400 ;;
401 "curl")
402 [ "${adb_fetchinsecure}" = "1" ] && insecure="--insecure"
403 adb_fetchparm="${adb_fetchparm:-"${insecure} --connect-timeout 20 --silent --show-error --location -o"}"
404 ;;
405 "uclient-fetch")
406 [ "${adb_fetchinsecure}" = "1" ] && insecure="--no-check-certificate"
407 adb_fetchparm="${adb_fetchparm:-"${insecure} --timeout=20 -O"}"
408 ;;
409 "wget")
410 [ "${adb_fetchinsecure}" = "1" ] && insecure="--no-check-certificate"
411 adb_fetchparm="${adb_fetchparm:-"${insecure} --no-cache --no-cookies --max-redirect=0 --timeout=20 -O"}"
412 ;;
413 esac
414 if [ -n "${adb_fetchutil}" ] && [ -n "${adb_fetchparm}" ]; then
415 adb_fetchutil="$(command -v "${adb_fetchutil}")"
416 else
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'"
418 fi
419 f_log "debug" "f_fetch ::: fetch_util: ${adb_fetchutil:-"-"}, fetch_parm: ${adb_fetchparm:-"-"}"
420 }
421
422 # create temporary files, directories and set dependent options
423 #
424 f_temp() {
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}"
430 else
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"
432 fi
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:-"-"}"
435 }
436
437 # remove temporary files and directories
438 #
439 f_rmtemp() {
440 [ -d "${adb_tmpdir}" ] && rm -rf "${adb_tmpdir}"
441 rm -f "${adb_srcfile}"
442 : >"${adb_pidfile}"
443 f_log "debug" "f_rmtemp ::: tmp_dir: ${adb_tmpdir:-"-"}, src_file: ${adb_srcfile:-"-"}, pid_file: ${adb_pidfile:-"-"}"
444 }
445
446 # remove dns related files
447 #
448 f_rmdns() {
449 local status
450
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
453 : >"${adb_rtfile}"
454 [ "${adb_backup}" = "1" ] && rm -f "${adb_backupdir}/${adb_dnsprefix}".*.gz
455 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
456 f_dnsup 4
457 fi
458 f_rmtemp
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:-"-"}"
460 }
461
462 # commit uci changes
463 #
464 f_uci() {
465 local change config="${1}"
466
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}"
471 case "${config}" in
472 "firewall")
473 "/etc/init.d/firewall" reload >/dev/null 2>&1
474 ;;
475 "resolver")
476 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
477 f_count
478 f_jsnup "running"
479 "/etc/init.d/${adb_dns}" reload >/dev/null 2>&1
480 ;;
481 esac
482 fi
483 f_log "debug" "f_uci ::: config: ${config}, change: ${change}"
484 fi
485 }
486
487 # get list counter
488 #
489 f_count() {
490 local file mode="${1}" name="${2}"
491
492 adb_cnt="0"
493 case "${mode}" in
494 "iplist")
495 [ -s "${adb_tmpdir}/tmp.add.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.add.${name}")"
496 ;;
497 "blacklist")
498 [ -s "${adb_tmpfile}.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpfile}.${name}")"
499 ;;
500 "whitelist")
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}"; }
502 ;;
503 "safesearch")
504 [ -s "${adb_tmpdir}/tmp.safesearch.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.safesearch.${name}")"
505 ;;
506 "merge")
507 [ -s "${adb_tmpdir}/${adb_dnsfile}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/${adb_dnsfile}")"
508 ;;
509 "download" | "backup" | "restore")
510 [ -s "${src_tmpfile}" ] && adb_cnt="$(wc -l 2>/dev/null <"${src_tmpfile}")"
511 ;;
512 "final")
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")))"
517 fi
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}")))"
521 fi
522 done
523 [ -n "${adb_dnsheader}" ] && adb_cnt="$(((adb_cnt - $(printf "%b" "${adb_dnsheader}" | grep -c "^")) / 2))"
524 fi
525 ;;
526 esac
527 }
528
529 # set external config options
530 #
531 f_extconf() {
532 local config config_dir config_file section zone port fwcfg
533
534 case "${adb_dns}" in
535 "dnsmasq")
536 config="dhcp"
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
540 fi
541 ;;
542 "kresd")
543 config="resolver"
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}"
549 fi
550 ;;
551 esac
552 f_uci "${config}"
553
554 config="firewall"
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
561 uci -q batch <<-EOC
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"
569 EOC
570 fi
571 fwcfg="${fwcfg/adblock_${zone}${port}[ |\$]/}"
572 done
573 done
574 fwcfg="${fwcfg#"${fwcfg%%[![:space:]]*}"}"
575 fwcfg="${fwcfg%"${fwcfg##*[![:space:]]}"}"
576 fi
577 if [ "${adb_enabled}" = "0" ] || [ "${adb_forcedns}" = "0" ] || [ -n "${fwcfg}" ]; then
578 for section in ${fwcfg}; do
579 uci_remove firewall "${section}"
580 done
581 fi
582 f_uci "${config}"
583 }
584
585 # restart dns backend
586 #
587 f_dnsup() {
588 local dns_service dns_up dns_pid restart_rc cnt="0" out_rc="4" in_rc="${1:-0}"
589
590 if [ "${adb_dns}" = "raw" ]; then
591 out_rc="0"
592 else
593 if [ "${in_rc}" = "0" ] && [ "${adb_dnsflush}" = "0" ]; then
594 case "${adb_dns}" in
595 "unbound")
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
598 fi
599 "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
600 restart_rc="${?}"
601 ;;
602 "named")
603 if [ -x "${adb_dnscachecmd}" ] && [ -f "/etc/bind/rndc.conf" ]; then
604 "${adb_dnscachecmd}" -c "/etc/bind/rndc.conf" reload >/dev/null 2>&1
605 restart_rc="${?}"
606 fi
607 if [ -z "${restart_rc}" ] || { [ -n "${restart_rc}" ] && [ "${restart_rc}" != "0" ]; }; then
608 "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
609 restart_rc="${?}"
610 fi
611 ;;
612 *)
613 "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
614 restart_rc="${?}"
615 ;;
616 esac
617 fi
618 if [ -z "${restart_rc}" ]; then
619 "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
620 restart_rc="${?}"
621 fi
622 fi
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
631 out_rc="0"
632 break
633 fi
634 else
635 sleep ${adb_dnstimeout}
636 cnt=${adb_dnstimeout}
637 out_rc="0"
638 break
639 fi
640 fi
641 cnt="$((cnt + 1))"
642 sleep 1
643 done
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
647 restart_rc="${?}"
648 fi
649 fi
650 fi
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}"
652 return "${out_rc}"
653 }
654
655 # backup/restore/remove blocklists
656 #
657 f_list() {
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"
659
660 case "${mode}" in
661 "iplist")
662 src_name="${mode}"
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}"
669 done
670 eval "${adb_dnsallowip}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.add.${src_name}"
671 out_rc="${?}"
672 fi
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}"
677 done
678 eval "${adb_dnsdenyip}" "${adb_tmpdir}/tmp.raw.${src_name}" >>"${adb_tmpdir}/tmp.add.${src_name}"
679 out_rc="${?}"
680 fi
681 rm -f "${adb_tmpdir}/tmp.raw.${src_name}"
682 fi
683 ;;
684 "blacklist" | "whitelist")
685 src_name="${mode}"
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}"
691 out_rc="${?}"
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}"
696 out_rc="${?}"
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}"
700 out_rc="${?}"
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}"
703 out_rc="${?}"
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}"
709 fi
710 fi
711 fi
712 fi
713 ;;
714 "safesearch")
715 case "${src_name}" in
716 "google")
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}"
723 out_rc="${?}"
724 else
725 "${adb_fetchutil}" ${adb_fetchparm} "${safe_domains}" "${safe_url}" 2>/dev/null
726 out_rc="${?}"
727 if [ "${adb_backup}" = "1" ] && [ "${out_rc}" = "0" ]; then
728 gzip -cf "${safe_domains}" >"${adb_backupdir}/safesearch.${src_name}.gz"
729 out_rc="${?}"
730 fi
731 fi
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}"
736 fi
737 out_rc="${?}"
738 fi
739 ;;
740 "bing")
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}"
746 fi
747 out_rc="${?}"
748 ;;
749 "duckduckgo")
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}"
755 fi
756 out_rc="${?}"
757 ;;
758 "pixabay")
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}"
764 fi
765 out_rc="${?}"
766 ;;
767 "yandex")
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}"
773 fi
774 out_rc="${?}"
775 ;;
776 "youtube")
777 if [ "${adb_safesearchmod}" = "0" ]; then
778 safe_cname="restrict.youtube.com"
779 else
780 safe_cname="restrictmoderate.youtube.com"
781 fi
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}"
786 fi
787 out_rc="${?}"
788 ;;
789 esac
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}"
796 break
797 fi
798 done
799 out_rc="${?}"
800 rm -f "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
801 fi
802 ;;
803 "backup")
804 (
805 gzip -cf "${src_tmpfile}" >"${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
806 out_rc="${?}"
807 ) &
808 ;;
809 "restore")
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}"
812 out_rc="${?}"
813 elif [ -z "${src_name}" ]; then
814 cnt="1"
815 for file in "${adb_backupdir}/${adb_dnsprefix}".*.gz; do
816 if [ -r "${file}" ]; then
817 name="${file##*/}"
818 name="${name%.*}"
819 zcat "${file}" >"${adb_tmpfile}.${name}" &
820 hold="$((cnt % adb_cores))"
821 if [ "${hold}" = "0" ]; then
822 wait
823 fi
824 cnt="$((cnt + 1))"
825 fi
826 done
827 wait
828 out_rc="${?}"
829 else
830 out_rc=4
831 fi
832 if [ "${adb_action}" != "start" ] && [ "${adb_action}" != "resume" ] && [ -n "${src_name}" ] && [ "${out_rc}" != "0" ]; then
833 adb_sources="${adb_sources/${src_name}}"
834 fi
835 ;;
836 "remove")
837 [ "${adb_backup}" = "1" ] && rm "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" 2>/dev/null
838 out_rc="${?}"
839 adb_sources="${adb_sources/${src_name}}"
840 ;;
841 "merge")
842 if [ "${adb_backup}" = "1" ]; then
843 for src_name in ${adb_sources}; do
844 ffiles="${ffiles} -a ! -name ${adb_dnsprefix}.${src_name}.gz"
845 done
846 if [ "${adb_safesearch}" = "1" ] && [ "${adb_dnssafesearch}" != "0" ]; then
847 ffiles="${ffiles} -a ! -name safesearch.google.gz"
848 fi
849 find "${adb_backupdir}" ${ffiles} -print0 2>/dev/null | xargs -0 rm 2>/dev/null
850 fi
851 unset src_name
852 "${adb_sort}" ${adb_srtopts} -mu "${adb_tmpfile}".* 2>/dev/null >"${adb_tmpdir}/${adb_dnsfile}"
853 out_rc="${?}"
854 rm -f "${adb_tmpfile}".*
855 ;;
856 "final")
857 unset src_name
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}"
863 done
864 { [ "${adb_dnsdeny}" != "0" ] && eval "${adb_dnsdeny}" "${adb_tmpdir}/${adb_dnsfile}" >>"${adb_dnsdir}/${adb_dnsfile}"; } || mv "${adb_tmpdir}/${adb_dnsfile}" "${adb_dnsdir}/${adb_dnsfile}"
865 out_rc="${?}"
866 ;;
867 esac
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}"
871 return "${out_rc}"
872 }
873
874 # top level domain compression
875 #
876 f_tld() {
877 local cnt cnt_tld source="${1}" temp_tld="${1}.tld"
878
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}")"
883 else
884 rm -f "${temp_tld}"
885 fi
886 f_log "debug" "f_tld ::: source: ${source}, cnt: ${adb_cnt:-"-"}, cnt_tld: ${cnt_tld:-"-"}"
887 }
888
889 # suspend/resume adblock processing
890 #
891 f_switch() {
892 local status entry done="false" mode="${1}"
893
894 json_init
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
899 f_env
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}"
905 fi
906 f_count
907 done="true"
908 elif [ "${mode}" = "resume" ] && [ "${status}" = "paused" ]; then
909 f_env
910 f_main
911 done="true"
912 fi
913 if [ "${done}" = "true" ]; then
914 [ "${mode}" = "suspend" ] && f_dnsup
915 f_jsnup "${mode}"
916 f_log "info" "${mode} adblock processing"
917 fi
918 f_rmtemp
919 }
920
921 # query blocklist for certain (sub-)domains
922 #
923 f_query() {
924 local search result prefix suffix field query_start query_end query_timeout=30 domain="${1}" tld="${1#*.}"
925
926 if [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]; then
927 printf "%s\n" "::: invalid input, please submit a single (sub-)domain :::"
928 else
929 case "${adb_dns}" in
930 "dnsmasq")
931 prefix=".*[\\/\\.]"
932 suffix="(\\/)"
933 field="2"
934 ;;
935 "unbound")
936 prefix=".*[\"\\.]"
937 suffix="(static)"
938 field="3"
939 ;;
940 "named")
941 prefix="[^\\*].*[\\.]"
942 suffix="( \\.)"
943 field="1"
944 ;;
945 "kresd")
946 prefix="[^\\*].*[\\.]"
947 suffix="( \\.)"
948 field="1"
949 ;;
950 "raw")
951 prefix=".*[\\.]"
952 suffix=""
953 field="1"
954 ;;
955 esac
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"}"
963 domain="${tld}"
964 tld="${domain#*.}"
965 done
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
971 suffix="${file##*.}"
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}"
975 else
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}"
977 fi
978 if [ "${?}" = "0" ]; then
979 result="true"
980 query_end="$(date "+%s")"
981 if [ "$((query_end - query_start))" -gt "${query_timeout}" ]; then
982 printf "%s\n\n" " - [...]"
983 break
984 fi
985 fi
986 done
987 [ "${result}" != "true" ] && printf "%s\n\n" " - no match"
988 fi
989 fi
990 }
991
992 # update runtime information
993 #
994 f_jsnup() {
995 local entry sources runtime utils bg_pid status="${1:-"enabled"}"
996
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)}')"
999
1000 case "${status}" in
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)"
1005 else
1006 runtime="${adb_action}, n/a, ${adb_memory:-0}, $(date -Iseconds)"
1007 fi
1008 [ "${status}" = "error" ] && adb_cnt="0"
1009 ;;
1010 "suspend")
1011 status="paused"
1012 ;;
1013 "resume")
1014 status=""
1015 ;;
1016 esac
1017 json_init
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"
1022 fi
1023 if [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then
1024 adb_cnt="0"
1025 sources="restrictive_jail"
1026 else
1027 sources="$(printf "%s\n" ${adb_sources} | "${adb_sort}" | "${adb_awk}" '{ORS=" ";print $0}')"
1028 fi
1029
1030 : >"${adb_rtfile}"
1031 json_init
1032 json_load_file "${adb_rtfile}" >/dev/null 2>&1
1033 json_init
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
1039 json_add_object
1040 json_add_string "source" "${entry}"
1041 json_close_object
1042 done
1043 json_close_array
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}"
1052
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) &
1056 bg_pid="${!}"
1057 fi
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:-"-"}"
1059 }
1060
1061 # write to syslog
1062 #
1063 f_log() {
1064 local class="${1}" log_msg="${2}"
1065
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
1070 f_rmdns
1071 f_jsnup "error"
1072 exit 1
1073 fi
1074 fi
1075 }
1076
1077 # main function for blocklist processing
1078 #
1079 f_main() {
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
1081
1082 f_log "debug" "f_main ::: memory: ${adb_memory:-0}, cores: ${adb_cores}, safe_search: ${adb_safesearch}, force_dns: ${adb_forcedns}, awk: ${adb_awk}"
1083
1084 # white- and blacklist preparation
1085 #
1086 for entry in ${adb_locallist}; do
1087 (f_list "${entry}" "${entry}") &
1088 done
1089
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
1093 if f_dnsup; then
1094 if [ "${adb_action}" != "resume" ]; then
1095 f_jsnup "enabled"
1096 fi
1097 f_log "info" "restrictive jail mode enabled successfully (${adb_sysver})"
1098 else
1099 f_log "err" "dns backend restart in jail mode failed"
1100 fi
1101 f_rmtemp
1102 return
1103 elif [ -f "${adb_dnsdir}/${adb_dnsjail}" ]; then
1104 rm -f "${adb_dnsdir}/${adb_dnsjail}"
1105 f_dnsup
1106 fi
1107
1108 # safe search preparation
1109 #
1110 if [ "${adb_safesearch}" = "1" ] && [ "${adb_dnssafesearch}" != "0" ]; then
1111 [ -z "${adb_safesearchlist}" ] && adb_safesearchlist="google bing duckduckgo pixabay yandex youtube"
1112 cnt="1"
1113 for entry in ${adb_safesearchlist}; do
1114 (f_list safesearch "${entry}") &
1115 hold="$((cnt % adb_cores))"
1116 [ "${hold}" = "0" ] && wait
1117 cnt="$((cnt + 1))"
1118 done
1119 fi
1120 wait
1121
1122 # main loop
1123 #
1124 cnt="1"
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}/}"
1128 continue
1129 fi
1130 json_get_var src_url "url" >/dev/null 2>&1
1131 json_get_var src_rset "rule" >/dev/null 2>&1
1132 json_select ..
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}"
1137 src_rc=4
1138
1139 # basic pre-checks
1140 #
1141 if [ -z "${src_url}" ] || [ -z "${src_rset}" ]; then
1142 f_list remove
1143 continue
1144 fi
1145
1146 # backup mode
1147 #
1148 if [ "${adb_backup}" = "1" ] && { [ "${adb_action}" = "start" ] || [ "${adb_action}" = "resume" ]; }; then
1149 if f_list restore && [ -s "${src_tmpfile}" ]; then
1150 continue
1151 fi
1152 fi
1153
1154 # download queue processing
1155 #
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
1160 (
1161 src_arc="${adb_tmpdir}/${src_url##*/}"
1162 src_log="$("${adb_fetchutil}" ${adb_fetchparm} "${src_arc}" "${src_url}" 2>&1)"
1163 src_rc="${?}"
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}$")"
1169 done
1170 if [ -n "${src_entries}" ]; then
1171 tar -xOzf "${src_arc}" ${src_entries} 2>/dev/null >"${src_tmpload}"
1172 src_rc="${?}"
1173 fi
1174 : >"${src_arc}"
1175 else
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:-"-"}"
1178 fi
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}"
1183 else
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}"
1186 fi
1187 : >"${src_tmpload}"
1188 "${adb_sort}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null >"${src_tmpfile}"
1189 src_rc="${?}"
1190 : >"${src_tmpsort}"
1191 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpfile}" ]; then
1192 f_list download
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}"
1196 f_list restore
1197 rm -f "${src_tmpfile}"
1198 fi
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}"
1201 f_list restore
1202 fi
1203 ) &
1204 fi
1205 else
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
1212 continue
1213 fi
1214 (
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)"
1218 src_rc="${?}"
1219 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpcat}" ]; then
1220 cat "${src_tmpcat}" >>"${src_tmpload}"
1221 : >"${src_tmpcat}"
1222 fi
1223 else
1224 src_log="$("${adb_fetchutil}" ${adb_fetchparm} "${src_tmpload}" "${src_url}" 2>&1)"
1225 src_rc="${?}"
1226 fi
1227 done
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}"
1232 else
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}"
1235 fi
1236 : >"${src_tmpload}"
1237 "${adb_sort}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null >"${src_tmpfile}"
1238 src_rc="${?}"
1239 : >"${src_tmpsort}"
1240 if [ "${src_rc}" = "0" ] && [ -s "${src_tmpfile}" ]; then
1241 f_list download
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}"
1245 f_list restore
1246 rm -f "${src_tmpfile}"
1247 fi
1248 else
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
1252 fi
1253 ) &
1254 fi
1255 hold="$((cnt % adb_cores))"
1256 [ "${hold}" = "0" ] && wait
1257 cnt="$((cnt + 1))"
1258 done
1259 wait
1260
1261 # tld compression and dns restart
1262 #
1263 if f_list merge && [ -s "${adb_tmpdir}/${adb_dnsfile}" ]; then
1264 f_tld "${adb_tmpdir}/${adb_dnsfile}"
1265 f_list final
1266 else
1267 printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"
1268 fi
1269 chown "${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
1270 if f_dnsup; then
1271 [ "${adb_action}" != "resume" ] && f_jsnup "enabled"
1272 f_log "info" "blocklist with overall ${adb_cnt} blocked domains loaded successfully (${adb_sysver})"
1273 else
1274 f_log "err" "dns backend restart with adblock blocklist failed"
1275 fi
1276 f_rmtemp
1277 }
1278
1279 # trace dns queries via tcpdump and prepare a report
1280 #
1281 f_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:-"+"}"
1283
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"
1288
1289 # build json file
1290 #
1291 if [ "${action}" != "json" ]; then
1292 : >"${report_raw}"
1293 : >"${report_srt}"
1294 : >"${report_txt}"
1295 : >"${report_jsn}"
1296 [ "${adb_represolve}" = "1" ] && resolve=""
1297 for file in "${adb_reportdir}/adb_report.pcap"*; do
1298 (
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}"
1302 ) &
1303 hold="$((cnt % adb_cores))"
1304 [ "${hold}" = "0" ] && wait
1305 cnt="$((cnt + 1))"
1306 done
1307 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}"
1313 fi
1314
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,"%"}')"
1321 : >"${report_jsn}"
1322 {
1323 printf "%s\n" "{ "
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}\", "
1331 } >>"${report_jsn}"
1332 top_list="top_clients top_domains top_blocked"
1333 for top in ${top_list}; do
1334 printf "\t%s" "\"${top}\": [ " >>"${report_jsn}"
1335 case "${top}" in
1336 "top_clients")
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}"
1340 ;;
1341 "top_domains")
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}"
1345 ;;
1346 "top_blocked")
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}"
1350 ;;
1351 esac
1352 printf "\n\t%s\n" "]," >>"${report_jsn}"
1353 done
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}"
1358 fi
1359 fi
1360
1361 # output preparation
1362 #
1363 if [ -s "${report_jsn}" ] && { [ "${action}" = "cli" ] || [ "${action}" = "mail" ]; }; then
1364 printf "%s\n%s\n%s\n" ":::" "::: Adblock DNS-Query Report" ":::" >>"${report_txt}"
1365 json_init
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}\""
1371 done
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
1376 case "${top}" in
1377 "top_clients")
1378 item="::: Top 10 Clients"
1379 ;;
1380 "top_domains")
1381 item="::: Top 10 Domains"
1382 ;;
1383 "top_blocked")
1384 item="::: Top 10 Blocked Domains"
1385 ;;
1386 esac
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}"
1390 index="1"
1391 item=""
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))"
1396 done
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}"
1401 index="1"
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))"
1406 done
1407 fi
1408 json_select ".."
1409 done
1410 content="$(cat "${report_txt}" 2>/dev/null)"
1411 rm -f "${report_txt}"
1412 fi
1413
1414 # report output
1415 #
1416 if [ "${action}" = "cli" ]; then
1417 printf "%s\n" "${content}"
1418 elif [ "${action}" = "json" ]; then
1419 cat "${report_jsn}"
1420 elif [ "${action}" = "mail" ] && [ "${adb_mail}" = "1" ] && [ -x "${adb_mailservice}" ]; then
1421 ("${adb_mailservice}" "${adb_ver}" "${content}" >/dev/null 2>&1) &
1422 bg_pid="${!}"
1423 fi
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}"
1425 }
1426
1427 # source required system libraries
1428 #
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"
1433 else
1434 f_log "err" "system libraries not found"
1435 fi
1436
1437 # awk check
1438 #
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"
1443 fi
1444
1445 # sort check
1446 #
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"
1450 fi
1451
1452 # handle different adblock actions
1453 #
1454 f_load
1455 case "${adb_action}" in
1456 "stop")
1457 f_rmdns
1458 ;;
1459 "restart")
1460 f_rmdns
1461 f_env
1462 f_main
1463 ;;
1464 "suspend")
1465 [ "${adb_dns}" != "raw" ] && f_switch suspend
1466 ;;
1467 "resume")
1468 [ "${adb_dns}" != "raw" ] && f_switch resume
1469 ;;
1470 "report")
1471 f_report "${2}" "${3}" "${4}"
1472 ;;
1473 "query")
1474 f_query "${2}"
1475 ;;
1476 "start" | "reload")
1477 f_env
1478 f_main
1479 ;;
1480 esac