#
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"
#
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"
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
#
# 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
# 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}"
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}"
#
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
#
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="${$}"
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
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
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)"
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
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
}