adblock: release 3.0

* add kresd & turris omnia support
* add dnscrypt-proxy support
* change start priority to 30, to fix possible trigger issues on slow
booting hardware
* simplify suspend/resume handling (no longer use a hideout directory)
* enhanced LuCI frontend
* many small changes & improvements
* default config change (please update your config!), adblock is now
disabled by default
* documentation update

Signed-off-by: Dirk Brenken <dev@brenken.org>
This commit is contained in:
Dirk Brenken
2017-09-09 17:35:42 +02:00
parent 7cdb6c7031
commit 206c847bb5
5 changed files with 238 additions and 188 deletions
+159 -123
View File
@@ -10,33 +10,31 @@
#
LC_ALL=C
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
adb_ver="2.8.5"
adb_ver="3.0.0"
adb_sysver="$(ubus -S call system board | jsonfilter -e '@.release.description')"
adb_enabled=1
adb_enabled=0
adb_debug=0
adb_minfree=2
adb_manmode=0
adb_forcesrt=0
adb_forcedns=0
adb_triggerdelay=0
adb_backup=0
adb_backupdir="/mnt"
adb_whitelist="/etc/adblock/adblock.whitelist"
adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
adb_fetch="/usr/bin/wget"
adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
adb_dnslist="dnsmasq unbound named kresd"
adb_dns="dnsmasq"
adb_dnsprefix="adb_list"
adb_dnsfile="${adb_dnsprefix}.overall"
adb_rtfile="/tmp/adb_runtime.json"
adb_sources=""
adb_src_cat_shalla=""
adb_action="${1}"
adb_action="${1:-"start"}"
adb_cnt=0
adb_rc=0
# f_envload: load adblock environment
#
f_envload()
{
local services dns_up cnt=0
local dns_up cnt=0
# source in system libraries
#
@@ -89,55 +87,60 @@ f_envload()
# set dns backend environment
#
while [ ${cnt} -le 20 ]
case "${adb_action}" in
start|restart|reload)
> "${adb_rtfile}"
if [ "${adb_action}" = "start" ] && [ "${adb_trigger}" = "timed" ]
then
sleep ${adb_triggerdelay}
fi
;;
esac
while [ ${cnt} -le 30 ]
do
services="$(ubus -S call service list 2>/dev/null)"
if [ -n "${services}" ]
dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running" 2>/dev/null)"
if [ "${dns_up}" = "true" ]
then
for dns in ${adb_dnslist}
do
dns_up="$(printf "%s" "${services}" | jsonfilter -l1 -e "@.${dns}.instances.*.running")"
if [ "${dns_up}" = "true" ]
then
case "${dns}" in
dnsmasq)
adb_dns="${dns}"
adb_dnsdir="${adb_dnsdir:="/tmp/dnsmasq.d"}"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
adb_dnsformat="awk '{print \"local=/\"\$0\"/\"}'"
break 2
;;
unbound)
adb_dns="${dns}"
adb_dnsdir="${adb_dnsdir:="/var/lib/unbound"}"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
adb_dnsformat="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
break 2
;;
named)
adb_dns="${dns}"
adb_dnsdir="${adb_dnsdir:="/var/lib/bind"}"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
adb_dnsformat="awk '{print \"\"\$0\" IN CNAME .\n*.\"\$0\" IN CNAME .\"}'"
break 2
;;
kresd)
adb_dns="${dns}"
adb_dnsdir="${adb_dnsdir:="/tmp/kresd"}"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
adb_dnsformat="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
break 2
;;
esac
fi
done
case "${adb_dns}" in
dnsmasq)
adb_dnsuser="${adb_dns}"
adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
adb_dnsformat="awk '{print \"local=/\"\$0\"/\"}'"
break
;;
unbound)
adb_dnsuser="${adb_dns}"
adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}"
adb_dnsformat="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
break
;;
named)
adb_dnsuser="bind"
adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}"
adb_dnsformat="awk '{print \"\"\$0\" IN CNAME .\n*.\"\$0\" IN CNAME .\"}'"
break
;;
kresd)
adb_dnsuser="root"
adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}"
adb_dnsformat="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
adb_dnsheader="\$TTL 2h"$'\n'"@ IN SOA localhost. root.localhost. (2 6h 1h 1w 2h)"$'\n'" IN NS localhost."
break
;;
dnscrypt-proxy)
adb_dnsuser="nobody"
adb_dnsdir="${adb_dnsdir:-"/tmp"}"
adb_dnsformat="awk '{print \$0}'"
break
;;
esac
fi
sleep 1
cnt=$((cnt+1))
done
if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
then
f_log "error" "no active/supported DNS backend found"
f_log "error" "'${adb_dns}' not running, DNS backend not found"
fi
# force dns to local resolver
@@ -180,6 +183,7 @@ f_envcheck()
f_rmdns
f_dnsrestart
fi
f_jsnupdate
f_log "info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
exit 0
fi
@@ -192,7 +196,8 @@ f_envcheck()
if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
then
adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
elif [ "$(readlink -fn "/bin/wget")" = "/bin/busybox" ] || [ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ]
elif [ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ] ||
([ "$(readlink -fn "/bin/wget")" = "/bin/busybox" ] && [ "$(readlink -fn "${adb_fetch}")" != "/usr/bin/wget" ])
then
adb_fetch="/bin/busybox"
adb_fetchparm="-q -O"
@@ -217,16 +222,6 @@ f_envcheck()
fi
adb_fetchinfo="${adb_fetch##*/} (${ssl_lib})"
# create dns hideout directory
#
if [ ! -d "${adb_dnshidedir}" ]
then
mkdir -p -m 660 "${adb_dnshidedir}"
chown -R "${adb_dns}":"${adb_dns}" "${adb_dnshidedir}" 2>/dev/null
else
rm -f "${adb_dnshidedir}/${adb_dnsprefix}"*
fi
# create adblock temp file/directory
#
adb_tmpload="$(mktemp -tu)"
@@ -235,7 +230,7 @@ f_envcheck()
# prepare whitelist entries
#
if [ -s "${adb_whitelist}" ]
if [ -s "${adb_whitelist}" ] && [ -n "${adb_whitelist_rset}" ]
then
awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
fi
@@ -259,10 +254,10 @@ f_rmdns()
{
if [ -n "${adb_dns}" ]
then
rm -f "${adb_dnsdir}/${adb_dnsprefix}"*
rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
rm -rf "${adb_dnshidedir}"
> "${adb_dnsdir}/${adb_dnsfile}"
> "${adb_rtfile}"
rm -f "${adb_dnsdir}/.${adb_dnsfile}"
rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
fi
}
@@ -270,19 +265,15 @@ f_rmdns()
#
f_dnsrestart()
{
local dns_up mem_free cnt=0
local dns_up cnt=0
"/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
while [ ${cnt} -le 10 ]
do
dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@.${adb_dns}.instances.*.running")"
dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running")"
if [ "${dns_up}" = "true" ]
then
mem_free="$(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")"
if [ ${mem_free} -ge ${adb_minfree} ]
then
return 0
fi
return 0
fi
cnt=$((cnt+1))
sleep 1
@@ -334,6 +325,11 @@ f_list()
else
eval "${adb_dnsformat}" "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
fi
if [ -n "${adb_dnsheader}" ]
then
printf '%s\n' "${adb_dnsheader}" | cat - "${adb_dnsdir}/${adb_dnsfile}" > "${adb_tmpdir}/${adb_dnsfile}"
cat "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
fi
adb_rc=${?}
;;
esac
@@ -358,25 +354,24 @@ f_switch()
{
local source target status mode="${1}"
if [ -d "${adb_dnshidedir}" ]
if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && [ "${mode}" = "suspend" ]
then
if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && [ "${mode}" = "suspend" ]
then
source="${adb_dnsdir}/${adb_dnsfile}"
target="${adb_dnshidedir}"
status="suspended"
elif [ -s "${adb_dnshidedir}/${adb_dnsfile}" ] && [ "${mode}" = "resume" ]
then
source="${adb_dnshidedir}/${adb_dnsfile}"
target="${adb_dnsdir}"
status="resumed"
fi
if [ -n "${status}" ]
then
mv -f "${source}"* "${target}"
f_dnsrestart
f_log "info " "adblock processing ${status}"
fi
source="${adb_dnsdir}/${adb_dnsfile}"
target="${adb_dnsdir}/.${adb_dnsfile}"
status="suspended"
elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ] && [ "${mode}" = "resume" ]
then
source="${adb_dnsdir}/.${adb_dnsfile}"
target="${adb_dnsdir}/${adb_dnsfile}"
status="resumed"
fi
if [ -n "${status}" ]
then
cat "${source}" > "${target}"
> "${source}"
f_dnsrestart
f_jsnupdate
f_log "info " "adblock processing ${status}"
fi
}
@@ -393,21 +388,74 @@ f_query()
printf "%s\n" "::: no active block list found, please start / resume adblock first"
elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
then
printf "%s\n" "::: invalid domain input, please submit a specific (sub-)domain, e.g. 'www.abc.xyz'"
printf "%s\n" "::: invalid domain input, please submit a single domain, e.g. 'doubleclick.net'"
else
cd "${adb_dnsdir}"
while [ "${domain}" != "${tld}" ]
do
search="${domain//./\.}"
result="$(grep -Hm5 "[/\"\.]${search}[/\"]" "${adb_dnsfile}" | awk -F ':|=|/|\"' '{printf(" + %s\n",$4)}')"
printf "%s\n" "::: results for (sub-)domain '${domain}' (max. 5)"
printf "%s\n" "${result:=" - no match"}"
if [ "${adb_dns}" = "dnsmasq" ] || [ "${adb_dns}" = "unbound" ]
then
result="$(awk -F '/|\"' "/[\/\"\.]${search}/{i++;{printf(\" + %s\n\",\$2)};if(i>9){exit}}" "${adb_dnsfile}")"
else
result="$(awk "/(^[^\*][a-z]*[\.]+${search}|^${search})/{i++;{printf(\" + %s\n\",\$1)};if(i>9){exit}}" "${adb_dnsfile}")"
fi
printf "%s\n" "::: max. ten results for domain '${domain}'"
printf "%s\n" "${result:-" - no match"}"
domain="${tld}"
tld="${domain#*.}"
done
fi
}
# f_jsnupdate: update runtime information
#
f_jsnupdate()
{
local status rundate="$(/bin/date "+%d.%m.%Y %H:%M:%S")"
if [ ${adb_rc} -gt 0 ]
then
status="error"
elif [ ${adb_enabled} -ne 1 ]
then
status="disabled"
elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ]
then
status="paused"
else
status="enabled"
if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
then
if [ "${adb_dns}" = "named" ] || [ "${adb_dns}" = "kresd" ]
then
adb_cnt="$(( ( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ) / 2 ))"
else
adb_cnt="$(( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ))"
fi
fi
fi
if [ -z "${adb_fetchinfo}" ] && [ -s "${adb_rtfile}" ]
then
json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
json_select data
json_get_var adb_fetchinfo "fetch_utility"
fi
json_init
json_add_object "data"
json_add_string "adblock_status" "${status}"
json_add_string "adblock_version" "${adb_ver}"
json_add_string "blocked_domains" "${adb_cnt}"
json_add_string "fetch_utility" "${adb_fetchinfo}"
json_add_string "dns_backend" "${adb_dns} (${adb_dnsdir})"
json_add_string "last_rundate" "${rundate}"
json_add_string "system_release" "${adb_sysver}"
json_close_object
json_dump > "${adb_rtfile}"
}
# f_status: output runtime information
#
f_status()
@@ -416,21 +464,14 @@ f_status()
if [ -s "${adb_rtfile}" ]
then
if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
then
value="active"
else
value="no domains blocked"
fi
printf "%s\n" "::: adblock runtime information"
printf " %-15s : %s\n" "status" "${value}"
json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
json_select data
json_get_keys keylist
for key in ${keylist}
do
json_get_var value "${key}"
printf " %-15s : %s\n" "${key}" "${value}"
printf " + %-15s : %s\n" "${key}" "${value}"
done
fi
}
@@ -453,6 +494,8 @@ f_log()
f_rmdns
f_dnsrestart
fi
adb_rc=1
f_jsnupdate
exit 1
fi
fi
@@ -462,7 +505,7 @@ f_log()
#
f_main()
{
local src_name src_rset shalla_archive enabled url hash_old hash_new cnt=0
local src_name src_rset shalla_archive enabled url hash_old hash_new
local mem_total="$(awk '/^MemTotal/ {print int($2/1000)}' "/proc/meminfo")"
f_log "info " "start adblock processing ..."
@@ -488,7 +531,7 @@ f_main()
# manual / backup mode
#
if [ ${adb_manmode} -eq 1 ] && [ -z "${adb_action}" ] && [ "${src_name}" != "blacklist" ]
if [ ${adb_manmode} -eq 1 ] && [ "${adb_action}" = "start" ] && [ "${src_name}" != "blacklist" ]
then
f_list restore
if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
@@ -536,7 +579,10 @@ f_main()
if [ -s "${adb_tmpfile}" ]
then
f_tldcompression "${adb_tmpfile}"
f_list backup
if [ "${src_name}" != "blacklist" ]
then
f_list backup
fi
else
f_list restore
fi
@@ -562,7 +608,7 @@ f_main()
#
if [ -f "${adb_dnsdir}/${adb_dnsfile}" ]
then
hash_old="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" | awk '{print $1}')"
hash_old="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
fi
if [ -s "${adb_tmpdir}/${adb_dnsfile}" ]
then
@@ -574,30 +620,20 @@ f_main()
else
> "${adb_dnsdir}/${adb_dnsfile}"
fi
hash_new="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" | awk '{print $1}')"
cnt="$(wc -l < "${adb_dnsdir}/${adb_dnsfile}")"
chown "${adb_dnsuser}":"${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
f_rmtemp
# conditional restart of the dns backend and runtime information export
#
chown "${adb_dns}":"${adb_dns}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
f_rmtemp
if [ "${hash_old}" != "${hash_new}" ]
hash_new="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
if [ -z "${hash_old}" ] || [ -z "${hash_new}" ] || [ "${hash_old}" != "${hash_new}" ]
then
f_dnsrestart
fi
if [ ${?} -eq 0 ]
then
json_init
json_add_object "data"
json_add_string "adblock_version" "${adb_ver}"
json_add_string "blocked_domains" "${cnt}"
json_add_string "fetch_info" "${adb_fetchinfo}"
json_add_string "dns_backend" "${adb_dns}"
json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
json_add_string "system" "${adb_sysver}"
json_close_object
json_dump > "${adb_rtfile}"
f_log "info " "block list with overall ${cnt} domains loaded successfully (${adb_sysver})"
f_jsnupdate "${adb_cnt}"
f_log "info " "block list with overall ${adb_cnt} domains loaded successfully (${adb_sysver})"
else
f_log "error" "dns backend restart with active block list failed"
fi