From 8afc26fafbd913d829734ab22fc09abaf8874ae8 Mon Sep 17 00:00:00 2001 From: Dirk Brenken Date: Sat, 23 Nov 2024 22:15:00 +0100 Subject: [PATCH] adblock: release 4.2.3-1 * optimized procd settings for better performance * reworked autodetection functions (still broken in master due to apk migration) * made the tld function optional, set 'adb_tld' accordingly (enabled by default) * reworked count function * various code improvements Signed-off-by: Dirk Brenken --- net/adblock/Makefile | 4 +- net/adblock/files/README.md | 22 +- net/adblock/files/adblock.init | 6 +- net/adblock/files/adblock.sh | 606 ++++++++++++++++----------------- 4 files changed, 304 insertions(+), 334 deletions(-) diff --git a/net/adblock/Makefile b/net/adblock/Makefile index a321d529fa..3d2ff3fb87 100644 --- a/net/adblock/Makefile +++ b/net/adblock/Makefile @@ -6,8 +6,8 @@ include $(TOPDIR)/rules.mk PKG_NAME:=adblock -PKG_VERSION:=4.2.2 -PKG_RELEASE:=8 +PKG_VERSION:=4.2.3 +PKG_RELEASE:=1 PKG_LICENSE:=GPL-3.0-or-later PKG_MAINTAINER:=Dirk Brenken diff --git a/net/adblock/files/README.md b/net/adblock/files/README.md index a375e4bdc2..e6702e9c16 100644 --- a/net/adblock/files/README.md +++ b/net/adblock/files/README.md @@ -177,6 +177,7 @@ Available commands: | adb_repchunksize | 1 | report chunk size used by tcpdump in MB | | adb_represolve | 0, disabled | resolve reporting IP addresses using reverse DNS (PTR) lookups | | adb_backup | 1, enabled | set to 0 to disable the backup function | +| adb_tld | 1, enabled | set to 0 to disable the top level domain compression (tld) function | | adb_backupdir | /tmp | path for adblock backups | | adb_tmpbase | /tmp | path for all adblock related runtime operations, e.g. downloading, sorting, merging etc. | | adb_safesearch | 0, disabled | set to 1 to enforce SafeSearch for google, bing, duckduckgo, yandex, youtube and pixabay | @@ -257,17 +258,16 @@ To get the status in the CLI, just call _/etc/init.d/adblock status_ or _/etc/in ~#@blackhole:~# /etc/init.d/adblock status ::: adblock runtime information + adblock_status : enabled - + adblock_version : 4.1.4 - + blocked_domains : 268355 - + active_sources : adaway, adguard, adguard_tracking, android_tracking, bitcoin, disconnect, firetv_tracking, games_t - racking, hblock, oisd_basic, phishing_army, smarttv_tracking, stopforumspam, wally3k, winspy, yoyo - + dns_backend : unbound (unbound-control), /var/lib/unbound - + run_utils : download: /usr/bin/curl, sort: /usr/libexec/sort-coreutils, awk: /bin/busybox - + run_ifaces : trigger: wan, report: br-lan - + run_directories : base: /tmp, backup: /mnt/data/adblock-Backup, report: /mnt/data/adblock-Report, jail: /tmp - + run_flags : backup: ✔, flush: ✘, force: ✔, search: ✘, report: ✔, mail: ✔, jail: ✘ - + last_run : restart, 3m 17s, 249/73/68, 2022-09-10T13:43:07+02:00 - + system : ASUS RT-AX53U, OpenWrt SNAPSHOT r20535-2ca5602864 + + adblock_version : 4.2.3-r1 + + blocked_domains : 785573 + + active_sources : adguard, doh_blocklist, hagezi, winspy + + dns_backend : dnsmasq (-), /tmp/dnsmasq.d + + run_utils : download: /usr/bin/curl, sort: /usr/libexec/sort-coreutils, awk: /usr/bin/gawk + + run_ifaces : trigger: trm_wwan, report: br-lan + + run_directories : base: /tmp, backup: /tmp/adblock-Backup, report: /tmp/adblock-Report, jail: /tmp + + run_flags : backup: ✔, tld: ✔, force: ✔, flush: ✘, search: ✘, report: ✔, mail: ✘, jail: ✘ + + last_run : reload, 1m 3s, 650 MB available, 1664 KB max. used, 2024-11-23T18:11:41+01:00 + + system : OpenWrt One, mediatek/filogic, OpenWrt SNAPSHOT r28034-ca53f2d430 The 'last\_run' line includes the used start type, the run duration, the memory footprint after DNS backend loading (total/free/available) and the date/time of the last run. diff --git a/net/adblock/files/adblock.init b/net/adblock/files/adblock.init index d3253e57a6..d35b3c1e7c 100755 --- a/net/adblock/files/adblock.init +++ b/net/adblock/files/adblock.init @@ -36,8 +36,8 @@ start_service() { procd_set_param command "${adb_script}" "${@}" procd_set_param pidfile "${adb_pidfile}" procd_set_param nice "$(uci_get adblock global adb_nice "0")" - procd_set_param stdout 1 - procd_set_param stderr 1 + procd_set_param stdout 0 + procd_set_param stderr 0 procd_close_instance fi } @@ -188,7 +188,7 @@ service_triggers() { delay="$(uci_get adblock global adb_triggerdelay "5")" PROCD_RELOAD_DELAY="$((delay * 1000))" - [ -n "${iface}" ] && procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start" + [ -n "${iface}" ] && procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" start PROCD_RELOAD_DELAY="$((2 * 1000))" procd_add_reload_trigger "adblock" diff --git a/net/adblock/files/adblock.sh b/net/adblock/files/adblock.sh index dab0a7c157..e9c5dca369 100755 --- a/net/adblock/files/adblock.sh +++ b/net/adblock/files/adblock.sh @@ -17,15 +17,15 @@ adb_forcedns="0" adb_dnsflush="0" adb_dnstimeout="20" adb_safesearch="0" -adb_safesearchlist="" adb_safesearchmod="0" adb_report="0" adb_trigger="" -adb_triggerdelay="0" +adb_triggerdelay="5" adb_backup="1" adb_mail="0" adb_mailcnt="0" adb_jail="0" +adb_tld="1" adb_dns="" adb_dnsprefix="adb_list" adb_locallist="blacklist whitelist iplist" @@ -44,8 +44,6 @@ adb_srcfile="${adb_tmpbase}/adb_sources.json" adb_rtfile="${adb_tmpbase}/adb_runtime.json" adb_fetchutil="" adb_fetchinsecure="" -adb_zonelist="" -adb_portlist="" adb_repiface="" adb_replisten="53" adb_repchunkcnt="5" @@ -54,7 +52,6 @@ adb_represolve="0" adb_lookupdomain="example.com" adb_action="${1:-"start"}" adb_packages="" -adb_sources="" adb_cnt="" # command selector @@ -87,7 +84,6 @@ f_load() { adb_ver="$(printf "%s" "${adb_packages}" | "${adb_jsoncmd}" -ql1 -e '@.packages.adblock')" adb_sysver="$("${adb_ubuscmd}" -S call system board 2>/dev/null | "${adb_jsoncmd}" -ql1 -e '@.model' -e '@.release.target' -e '@.release.distribution' -e '@.release.version' -e '@.release.revision' | "${adb_awkcmd}" 'BEGIN{RS="";FS="\n"}{printf "%s, %s, %s %s %s %s",$1,$2,$3,$4,$5,$6}')" - adb_memory="$("${adb_awkcmd}" '/^MemAvailable/{printf "%s",int($2/1000)}' "/proc/meminfo" 2>/dev/null)" f_conf if [ -z "${adb_cores}" ]; then @@ -99,11 +95,6 @@ f_load() { [ "${adb_cores}" -gt "16" ] && adb_cores="16" fi - if [ "${adb_action}" != "report" ]; then - f_dns - f_fetch - fi - if [ "${adb_enabled}" = "0" ]; then f_extconf f_temp @@ -113,66 +104,70 @@ f_load() { exit 0 fi + if [ "${adb_action}" != "report" ] && [ "${adb_action}" != "mail" ]; then + f_dns + f_fetch + fi + if [ "${adb_report}" = "1" ] && [ ! -x "${adb_dumpcmd}" ]; then f_log "info" "Please install the package 'tcpdump' or 'tcpdump-mini' to use the reporting feature" - elif [ "${adb_report}" = "0" ] && [ "${adb_action}" = "report" ]; then - f_log "info" "Please enable the 'DNS Report' option to use the reporting feature" - exit 0 - fi + elif [ -x "${adb_dumpcmd}" ]; then + bg_pid="$("${adb_pgrepcmd}" -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awkcmd}" '{ORS=" "; print $1}')" + if [ "${adb_report}" = "0" ] || { [ -n "${bg_pid}" ] && { [ "${adb_action}" = "stop" ] || [ "${adb_action}" = "restart" ]; }; }; then + if [ -n "${bg_pid}" ]; then + kill -HUP "${bg_pid}" 2>/dev/null + while kill -0 "${bg_pid}" 2>/dev/null; do + sleep 1 + done + unset bg_pid + fi + rm -f "${adb_reportdir}"/adb_report.pcap* + fi - bg_pid="$("${adb_pgrepcmd}" -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awkcmd}" '{ORS=" "; print $1}')" - if [ -x "${adb_dumpcmd}" ] && { [ "${adb_report}" = "0" ] || { [ -n "${bg_pid}" ] && { [ "${adb_action}" = "stop" ] || [ "${adb_action}" = "restart" ]; }; }; }; then - if [ -n "${bg_pid}" ]; then - kill -HUP "${bg_pid}" 2>/dev/null - while kill -0 "${bg_pid}" 2>/dev/null; do - sleep 1 + if [ "${adb_report}" = "1" ] && [ -z "${bg_pid}" ] && [ "${adb_action}" != "report" ] && [ "${adb_action}" != "stop" ]; then + [ ! -d "${adb_reportdir}" ] && mkdir -p "${adb_reportdir}" + + for port in ${adb_replisten}; do + [ -z "${ports}" ] && ports="port ${port}" || ports="${ports} or port ${port}" done - unset bg_pid - fi - rm -f "${adb_reportdir}"/adb_report.pcap* - fi + if [ -z "${adb_repiface}" ]; then + network_get_device iface "lan" + [ -z "${iface}" ] && network_get_physdev iface "lan" + [ -n "${iface}" ] && adb_repiface="${iface}" + [ -n "${adb_repiface}" ] && { uci_set adblock global adb_repiface "${adb_repiface}"; f_uci "adblock"; } + fi - if [ -x "${adb_dumpcmd}" ] && [ "${adb_report}" = "1" ] && [ -z "${bg_pid}" ] && [ "${adb_action}" != "report" ] && [ "${adb_action}" != "stop" ]; then - for port in ${adb_replisten}; do - [ -z "${ports}" ] && ports="port ${port}" || ports="${ports} or port ${port}" - done - if [ -z "${adb_repiface}" ]; then - network_get_device iface "lan" - [ -z "${iface}" ] && network_get_physdev iface "lan" - [ -n "${iface}" ] && adb_repiface="${iface}" - [ -n "${adb_repiface}" ] && { uci_set adblock global adb_repiface "${adb_repiface}"; f_uci "adblock"; } - fi - if [ -n "${adb_reportdir}" ] && [ ! -d "${adb_reportdir}" ]; then - mkdir -p "${adb_reportdir}" - f_log "info" "report directory '${adb_reportdir}' created" - fi - if [ -n "${adb_repiface}" ] && [ -d "${adb_reportdir}" ]; then - ("${adb_dumpcmd}" --immediate-mode -nn -p -s0 -l -i ${adb_repiface} ${ports} -C${adb_repchunksize} -W${adb_repchunkcnt} -w "${adb_reportdir}/adb_report.pcap" >/dev/null 2>&1 &) - bg_pid="$("${adb_pgrepcmd}" -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awkcmd}" '{ORS=" "; print $1}')" - else - f_log "info" "Please set the name of the reporting network device 'adb_repiface' manually" + if [ -n "${adb_repiface}" ] && [ -d "${adb_reportdir}" ]; then + ("${adb_dumpcmd}" --immediate-mode -nn -p -s0 -l -i ${adb_repiface} ${ports} -C${adb_repchunksize} -W${adb_repchunkcnt} -w "${adb_reportdir}/adb_report.pcap" >/dev/null 2>&1 &) + bg_pid="$("${adb_pgrepcmd}" -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awkcmd}" '{ORS=" "; print $1}')" + else + f_log "info" "Please set the name of the reporting network device 'adb_repiface' manually" + fi fi fi - f_log "info" "DEBUG ::: $(ubus call rpc-sys packagelist '{ "all": true }' | wc -l) packages" } # check & set environment # f_env() { + local mem_free + adb_starttime="$(date "+%s")" + mem_free="$("${adb_awkcmd}" '/^MemAvailable/{printf "%s",int($2/1000)}' "/proc/meminfo" 2>/dev/null)" + f_log "info" "adblock instance started ::: action: ${adb_action}, priority: ${adb_nice:-"0"}, pid: ${$}" f_jsnup "running" f_extconf f_temp - if [ "${adb_dnsflush}" = "1" ] || [ "${adb_memory##*/}" -lt "64" ]; then + if [ "${adb_dnsflush}" = "1" ] || [ "${mem_free}" -lt "64" ]; then printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" f_dnsup fi if [ ! -r "${adb_srcfile}" ]; then if [ -r "${adb_srcarc}" ]; then - zcat "${adb_srcarc}" >"${adb_srcfile}" + "${adb_zcatcmd}" "${adb_srcarc}" >"${adb_srcfile}" else f_log "err" "adblock source archive not found" fi @@ -188,7 +183,7 @@ f_env() { # load adblock config # f_conf() { - [ ! -r "/etc/config/adblock" ] && f_log "err" "no valid adblock config found, please re-install the adblock package" + unset adb_sources adb_hag_sources adb_hst_sources adb_stb_sources adb_utc_sources adb_denyip adb_allowip adb_safesearchlist adb_zonelist adb_portlist config_cb() { option_cb() { @@ -199,27 +194,38 @@ f_conf() { list_cb() { local option="${1}" local value="${2}" - if [ "${option}" = "adb_sources" ]; then - eval "${option}=\"$(printf "%s" "${adb_sources}") ${value}\"" - elif [ "${option}" = "adb_hag_sources" ]; then - eval "${option}=\"$(printf "%s" "${adb_hag_sources}") ${value}\"" - elif [ "${option}" = "adb_hst_sources" ]; then - eval "${option}=\"$(printf "%s" "${adb_hst_sources}") ${value}\"" - elif [ "${option}" = "adb_stb_sources" ]; then - eval "${option}=\"$(printf "%s" "${adb_stb_sources}") ${value}\"" - elif [ "${option}" = "adb_utc_sources" ]; then - eval "${option}=\"$(printf "%s" "${adb_utc_sources}") ${value}\"" - elif [ "${option}" = "adb_denyip" ]; then - eval "${option}=\"$(printf "%s" "${adb_denyip}") ${value}\"" - elif [ "${option}" = "adb_allowip" ]; then - eval "${option}=\"$(printf "%s" "${adb_allowip}") ${value}\"" - elif [ "${option}" = "adb_safesearchlist" ]; then - eval "${option}=\"$(printf "%s" "${adb_safesearchlist}") ${value}\"" - elif [ "${option}" = "adb_zonelist" ]; then - eval "${option}=\"$(printf "%s" "${adb_zonelist}") ${value}\"" - elif [ "${option}" = "adb_portlist" ]; then - eval "${option}=\"$(printf "%s" "${adb_portlist}") ${value}\"" - fi + case "${option}" in + "adb_sources") + eval "${option}=\"$(printf "%s" "${adb_sources}") ${value}\"" + ;; + "adb_hag_sources") + eval "${option}=\"$(printf "%s" "${adb_hag_sources}") ${value}\"" + ;; + "adb_hst_sources") + eval "${option}=\"$(printf "%s" "${adb_hst_sources}") ${value}\"" + ;; + "adb_stb_sources") + eval "${option}=\"$(printf "%s" "${adb_stb_sources}") ${value}\"" + ;; + "adb_utc_sources") + eval "${option}=\"$(printf "%s" "${adb_utc_sources}") ${value}\"" + ;; + "adb_denyip") + eval "${option}=\"$(printf "%s" "${adb_denyip}") ${value}\"" + ;; + "adb_allowip") + eval "${option}=\"$(printf "%s" "${adb_allowip}") ${value}\"" + ;; + "adb_safesearchlist") + eval "${option}=\"$(printf "%s" "${adb_safesearchlist}") ${value}\"" + ;; + "adb_zonelist") + eval "${option}=\"$(printf "%s" "${adb_zonelist}") ${value}\"" + ;; + "adb_portlist") + eval "${option}=\"$(printf "%s" "${adb_portlist}") ${value}\"" + ;; + esac } } config_load adblock @@ -241,20 +247,32 @@ f_char() { # load dns backend config # f_dns() { - local util utils dns_up cnt="0" + local util utils + + if [ "${adb_action}" = "start" ] && [ -z "${adb_trigger}" ]; then + sleep ${adb_triggerdelay} + fi if [ -z "${adb_dns}" ]; then - utils="knot-resolver bind-server unbound-daemon dnsmasq raw" + utils="knot-resolver bind-server unbound-daemon dnsmasq-full dnsmasq-dhcpv6 dnsmasq" for util in ${utils}; do - if [ "${util}" = "raw" ] || printf "%s" "${adb_packages}" | "${adb_grepcmd}" -q "${util}"; then - if [ "${util}" = "knot-resolver" ]; then - util="kresd" - elif [ "${util}" = "bind-server" ]; then - util="named" - elif [ "${util}" = "unbound-daemon" ]; then - util="unbound" - fi - if [ "${util}" = "raw" ] || [ -x "$(command -v "${util}")" ]; then + if printf "%s" "${adb_packages}" | "${adb_jsoncmd}" -ql1 -e "@.packages.${util}" >/dev/null 2>&1; then + case "${util}" in + "knot-resolver") + util="kresd" + ;; + "bind-server") + util="named" + ;; + "unbound-daemon") + util="unbound" + ;; + "dnsmasq-full" | "dnsmasq-dhcpv6") + util="dnsmasq" + ;; + esac + + if [ -x "$(command -v "${util}")" ]; then adb_dns="${util}" uci_set adblock global adb_dns "${util}" f_uci "adblock" @@ -262,141 +280,103 @@ f_dns() { fi fi done - elif [ "${adb_dns}" != "raw" ] && [ ! -x "$(command -v "${adb_dns}")" ]; then - unset adb_dns fi - if [ -n "${adb_dns}" ]; then - case "${adb_dns}" in - "dnsmasq") - adb_dnscachecmd="-" - adb_dnsinstance="${adb_dnsinstance:-"0"}" - adb_dnsuser="${adb_dnsuser:-"dnsmasq"}" - adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}" - adb_dnsheader="${adb_dnsheader:-""}" - adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"local=/\"\$0\"/\"}'"}" - adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"local=/\"\$0\"/#\"}'"}" - adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -v item=\"\$item\" '{print \"address=/\"\$0\"/\"item\"\";print \"local=/\"\$0\"/\"}'"}" - adb_dnsstop="${adb_dnsstop:-"address=/#/\nlocal=/#/"}" - ;; - "unbound") - adb_dnscachecmd="$(command -v unbound-control || printf "%s" "-")" - adb_dnsinstance="${adb_dnsinstance:-"0"}" - adb_dnsuser="${adb_dnsuser:-"unbound"}" - adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}" - adb_dnsheader="${adb_dnsheader:-""}" - adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"local-zone: \\042\"\$0\"\\042 always_nxdomain\"}'"}" - adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"local-zone: \\042\"\$0\"\\042 always_transparent\"}'"}" - adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -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\"}'"}" - adb_dnsstop="${adb_dnsstop:-"local-zone: \".\" always_nxdomain"}" - ;; - "named") - adb_dnscachecmd="$(command -v rndc || printf "%s" "-")" - adb_dnsinstance="${adb_dnsinstance:-"0"}" - adb_dnsuser="${adb_dnsuser:-"bind"}" - adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}" - adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n IN NS localhost.\n"}" - adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}" - adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}" - adb_dnsdenyip="${adb_dnsdenyip:-"${adb_awkcmd} '{print \"\"\$0\".rpz-client-ip CNAME .\"}'"}" - adb_dnsallowip="${adb_dnsallowip:-"${adb_awkcmd} '{print \"\"\$0\".rpz-client-ip CNAME rpz-passthru.\"}'"}" - adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -v item=\"\$item\" '{print \"\"\$0\" CNAME \"item\".\\n*.\"\$0\" CNAME \"item\".\"}'"}" - adb_dnsstop="${adb_dnsstop:-"* CNAME ."}" - ;; - "kresd") - adb_dnscachecmd="-" - adb_dnsinstance="${adb_dnsinstance:-"0"}" - adb_dnsuser="${adb_dnsuser:-"root"}" - adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}" - adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n"}" - adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}" - adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}" - adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -v item=\"\$item\" '{type=\"AAAA\";if(match(item,/^([0-9]{1,3}\.){3}[0-9]{1,3}$/)){type=\"A\"}}{print \"\"\$0\" \"type\" \"item\"\"}'"}" - adb_dnsstop="${adb_dnsstop:-"* CNAME ."}" - ;; - "raw") - adb_dnscachecmd="-" - adb_dnsinstance="${adb_dnsinstance:-"0"}" - adb_dnsuser="${adb_dnsuser:-"root"}" - adb_dnsdir="${adb_dnsdir:-"/tmp"}" - adb_dnsheader="${adb_dnsheader:-""}" - adb_dnsdeny="${adb_dnsdeny:-"0"}" - adb_dnsallow="${adb_dnsallow:-"1"}" - adb_dnssafesearch="${adb_dnssafesearch:-"0"}" - adb_dnsstop="${adb_dnsstop:-"0"}" - ;; - esac - fi - - if [ "${adb_dns}" != "raw" ] && { [ -z "${adb_dns}" ] || [ ! -x "$(command -v "${adb_dns}")" ]; }; then + if [ "${adb_dns}" != "raw" ] && [ ! -x "$(command -v "${adb_dns}")" ]; then f_log "err" "dns backend not found, please set 'adb_dns' manually" fi - if [ "${adb_dns}" != "raw" ] && { [ "${adb_dnsdir}" = "${adb_tmpbase}" ] || [ "${adb_dnsdir}" = "${adb_backupdir}" ] || [ "${adb_dnsdir}" = "${adb_reportdir}" ]; }; then - f_log "err" "dns directory '${adb_dnsdir}' has been misconfigured, it must not point to the 'adb_tmpbase', 'adb_backupdir', 'adb_reportdir'" - fi - - if [ "${adb_action}" = "start" ] && [ -z "${adb_trigger}" ]; then - sleep ${adb_triggerdelay} - fi - - if [ "${adb_dns}" != "raw" ] && [ "${adb_action}" != "stop" ]; then - while [ "${cnt}" -le 30 ]; do - dns_up="$("${adb_ubuscmd}" -S call service list "{\"name\":\"${adb_dns}\"}" 2>/dev/null | "${adb_jsoncmd}" -ql1 -e "@[\"${adb_dns}\"].instances.*.running" 2>/dev/null)" - if [ "${dns_up}" = "true" ]; then - break - fi - sleep 1 - cnt="$((cnt + 1))" - done - fi + case "${adb_dns}" in + "dnsmasq") + adb_dnscachecmd="-" + adb_dnsinstance="${adb_dnsinstance:-"0"}" + adb_dnsuser="${adb_dnsuser:-"dnsmasq"}" + adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}" + adb_dnsheader="${adb_dnsheader:-""}" + adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"local=/\"\$0\"/\"}'"}" + adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"local=/\"\$0\"/#\"}'"}" + adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -v item=\"\$item\" '{print \"address=/\"\$0\"/\"item\"\";print \"local=/\"\$0\"/\"}'"}" + adb_dnsstop="${adb_dnsstop:-"address=/#/\nlocal=/#/"}" + ;; + "unbound") + adb_dnscachecmd="$(command -v unbound-control || printf "%s" "-")" + adb_dnsinstance="${adb_dnsinstance:-"0"}" + adb_dnsuser="${adb_dnsuser:-"unbound"}" + adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}" + adb_dnsheader="${adb_dnsheader:-""}" + adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"local-zone: \\042\"\$0\"\\042 always_nxdomain\"}'"}" + adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"local-zone: \\042\"\$0\"\\042 always_transparent\"}'"}" + adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -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\"}'"}" + adb_dnsstop="${adb_dnsstop:-"local-zone: \".\" always_nxdomain"}" + ;; + "named") + adb_dnscachecmd="$(command -v rndc || printf "%s" "-")" + adb_dnsinstance="${adb_dnsinstance:-"0"}" + adb_dnsuser="${adb_dnsuser:-"bind"}" + adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}" + adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n IN NS localhost.\n"}" + adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}" + adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}" + adb_dnsdenyip="${adb_dnsdenyip:-"${adb_awkcmd} '{print \"\"\$0\".rpz-client-ip CNAME .\"}'"}" + adb_dnsallowip="${adb_dnsallowip:-"${adb_awkcmd} '{print \"\"\$0\".rpz-client-ip CNAME rpz-passthru.\"}'"}" + adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -v item=\"\$item\" '{print \"\"\$0\" CNAME \"item\".\\n*.\"\$0\" CNAME \"item\".\"}'"}" + adb_dnsstop="${adb_dnsstop:-"* CNAME ."}" + ;; + "kresd") + adb_dnscachecmd="-" + adb_dnsinstance="${adb_dnsinstance:-"0"}" + adb_dnsuser="${adb_dnsuser:-"root"}" + adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}" + adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n"}" + adb_dnsdeny="${adb_dnsdeny:-"${adb_awkcmd} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}" + adb_dnsallow="${adb_dnsallow:-"${adb_awkcmd} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}" + adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awkcmd} -v item=\"\$item\" '{type=\"AAAA\";if(match(item,/^([0-9]{1,3}\.){3}[0-9]{1,3}$/)){type=\"A\"}}{print \"\"\$0\" \"type\" \"item\"\"}'"}" + adb_dnsstop="${adb_dnsstop:-"* CNAME ."}" + ;; + "raw") + adb_dnscachecmd="-" + adb_dnsinstance="${adb_dnsinstance:-"0"}" + adb_dnsuser="${adb_dnsuser:-"root"}" + adb_dnsdir="${adb_dnsdir:-"/tmp"}" + adb_dnsheader="${adb_dnsheader:-""}" + adb_dnsdeny="${adb_dnsdeny:-"0"}" + adb_dnsallow="${adb_dnsallow:-"1"}" + adb_dnssafesearch="${adb_dnssafesearch:-"0"}" + adb_dnsstop="${adb_dnsstop:-"0"}" + ;; + esac if [ "${adb_action}" != "stop" ]; then - if [ -n "${adb_dnsdir}" ] && [ ! -d "${adb_dnsdir}" ]; then - if mkdir -p "${adb_dnsdir}"; then - f_log "info" "dns backend directory '${adb_dnsdir}' created" - else - f_log "err" "dns backend directory '${adb_dnsdir}' could not be created" - fi - fi + [ ! -d "${adb_dnsdir}" ] && mkdir -p "${adb_dnsdir}" + [ "${adb_jail}" = "1" ] && [ ! -d "${adb_jaildir}" ] && mkdir -p "${adb_jaildir}" + [ "${adb_backup}" = "1" ] && [ ! -d "${adb_backupdir}" ] && mkdir -p "${adb_backupdir}" [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ] && printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" - - if [ "${dns_up}" != "true" ]; then - if ! f_dnsup 4; then - f_log "err" "dns backend '${adb_dns}' not running or executable" - fi - fi - - if [ "${adb_backup}" = "1" ] && [ -n "${adb_backupdir}" ] && [ ! -d "${adb_backupdir}" ]; then - if mkdir -p "${adb_backupdir}"; then - f_log "info" "backup directory '${adb_backupdir}' created" - else - f_log "err" "backup directory '${adb_backupdir}' could not be created" - fi - fi - - if [ -n "${adb_jaildir}" ] && [ ! -d "${adb_jaildir}" ]; then - if mkdir -p "${adb_jaildir}"; then - f_log "info" "jail directory '${adb_jaildir}' created" - else - f_log "err" "jail directory '${adb_jaildir}' could not be created" - fi - fi fi - 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}" + + 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_dir: ${adb_backupdir}, jail_dir: ${adb_jaildir}" } # load fetch utility # f_fetch() { - local util utils insecure cnt="0" + local util utils insecure if [ -z "${adb_fetchutil}" ]; then - utils="aria2c curl wget uclient-fetch" + utils="aria2 curl wget-ssl libustream-openssl libustream-wolfssl libustream-mbedtls" for util in ${utils}; do - if { [ "${util}" = "uclient-fetch" ] && printf "%s" "${adb_packages}" | "${adb_grepcmd}" -q "libustream-"; } || - { [ "${util}" = "wget" ] && printf "%s" "${adb_packages}" | "${adb_grepcmd}" -q "wget-ssl"; } || - [ "${util}" = "curl" ] || [ "${util}" = "aria2c" ]; then + if printf "%s" "${adb_packages}" | "${adb_jsoncmd}" -ql1 -e "@.packages.${util}" >/dev/null 2>&1; then + case "${util}" in + "aria2") + util="aria2c" + ;; + "wget-ssl") + util="wget" + ;; + "libustream-openssl" | "libustream-wolfssl" | "libustream-mbedtls") + util="uclient-fetch" + ;; + esac + if [ -x "$(command -v "${util}")" ]; then adb_fetchutil="${util}" uci_set adblock global adb_fetchutil "${util}" @@ -405,9 +385,12 @@ f_fetch() { fi fi done - elif [ ! -x "$(command -v "${adb_fetchutil}")" ]; then - unset adb_fetchutil fi + + if [ ! -x "$(command -v "${adb_fetchutil}")" ]; then + f_log "err" "download utility with SSL support not found, please set 'adb_fetchutil' manually" + fi + case "${adb_fetchutil}" in "aria2c") [ "${adb_fetchinsecure}" = "1" ] && insecure="--check-certificate=false" @@ -426,11 +409,9 @@ f_fetch() { adb_fetchparm="${adb_fetchparm:-"${insecure} --no-cache --no-cookies --max-redirect=0 --timeout=20 -O"}" ;; esac - if [ -n "${adb_fetchutil}" ] && [ -n "${adb_fetchparm}" ]; then - adb_fetchutil="$(command -v "${adb_fetchutil}")" - else - 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'" - fi + + adb_fetchutil="$(command -v "${adb_fetchutil}")" + f_log "debug" "f_fetch ::: fetch_util: ${adb_fetchutil:-"-"}, fetch_parm: ${adb_fetchparm:-"-"}" } @@ -446,32 +427,27 @@ f_temp() { 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" fi [ ! -s "${adb_pidfile}" ] && printf "%s" "${$}" >"${adb_pidfile}" - f_log "debug" "f_temp ::: tmp_base: ${adb_tmpbase:-"-"}, tmp_dir: ${adb_tmpdir:-"-"}, sort_options: ${adb_srtopts}, pid_file: ${adb_pidfile:-"-"}" } # remove temporary files and directories # f_rmtemp() { - [ -d "${adb_tmpdir}" ] && rm -rf "${adb_tmpdir}" - rm -f "${adb_srcfile}" + rm -rf "${adb_tmpdir}" "${adb_srcfile}" : >"${adb_pidfile}" - f_log "debug" "f_rmtemp ::: tmp_dir: ${adb_tmpdir:-"-"}, src_file: ${adb_srcfile:-"-"}, pid_file: ${adb_pidfile:-"-"}" } # remove dns related files # f_rmdns() { - local status - - status="$("${adb_ubuscmd}" -S call service list '{"name":"adblock"}' 2>/dev/null | "${adb_jsoncmd}" -l1 -e '@["adblock"].instances.*.running' 2>/dev/null)" - if [ "${adb_dns}" = "raw" ] || { [ -n "${adb_dns}" ] && [ -n "${status}" ]; }; then + if "${adb_ubuscmd}" -S call service list '{"name":"adblock"}' | "${adb_jsoncmd}" -ql1 -e '@["adblock"].instances.*.running' >/dev/null; then : >"${adb_rtfile}" - [ "${adb_backup}" = "1" ] && rm -f "${adb_backupdir}/${adb_dnsprefix}".*.gz + if [ "${adb_backup}" = "0" ] || [ "${adb_action}" = "stop" ]; then + rm -f "${adb_backupdir}/${adb_dnsprefix}".*.gz + fi printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" f_dnsup 4 fi f_rmtemp - 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:-"-"}" } # commit uci changes @@ -502,43 +478,27 @@ f_uci() { # get list counter # f_count() { - local file mode="${1}" name="${2}" + local mode="${1}" file="${2}" var="${3}" adb_cnt="0" - case "${mode}" in - "iplist") - [ -s "${adb_tmpdir}/tmp.add.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.add.${name}")" - ;; - "blacklist") - [ -s "${adb_tmpfile}.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpfile}.${name}")" - ;; - "whitelist") - [ -s "${adb_tmpdir}/tmp.raw.${name}" ] && { adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.raw.${name}")"; : >"${adb_tmpdir}/tmp.raw.${name}"; } - ;; - "safesearch") - [ -s "${adb_tmpdir}/tmp.safesearch.${name}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.safesearch.${name}")" - ;; - "merge") - [ -s "${adb_tmpdir}/${adb_dnsfile}" ] && adb_cnt="$(wc -l 2>/dev/null <"${adb_tmpdir}/${adb_dnsfile}")" - ;; - "download" | "backup" | "restore") - [ -s "${src_tmpfile}" ] && adb_cnt="$(wc -l 2>/dev/null <"${src_tmpfile}")" - ;; - "final") - if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]; then - adb_cnt="$(wc -l 2>/dev/null <"${adb_dnsdir}/${adb_dnsfile}")" + if [ -s "${file}" ]; then + adb_cnt="$("${adb_wccmd}" -l 2>/dev/null <"${file}")" + if [ -n "${var}" ]; then + printf "%s" "${adb_cnt}" + else + if [ "${mode}" = "final" ]; then if [ -s "${adb_tmpdir}/tmp.add.whitelist" ]; then - adb_cnt="$((adb_cnt - $(wc -l 2>/dev/null <"${adb_tmpdir}/tmp.add.whitelist")))" + adb_cnt="$((adb_cnt - $("${adb_wccmd}" -l 2>/dev/null <"${adb_tmpdir}/tmp.add.whitelist")))" fi for file in "${adb_tmpdir}/tmp.safesearch".*; do if [ -r "${file}" ]; then - adb_cnt="$((adb_cnt - $(wc -l 2>/dev/null <"${file}")))" + adb_cnt="$((adb_cnt - $("${adb_wccmd}" -l 2>/dev/null <"${file}")))" fi done [ -n "${adb_dnsheader}" ] && adb_cnt="$(((adb_cnt - $(printf "%b" "${adb_dnsheader}" | "${adb_grepcmd}" -c "^")) / 2))" fi - ;; - esac + fi + fi } # set external config options @@ -603,7 +563,7 @@ f_extconf() { f_dnsup() { local rset dns_service dns_up dns_pid restart_rc cnt="0" out_rc="4" in_rc="${1:-0}" - if [ "${adb_dns}" = "raw" ]; then + if [ "${adb_dns}" = "raw" ] || [ -z "${adb_dns}" ]; then out_rc="0" else if [ "${in_rc}" = "0" ] && [ "${adb_dnsflush}" = "0" ]; then @@ -665,6 +625,7 @@ f_dnsup() { fi fi fi + 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}" return "${out_rc}" } @@ -672,11 +633,12 @@ f_dnsup() { # backup/restore/remove blocklists # f_list() { - 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" + local hold file rset item array safe_url safe_ips safe_cname safe_domains ip out_rc file_name mode="${1}" src_name="${2:-"${src_name}"}" in_rc="${src_rc:-0}" cnt ffiles="-maxdepth 1 -name ${adb_dnsprefix}.*.gz" case "${mode}" in "iplist") src_name="${mode}" + file_name="${adb_tmpdir}/tmp.add.${src_name}" if [ "${adb_dns}" = "named" ]; then 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\"}" if [ -n "${adb_allowip}" ]; then @@ -684,7 +646,7 @@ f_list() { for ip in ${adb_allowip}; do printf "%s" "${ip}" | "${adb_awkcmd}" "${rset}" >>"${adb_tmpdir}/tmp.raw.${src_name}" done - eval "${adb_dnsallowip}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.add.${src_name}" + eval "${adb_dnsallowip}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${file_name}" out_rc="${?}" fi if [ -n "${adb_denyip}" ] && { [ -z "${out_rc}" ] || [ "${out_rc}" = "0" ]; }; then @@ -692,7 +654,7 @@ f_list() { for ip in ${adb_denyip}; do printf "%s" "${ip}" | "${adb_awkcmd}" "${rset}" >>"${adb_tmpdir}/tmp.raw.${src_name}" done - eval "${adb_dnsdenyip}" "${adb_tmpdir}/tmp.raw.${src_name}" >>"${adb_tmpdir}/tmp.add.${src_name}" + eval "${adb_dnsdenyip}" "${adb_tmpdir}/tmp.raw.${src_name}" >>"${file_name}" out_rc="${?}" fi : >"${adb_tmpdir}/tmp.raw.${src_name}" @@ -701,21 +663,22 @@ f_list() { "blacklist" | "whitelist") src_name="${mode}" if [ "${src_name}" = "blacklist" ] && [ -f "${adb_blacklist}" ]; then + file_name="${adb_tmpfile}.${src_name}" rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}" "${adb_awkcmd}" "${rset}" "${adb_blacklist}" >"${adb_tmpdir}/tmp.raw.${src_name}" if [ -s "${adb_whitelist}" ]; then "${adb_awkcmd}" 'NR==FNR{member[$1];next}!($1 in member)' "${adb_whitelist}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.deduplicate.${src_name}" else - cat "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.deduplicate.${src_name}" + "${adb_catcmd}" "${adb_tmpdir}/tmp.raw.${src_name}" >"${adb_tmpdir}/tmp.deduplicate.${src_name}" fi "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "${adb_tmpdir}/tmp.deduplicate.${src_name}" >"${adb_tmpdir}/tmp.raw.${src_name}" - "${adb_sortcmd}" ${adb_srtopts} -u "${adb_tmpdir}/tmp.raw.${src_name}" 2>/dev/null >"${adb_tmpfile}.${src_name}" + "${adb_sortcmd}" ${adb_srtopts} -u "${adb_tmpdir}/tmp.raw.${src_name}" 2>/dev/null >"${file_name}" out_rc="${?}" - : > "${adb_tmpdir}/tmp.raw.${src_name}" elif [ "${src_name}" = "whitelist" ] && [ -f "${adb_whitelist}" ]; then + file_name="${adb_tmpdir}/tmp.raw.${src_name}" rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}" - printf "%s\n" "${adb_lookupdomain}" | "${adb_awkcmd}" "${rset}" >"${adb_tmpdir}/tmp.raw.${src_name}" - "${adb_awkcmd}" "${rset}" "${adb_whitelist}" >>"${adb_tmpdir}/tmp.raw.${src_name}" + printf "%s\n" "${adb_lookupdomain}" | "${adb_awkcmd}" "${rset}" >"${file_name}" + "${adb_awkcmd}" "${rset}" "${adb_whitelist}" >>"${file_name}" out_rc="${?}" if [ "${out_rc}" = "0" ]; then rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}" @@ -727,7 +690,7 @@ f_list() { if [ "${out_rc}" = "0" ] && [ "${adb_jail}" = "1" ] && [ "${adb_dnsstop}" != "0" ]; then rm -f "${adb_jaildir}/${adb_dnsjail}" [ -n "${adb_dnsheader}" ] && printf "%b" "${adb_dnsheader}" >>"${adb_jaildir}/${adb_dnsjail}" - cat "${adb_tmpdir}/tmp.add.${src_name}" >>"${adb_jaildir}/${adb_dnsjail}" + "${adb_catcmd}" "${adb_tmpdir}/tmp.add.${src_name}" >>"${adb_jaildir}/${adb_dnsjail}" printf "%b\n" "${adb_dnsstop}" >>"${adb_jaildir}/${adb_dnsjail}" fi fi @@ -735,6 +698,7 @@ f_list() { fi ;; "safesearch") + file_name="${adb_tmpdir}/tmp.safesearch.${src_name}" case "${src_name}" in "google") rset="/^\\.([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{printf \"%s\n%s\n\",tolower(\"www\"\$1),tolower(substr(\$1,2,length(\$1)))}" @@ -742,13 +706,13 @@ f_list() { safe_cname="forcesafesearch.google.com" safe_domains="${adb_tmpdir}/tmp.load.safesearch.${src_name}" if [ "${adb_backup}" = "1" ] && [ -s "${adb_backupdir}/safesearch.${src_name}.gz" ]; then - zcat "${adb_backupdir}/safesearch.${src_name}.gz" >"${safe_domains}" + "${adb_zcatcmd}" "${adb_backupdir}/safesearch.${src_name}.gz" >"${safe_domains}" out_rc="${?}" else "${adb_fetchutil}" ${adb_fetchparm} "${safe_domains}" "${safe_url}" 2>/dev/null out_rc="${?}" if [ "${adb_backup}" = "1" ] && [ "${out_rc}" = "0" ]; then - gzip -cf "${safe_domains}" >"${adb_backupdir}/safesearch.${src_name}.gz" + "${adb_gzipcmd}" -cf "${safe_domains}" >"${adb_backupdir}/safesearch.${src_name}.gz" out_rc="${?}" fi fi @@ -811,11 +775,11 @@ f_list() { ;; esac if [ "${out_rc}" = "0" ] && [ -s "${adb_tmpdir}/tmp.raw.safesearch.${src_name}" ]; then - : >"${adb_tmpdir}/tmp.safesearch.${src_name}" + : >"${file_name}" [ "${adb_dns}" = "named" ] && array="${safe_cname}" || array="${safe_ips}" for item in ${array}; do - if ! eval "${adb_dnssafesearch}" "${adb_tmpdir}/tmp.raw.safesearch.${src_name}" >>"${adb_tmpdir}/tmp.safesearch.${src_name}"; then - : >"${adb_tmpdir}/tmp.safesearch.${src_name}" + if ! eval "${adb_dnssafesearch}" "${adb_tmpdir}/tmp.raw.safesearch.${src_name}" >>"${file_name}"; then + : >"${file_name}" break fi done @@ -823,15 +787,18 @@ f_list() { : >"${adb_tmpdir}/tmp.raw.safesearch.${src_name}" fi ;; + "download") + file_name="${src_tmpfile}" + ;; "backup") - ( - gzip -cf "${src_tmpfile}" >"${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" - out_rc="${?}" - ) & + file_name="${src_tmpfile}" + "${adb_gzipcmd}" -cf "${src_tmpfile}" >"${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" + out_rc="${?}" ;; "restore") + file_name="${src_tmpfile}" if [ -n "${src_name}" ] && [ -s "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]; then - zcat "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" >"${src_tmpfile}" + "${adb_zcatcmd}" "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" >"${src_tmpfile}" out_rc="${?}" elif [ -z "${src_name}" ]; then cnt="1" @@ -839,7 +806,7 @@ f_list() { if [ -r "${file}" ]; then name="${file##*/}" name="${name%.*}" - zcat "${file}" >"${adb_tmpfile}.${name}" & + "${adb_zcatcmd}" "${file}" >"${adb_tmpfile}.${name}" & hold="$((cnt % adb_cores))" if [ "${hold}" = "0" ]; then wait @@ -862,34 +829,40 @@ f_list() { adb_sources="${adb_sources/${src_name}}" ;; "merge") + src_name="" + file_name="${adb_tmpdir}/${adb_dnsfile}" if [ "${adb_backup}" = "1" ]; then - for src_name in ${adb_sources}; do - ffiles="${ffiles} -a ! -name ${adb_dnsprefix}.${src_name}.gz" + for file in ${adb_sources}; do + ffiles="${ffiles} -a ! -name ${adb_dnsprefix}.${file}.gz" done if [ "${adb_safesearch}" = "1" ] && [ "${adb_dnssafesearch}" != "0" ]; then ffiles="${ffiles} -a ! -name safesearch.google.gz" fi find "${adb_backupdir}" ${ffiles} -print0 2>/dev/null | xargs -0 rm 2>/dev/null fi - unset src_name - "${adb_sortcmd}" ${adb_srtopts} -mu "${adb_tmpfile}".* 2>/dev/null >"${adb_tmpdir}/${adb_dnsfile}" + "${adb_sortcmd}" ${adb_srtopts} -mu "${adb_tmpfile}".* 2>/dev/null >"${file_name}" out_rc="${?}" rm -f "${adb_tmpfile}".* ;; "final") - unset src_name - { [ -n "${adb_dnsheader}" ] && printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}"; } || : >"${adb_dnsdir}/${adb_dnsfile}" - [ -s "${adb_tmpdir}/tmp.add.iplist" ] && cat "${adb_tmpdir}/tmp.add.iplist" >>"${adb_dnsdir}/${adb_dnsfile}" - [ -s "${adb_tmpdir}/tmp.add.whitelist" ] && cat "${adb_tmpdir}/tmp.add.whitelist" >>"${adb_dnsdir}/${adb_dnsfile}" - for file in "${adb_tmpdir}/tmp.safesearch".*; do - [ -r "${file}" ] && cat "${file}" >>"${adb_dnsdir}/${adb_dnsfile}" - done - { [ "${adb_dnsdeny}" != "0" ] && eval "${adb_dnsdeny}" "${adb_tmpdir}/${adb_dnsfile}" >>"${adb_dnsdir}/${adb_dnsfile}"; } || mv "${adb_tmpdir}/${adb_dnsfile}" "${adb_dnsdir}/${adb_dnsfile}" + src_name="" + file_name="${adb_dnsdir}/${adb_dnsfile}" + : >"${file_name}" + [ -n "${adb_dnsheader}" ] && printf "%b" "${adb_dnsheader}" >>"${file_name}" + [ -s "${adb_tmpdir}/tmp.add.iplist" ] && "${adb_catcmd}" "${adb_tmpdir}/tmp.add.iplist" >>"${file_name}" + [ -s "${adb_tmpdir}/tmp.add.whitelist" ] && "${adb_catcmd}" "${adb_tmpdir}/tmp.add.whitelist" >>"${file_name}" + "${adb_catcmd}" "${adb_tmpdir}/tmp.safesearch".* 2>/dev/null >>"${file_name}" + if [ "${adb_dnsdeny}" != "0" ]; then + eval "${adb_dnsdeny}" "${adb_tmpdir}/${adb_dnsfile}" >>"${file_name}" + else + "${adb_catcmd}" "${adb_tmpdir}/${adb_dnsfile}" >>"${file_name}" + fi out_rc="${?}" ;; esac - f_count "${mode}" "${src_name}" + [ "${adb_debug}" = "1" ] || [ "${mode}" = "final" ] && f_count "${mode}" "${file_name}" out_rc="${out_rc:-"${in_rc}"}" + f_log "debug" "f_list ::: name: ${src_name:-"-"}, mode: ${mode}, cnt: ${adb_cnt}, in_rc: ${in_rc}, out_rc: ${out_rc}" return "${out_rc}" } @@ -901,16 +874,17 @@ f_tld() { if "${adb_awkcmd}" '{if(NR==1){tld=$NF};while(getline){if(index($NF,tld".")==0){print tld;tld=$NF}}print tld}' "${source}" | "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${temp_tld}"; then - cnt_tld="$(wc -l 2>/dev/null <"${temp_tld}")" + [ "${adb_debug}" = "1" ] && cnt_tld="$(f_count tld "${temp_tld}" "var")" if [ -s "${adb_tmpdir}/tmp.rem.whitelist" ]; then "${adb_awkcmd}" 'NR==FNR{del[$0];next};!($0 in del)' "${adb_tmpdir}/tmp.rem.whitelist" "${temp_tld}" >"${source}" - cnt_rem="$(wc -l 2>/dev/null <"${source}")" + [ "${adb_debug}" = "1" ] && cnt_rem="$(f_count tld "${source}" "var")" else - mv -f "${temp_tld}" "${source}" + "${adb_catcmd}" "${temp_tld}" >"${source}" fi fi : > "${temp_tld}" - f_log "debug" "f_tld ::: source: ${source}, cnt: ${adb_cnt:-"-"}, cnt_tld: ${cnt_tld:-"-"}, cnt_rem: ${cnt_rem:-"-"}" + + f_log "debug" "f_tld ::: name: -, cnt: ${adb_cnt:-"-"}, cnt_tld: ${cnt_tld:-"-"}, cnt_rem: ${cnt_rem:-"-"}" } # suspend/resume adblock processing @@ -997,7 +971,7 @@ f_query() { for file in "${adb_backupdir}/${adb_dnsprefix}".*.gz "${adb_blacklist}" "${adb_whitelist}"; do suffix="${file##*.}" if [ "${suffix}" = "gz" ]; then - zcat "${file}" 2>/dev/null | + "${adb_zcatcmd}" "${file}" 2>/dev/null | "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' | "${adb_awkcmd}" -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}" else "${adb_awkcmd}" -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}" @@ -1019,18 +993,18 @@ f_query() { # update runtime information # f_jsnup() { - local object sources runtime utils bg_pid status="${1:-"enabled"}" + local object sources runtime utils mem_free mem_max status="${1:-"enabled"}" - adb_memory="$("${adb_awkcmd}" '/^MemTotal|^MemFree|^MemAvailable/{ORS="/"; print int($2/1000)}' "/proc/meminfo" 2>/dev/null | - "${adb_awkcmd}" '{print substr($0,1,length($0)-1)}')" + mem_free="$("${adb_awkcmd}" '/^MemAvailable/{printf "%s",int($2/1024)}' "/proc/meminfo" 2>/dev/null)" + mem_max="$("${adb_awkcmd}" '/^VmHWM/{printf "%s",int($2)}' /proc/${$}/status 2>/dev/null)" case "${status}" in "enabled" | "error") adb_endtime="$(date "+%s")" if [ "$(((adb_endtime - adb_starttime) / 60))" -lt 60 ]; then - runtime="${adb_action}, $(((adb_endtime - adb_starttime) / 60))m $(((adb_endtime - adb_starttime) % 60))s, ${adb_memory:-0}, $(date -Iseconds)" + runtime="${adb_action}, $(((adb_endtime - adb_starttime) / 60))m $(((adb_endtime - adb_starttime) % 60))s, ${mem_free:-0} MB available, ${mem_max:-0} KB max. used, $(date -Iseconds)" else - runtime="${adb_action}, n/a, ${adb_memory:-0}, $(date -Iseconds)" + runtime="${adb_action}, n/a, ${mem_free:-0} MB available, ${mem_max:-0} KB max. used, $(date -Iseconds)" fi [ "${status}" = "error" ] && adb_cnt="0" ;; @@ -1069,7 +1043,7 @@ f_jsnup() { json_add_string "run_utils" "${utils:-"-"}" json_add_string "run_ifaces" "trigger: ${adb_trigger:-"-"}, report: ${adb_repiface:-"-"}" json_add_string "run_directories" "base: ${adb_tmpbase}, backup: ${adb_backupdir}, report: ${adb_reportdir}, jail: ${adb_jaildir}" - 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})" + json_add_string "run_flags" "backup: $(f_char ${adb_backup}), tld: $(f_char ${adb_tld}), force: $(f_char ${adb_forcedns}), flush: $(f_char ${adb_dnsflush}), search: $(f_char ${adb_safesearch}), report: $(f_char ${adb_report}), mail: $(f_char ${adb_mail}), jail: $(f_char ${adb_jail})" json_add_string "last_run" "${runtime:-"-"}" json_add_string "system" "${adb_sysver}" json_dump >"${adb_rtfile}" @@ -1078,7 +1052,6 @@ f_jsnup() { [ "${status}" = "enabled" ] && [ "${adb_cnt}" -le "${adb_mailcnt}" ]; then "${adb_mailservice}" >/dev/null 2>&1 fi - 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:-"-"}" } # write to syslog @@ -1102,8 +1075,6 @@ f_log() { f_main() { 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 - f_log "debug" "f_main ::: memory: ${adb_memory:-0}, cores: ${adb_cores}, safe_search: ${adb_safesearch}, force_dns: ${adb_forcedns}, awk: ${adb_awkcmd}" - # white- and blacklist preparation # for entry in ${adb_locallist}; do @@ -1155,7 +1126,6 @@ f_main() { json_select .. src_tmpcat="${adb_tmpload}.${src_name}.cat" src_tmpload="${adb_tmpload}.${src_name}.load" - src_tmpsort="${adb_tmpload}.${src_name}.sort" src_tmpfile="${adb_tmpfile}.${src_name}" src_rc=4 @@ -1166,9 +1136,9 @@ f_main() { continue fi - # backup mode + # backup/restore mode # - if [ "${adb_backup}" = "1" ] && { [ "${adb_action}" = "start" ] || [ "${adb_action}" = "resume" ]; }; then + if [ "${adb_backup}" = "1" ] && { [ "${adb_action}" = "start" ] || [ "${adb_action}" = "restart" ] || [ "${adb_action}" = "resume" ]; }; then if f_list restore && [ -s "${src_tmpfile}" ]; then continue fi @@ -1200,11 +1170,10 @@ f_main() { f_log "info" "download of '${src_name}' failed, url: ${src_url}, rule: ${src_rset:-"-"}, categories: ${src_cat:-"-"}, rc: ${src_rc}, log: ${src_log:-"-"}" fi if [ "${src_rc}" = "0" ] && [ -s "${src_tmpload}" ]; then - "${adb_awkcmd}" "${src_rset}" "${src_tmpload}" | "${adb_sedcmd}" "s/\r//g" | "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${src_tmpsort}" - : >"${src_tmpload}" - "${adb_sortcmd}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null >"${src_tmpfile}" + "${adb_awkcmd}" "${src_rset}" "${src_tmpload}" | "${adb_sedcmd}" "s/\r//g" | + "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' | + "${adb_sortcmd}" ${adb_srtopts} -u 2>/dev/null >"${src_tmpfile}" src_rc="${?}" - : >"${src_tmpsort}" if [ "${src_rc}" = "0" ] && [ -s "${src_tmpfile}" ]; then f_list download [ "${adb_backup}" = "1" ] && f_list backup @@ -1220,24 +1189,24 @@ f_main() { ) & fi else - if [ "${src_name}" = "1hosts" ] && [ -n "${adb_hst_sources}" ]; then - src_cat="${adb_hst_sources}" - elif [ "${src_name}" = "hagezi" ] && [ -n "${adb_hag_sources}" ]; then - src_cat="${adb_hag_sources}" - elif [ "${src_name}" = "stevenblack" ] && [ -n "${adb_stb_sources}" ]; then - src_cat="${adb_stb_sources}" - elif { [ "${src_name}" = "1hosts" ] && [ -z "${adb_hst_sources}" ]; } || - { [ "${src_name}" = "hagezi" ] && [ -z "${adb_hag_sources}" ]; } || - { [ "${src_name}" = "stevenblack" ] && [ -z "${adb_stb_sources}" ]; }; then - continue - fi + case "${src_name}" in + "1hosts") + [ -n "${adb_hst_sources}" ] && src_cat="${adb_hst_sources}" || continue + ;; + "hagezi") + [ -n "${adb_hag_sources}" ] && src_cat="${adb_hag_sources}" || continue + ;; + "stevenblack") + [ -n "${adb_stb_sources}" ] && src_cat="${adb_stb_sources}" || continue + ;; + esac ( for suffix in ${src_cat:-${src_url}}; do if [ "${src_url}" != "${suffix}" ]; then src_log="$("${adb_fetchutil}" ${adb_fetchparm} "${src_tmpcat}" "${src_url}${suffix}" 2>&1)" src_rc="${?}" if [ "${src_rc}" = "0" ] && [ -s "${src_tmpcat}" ]; then - cat "${src_tmpcat}" >>"${src_tmpload}" + "${adb_catcmd}" "${src_tmpcat}" >>"${src_tmpload}" : >"${src_tmpcat}" fi else @@ -1247,11 +1216,9 @@ f_main() { done if [ "${src_rc}" = "0" ] && [ -s "${src_tmpload}" ]; then "${adb_awkcmd}" "${src_rset}" "${src_tmpload}" | "${adb_sedcmd}" "s/\r//g" | - "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' >"${src_tmpsort}" - : >"${src_tmpload}" - "${adb_sortcmd}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null >"${src_tmpfile}" + "${adb_awkcmd}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' | + "${adb_sortcmd}" ${adb_srtopts} -u >"${src_tmpfile}" src_rc="${?}" - : >"${src_tmpsort}" if [ "${src_rc}" = "0" ] && [ -s "${src_tmpfile}" ]; then f_list download [ "${adb_backup}" = "1" ] && f_list backup @@ -1276,7 +1243,7 @@ f_main() { # tld compression and dns restart # if f_list merge && [ -s "${adb_tmpdir}/${adb_dnsfile}" ]; then - f_tld "${adb_tmpdir}/${adb_dnsfile}" + [ "${adb_tld}" = "1" ] && f_tld "${adb_tmpdir}/${adb_dnsfile}" f_list final else printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" @@ -1348,7 +1315,7 @@ f_report() { if [ -s "${report_srt}" ]; then start="$("${adb_awkcmd}" 'END{printf "%s_%s",$1,$2}' "${report_srt}")" end="$("${adb_awkcmd}" 'NR==1{printf "%s_%s",$1,$2}' "${report_srt}")" - total="$(wc -l <"${report_srt}")" + total="$(f_count tld "${report_srt}" "var")" blocked="$("${adb_awkcmd}" '{if($5=="NX")cnt++}END{printf "%s",cnt}' "${report_srt}")" percent="$("${adb_awkcmd}" -v t="${total}" -v b="${blocked}" 'BEGIN{printf "%.2f%s",b/t*100,"%"}')" : >"${report_jsn}" @@ -1440,7 +1407,7 @@ f_report() { fi json_select ".." done - content="$(cat "${report_txt}" 2>/dev/null)" + content="$("${adb_catcmd}" "${report_txt}" 2>/dev/null)" : >"${report_txt}" fi @@ -1449,11 +1416,10 @@ f_report() { if [ "${action}" = "cli" ]; then printf "%s\n" "${content}" elif [ "${action}" = "json" ]; then - cat "${report_jsn}" + "${adb_catcmd}" "${report_jsn}" elif [ "${action}" = "mail" ] && [ "${adb_mail}" = "1" ] && [ -x "${adb_mailservice}" ]; then "${adb_mailservice}" "${content}" >/dev/null 2>&1 fi - f_log "debug" "f_report ::: action: ${action}, top_count: ${top_count}, res_count: ${res_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}" } # source required system libraries @@ -1468,9 +1434,13 @@ fi # reference required system utilities # +adb_wccmd="$(f_cmd wc)" +adb_catcmd="$(f_cmd cat)" +adb_zcatcmd="$(f_cmd zcat)" adb_awkcmd="$(f_cmd gawk awk)" adb_sortcmd="$(f_cmd sort)" adb_grepcmd="$(f_cmd grep)" +adb_gzipcmd="$(f_cmd gzip)" adb_pgrepcmd="$(f_cmd pgrep)" adb_sedcmd="$(f_cmd sed)" adb_jsoncmd="$(f_cmd jsonfilter)" -- 2.30.2