|
@ -11,7 +11,7 @@ |
|
|
export LC_ALL=C |
|
|
export LC_ALL=C |
|
|
export PATH="/usr/sbin:/usr/bin:/sbin:/bin" |
|
|
export PATH="/usr/sbin:/usr/bin:/sbin:/bin" |
|
|
set -o pipefail |
|
|
set -o pipefail |
|
|
adb_ver="4.0.7" |
|
|
|
|
|
|
|
|
adb_ver="4.0.8" |
|
|
adb_enabled=0 |
|
|
adb_enabled=0 |
|
|
adb_debug=0 |
|
|
adb_debug=0 |
|
|
adb_forcedns=0 |
|
|
adb_forcedns=0 |
|
@ -50,7 +50,8 @@ adb_loggercmd="$(command -v logger)" |
|
|
adb_dumpcmd="$(command -v tcpdump)" |
|
|
adb_dumpcmd="$(command -v tcpdump)" |
|
|
adb_lookupcmd="$(command -v nslookup)" |
|
|
adb_lookupcmd="$(command -v nslookup)" |
|
|
adb_fetchutil="" |
|
|
adb_fetchutil="" |
|
|
adb_portlist="53 853 5353" |
|
|
|
|
|
|
|
|
adb_zonelist="" |
|
|
|
|
|
adb_portlist="" |
|
|
adb_repiface="" |
|
|
adb_repiface="" |
|
|
adb_replisten="53" |
|
|
adb_replisten="53" |
|
|
adb_repchunkcnt="5" |
|
|
adb_repchunkcnt="5" |
|
@ -148,6 +149,12 @@ f_conf() |
|
|
elif [ "${option}" = "adb_safesearchlist" ] |
|
|
elif [ "${option}" = "adb_safesearchlist" ] |
|
|
then |
|
|
then |
|
|
eval "${option}=\"$(printf "%s" "${adb_safesearchlist}") ${value}\"" |
|
|
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 |
|
|
fi |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -553,7 +560,7 @@ f_count() |
|
|
# |
|
|
# |
|
|
f_extconf() |
|
|
f_extconf() |
|
|
{ |
|
|
{ |
|
|
local config config_dir config_file port fwcfg |
|
|
|
|
|
|
|
|
local config config_dir config_file section zone port fwcfg |
|
|
|
|
|
|
|
|
case "${adb_dns}" in |
|
|
case "${adb_dns}" in |
|
|
"dnsmasq") |
|
|
"dnsmasq") |
|
@ -579,33 +586,37 @@ f_extconf() |
|
|
f_uci "${config}" |
|
|
f_uci "${config}" |
|
|
|
|
|
|
|
|
config="firewall" |
|
|
config="firewall" |
|
|
fwcfg="$(uci -qNX show "${config}")" |
|
|
|
|
|
|
|
|
fwcfg="$(uci -qNX show "${config}" | "${adb_awk}" 'BEGIN{FS="[.=]"};/adblock_/{if(zone==$2){next}else{ORS=" ";zone=$2;print zone}}')" |
|
|
if [ "${adb_enabled}" -eq 1 ] && [ "${adb_forcedns}" -eq 1 ] && \ |
|
|
if [ "${adb_enabled}" -eq 1 ] && [ "${adb_forcedns}" -eq 1 ] && \ |
|
|
[ "$(/etc/init.d/firewall enabled; printf "%u" ${?})" -eq 0 ] |
|
|
[ "$(/etc/init.d/firewall enabled; printf "%u" ${?})" -eq 0 ] |
|
|
then |
|
|
then |
|
|
for port in ${adb_portlist} |
|
|
|
|
|
|
|
|
for zone in ${adb_zonelist} |
|
|
do |
|
|
do |
|
|
if [ -z "$(printf "%s" "${fwcfg}" | grep -Fo -m1 "adblock_dns_${port}")" ] |
|
|
|
|
|
then |
|
|
|
|
|
uci -q batch <<-EOC |
|
|
|
|
|
set firewall."adblock_dns_${port}"="redirect" |
|
|
|
|
|
set firewall."adblock_dns_${port}".name="Adblock DNS, port ${port}" |
|
|
|
|
|
set firewall."adblock_dns_${port}".src="lan" |
|
|
|
|
|
set firewall."adblock_dns_${port}".proto="tcp udp" |
|
|
|
|
|
set firewall."adblock_dns_${port}".src_dport="${port}" |
|
|
|
|
|
set firewall."adblock_dns_${port}".dest_port="${port}" |
|
|
|
|
|
set firewall."adblock_dns_${port}".target="DNAT" |
|
|
|
|
|
EOC |
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
for port in ${adb_portlist} |
|
|
|
|
|
do |
|
|
|
|
|
if [ -z "$(printf "%s" "${fwcfg}" | grep -o -m1 "adblock_${zone}${port}[ |\$]")" ] |
|
|
|
|
|
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}".dest_port="${port}" |
|
|
|
|
|
set firewall."adblock_${zone}${port}".target="DNAT" |
|
|
|
|
|
EOC |
|
|
|
|
|
fi |
|
|
|
|
|
fwcfg="${fwcfg/adblock_${zone}${port}[ |\$]/}" |
|
|
|
|
|
done |
|
|
done |
|
|
done |
|
|
elif [ "${adb_enabled}" -eq 0 ] || [ "${adb_forcedns}" -eq 0 ] |
|
|
|
|
|
|
|
|
fwcfg="${fwcfg#"${fwcfg%%[![:space:]]*}"}" |
|
|
|
|
|
fwcfg="${fwcfg%"${fwcfg##*[![:space:]]}"}" |
|
|
|
|
|
fi |
|
|
|
|
|
if [ "${adb_enabled}" -eq 0 ] || [ "${adb_forcedns}" -eq 0 ] || [ -n "${fwcfg}" ] |
|
|
then |
|
|
then |
|
|
for port in ${adb_portlist} |
|
|
|
|
|
|
|
|
for section in ${fwcfg} |
|
|
do |
|
|
do |
|
|
if [ -n "$(printf "%s" "${fwcfg}" | grep -Fo -m1 "adblock_dns_${port}")" ] |
|
|
|
|
|
then |
|
|
|
|
|
uci_remove firewall "adblock_dns_${port}" |
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
uci_remove firewall "${section}" |
|
|
done |
|
|
done |
|
|
fi |
|
|
fi |
|
|
f_uci "${config}" |
|
|
f_uci "${config}" |
|
@ -671,7 +682,7 @@ f_list() |
|
|
rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}" |
|
|
rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}" |
|
|
"${adb_awk}" "${rset}" "${adb_blacklist}" | \ |
|
|
"${adb_awk}" "${rset}" "${adb_blacklist}" | \ |
|
|
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${adb_tmpdir}/tmp.raw.${src_name}" |
|
|
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${adb_tmpdir}/tmp.raw.${src_name}" |
|
|
sort ${adb_srtopts} -u "${adb_tmpdir}/tmp.raw.${src_name}" 2>/dev/null > "${adb_tmpfile}.${src_name}" |
|
|
|
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -u "${adb_tmpdir}/tmp.raw.${src_name}" 2>/dev/null > "${adb_tmpfile}.${src_name}" |
|
|
out_rc="${?}" |
|
|
out_rc="${?}" |
|
|
rm -f "${adb_tmpdir}/tmp.raw.${src_name}" |
|
|
rm -f "${adb_tmpdir}/tmp.raw.${src_name}" |
|
|
elif [ "${src_name}" = "whitelist" ] && [ -s "${adb_whitelist}" ] |
|
|
elif [ "${src_name}" = "whitelist" ] && [ -s "${adb_whitelist}" ] |
|
@ -887,7 +898,7 @@ f_list() |
|
|
find "${adb_backupdir}" ${ffiles} -print0 2>/dev/null | xargs -0 rm 2>/dev/null |
|
|
find "${adb_backupdir}" ${ffiles} -print0 2>/dev/null | xargs -0 rm 2>/dev/null |
|
|
fi |
|
|
fi |
|
|
unset src_name |
|
|
unset src_name |
|
|
sort ${adb_srtopts} -mu "${adb_tmpfile}".* 2>/dev/null > "${adb_tmpdir}/${adb_dnsfile}" |
|
|
|
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -mu "${adb_tmpfile}".* 2>/dev/null > "${adb_tmpdir}/${adb_dnsfile}" |
|
|
out_rc="${?}" |
|
|
out_rc="${?}" |
|
|
rm -f "${adb_tmpfile}".* |
|
|
rm -f "${adb_tmpfile}".* |
|
|
;; |
|
|
;; |
|
@ -1297,7 +1308,7 @@ f_main() |
|
|
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${src_tmpsort}" |
|
|
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${src_tmpsort}" |
|
|
fi |
|
|
fi |
|
|
rm -f "${src_tmpload}" |
|
|
rm -f "${src_tmpload}" |
|
|
sort ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null > "${src_tmpfile}" |
|
|
|
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null > "${src_tmpfile}" |
|
|
src_rc="${?}" |
|
|
src_rc="${?}" |
|
|
rm -f "${src_tmpsort}" |
|
|
rm -f "${src_tmpsort}" |
|
|
if [ "${src_rc}" -eq 0 ] && [ -s "${src_tmpfile}" ] |
|
|
if [ "${src_rc}" -eq 0 ] && [ -s "${src_tmpfile}" ] |
|
@ -1335,7 +1346,7 @@ f_main() |
|
|
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${src_tmpsort}" |
|
|
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${src_tmpsort}" |
|
|
fi |
|
|
fi |
|
|
rm -f "${src_tmpload}" |
|
|
rm -f "${src_tmpload}" |
|
|
sort ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null > "${src_tmpfile}" |
|
|
|
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -u "${src_tmpsort}" 2>/dev/null > "${src_tmpfile}" |
|
|
src_rc="${?}" |
|
|
src_rc="${?}" |
|
|
rm -f "${src_tmpsort}" |
|
|
rm -f "${src_tmpsort}" |
|
|
if [ "${src_rc}" -eq 0 ] && [ -s "${src_tmpfile}" ] |
|
|
if [ "${src_rc}" -eq 0 ] && [ -s "${src_tmpfile}" ] |
|
@ -1499,9 +1510,9 @@ f_report() |
|
|
wait |
|
|
wait |
|
|
if [ -s "${adb_reportdir}/adb_report.raw" ] |
|
|
if [ -s "${adb_reportdir}/adb_report.raw" ] |
|
|
then |
|
|
then |
|
|
sort ${adb_srtopts} -k1 -k3 -k4 -k5 -k1 -ur "${adb_reportdir}/adb_report.raw" | \ |
|
|
|
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -k1 -k3 -k4 -k5 -k1 -ur "${adb_reportdir}/adb_report.raw" | \ |
|
|
"${adb_awk}" '{currA=($1+0);currB=$1;currC=substr($1,length($1),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}}' | \ |
|
|
"${adb_awk}" '{currA=($1+0);currB=$1;currC=substr($1,length($1),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 ${adb_srtopts} -k1 -k2 -k3 -k4 -ur > "${adb_reportdir}/adb_report.srt" |
|
|
|
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -k1 -k2 -k3 -k4 -ur > "${adb_reportdir}/adb_report.srt" |
|
|
rm -f "${adb_reportdir}/adb_report.raw" |
|
|
rm -f "${adb_reportdir}/adb_report.raw" |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
@ -1528,16 +1539,16 @@ f_report() |
|
|
printf "%s" " \"${top}\": [ " >> "${adb_reportdir}/adb_report.json" |
|
|
printf "%s" " \"${top}\": [ " >> "${adb_reportdir}/adb_report.json" |
|
|
case "${top}" in |
|
|
case "${top}" in |
|
|
"top_clients") |
|
|
"top_clients") |
|
|
"${adb_awk}" '{print $3}' "${adb_reportdir}/adb_report.srt" | sort ${adb_srtopts} | uniq -c | \ |
|
|
|
|
|
sort ${adb_srtopts} -nr | "${adb_awk}" '{ORS=" ";if(NR==1)printf "{ \"count\": \"%s\", \"address\": \"%s\" }",$1,$2; else if(NR<10)printf ", { \"count\": \"%s\", \"address\": \"%s\" }",$1,$2}' >> "${adb_reportdir}/adb_report.json" |
|
|
|
|
|
|
|
|
"${adb_awk}" '{print $3}' "${adb_reportdir}/adb_report.srt" | "${adb_sort}" ${adb_srtopts} | uniq -c | \ |
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -nr | "${adb_awk}" '{ORS=" ";if(NR==1)printf "{ \"count\": \"%s\", \"address\": \"%s\" }",$1,$2; else if(NR<10)printf ", { \"count\": \"%s\", \"address\": \"%s\" }",$1,$2}' >> "${adb_reportdir}/adb_report.json" |
|
|
;; |
|
|
;; |
|
|
"top_domains") |
|
|
"top_domains") |
|
|
"${adb_awk}" '{if($5!="NX")print $4}' "${adb_reportdir}/adb_report.srt" | sort ${adb_srtopts} | uniq -c | \ |
|
|
|
|
|
sort ${adb_srtopts} -nr | "${adb_awk}" '{ORS=" ";if(NR==1)printf "{ \"count\": \"%s\", \"address\": \"%s\" }",$1,$2; else if(NR<10)printf ", { \"count\": \"%s\", \"address\": \"%s\" }",$1,$2}' >> "${adb_reportdir}/adb_report.json" |
|
|
|
|
|
|
|
|
"${adb_awk}" '{if($5!="NX")print $4}' "${adb_reportdir}/adb_report.srt" | "${adb_sort}" ${adb_srtopts} | uniq -c | \ |
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -nr | "${adb_awk}" '{ORS=" ";if(NR==1)printf "{ \"count\": \"%s\", \"address\": \"%s\" }",$1,$2; else if(NR<10)printf ", { \"count\": \"%s\", \"address\": \"%s\" }",$1,$2}' >> "${adb_reportdir}/adb_report.json" |
|
|
;; |
|
|
;; |
|
|
"top_blocked") |
|
|
"top_blocked") |
|
|
"${adb_awk}" '{if($5=="NX")print $4}' "${adb_reportdir}/adb_report.srt" | sort ${adb_srtopts} | uniq -c | \ |
|
|
|
|
|
sort ${adb_srtopts} -nr | "${adb_awk}" '{ORS=" ";if(NR==1)printf "{ \"count\": \"%s\", \"address\": \"%s\" }",$1,$2; else if(NR<10)printf ", { \"count\": \"%s\", \"address\": \"%s\" }",$1,$2}' >> "${adb_reportdir}/adb_report.json" |
|
|
|
|
|
|
|
|
"${adb_awk}" '{if($5=="NX")print $4}' "${adb_reportdir}/adb_report.srt" | "${adb_sort}" ${adb_srtopts} | uniq -c | \ |
|
|
|
|
|
"${adb_sort}" ${adb_srtopts} -nr | "${adb_awk}" '{ORS=" ";if(NR==1)printf "{ \"count\": \"%s\", \"address\": \"%s\" }",$1,$2; else if(NR<10)printf ", { \"count\": \"%s\", \"address\": \"%s\" }",$1,$2}' >> "${adb_reportdir}/adb_report.json" |
|
|
;; |
|
|
;; |
|
|
esac |
|
|
esac |
|
|
printf "%s" " ], " >> "${adb_reportdir}/adb_report.json" |
|
|
printf "%s" " ], " >> "${adb_reportdir}/adb_report.json" |
|
@ -1614,23 +1625,35 @@ f_report() |
|
|
f_log "debug" "f_report ::: action: ${adb_action}, report: ${adb_report}, search: ${1}, count: ${2}, process: ${3}, print: ${4}, dump_util: ${adb_dumpcmd}, repdir: ${adb_reportdir}, repiface: ${adb_repiface:-"-"}, replisten: ${adb_replisten}, repchunksize: ${adb_repchunksize}, repchunkcnt: ${adb_repchunkcnt}, bg_pid: ${bg_pid}" |
|
|
f_log "debug" "f_report ::: action: ${adb_action}, report: ${adb_report}, search: ${1}, count: ${2}, process: ${3}, print: ${4}, dump_util: ${adb_dumpcmd}, repdir: ${adb_reportdir}, repiface: ${adb_repiface:-"-"}, replisten: ${adb_replisten}, repchunksize: ${adb_repchunksize}, repchunkcnt: ${adb_repchunkcnt}, bg_pid: ${bg_pid}" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# source required system libraries |
|
|
|
|
|
# |
|
|
|
|
|
if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ] |
|
|
|
|
|
then |
|
|
|
|
|
. "/lib/functions.sh" |
|
|
|
|
|
. "/lib/functions/network.sh" |
|
|
|
|
|
. "/usr/share/libubox/jshn.sh" |
|
|
|
|
|
else |
|
|
|
|
|
f_log "err" "system libraries not found" |
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
# awk selection |
|
|
# awk selection |
|
|
# |
|
|
# |
|
|
adb_awk="$(command -v gawk)" |
|
|
adb_awk="$(command -v gawk)" |
|
|
if [ -z "${adb_awk}" ] |
|
|
if [ -z "${adb_awk}" ] |
|
|
then |
|
|
then |
|
|
adb_awk="$(command -v awk)" |
|
|
adb_awk="$(command -v awk)" |
|
|
|
|
|
if [ -z "${adb_awk}" ] |
|
|
|
|
|
then |
|
|
|
|
|
f_log "err" "awk not found" |
|
|
|
|
|
fi |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
# source required system libraries |
|
|
|
|
|
|
|
|
# sort selection |
|
|
# |
|
|
# |
|
|
if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ] |
|
|
|
|
|
|
|
|
adb_sort="$(command -v gnu-sort)" |
|
|
|
|
|
if [ -z "${adb_sort}" ] |
|
|
then |
|
|
then |
|
|
. "/lib/functions.sh" |
|
|
|
|
|
. "/lib/functions/network.sh" |
|
|
|
|
|
. "/usr/share/libubox/jshn.sh" |
|
|
|
|
|
else |
|
|
|
|
|
f_log "err" "system libraries not found" |
|
|
|
|
|
|
|
|
f_log "err" "gnu-sort not found" |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
# version information |
|
|
# version information |
|
|