From bf87a6dcecc75b765b14d49e5c89b9106218f4a0 Mon Sep 17 00:00:00 2001 From: Dirk Brenken Date: Mon, 3 Mar 2025 21:50:01 +0100 Subject: [PATCH] adblock: release 4.2.7-1 * optimized uci config processing (list options) * no longer set a hardcoded confdir in dhcp/dnsmasq config * rework suspend/resume handling * clean up download queue handling * various small improvements & fixes Signed-off-by: Dirk Brenken --- net/adblock/Makefile | 2 +- net/adblock/files/adblock.init | 2 +- net/adblock/files/adblock.sh | 174 ++++++++++++++------------------- 3 files changed, 75 insertions(+), 103 deletions(-) diff --git a/net/adblock/Makefile b/net/adblock/Makefile index c27fef94c1..ac839a62a2 100644 --- a/net/adblock/Makefile +++ b/net/adblock/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=adblock -PKG_VERSION:=4.2.6 +PKG_VERSION:=4.2.7 PKG_RELEASE:=1 PKG_LICENSE:=GPL-3.0-or-later PKG_MAINTAINER:=Dirk Brenken diff --git a/net/adblock/files/adblock.init b/net/adblock/files/adblock.init index d1b699c04c..ebc72f2cc9 100755 --- a/net/adblock/files/adblock.init +++ b/net/adblock/files/adblock.init @@ -37,7 +37,7 @@ start_service() { procd_set_param pidfile "${adb_pidfile}" procd_set_param nice "$(uci_get adblock global adb_nice "0")" procd_set_param stdout 0 - procd_set_param stderr 0 + procd_set_param stderr 1 procd_close_instance fi } diff --git a/net/adblock/files/adblock.sh b/net/adblock/files/adblock.sh index 4ae538f5c8..f93a2a50e3 100755 --- a/net/adblock/files/adblock.sh +++ b/net/adblock/files/adblock.sh @@ -164,11 +164,6 @@ f_env() { f_extconf f_temp - 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 "${adb_zcatcmd}" "${adb_srcarc}" >"${adb_srcfile}" @@ -187,49 +182,17 @@ f_env() { # load adblock config # f_conf() { - 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() { - local option="${1}" - local value="${2}" + local option="${1}" value="${2//\"/\\\"}" + eval "${option}=\"${value}\"" } list_cb() { - local option="${1}" - local value="${2}" - 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 + local append option="${1}" value="${2//\"/\\\"}" + + eval "append=\"\${${option}}\"" + eval "${option}=\"${append}${value} \"" } } config_load adblock @@ -251,7 +214,7 @@ f_char() { # load dns backend config # f_dns() { - local util utils + local util utils dns_section dns_info if [ "${adb_action}" = "start" ] && [ -z "${adb_trigger}" ]; then sleep ${adb_triggerdelay} @@ -295,12 +258,22 @@ f_dns() { adb_dnscachecmd="-" adb_dnsinstance="${adb_dnsinstance:-"0"}" adb_dnsuser="${adb_dnsuser:-"dnsmasq"}" - adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}" + adb_dnsdir="${adb_dnsdir:-""}" 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=/#/"}" + if [ -z "${adb_dnsdir}" ]; then + dns_section="$("${adb_ubuscmd}" -S call uci get "{\"config\":\"dhcp\", \"section\":\"@dnsmasq[${adb_dnsinstance}]\", \"type\":\"dnsmasq\"}" 2>/dev/null)" + dns_info="$(printf "%s" "${dns_section}" | "${adb_jsoncmd}" -l1 -e '@.values["confdir"]')" + if [ -n "${dns_info}" ]; then + adb_dnsdir="${dns_info}" + else + dns_info="$(printf "%s" "${dns_section}" | "${adb_jsoncmd}" -l1 -e '@.values[".name"]')" + [ -n "${dns_info}" ] && adb_dnsdir="/tmp/dnsmasq.${dns_info}.d" + fi + fi ;; "unbound") adb_dnscachecmd="$(command -v unbound-control || printf "%s" "-")" @@ -365,7 +338,12 @@ f_dns() { [ ! -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 [ "${adb_dnsflush}" = "1" ] || [ "${mem_free}" -lt "64" ]; then + printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" + f_dnsup + elif [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ]; then + printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" + 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_dir: ${adb_backupdir}, jail_dir: ${adb_jaildir}" @@ -449,6 +427,9 @@ f_rmtemp() { if [ "${adb_backup}" = "0" ]; then rm -f "${adb_backupdir}/${adb_dnsprefix}".*.gz fi + if [ "${adb_action}" != "suspend" ]; then + rm -f "${adb_backupdir}/${adb_dnsfile}.gz" + fi } # remove dns related files @@ -475,7 +456,7 @@ f_uci() { ;; "resolver") printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" - f_count + adb_cnt="0" f_jsnup "running" "/etc/init.d/${adb_dns}" reload >/dev/null 2>&1 ;; @@ -515,13 +496,6 @@ f_extconf() { local config config_option section zone port fwcfg case "${adb_dns}" in - "dnsmasq") - config="dhcp" - config_option="$(uci_get ${config} "@dnsmasq[${adb_dnsinstance}]" confdir | "${adb_grepcmd}" -Fo "${adb_dnsdir}")" - if [ "${adb_enabled}" = "1" ] && [ -z "${config_option}" ]; then - uci_set dhcp "@dnsmasq[${adb_dnsinstance}]" confdir "${adb_dnsdir}" 2>/dev/null - fi - ;; "kresd") config="resolver" config_option="$(uci_get ${config} kresd rpz_file | "${adb_grepcmd}" -Fo "${adb_dnsdir}/${adb_dnsfile}")" @@ -543,20 +517,20 @@ f_extconf() { esac f_uci "${config}" - config="firewall" - fwcfg="$(uci -qNX show "${config}" | "${adb_awkcmd}" 'BEGIN{FS="[.=]"};/adblock_/{if(zone==$2){next}else{ORS=" ";zone=$2;print zone}}')" + fwcfg="$(uci -qNX show "firewall" | "${adb_awkcmd}" 'BEGIN{FS="[.=]"};/adblock_/{if(zone==$2){next}else{ORS=" ";zone=$2;print zone}}')" if [ "${adb_enabled}" = "1" ] && [ "${adb_forcedns}" = "1" ] && /etc/init.d/firewall enabled; then for zone in ${adb_zonelist}; do for port in ${adb_portlist}; do - if ! printf "%s" "${fwcfg}" | "${adb_grepcmd}" -q "adblock_${zone}${port}[ |\$]"; then - if [ "${port}" = "53" ]; then + if ! printf "%s" "${fwcfg}" | "${adb_grepcmd}" -q "adblock_${zone}${port}"; then + config="firewall" + if "${adb_lookupcmd}" "localhost" "127.0.0.1:${port}" >/dev/null 2>&1; then uci -q batch <<-EOC set firewall."adblock_${zone}${port}"="redirect" set firewall."adblock_${zone}${port}".name="Adblock DNS (${zone}, ${port})" set firewall."adblock_${zone}${port}".src="${zone}" set firewall."adblock_${zone}${port}".proto="tcp udp" - set firewall."adblock_${zone}${port}".src_dport="${port}" + set firewall."adblock_${zone}${port}".src_dport="53" set firewall."adblock_${zone}${port}".dest_port="${port}" set firewall."adblock_${zone}${port}".target="DNAT" set firewall."adblock_${zone}${port}".family="any" @@ -580,6 +554,7 @@ f_extconf() { fwcfg="${fwcfg%"${fwcfg##*[![:space:]]}"}" fi if [ "${adb_enabled}" = "0" ] || [ "${adb_forcedns}" = "0" ] || [ -n "${fwcfg}" ]; then + config="firewall" for section in ${fwcfg}; do uci_remove firewall "${section}" done @@ -592,6 +567,7 @@ f_extconf() { f_dnsup() { local rset dns_service dns_up restart_rc cnt="0" out_rc="4" in_rc="${1:-0}" + adb_dnspid="" if [ "${adb_dns}" = "raw" ] || [ -z "${adb_dns}" ]; then out_rc="0" else @@ -626,23 +602,16 @@ f_dnsup() { fi fi if [ "${restart_rc}" = "0" ] && [ "${in_rc}" = "0" ]; then - rset="/^(([[:alnum:]_-]{1,63}\\.)+[[:alnum:]-]+|[[:alnum:]-]+)([[:space:]]|$)/{print tolower(\$1)}" while [ "${cnt}" -le "${adb_dnstimeout}" ]; do - dns_service="$("${adb_ubuscmd}" -S call service list 2>/dev/null)" - dns_up="$(printf "%s" "${dns_service}" | "${adb_jsoncmd}" -l1 -e "@[\"${adb_dns}\"].instances.*.running")" - adb_dnspid="$(printf "%s" "${dns_service}" | "${adb_jsoncmd}" -l1 -e "@[\"${adb_dns}\"].instances.*.pid")" - if [ "${dns_up}" = "true" ] && [ -n "${adb_dnspid}" ] && ! ls "/proc/${adb_dnspid}/fd/${adb_dnsdir}/${adb_dnsfile}" >/dev/null 2>&1; then - if [ -x "${adb_lookupcmd}" ] && [ "${adb_lookupdomain}" != "false" ] && [ -n "$(printf "%s" "${adb_lookupdomain}" | "${adb_awkcmd}" "${rset}")" ]; then - if "${adb_lookupcmd}" "${adb_lookupdomain}" >/dev/null 2>&1; then - out_rc="0" - break - fi - else - sleep "${adb_dnstimeout}" - cnt="${adb_dnstimeout}" + if [ -n "${adb_lookupdomain}" ] && [ "${adb_lookupdomain}" != "false" ]; then + if "${adb_lookupcmd}" "${adb_lookupdomain}" >/dev/null 2>&1; then out_rc="0" break fi + else + sleep "${adb_dnstimeout}" + cnt="${adb_dnstimeout}" + out_rc="0" fi cnt="$((cnt + 1))" sleep 1 @@ -654,8 +623,9 @@ f_dnsup() { fi fi fi + adb_dnspid="$("${adb_ubuscmd}" -S call service list 2>/dev/null | "${adb_jsoncmd}" -l1 -e "@[\"${adb_dns}\"].instances.*.pid")" - 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_pid: ${adb_dnspid}, dns_cnt: ${cnt}, in_rc: ${in_rc}, out_rc: ${out_rc}" + f_log "debug" "f_dnsup ::: dns: ${adb_dns}, cache_cmd: ${adb_dnscachecmd:-"-"}, lookup_domain: ${adb_lookupdomain:-"-"}, restart_rc: ${restart_rc:-"-"}, dns_flush: ${adb_dnsflush}, dns_timeout: ${adb_dnstimeout}, dns_pid: ${adb_dnspid}, dns_cnt: ${cnt}, in_rc: ${in_rc}, out_rc: ${out_rc}" return "${out_rc}" } @@ -773,7 +743,7 @@ f_list() { ;; esac if [ -n "${safe_domains}" ] && [ -n "${safe_cname}" ]; then - if [ -x "${adb_lookupcmd}" ] && [ "${use_cname}" = "0" ]; then + if [ "${use_cname}" = "0" ]; then safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awkcmd}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')" fi if [ -n "${safe_ips}" ] || [ "${use_cname}" = "1" ]; then @@ -896,25 +866,24 @@ f_switch() { json_load_file "${adb_rtfile}" >/dev/null 2>&1 json_select "data" >/dev/null 2>&1 json_get_var status "adblock_status" + f_env if [ "${mode}" = "suspend" ] && [ "${status}" = "enabled" ]; then - f_env - printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" - if [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then - printf "%b" "${adb_dnsheader}" >"${adb_jaildir}/${adb_dnsjail}" - elif [ -f "${adb_dnsdir}/${adb_dnsjail}" ]; then - rm -f "${adb_dnsdir}/${adb_dnsjail}" + if [ -f "${adb_dnsdir}/${adb_dnsfile}" ]; then + "${adb_gzipcmd}" -cf "${adb_dnsdir}/${adb_dnsfile}" >"${adb_backupdir}/${adb_dnsfile}.gz" + printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" + done="true" fi - f_count - done="true" elif [ "${mode}" = "resume" ] && [ "${status}" = "paused" ]; then - f_env - f_main - done="true" + if [ -f "${adb_backupdir}/${adb_dnsfile}.gz" ]; then + "${adb_gzipcmd}" -dcf "${adb_backupdir}/${adb_dnsfile}.gz" >"${adb_dnsdir}/${adb_dnsfile}" + f_count "resume" "${adb_dnsdir}/${adb_dnsfile}" + done="true" + fi fi if [ "${done}" = "true" ]; then - [ "${mode}" = "suspend" ] && f_dnsup + f_dnsup f_jsnup "${mode}" - f_log "info" "${mode} adblock processing" + f_log "info" "${mode} adblock service" fi f_rmtemp } @@ -1013,28 +982,33 @@ f_jsnup() { adb_cnt="$("${adb_awkcmd}" -v cnt="${adb_cnt}" 'BEGIN{res="";pos=0;for(i=length(cnt);i>0;i--){res=substr(cnt,i,1)res;pos++;if(pos==3&&i>1){res=" "res;pos=0;}}; printf"%s",res}')" case "${status}" in - "enabled" | "error") + "enabled") 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, ${mem_free:-0} MB available, ${mem_max:-0} MB max. used, $(date -Iseconds)" else runtime="${adb_action}, n/a, ${mem_free:-0} MB available, ${mem_max:-0} MB max. used, $(date -Iseconds)" fi - [ "${status}" = "error" ] && adb_cnt="0" ;; - "suspend") - status="paused" + "error") + adb_cnt="0" ;; "resume") - status="" + status="enabled" + ;; + "suspend") + adb_cnt="0" + status="paused" ;; esac + json_init if json_load_file "${adb_rtfile}" >/dev/null 2>&1; then utils="download: $(readlink -fn "${adb_fetchutil}"), sort: $(readlink -fn "${adb_sortcmd}"), awk: $(readlink -fn "${adb_awkcmd}")" - [ -z "${adb_cnt}" ] && { json_get_var adb_cnt "blocked_domains"; adb_cnt="${adb_cnt%% *}"; } + [ -z "${adb_cnt}" ] && json_get_var adb_cnt "blocked_domains" [ -z "${runtime}" ] && json_get_var runtime "last_run" fi + if [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then adb_cnt="0" sources="restrictive_jail" @@ -1045,7 +1019,7 @@ f_jsnup() { : >"${adb_rtfile}" json_init json_load_file "${adb_rtfile}" >/dev/null 2>&1 - json_add_string "adblock_status" "${status:-"enabled"}" + json_add_string "adblock_status" "${status}" json_add_string "adblock_version" "${adb_ver}" json_add_string "blocked_domains" "${adb_cnt:-"0"}" json_add_array "active_sources" @@ -1096,10 +1070,9 @@ f_main() { f_list "${entry}" "${entry}" ) & hold="$((cnt % adb_cores))" - [ "${hold}" = "0" ] && wait + [ "${hold}" = "0" ] && wait -n cnt="$((cnt + 1))" done - wait if [ "${adb_dns}" != "raw" ] && [ "${adb_jail}" = "1" ] && [ "${adb_jaildir}" = "${adb_dnsdir}" ]; then printf "%b" "${adb_dnsheader}" >"${adb_dnsdir}/${adb_dnsfile}" @@ -1129,11 +1102,10 @@ f_main() { f_list safesearch "${entry}" ) & hold="$((cnt % adb_cores))" - [ "${hold}" = "0" ] && wait + [ "${hold}" = "0" ] && wait -n cnt="$((cnt + 1))" done fi - wait # main loop # @@ -1255,7 +1227,7 @@ f_main() { ) & fi hold="$((cnt % adb_cores))" - [ "${hold}" = "0" ] && wait + [ "${hold}" = "0" ] && wait -n cnt="$((cnt + 1))" done wait @@ -1321,7 +1293,7 @@ f_report() { fi ) & hold="$((cnt % adb_cores))" - [ "${hold}" = "0" ] && wait + [ "${hold}" = "0" ] && wait -n cnt="$((cnt + 1))" done wait @@ -1466,8 +1438,8 @@ adb_sedcmd="$(f_cmd sed)" adb_jsoncmd="$(f_cmd jsonfilter)" adb_ubuscmd="$(f_cmd ubus)" adb_loggercmd="$(f_cmd logger)" -adb_dumpcmd="$(f_cmd tcpdump optional)" adb_lookupcmd="$(f_cmd nslookup)" +adb_dumpcmd="$(f_cmd tcpdump optional)" adb_mailcmd="$(f_cmd msmtp optional)" adb_stringscmd="$(f_cmd strings optional)" adb_logreadcmd="$(f_cmd logread optional)" -- 2.30.2