2 # dns based ad/abuse domain blocking
3 # written by Dirk Brenken (dev@brenken.org)
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
14 adb_sysver
="$(ubus -S call system board | jsonfilter -e '@.release.description')"
23 adb_fetch
="/usr/bin/wget"
24 adb_fetchparm
="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
26 adb_dnsprefix
="adb_list"
27 adb_dnsfile
="${adb_dnsprefix}.overall"
28 adb_rtfile
="/tmp/adb_runtime.json"
29 adb_action
="${1:-"start"}"
33 # f_envload: load adblock environment
39 # source in system libraries
41 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
44 .
"/usr/share/libubox/jshn.sh"
46 f_log
"error" "system libraries not found"
49 # parse global section by callback
54 if [ "${type}" = "adblock" ]
60 eval "${option}=\"${value}\""
67 # parse 'source' sections
71 local value opt section
="${1}" options
="enabled adb_src adb_src_rset adb_src_cat"
72 eval "adb_sources=\"${adb_sources} ${section}\""
75 config_get value
"${section}" "${opt}"
78 eval "${opt}_${section}=\"${value}\""
86 config_foreach parse_config
source
88 # set/check dns backend environment
92 adb_dnsuser
="${adb_dns}"
93 adb_dnsdir
="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
94 adb_dnsformat
="awk '{print \"local=/\"\$0\"/\"}'"
97 adb_dnsuser
="${adb_dns}"
98 adb_dnsdir
="${adb_dnsdir:-"/var/lib/unbound"}"
99 adb_dnsformat
="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
103 adb_dnsdir
="${adb_dnsdir:-"/var/lib/bind"}"
104 adb_dnsformat
="awk '{print \"\"\$0\" IN CNAME .\n*.\"\$0\" IN CNAME .\"}'"
108 adb_dnsdir
="${adb_dnsdir:-"/etc/kresd"}"
109 adb_dnsformat
="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
110 adb_dnsheader
="\$TTL 2h"$
'\n'"@ IN SOA localhost. root.localhost. (2 6h 1h 1w 2h)"$
'\n'" IN NS localhost."
114 adb_dnsdir
="${adb_dnsdir:-"/tmp"}"
115 adb_dnsformat
="awk '{print \$0}'"
119 if [ -d "${adb_dnsdir}" ] && [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ]
121 > "${adb_dnsdir}/${adb_dnsfile}"
124 case "${adb_action}" in
125 start|restart|reload
)
127 if [ "${adb_action}" = "start" ] && [ "${adb_trigger}" = "timed" ]
129 sleep ${adb_triggerdelay}
134 while [ ${cnt} -le 30 ]
136 dns_up
="$(ubus -S call service list "{\"name
\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@
[\"${adb_dns}\"].instances.
*.running
" 2>/dev/null)"
137 if [ "${dns_up}" = "true" ]
145 if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
147 f_log
"error" "'${adb_dns}' not running, DNS backend not found"
150 # force dns to local resolver
152 if [ ${adb_forcedns} -eq 1 ] && [ -z "$(uci -q get firewall.adblock_dns)" ]
154 uci
-q set firewall.adblock_dns
="redirect"
155 uci
-q set firewall.adblock_dns.name
="Adblock DNS"
156 uci
-q set firewall.adblock_dns.src
="lan"
157 uci
-q set firewall.adblock_dns.proto
="tcp udp"
158 uci
-q set firewall.adblock_dns.src_dport
="53"
159 uci
-q set firewall.adblock_dns.dest_port
="53"
160 uci
-q set firewall.adblock_dns.target
="DNAT"
161 elif [ ${adb_forcedns} -eq 0 ] && [ -n "$(uci -q get firewall.adblock_dns)" ]
163 uci
-q delete firewall.adblock_dns
165 if [ -n "$(uci -q changes firewall)" ]
167 uci
-q commit firewall
168 if [ $
(/etc
/init.d
/firewall enabled
; printf "%u" ${?}) -eq 0 ]
170 /etc
/init.d
/firewall reload
>/dev
/null
2>&1
175 # f_envcheck: check/set environment prerequisites
181 # check 'enabled' option
183 if [ ${adb_enabled} -ne 1 ]
185 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
191 f_log
"info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
195 # check fetch utility
198 if [ -x "${adb_fetch}" ]
200 if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
202 adb_fetchparm
="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
203 elif [ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ] ||
204 ([ "$(readlink -fn "/bin
/wget
")" = "/bin/busybox" ] && [ "$(readlink -fn "${adb_fetch}")" != "/usr/bin/wget" ])
206 adb_fetch
="/bin/busybox"
207 adb_fetchparm
="-q -O"
212 if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin
/wget
")" = "/bin/uclient-fetch" ]
214 adb_fetch
="/bin/uclient-fetch"
215 if [ -f "/lib/libustream-ssl.so" ]
217 adb_fetchparm
="-q --timeout=10 --no-check-certificate -O"
218 ssl_lib
="libustream-ssl"
220 adb_fetchparm
="-q --timeout=10 -O"
223 if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
225 f_log
"error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
227 adb_fetchinfo
="${adb_fetch##*/} (${ssl_lib})"
229 # create adblock temp file/directory
231 adb_tmpload
="$(mktemp -tu)"
232 adb_tmpfile
="$(mktemp -tu)"
233 adb_tmpdir
="$(mktemp -p /tmp -d)"
235 # prepare whitelist entries
237 if [ -s "${adb_whitelist}" ] && [ -n "${adb_whitelist_rset}" ]
239 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
243 # f_rmtemp: remove temporary files & directories
247 if [ -d "${adb_tmpdir}" ]
249 rm -f "${adb_tmpload}"
250 rm -f "${adb_tmpfile}"
251 rm -rf "${adb_tmpdir}"
255 # f_rmdns: remove dns related files & directories
259 if [ -n "${adb_dns}" ]
261 > "${adb_dnsdir}/${adb_dnsfile}"
263 rm -f "${adb_dnsdir}/.${adb_dnsfile}"
264 rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
268 # f_dnsrestart: restart the dns backend
274 "/etc/init.d/${adb_dns}" restart
>/dev
/null
2>&1
275 while [ ${cnt} -le 10 ]
277 dns_up
="$(ubus -S call service list "{\"name
\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@
[\"${adb_dns}\"].instances.
*.running
")"
278 if [ "${dns_up}" = "true" ]
288 # f_list: backup/restore/remove block lists
292 local mode
="${1}" in_rc
="${adb_rc}" cnt
=0
296 cnt
="$(wc -l < "${adb_tmpfile}")"
297 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
299 gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
304 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ] &&
305 [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
307 gunzip
-cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
312 if [ -d "${adb_backupdir}" ]
314 rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
319 if [ -s "${adb_tmpfile}" ]
321 cat "${adb_tmpfile}" >> "${adb_tmpdir}/${adb_dnsfile}"
326 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
328 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpdir}/${adb_dnsfile}" | eval "${adb_dnsformat}" > "${adb_dnsdir}/${adb_dnsfile}"
330 eval "${adb_dnsformat}" "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
332 if [ -n "${adb_dnsheader}" ]
334 printf '%s\n' "${adb_dnsheader}" | cat - "${adb_dnsdir}/${adb_dnsfile}" > "${adb_tmpdir}/${adb_dnsfile}"
335 cat "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
340 f_log
"debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
343 # f_tldcompression: top level domain compression
347 local source="${1}" temp
="${adb_tmpload}"
349 awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev
/null |
sort -u > "${temp}"
350 awk '{if(NR==1){tld=$NF};while(getline){if($NF !~ tld"\\."){print tld;tld=$NF}}print tld}' "${temp}" 2>/dev
/null
> "${source}"
351 awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev
/null
> "${temp}"
352 sort -u "${temp}" > "${source}"
355 # f_switch: suspend/resume adblock processing
359 local source target status mode
="${1}"
361 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && [ "${mode}" = "suspend" ]
363 source="${adb_dnsdir}/${adb_dnsfile}"
364 target
="${adb_dnsdir}/.${adb_dnsfile}"
366 elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ] && [ "${mode}" = "resume" ]
368 source="${adb_dnsdir}/.${adb_dnsfile}"
369 target
="${adb_dnsdir}/${adb_dnsfile}"
372 if [ -n "${status}" ]
374 cat "${source}" > "${target}"
378 f_log
"info " "adblock processing ${status}"
382 # f_query: query block list for certain (sub-)domains
386 local search result cnt
388 local tld
="${domain#*.}"
390 if [ ! -s "${adb_dnsdir}/${adb_dnsfile}" ]
392 printf "%s\n" "::: no active block list found, please start / resume adblock first"
393 elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
395 printf "%s\n" "::: invalid domain input, please submit a single domain, e.g. 'doubleclick.net'"
398 while [ "${domain}" != "${tld}" ]
400 search
="${domain//./\.}"
401 if [ "${adb_dns}" = "dnsmasq" ] ||
[ "${adb_dns}" = "unbound" ]
403 result
="$(awk -F '/|\"' "/[\
/\"\.
]${search}/{i
++;{printf(\" + %s
\n\",\
$2)};if(i
>9){exit}}" "${adb_dnsfile}")"
405 result
="$(awk "/(^
[^\
*][a-z
]*[\.
]+${search}|^${search})/{i++;{printf(\" + %s\n\",\$1)};if(i
>9){exit}}" "${adb_dnsfile}")"
407 printf "%s\n" "::: max. ten results for domain '${domain}'"
408 printf "%s\n" "${result:-" - no match"}"
415 # f_jsnupdate: update runtime information
419 local status rundate
="$(/bin/date "+%d.
%m.
%Y
%H
:%M
:%S
")"
421 if [ ${adb_rc} -gt 0 ]
424 elif [ ${adb_enabled} -ne 1 ]
427 elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ]
432 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
434 if [ "${adb_dns}" = "named" ] ||
[ "${adb_dns}" = "kresd" ]
436 adb_cnt
="$(( ( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^
") ) / 2 ))"
438 adb_cnt
="$(( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^
") ))"
443 if [ -z "${adb_fetchinfo}" ] && [ -s "${adb_rtfile}" ]
445 json_load
"$(cat "${adb_rtfile}" 2>/dev/null)"
447 json_get_var adb_fetchinfo
"fetch_utility"
451 json_add_object
"data"
452 json_add_string
"adblock_status" "${status}"
453 json_add_string
"adblock_version" "${adb_ver}"
454 json_add_string
"blocked_domains" "${adb_cnt}"
455 json_add_string
"fetch_utility" "${adb_fetchinfo}"
456 json_add_string
"dns_backend" "${adb_dns} (${adb_dnsdir})"
457 json_add_string
"last_rundate" "${rundate}"
458 json_add_string
"system_release" "${adb_sysver}"
460 json_dump
> "${adb_rtfile}"
463 # f_status: output runtime information
467 local key keylist value
469 if [ -s "${adb_rtfile}" ]
471 printf "%s\n" "::: adblock runtime information"
472 json_load
"$(cat "${adb_rtfile}" 2>/dev/null)"
474 json_get_keys keylist
475 for key
in ${keylist}
477 json_get_var value
"${key}"
478 printf " + %-15s : %s\n" "${key}" "${value}"
483 # f_log: write to syslog, exit on error
487 local class
="${1}" log_msg
="${2}"
489 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
491 logger -t "adblock-
[${adb_ver}] ${class}" "${log_msg}"
492 if [ "${class}" = "error
" ]
494 logger -t "adblock-
[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
496 if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
508 # main function for block list processing
512 local src_name src_rset shalla_archive enabled url hash_old hash_new
513 local mem_total="$
(awk '/^MemTotal/ {print int($2/1000)}' "/proc/meminfo")"
515 f_log "info
" "start adblock processing ...
"
516 f_log "debug
" "action
: ${adb_action}, manual_mode:${adb_manmode}, backup: ${adb_backup}, dns: ${adb_dns}, fetch: ${adb_fetchinfo}, mem_total: ${mem_total}, force_srt/_dns: ${adb_forcesrt}/${adb_forcedns}"
518 for src_name in ${adb_sources}
520 eval "enabled
=\"\
${enabled_${src_name}}\""
521 eval "url
=\"\
${adb_src_${src_name}}\""
522 eval "src_rset
=\"\
${adb_src_rset_${src_name}}\""
529 f_log "debug
" "name
: ${src_name}, enabled: ${enabled}, url: ${url}, rset: ${src_rset}"
530 if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
536 # manual / backup mode
538 if [ ${adb_manmode} -eq 1 ] && [ "${adb_action}" = "start" ] && [ "${src_name}" != "blacklist" ]
541 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
548 # download block list
550 if [ "${src_name}" = "blacklist" ] && [ -s "${url}" ]
552 cat "${url}" > "${adb_tmpload}"
554 elif [ "${src_name}" = "shalla" ]
556 shalla_archive
="${adb_tmpdir}/shallalist.tar.gz"
557 "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}" 2>/dev
/null
559 if [ ${adb_rc} -eq 0 ]
561 for category
in ${adb_src_cat_shalla}
563 tar -xOzf "${shalla_archive}" "BL/${category}/domains" >> "${adb_tmpload}"
565 if [ ${adb_rc} -ne 0 ]
571 rm -f "${shalla_archive}"
572 rm -rf "${adb_tmpdir}/BL"
574 "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}" 2>/dev
/null
578 # check download result and prepare list output (incl. tld compression, list backup & restore)
580 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
582 awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
583 if [ -s "${adb_tmpfile}" ]
585 f_tldcompression
"${adb_tmpfile}"
586 if [ "${src_name}" != "blacklist" ]
599 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
602 if [ ${adb_rc} -ne 0 ]
611 # hash preparation, whitelist removal and overall sort
613 if [ -f "${adb_dnsdir}/${adb_dnsfile}" ]
615 hash_old
="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
617 if [ -s "${adb_tmpdir}/${adb_dnsfile}" ]
619 if [ ${mem_total} -ge 64 ] || [ ${adb_forcesrt} -eq 1 ]
621 f_tldcompression "${adb_tmpdir}/${adb_dnsfile}"
625 > "${adb_dnsdir}/${adb_dnsfile}"
627 chown "${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
630 # conditional restart of the dns backend and runtime information export
632 hash_new="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
633 if [ -z "${hash_old}" ] || [ -z "${hash_new}" ] || [ "${hash_old}" != "${hash_new}" ]
639 f_jsnupdate "${adb_cnt}"
640 f_log "info
" "block list with overall
${adb_cnt} domains loaded successfully
(${adb_sysver})"
642 f_log "error
" "dns backend restart with active block list failed
"
646 # handle different adblock actions
649 case "${adb_action}" in