From 4e8d22eced0be25b28464681981a0f9640ce6e7e Mon Sep 17 00:00:00 2001 From: Dirk Brenken Date: Wed, 19 Dec 2018 17:32:29 +0100 Subject: [PATCH] adblock: release 3.6.0 * add adblock dns query reporting via tcpdump (see readme for details) * fix tld compression on low memory systems (< 64 MB) * fix various small issues Signed-off-by: Dirk Brenken --- net/adblock/Makefile | 4 +- net/adblock/files/README.md | 81 +++++++++--- net/adblock/files/adblock.conf | 4 +- net/adblock/files/adblock.init | 9 +- net/adblock/files/adblock.sh | 231 +++++++++++++++++++++++++++++---- 5 files changed, 280 insertions(+), 49 deletions(-) diff --git a/net/adblock/Makefile b/net/adblock/Makefile index f4f18c4b31..405326e5a0 100644 --- a/net/adblock/Makefile +++ b/net/adblock/Makefile @@ -6,8 +6,8 @@ include $(TOPDIR)/rules.mk PKG_NAME:=adblock -PKG_VERSION:=3.5.5 -PKG_RELEASE:=4 +PKG_VERSION:=3.6.0 +PKG_RELEASE:=1 PKG_LICENSE:=GPL-3.0+ PKG_MAINTAINER:=Dirk Brenken diff --git a/net/adblock/files/README.md b/net/adblock/files/README.md index fb7856624d..4dee3f7010 100644 --- a/net/adblock/files/README.md +++ b/net/adblock/files/README.md @@ -1,7 +1,7 @@ # dns based ad/abuse domain blocking ## Description -A lot of people already use adblocker plugins within their desktop browsers, but what if you are using your (smart) phone, tablet, watch or any other wlan gadget...getting rid of annoying ads, trackers and other abuse sites (like facebook ;-) is simple: block them with your router. When the dns server on your router receives dns requests, you will sort out queries that ask for the resource records of ad servers and return a simple 'NXDOMAIN'. This is nothing but **N**on-e**X**istent Internet or Intranet domain name, if domain name is unable to resolved using the dns server, a condition called the 'NXDOMAIN' occurred. +A lot of people already use adblocker plugins within their desktop browsers, but what if you are using your (smart) phone, tablet, watch or any other (wlan) gadget!? Getting rid of annoying ads, trackers and other abuse sites (like facebook) is simple: block them with your router. When the dns server on your router receives dns requests, you will sort out queries that ask for the resource records of ad servers and return a simple 'NXDOMAIN'. This is nothing but **N**on-e**X**istent Internet or Intranet domain name, if domain name is unable to resolved using the dns server, a condition called the 'NXDOMAIN' occurred. ## Main Features * support of the following domain blocklist sources (free for private usage, for commercial use please check their individual licenses): @@ -86,16 +86,17 @@ A lot of people already use adblocker plugins within their desktop browsers, but * keep the dns cache intact after adblock processing (currently supported by unbound, named and kresd) * conditional dns backend restarts by old/new blocklist comparison with sha256sum (default) or md5sum * suspend & resume adblock actions temporarily without blocklist reloading -* output comprehensive runtime information via LuCI or via 'status' init command +* provide comprehensive runtime information via LuCI or via 'status' init command +* provide a detailed DNS Query Report with dns related information about client requests, top (blocked) domains and more * query function to quickly identify blocked (sub-)domains, e.g. for whitelisting +* force dns requests to local resolver +* force overall sort / duplicate removal for low memory devices (handle with care!) +* automatic blocklist backup & restore, they will be used in case of download errors or during startup in backup mode +* 'backup mode' to re-use blocklist backups during startup, get fresh lists only via reload or restart action +* 'Jail' blocklist generation which builds an additional list (/tmp/adb_list.jail) to block access to all domains except those listed in the whitelist file. You can use this restrictive blocklist manually e.g. for guest wifi or kidsafe configurations +* send notification emails in case of a processing error or if the overall domain count is ≤ 0 +* add new adblock sources on your own, see example below * strong LuCI support -* optional: force dns requests to local resolver -* optional: force overall sort / duplicate removal for low memory devices (handle with care!) -* optional: automatic blocklist backup & restore, they will be used in case of download errors or during startup in backup mode -* optional: 'backup mode' to re-use blocklist backups during startup, get fresh lists only via reload or restart action -* optional: 'Jail' blocklist generation which builds an additional list (/tmp/adb_list.jail) to block access to all domains except those listed in the whitelist file. You can use this restrictive blocklist manually e.g. for guest wifi or kidsafe configurations -* optional: send notification emails in case of a processing error or if the overall domain count is ≤ 0 -* optional: add new adblock sources on your own, see example below ## Prerequisites * [OpenWrt](https://openwrt.org), tested with the stable release series (18.06) and with the latest snapshot @@ -104,6 +105,8 @@ A lot of people already use adblocker plugins within their desktop browsers, but * to support all blocklist sources a full version (with ssl support) of 'wget', 'uclient-fetch' with one of the 'libustream-*' ssl libraries, 'aria2c' or 'curl' is required * for limited devices with real memory constraints, adblock provides also a 'http only' option and supports wget-nossl and uclient-fetch (without libustream-ssl) as well * for more configuration options see examples below +* email notification (optional): for email notification support you need to install and configure the additional 'msmtp' package +* DNS Query Report (optional): for this detailed report you need to install the additional package 'tcpdump-mini' ## Installation & Usage * install 'adblock' (_opkg install adblock_) @@ -111,7 +114,7 @@ A lot of people already use adblocker plugins within their desktop browsers, but * control the adblock service manually with _/etc/init.d/adblock_ start/stop/restart/reload/suspend/resume/status or use the LuCI frontend ## LuCI adblock companion package -* for easy management of the various blocklist sources and adblock runtime options you should use the provided LuCI frontend +* it's strongly recommended to use the LuCI frontend to easily configure all powerful aspects of adblock * install 'luci-app-adblock' (_opkg install luci-app-adblock_) * the application is located in LuCI under 'Services' menu @@ -141,11 +144,19 @@ A lot of people already use adblocker plugins within their desktop browsers, but * adb\_trigger => set the startup trigger to a certain interface, to 'timed' or to 'none' (default: 'wan') * the following options apply to the 'extra' config section: + * adb\_nice => set the nice level of the adblock process and all sub-processes (int/default: '0', standard priority) * adb\_triggerdelay => additional trigger delay in seconds before adblock processing begins (int/default: '2') * adb\_forcedns => force dns requests to local resolver (bool/default: '0', disabled) * adb\_forcesrt => force overall sort on low memory devices with less than 64 MB RAM (bool/default: '0', disabled) + * adb\_backup => create compressed blocklist backups, they will be used in case of download errors or during startup in backup mode (bool/default: '0', disabled) + * adb\_backupdir => target directory for adblock backups (default: not set) * adb\_backup_mode => do not automatically update blocklists during startup, use backups instead (bool/default: '0', disabled) - * adb\_maxqueue => size of the download queue to handle downloads & list processing in parallel (int/default: '4') + * adb\_report => enable the background tcpdump gathering process to provide a detailed DNS Query Report (bool/default: '0', disabled) + * adb\_repdir => target directory for dns related report files generated by tcpdump (default: '/tmp') + * adb\_repchunksize => report chunk size used by tcpdump in MB (int/default: '1') + * adb\_repiface => reporting interface used by tcpdump (default: 'br-lan') + * adb\_repchunkcnt => report chunk count used by tcpdump (default: '5') + * adb\_maxqueue => size of the download queue to handle downloads & list processing in parallel (int/default: '8') * adb\_jail => builds an additional 'Jail' list (/tmp/adb_list.jail) to block access to all domains except those listed in the whitelist file (bool/default: '0', disabled) * adb\_dnsflush => flush DNS cache after adblock processing, i.e. enable the old restart behavior (bool/default: '0', disabled) * adb\_notify => send notification emails in case of a processing error or if the overall domain count is ≤ 0 (bool/default: '0', disabled) @@ -218,12 +229,48 @@ Finally make this file executable via 'chmod' and test it directly. If no more e /etc/init.d/adblock status ::: adblock runtime information + adblock_status : enabled - + adblock_version : 3.5.5 - + overall_domains : 97199 (backup mode) + + adblock_version : 3.6.0 + + overall_domains : 30267 (backup mode) + fetch_utility : /bin/uclient-fetch (libustream-ssl) - + dns_backend : unbound (/var/lib/unbound) - + last_rundate : 01.09.2018 07:09:16 - + system_release : PC Engines APU, OpenWrt SNAPSHOT r7986-dc9388ac55 + + dns_backend : dnsmasq (/tmp) + + last_rundate : 19.12.2018 16:29:25 + + system_release : GL-AR750S, OpenWrt SNAPSHOT r8814-6835c13e5a + + +**receive adblock DNS Query Report information:** +

+/etc/init.d/adblock report
+:::
+::: Adblock DNS-Query Report
+:::
+  + Start   ::: 2018-12-19, 16:29:40
+  + End     ::: 2018-12-19, 16:45:08
+  + Total   ::: 42
+  + Blocked ::: 17 (40.48 %)
+:::
+::: Top 10 Clients
+  + 32       ::: 101.167.1.103
+  + 10       ::: abc1:abc1:abc0:0:abc1:abcb:abc5:abc3
+:::
+::: Top 10 Domains
+  + 7        ::: dns.msftncsi.com
+  + 4        ::: forum.openwrt.org
+  + 2        ::: outlook.office365.com
+  + 1        ::: www.google.com
+  + 1        ::: www.deepl.com
+  + 1        ::: safebrowsing.googleapis.com
+  + 1        ::: play.googleapis.com
+  + 1        ::: odc.officeapps.live.com
+  + 1        ::: login.microsoftonline.com
+  + 1        ::: test-my.sharepoint.com
+:::
+::: Top 10 Blocked Domains
+  + 4        ::: nexus.officeapps.live.com
+  + 4        ::: mobile.pipe.aria.microsoft.com
+  + 3        ::: watson.telemetry.microsoft.com
+  + 2        ::: v10.events.data.microsoft.com
+  + 2        ::: settings-win.data.microsoft.com
+  + 2        ::: nexusrules.officeapps.live.com
 
**cronjob for a regular block list update (/etc/crontabs/root):** @@ -302,7 +349,7 @@ config source 'reg_ro' To add a really new source with different domain/host format you have to write a suitable awk one-liner on your own, so basic awk skills are needed. As a starting point check the already existing awk rulesets 'adb_src_rset' in the config file, probably you need only small changes for your individual list. Download the desired list and test your new awk string locally. The output result should be a sequential list with one domain/host per line - nothing more. If your awk one-liner works quite well, add a new source section to the adblock config file and test the new source. ## Support -Please join the adblock discussion in this [forum thread](https://forum.lede-project.org/t/adblock-2-x-support-thread/507) or contact me by mail +Please join the adblock discussion in this [forum thread](https://forum.openwrt.org/t/adblock-support-thread/507) or contact me by mail ## Removal * stop all adblock related services with _/etc/init.d/adblock stop_ diff --git a/net/adblock/files/adblock.conf b/net/adblock/files/adblock.conf index 49ca1bce75..1c1a19d971 100644 --- a/net/adblock/files/adblock.conf +++ b/net/adblock/files/adblock.conf @@ -9,10 +9,10 @@ config adblock 'global' config adblock 'extra' option adb_debug '0' - option adb_forcesrt '0' option adb_forcedns '0' option adb_backup '0' - option adb_maxqueue '4' + option adb_report '0' + option adb_maxqueue '8' config source 'adaway' option adb_src 'https://adaway.org/hosts.txt' diff --git a/net/adblock/files/adblock.init b/net/adblock/files/adblock.init index 2ae2b086bb..2378a55b33 100755 --- a/net/adblock/files/adblock.init +++ b/net/adblock/files/adblock.init @@ -4,10 +4,11 @@ START=30 USE_PROCD=1 -EXTRA_COMMANDS="suspend resume query status" +EXTRA_COMMANDS="suspend resume query report status" EXTRA_HELP=" suspend Suspend adblock processing resume Resume adblock processing query Query active blocklists for specific domains + report Print dns query statistics status Print runtime information" adb_init="/etc/init.d/adblock" @@ -77,6 +78,12 @@ query() rc_procd "${adb_script}" query "${1}" } +report() +{ + [ -s "${adb_pidfile}" ] && return 1 + rc_procd "${adb_script}" report "${1}" +} + status() { local key keylist value rtfile="$(uci_get adblock extra adb_rtfile)" diff --git a/net/adblock/files/adblock.sh b/net/adblock/files/adblock.sh index e0c9b89e37..fb956b41ac 100755 --- a/net/adblock/files/adblock.sh +++ b/net/adblock/files/adblock.sh @@ -10,7 +10,7 @@ # LC_ALL=C PATH="/usr/sbin:/usr/bin:/sbin:/bin" -adb_ver="3.5.5-4" +adb_ver="3.6.0" adb_sysver="unknown" adb_enabled=0 adb_debug=0 @@ -18,7 +18,7 @@ adb_backup_mode=0 adb_forcesrt=0 adb_forcedns=0 adb_jail=0 -adb_maxqueue=4 +adb_maxqueue=8 adb_notify=0 adb_notifycnt=0 adb_triggerdelay=0 @@ -35,6 +35,12 @@ adb_rtfile="/tmp/adb_runtime.json" adb_hashutil="$(command -v sha256sum)" adb_hashold="" adb_hashnew="" +adb_report=0 +adb_repiface="br-lan" +adb_repdir="/tmp" +adb_reputil="$(command -v tcpdump)" +adb_repchunkcnt="5" +adb_repchunksize="1" adb_cnt="" adb_rc=0 adb_action="${1:-"start"}" @@ -300,7 +306,7 @@ f_rmdns() fi f_rmtemp fi - f_log "debug" "f_rmdns::: dns: ${adb_dns}, dns_dir: ${adb_dnsdir}, dns_prefix: ${adb_dnsprefix}, dns_file: ${adb_dnsfile}, rt_file: ${adb_rtfile}, backup_dir: ${adb_backupdir}" + f_log "debug" "f_rmdns ::: dns: ${adb_dns}, dns_dir: ${adb_dnsdir}, dns_prefix: ${adb_dnsprefix}, dns_file: ${adb_dnsfile}, rt_file: ${adb_rtfile}, backup_dir: ${adb_backupdir}" } # commit uci changes @@ -325,7 +331,7 @@ f_uci() esac fi fi - f_log "debug" "f_uci ::: config: ${config}, change: ${change}" + f_log "debug" "f_uci ::: config: ${config}, change: ${change}" } # list/overall count @@ -337,7 +343,7 @@ f_count() adb_cnt=0 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && ([ -z "${mode}" ] || [ "${mode}" = "final" ]) then - adb_cnt="$(( $(wc -l 2>/dev/null < "${adb_dnsdir}/${adb_dnsfile}") ))" + adb_cnt="$(wc -l 2>/dev/null < "${adb_dnsdir}/${adb_dnsfile}")" if [ -s "${adb_tmpdir}/tmp.add_whitelist" ] then adb_cnt="$(( ${adb_cnt} - $(wc -l 2>/dev/null < "${adb_tmpdir}/tmp.add_whitelist") ))" @@ -488,7 +494,7 @@ f_dnsup() cnt=$((cnt+1)) sleep 1 done - f_log "debug" "f_dnsup::: cache_util: ${cache_util:-"-"}, cache_rc: ${cache_rc:-"-"}, cache_flush: ${adb_dnsflush}, cache_cnt: ${cnt}, rc: ${adb_rc}" + f_log "debug" "f_dnsup ::: cache_util: ${cache_util:-"-"}, cache_rc: ${cache_rc:-"-"}, cache_flush: ${adb_dnsflush}, cache_cnt: ${cnt}, rc: ${adb_rc}" return ${adb_rc} } @@ -557,7 +563,7 @@ f_list() ;; esac f_count "${mode}" - f_log "debug" "f_list ::: name: ${src_name:-"-"}, mode: ${mode}, cnt: ${adb_cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}" + f_log "debug" "f_list ::: name: ${src_name:-"-"}, mode: ${mode}, cnt: ${adb_cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}" } # top level domain compression @@ -581,6 +587,7 @@ f_tld() if [ ${?} -eq 0 ] then cnt_tld="$(wc -l 2>/dev/null < "${source}")" + rm -f "${temp}" else mv -f "${temp}" > "${source}" fi @@ -589,7 +596,7 @@ f_tld() mv -f "${temp}" "${source}" fi fi - f_log "debug" "f_tld ::: source: ${source}, cnt: ${cnt:-"-"}, cnt_srt: ${cnt_srt:-"-"}, cnt_tld: ${cnt_tld:-"-"}" + f_log "debug" "f_tld ::: source: ${source}, cnt: ${cnt:-"-"}, cnt_srt: ${cnt_srt:-"-"}, cnt_tld: ${cnt_tld:-"-"}" } # blocklist hash compare @@ -618,7 +625,7 @@ f_hash() adb_hashnew="" fi fi - f_log "debug" "f_hash ::: hash_util: ${adb_hashutil}, hash: ${hash}, out_rc: ${hash_rc}" + f_log "debug" "f_hash ::: hash_util: ${adb_hashutil}, hash: ${hash}, out_rc: ${hash_rc}" return ${hash_rc} } @@ -628,6 +635,8 @@ f_switch() { local status cnt mode="${1}" + json_load_file "${adb_rtfile}" >/dev/null 2>&1 + json_select "data" json_get_var status "adblock_status" json_get_var cnt "overall_domains" @@ -739,6 +748,8 @@ f_jsnup() mode="backup mode" fi + json_load_file "${adb_rtfile}" >/dev/null 2>&1 + json_select "data" if [ -z "${adb_fetchinfo}" ] then json_get_var adb_fetchinfo "fetch_utility" @@ -752,6 +763,11 @@ f_jsnup() then json_get_var run_time "last_rundate" fi + + > "${adb_rtfile}" + json_load_file "${adb_rtfile}" >/dev/null 2>&1 + json_init + json_add_object "data" json_add_string "adblock_status" "${status}" json_add_string "adblock_version" "${adb_ver}" json_add_string "overall_domains" "${adb_cnt:-0} (${mode})" @@ -759,15 +775,16 @@ f_jsnup() json_add_string "dns_backend" "${adb_dns} (${adb_dnsdir})" json_add_string "last_rundate" "${run_time:-"-"}" json_add_string "system_release" "${adb_sysver}" + json_close_object json_dump > "${adb_rtfile}" if [ ${adb_notify} -eq 1 ] && [ ${no_mail} -eq 0 ] && [ -x /etc/adblock/adblock.notify ] && \ ([ "${status}" = "error" ] || ([ "${status}" = "enabled" ] && [ ${adb_cnt} -le ${adb_notifycnt} ])) then - (/etc/adblock/adblock.notify >/dev/null 2>&1) & + (/etc/adblock/adblock.notify >/dev/null 2>&1)& bg_pid=${!} fi - f_log "debug" "f_jsnup::: status: ${status}, mode: ${mode}, cnt: ${adb_cnt}, notify: ${adb_notify}, notify_cnt: ${adb_notifycnt}, notify_pid: ${bg_pid:-"-"}" + f_log "debug" "f_jsnup ::: status: ${status}, mode: ${mode}, cnt: ${adb_cnt}, notify: ${adb_notify}, notify_cnt: ${adb_notifycnt}, notify_pid: ${bg_pid:-"-"}" } # write to syslog @@ -803,7 +820,7 @@ f_main() > "${adb_tmpdir}/tmp.raw_whitelist" > "${adb_tmpdir}/tmp.add_whitelist" > "${adb_tmpdir}/tmp.rem_whitelist" - f_log "debug" "f_main ::: dns: ${adb_dns}, fetch_util: ${adb_fetchinfo}, backup: ${adb_backup}, backup_mode: ${adb_backup_mode}, dns_jail: ${adb_jail}, force_srt: ${adb_forcesrt}, force_dns: ${adb_forcedns}, mem_total: ${mem_total:-0}, mem_free: ${mem_free:-0}, max_queue: ${adb_maxqueue}" + f_log "debug" "f_main ::: dns: ${adb_dns}, fetch_util: ${adb_fetchinfo}, backup: ${adb_backup}, backup_mode: ${adb_backup_mode}, dns_jail: ${adb_jail}, force_srt: ${adb_forcesrt}, force_dns: ${adb_forcedns}, mem_total: ${mem_total:-0}, mem_free: ${mem_free:-0}, max_queue: ${adb_maxqueue}" # prepare whitelist entries # @@ -848,7 +865,7 @@ f_main() # basic pre-checks # - f_log "debug" "f_main ::: name: ${src_name}, enabled: ${enabled}" + f_log "debug" "f_main ::: name: ${src_name}, enabled: ${enabled}" if [ "${enabled}" != "1" ] || [ -z "${src_url}" ] || [ -z "${src_rset}" ] then f_list remove @@ -894,7 +911,7 @@ f_main() fi else src_log="$(printf '%s' "${src_log}" | awk '{ORS=" ";print $0}')" - f_log "debug" "f_main ::: name: ${src_name}, url: ${src_url}, rc: ${adb_rc}, log: ${src_log:-"-"}" + f_log "debug" "f_main ::: name: ${src_name}, url: ${src_url}, rc: ${adb_rc}, log: ${src_log:-"-"}" fi ) & else @@ -925,7 +942,7 @@ f_main() done else src_log="$(printf '%s' "${src_log}" | awk '{ORS=" ";print $0}')" - f_log "debug" "f_main ::: name: ${src_name}, url: ${src_url}, rc: ${adb_rc}, log: ${src_log:-"-"}" + f_log "debug" "f_main ::: name: ${src_name}, url: ${src_url}, rc: ${adb_rc}, log: ${src_log:-"-"}" fi if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ] then @@ -979,7 +996,7 @@ f_main() fi else src_log="$(printf '%s' "${src_log}" | awk '{ORS=" ";print $0}')" - f_log "debug" "f_main ::: name: ${src_name}, url: ${src_url}, rc: ${adb_rc}, log: ${src_log:-"-"}" + f_log "debug" "f_main ::: name: ${src_name}, url: ${src_url}, rc: ${adb_rc}, log: ${src_log:-"-"}" if [ ${adb_backup} -eq 1 ] then f_list restore @@ -1032,6 +1049,171 @@ f_main() exit ${adb_rc} } +# trace dns queries via tcpdump and prepare a report +# +f_report() +{ + local bg_pid total blocked percent rep_clients rep_domains rep_blocked index hold cnt=0 print="${1:-"true"}" + + if [ ! -x "${adb_reputil}" ] + then + f_log "info" "Please install the package 'tcpdump-mini' manually to use the adblock reporting feature!" + return 0 + fi + + bg_pid="$(pgrep -f "^${adb_reputil}.*adb_report\.pcap$" | awk '{ORS=" "; print $1}')" + if [ ${adb_report} -eq 0 ] || ([ -n "${bg_pid}" ] && [ "${adb_action}" = "stop" ]) + then + if [ -n "${bg_pid}" ] + then + kill -HUP ${bg_pid} + fi + return 0 + fi + if [ -z "${bg_pid}" ] && [ "${adb_action}" != "report" ] + then + ("${adb_reputil}" -nn -s0 -l -i ${adb_repiface} port 53 -C${adb_repchunksize} -W${adb_repchunkcnt} -w "${adb_repdir}/adb_report.pcap" >/dev/null 2>&1 &) + fi + if [ "${adb_action}" = "report" ] + then + > "${adb_repdir}/adb_report.raw" + for file in "${adb_repdir}"/adb_report.pcap* + do + ( + "${adb_reputil}" -nn -tttt -r $file 2>/dev/null | \ + awk -v cnt=${cnt} '!/\.lan\. /&&/ A[\? ]+|NXDomain/{a=$1;b=substr($2,0,8);c=$4;sub(/\.[0-9]+$/,"",c); \ + d=cnt $7;e=$(NF-1);sub(/[0-9]\/[0-9]\/[0-9]/,"NX",e);sub(/\.$/,"",e);sub(/([0-9]{1,3}\.){3}[0-9]{1,3}/,"OK",e);printf("%s\t%s\t%s\t%s\t%s\n", a,b,c,d,e)}' >> "${adb_repdir}/adb_report.raw" + )& + hold=$(( cnt % adb_maxqueue )) + if [ ${hold} -eq 0 ] + then + wait + fi + cnt=$(( cnt + 1 )) + done + wait + + if [ -s "${adb_repdir}/adb_report.raw" ] + then + awk '{printf("%s\t%s\t%s\t%s\t%s\t%s\n", $4,$5,$1,$2,$3,$4)}' "${adb_repdir}/adb_report.raw" | \ + sort -ur | uniq -uf2 | awk '{currA=($6+0);currB=$6;currC=substr($6,length($6),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}}' | sort -ur > "${adb_repdir}/adb_report" + fi + + if [ -s "${adb_repdir}/adb_report" ] + then + total="$(wc -l < ${adb_repdir}/adb_report)" + blocked="$(awk '{if($5=="NX")print $4}' ${adb_repdir}/adb_report | wc -l)" + percent="$(awk -v t=${total} -v b=${blocked} 'BEGIN{printf("%.2f %s\n",b/t*100, "%")}')" + rep_clients="$(awk '{print $3}' ${adb_repdir}/adb_report | sort | uniq -c | sort -r | awk '{ORS=" ";if(NR<=10) printf("%s_%s ",$1,$2)}')" + rep_domains="$(awk '{if($5!="NX")print $4}' ${adb_repdir}/adb_report | sort | uniq -c | sort -r | awk '{ORS=" ";if(NR<=10)printf("%s_%s ",$1,$2)}')" + rep_blocked="$(awk '{if($5=="NX")print $4}' ${adb_repdir}/adb_report | sort | uniq -c | sort -r | awk '{ORS=" ";if(NR<=10)printf("%s_%s ",$1,$2)}')" + + > "${adb_repdir}/adb_report.json" + json_load_file "${adb_repdir}/adb_report.json" >/dev/null 2>&1 + json_init + json_add_object "data" + json_add_string "start_date" "$(awk 'END{printf("%s",$1)}' ${adb_repdir}/adb_report)" + json_add_string "start_time" "$(awk 'END{printf("%s",$2)}' ${adb_repdir}/adb_report)" + json_add_string "end_date" "$(awk 'NR==1{printf("%s",$1)}' ${adb_repdir}/adb_report)" + json_add_string "end_time" "$(awk 'NR==1{printf("%s",$2)}' ${adb_repdir}/adb_report)" + json_add_string "total" "${total}" + json_add_string "blocked" "${blocked}" + json_add_string "percent" "${percent}" + json_close_array + json_add_array "top_clients" + for client in ${rep_clients} + do + json_add_object + json_add_string "count" "${client%_*}" + json_add_string "address" "${client#*_}" + json_close_object + done + json_close_array + json_add_array "top_domains" + for domain in ${rep_domains} + do + json_add_object + json_add_string "count" "${domain%_*}" + json_add_string "address" "${domain#*_}" + json_close_object + done + json_close_array + json_add_array "top_blocked" + for block in ${rep_blocked} + do + json_add_object + json_add_string "count" "${block%_*}" + json_add_string "address" "${block#*_}" + json_close_object + done + json_close_object + json_dump > "${adb_repdir}/adb_report.json" + fi + rm -f "${adb_repdir}/adb_report.raw" + + if [ "${print}" = "true" ] + then + if [ -s "${adb_repdir}/adb_report.json" ] + then + printf "%s\n%s\n%s\n" ":::" "::: Adblock DNS-Query Report" ":::" + json_load_file "${adb_repdir}/adb_report.json" + json_select "data" + json_get_keys keylist + for key in ${keylist} + do + json_get_var value "${key}" + eval "${key}=\"${value}\"" + done + printf " + %s\n + %s\n" "Start ::: ${start_date}, ${start_time}" "End ::: ${end_date}, ${end_time}" + printf " + %s\n + %s %s\n" "Total ::: ${total}" "Blocked ::: ${blocked}" "(${percent})" + json_select ".." + if json_get_type Status "top_clients" && [ "${Status}" = "array" ] + then + printf "%s\n%s\n" ":::" "::: Top 10 Clients" + json_select "top_clients" + index=1 + while json_get_type Status ${index} && [ "${Status}" = "object" ] + do + json_get_values client ${index} + printf " + %-9s::: %s\n" ${client} + index=$((index + 1)) + done + fi + json_select ".." + if json_get_type Status "top_domains" && [ "${Status}" = "array" ] + then + printf "%s\n%s\n" ":::" "::: Top 10 Domains" + json_select "top_domains" + index=1 + while json_get_type Status ${index} && [ "${Status}" = "object" ] + do + json_get_values domain ${index} + printf " + %-9s::: %s\n" ${domain} + index=$((index + 1)) + done + fi + json_select ".." + if json_get_type Status "top_blocked" && [ "${Status}" = "array" ] + then + printf "%s\n%s\n" ":::" "::: Top 10 Blocked Domains" + json_select "top_blocked" + index=1 + while json_get_type Status ${index} && [ "${Status}" = "object" ] + do + json_get_values blocked ${index} + printf " + %-9s::: %s\n" ${blocked} + index=$((index + 1)) + done + fi + else + printf "%s\n" "::: no reporting data available yet" + fi + fi + fi + f_log "debug" "f_report ::: action: ${adb_action}, report: ${adb_report}, print: ${print}, reputil: ${adb_reputil}, repdir: ${adb_repdir}, repiface: ${adb_repiface}, repchunksize: ${adb_repchunksize}, repchunkcnt: ${adb_repchunkcnt}, bg_pid: ${bg_pid}" +} + # source required system libraries # if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ] @@ -1042,25 +1224,16 @@ else f_log "err" "system libraries not found" fi -# initialize json runtime file -# -json_load_file "${adb_rtfile}" >/dev/null 2>&1 -json_select data >/dev/null 2>&1 -if [ ${?} -ne 0 ] -then - > "${adb_rtfile}" - json_init - json_add_object "data" -fi - # handle different adblock actions # f_envload case "${adb_action}" in stop) + f_report false f_rmdns ;; restart) + f_report false f_rmdns f_envcheck f_main @@ -1071,10 +1244,14 @@ case "${adb_action}" in resume) f_switch resume ;; + report) + f_report "${2}" + ;; query) f_query "${2}" ;; start|reload) + f_report false f_envcheck f_main ;; -- 2.30.2