diff --git a/net/adblock/Makefile b/net/adblock/Makefile index 56c0d5716..211aad1d0 100644 --- a/net/adblock/Makefile +++ b/net/adblock/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=adblock -PKG_VERSION:=1.2.6 +PKG_VERSION:=1.2.7 PKG_RELEASE:=1 PKG_LICENSE:=GPL-3.0+ PKG_MAINTAINER:=Dirk Brenken diff --git a/net/adblock/files/adblock-helper.sh b/net/adblock/files/adblock-helper.sh index f7841cb8f..89c350594 100644 --- a/net/adblock/files/adblock-helper.sh +++ b/net/adblock/files/adblock-helper.sh @@ -33,10 +33,7 @@ f_envload() # if [ -r "/lib/functions.sh" ] then - if [ -z "$(type -f config_load)" ] - then - . "/lib/functions.sh" - fi + . "/lib/functions.sh" else rc=-10 f_log "system function library not found, please check your installation" @@ -47,10 +44,7 @@ f_envload() # if [ -r "/lib/functions/network.sh" ] then - if [ -z "$(type -f network_get_device)" ] - then - . "/lib/functions/network.sh" - fi + . "/lib/functions/network.sh" else rc=-10 f_log "system network library not found, please check your installation" @@ -108,21 +102,28 @@ f_envload() config_load adblock config_foreach parse_config service config_foreach parse_config source -} -# f_envcheck: check/set environment prerequisites -# -f_envcheck() -{ - local check + # get ip addresses & logical wan devices + # + network_get_ipaddr adb_ipv4 "${adb_lanif}" + network_get_ipaddr6 adb_ipv6 "${adb_lanif}" + network_find_wan adb_wanif4 + network_find_wan6 adb_wanif6 - # check restricted mode + # set restricted mode # if [ "${adb_restricted}" = "1" ] then adb_uci="$(which true)" restricted_ok="true" fi +} + +# f_envcheck: check/set environment prerequisites +# +f_envcheck() +{ + local check # check 'enabled' & 'version' config options # @@ -156,8 +157,6 @@ f_envcheck() # get lan ip addresses # - network_get_ipaddr adb_ipv4 "${adb_lanif}" - network_get_ipaddr6 adb_ipv6 "${adb_lanif}" if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ] then rc=-1 @@ -170,8 +169,6 @@ f_envcheck() # check logical update interfaces (with default route) # - network_find_wan adb_wanif4 - network_find_wan6 adb_wanif6 if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ] then adb_wanif4="${adb_lanif}" @@ -675,12 +672,38 @@ f_log() fi } +# f_statistics: adblock runtime statistics +f_statistics() +{ + local ipv4_blk=0 ipv4_all=0 ipv4_pct=0 + local ipv6_blk=0 ipv6_all=0 ipv6_pct=0 + + if [ -n "${adb_wanif4}" ] + then + ipv4_blk="$(iptables -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')" + ipv4_all="$(iptables -t nat -vnL PREROUTING | awk '$3 ~ /^prerouting_rule$/ {sum += $1} END {printf sum}')" + if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ] + then + ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')" + fi + fi + if [ -n "${adb_wanif6}" ] + then + ipv6_blk="$(ip6tables -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')" + ipv6_all="$(ip6tables -t nat -vnL PREROUTING | awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')" + if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ] + then + ipv6_pct="$(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')" + fi + fi + "${adb_uci}" -q set "adblock.global.adb_percentage=${ipv4_pct}%/${ipv6_pct}%" + f_log "firewall statistics (IPv4/IPv6): ${ipv4_pct}%/${ipv6_pct}% of all packets in prerouting chain are ad related & blocked" +} + # f_exit: delete temporary files, generate statistics and exit # f_exit() { - local ipv4_blk=0 ipv4_all=0 ipv4_pct=0 - local ipv6_blk=0 ipv6_all=0 ipv6_pct=0 local lastrun="$(date "+%d.%m.%Y %H:%M:%S")" rm -f "${adb_tmpfile}" @@ -690,28 +713,9 @@ f_exit() # if [ $((rc)) -eq 0 ] then - if [ -n "${adb_wanif4}" ] - then - ipv4_blk="$(iptables -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')" - ipv4_all="$(iptables -t nat -vnL PREROUTING | awk '$3 ~ /^prerouting_rule$/ {sum += $1} END {printf sum}')" - if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ] - then - ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')" - fi - fi - if [ -n "${adb_wanif6}" ] - then - ipv6_blk="$(ip6tables -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')" - ipv6_all="$(ip6tables -t nat -vnL PREROUTING | awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')" - if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ] - then - ipv6_pct="$(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')" - fi - fi - "${adb_uci}" -q set "adblock.global.adb_percentage=${ipv4_pct}%/${ipv6_pct}%" + f_statistics "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun}" "${adb_uci}" -q commit "adblock" - f_log "firewall statistics (IPv4/IPv6): ${ipv4_pct}%/${ipv6_pct}% of all packets in prerouting chain are ad related & blocked" f_log "domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})" elif [ $((rc)) -gt 0 ] then diff --git a/net/adblock/files/adblock-update.sh b/net/adblock/files/adblock-update.sh index 7c0f574ee..f14d750b8 100755 --- a/net/adblock/files/adblock-update.sh +++ b/net/adblock/files/adblock-update.sh @@ -10,7 +10,7 @@ # adb_pid="${$}" adb_pidfile="/var/run/adblock.pid" -adb_scriptver="1.2.6" +adb_scriptver="1.2.7" adb_mincfgver="2.2" adb_scriptdir="${0%/*}" if [ -r "${adb_pidfile}" ] @@ -22,11 +22,8 @@ else printf "${adb_pid}" > "${adb_pidfile}" if [ -r "${adb_scriptdir}/adblock-helper.sh" ] then - if [ -z "$(type -f f_envload)" ] - then - . "${adb_scriptdir}/adblock-helper.sh" - f_envload - fi + . "${adb_scriptdir}/adblock-helper.sh" + f_envload else rc=254 logger -s -t "adblock[${adb_pid}] error" "adblock function library not found" diff --git a/net/adblock/files/adblock.init b/net/adblock/files/adblock.init index d4eb3fff9..6a2f6bf0e 100755 --- a/net/adblock/files/adblock.init +++ b/net/adblock/files/adblock.init @@ -2,9 +2,10 @@ # START=99 -EXTRA_COMMANDS="toggle cfgup" -EXTRA_HELP=" toggle Toggle adblocking 'on' or 'off' - cfgup Update the adblock configuration file" +EXTRA_COMMANDS="toggle stats cfgup" +EXTRA_HELP=" toggle Toggle adblock 'on' or 'off' + stats Update adblock statistics + cfgup Update adblock configuration file" adb_debug=0 adb_pid="${$}" @@ -12,25 +13,25 @@ adb_script="/usr/bin/adblock-update.sh" adb_helper="/usr/bin/adblock-helper.sh" adb_pidfile="/var/run/adblock.pid" bg_parm="&" -unset log_parm - -if [ $((adb_debug)) -eq 0 ] +if [ -t 1 ] then - exec 2>/dev/null + unset bg_parm fi -if [ -t 1 ] +if [ $((adb_debug)) -eq 0 ] then - log_parm="-s" - unset bg_parm + exec 2>/dev/null fi if [ -r "${adb_pidfile}" ] then - logger ${log_parm} -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1 + logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1 exit 255 fi +. "${adb_helper}" +f_envload + boot() { return 0 @@ -57,10 +58,9 @@ reload() stop() { - . "${adb_helper}" - f_envload f_rmdns f_rmuhttpd + adb_uci="$(which uci)" config_foreach f_rmconfig source if [ -n "$(${adb_uci} -q changes adblock)" ] then @@ -72,14 +72,13 @@ stop() fi if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "${rm_cfg}" ] then - logger ${log_parm} -t "adblock[${adb_pid}] info " "all adblock related services stopped" 2>&1 + f_log "all adblock related services stopped" fi return 0 } toggle() { - . "${adb_helper}" if [ -d "${adb_dnshidedir}" ] then list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)" @@ -97,20 +96,23 @@ toggle() fi if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ] then - if [ "$(${adb_uci} -q get adblock.global.adb_restricted)" = "1" ] - then - adb_uci="$(which true)" - fi mv -f "${source}"* "${target}" "${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}" "${adb_uci}" -q commit "adblock" /etc/init.d/dnsmasq restart - logger ${log_parm} -t "adblock[${adb_pid}] info " "toggle for adblock switched '${pos}'" 2>&1 + f_log "adblock toggle switched '${pos}'" fi fi return 0 } +stats() +{ + f_statistics + "${adb_uci}" -q commit "adblock" + return 0 +} + cfgup() { stop @@ -118,9 +120,9 @@ cfgup() rc=$? if [ $((rc)) -eq 0 ] then - logger ${log_parm} -t "adblock[${adb_pid}] info " "default adblock configuration applied, please check the settings in '/etc/config/adblock'" 2>&1 + f_log "default adblock configuration applied, please check the settings in '/etc/config/adblock'" else - logger ${log_parm} -t "adblock[${adb_pid}] info " "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'" 2>&1 + f_log "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'" fi return 0 }