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_whitelist
="/etc/adblock/adblock.whitelist"
24 adb_whitelist_rset
="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
25 adb_fetch
="/usr/bin/wget"
26 adb_fetchparm
="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
27 adb_dnslist
="dnsmasq unbound"
28 adb_dnsprefix
="adb_list"
29 adb_rtfile
="/tmp/adb_runtime.json"
34 # f_envload: load adblock environment
38 local services dns_up cnt
=0
40 # source in system libraries
42 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
45 .
"/usr/share/libubox/jshn.sh"
47 f_log
"error" "system libraries not found"
50 # parse global section by callback
55 if [ "${type}" = "adblock" ]
61 eval "${option}=\"${value}\""
68 # parse 'source' sections
72 local value opt section
="${1}" options
="enabled adb_src adb_src_rset adb_src_cat"
73 eval "adb_sources=\"${adb_sources} ${section}\""
76 config_get value
"${section}" "${opt}"
79 eval "${opt}_${section}=\"${value}\""
87 config_foreach parse_config
source
89 # set dns backend environment
91 while [ ${cnt} -le 20 ]
93 services
="$(ubus -S call service list 2>/dev/null)"
94 if [ -n "${services}" ]
96 for dns
in ${adb_dnslist}
98 dns_up
="$(printf "%s
" "${services}" | jsonfilter -l1 -e "@.
${dns}.instances.
*.running
")"
99 if [ "${dns_up}" = "true" ]
104 adb_dnsdir
="${adb_dnsdir:="/tmp/dnsmasq.d"}"
105 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
106 adb_dnsformat
="awk '{print \"local=/\"\$0\"/\"}'"
111 adb_dnsdir
="${adb_dnsdir:="/var/lib/unbound"}"
112 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
113 adb_dnsformat
="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
123 if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
125 f_log
"error" "no active/supported DNS backend found"
128 # force dns to local resolver
130 if [ ${adb_forcedns} -eq 1 ] && [ -z "$(uci -q get firewall.adblock_dns)" ]
132 uci
-q set firewall.adblock_dns
="redirect"
133 uci
-q set firewall.adblock_dns.name
="Adblock DNS"
134 uci
-q set firewall.adblock_dns.src
="lan"
135 uci
-q set firewall.adblock_dns.proto
="tcp udp"
136 uci
-q set firewall.adblock_dns.src_dport
="53"
137 uci
-q set firewall.adblock_dns.dest_port
="53"
138 uci
-q set firewall.adblock_dns.target
="DNAT"
139 elif [ ${adb_forcedns} -eq 0 ] && [ -n "$(uci -q get firewall.adblock_dns)" ]
141 uci
-q delete firewall.adblock_dns
143 if [ -n "$(uci -q changes firewall)" ]
145 uci
-q commit firewall
146 if [ $
(/etc
/init.d
/firewall enabled
; printf "%u" ${?}) -eq 0 ]
148 /etc
/init.d
/firewall reload
>/dev
/null
2>&1
153 # f_envcheck: check/set environment prerequisites
159 # check 'enabled' option
161 if [ ${adb_enabled} -ne 1 ]
163 if [ -n "$(ls -dA "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null)" ]
168 f_log
"info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
172 # check fetch utility
175 if [ -x "${adb_fetch}" ]
177 if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
179 adb_fetchparm
="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
180 elif [ "$(readlink -fn "/bin
/wget
")" = "/bin/busybox" ] ||
[ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ]
182 adb_fetch
="/bin/busybox"
183 adb_fetchparm
="-q -O"
188 if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin
/wget
")" = "/bin/uclient-fetch" ]
190 adb_fetch
="/bin/uclient-fetch"
191 if [ -f "/lib/libustream-ssl.so" ]
193 adb_fetchparm
="-q --timeout=10 --no-check-certificate -O"
194 ssl_lib
="libustream-ssl"
196 adb_fetchparm
="-q --timeout=10 -O"
199 if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
201 f_log
"error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
203 adb_fetchinfo
="${adb_fetch##*/} (${ssl_lib})"
205 # create dns hideout directory
207 if [ ! -d "${adb_dnshidedir}" ]
209 mkdir
-p -m 660 "${adb_dnshidedir}"
210 chown
-R "${adb_dns}":"${adb_dns}" "${adb_dnshidedir}" 2>/dev
/null
212 rm -f "${adb_dnshidedir}/${adb_dnsprefix}"*
215 # create adblock temp file/directory
217 adb_tmpload
="$(mktemp -tu)"
218 adb_tmpfile
="$(mktemp -tu)"
219 adb_tmpdir
="$(mktemp -p /tmp -d)"
221 # prepare whitelist entries
223 if [ -s "${adb_whitelist}" ]
225 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
229 # f_rmtemp: remove temporary files & directories
233 if [ -d "${adb_tmpdir}" ]
235 rm -f "${adb_tmpload}"
236 rm -f "${adb_tmpfile}"
237 rm -rf "${adb_tmpdir}"
241 # f_rmdns: remove dns related files & directories
245 if [ -n "${adb_dns}" ]
247 rm -f "${adb_dnsdir}/${adb_dnsprefix}"*
248 rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
249 rm -rf "${adb_dnshidedir}"
254 # f_dnsrestart: restart the dns backend
258 local dns_up mem_free cnt
=0
260 "/etc/init.d/${adb_dns}" restart
>/dev
/null
2>&1
261 while [ ${cnt} -le 10 ]
263 dns_up
="$(ubus -S call service list "{\"name
\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@.
${adb_dns}.instances.
*.running
")"
264 if [ "${dns_up}" = "true" ]
266 mem_free
="$(awk '/^MemFree/ {print int($2/1000)}' "/proc
/meminfo
")"
267 if [ ${mem_free} -ge ${adb_minfree} ]
278 # f_list: backup/restore/remove block lists
282 local mode
="${1}" in_rc
="${adb_rc}" cnt
=0
286 cnt
="$(wc -l < "${adb_tmpfile}")"
287 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
289 gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
294 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
296 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
297 if [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
299 gunzip
-cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
305 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
306 if [ -d "${adb_backupdir}" ]
308 rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
313 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
315 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpfile}" | eval "${adb_dnsformat}" > "${adb_dnsfile}"
317 eval "${adb_dnsformat}" "${adb_tmpfile}" > "${adb_dnsfile}"
322 f_log
"debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
325 # f_switch: suspend/resume adblock processing
329 if [ -d "${adb_dnshidedir}" ]
331 local source target status mode
="${1}"
332 local dns_active
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
333 local dns_passive
="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
335 if [ -n "${dns_active}" ] && [ "${mode}" = "suspend" ]
337 source="${adb_dnsdir}/${adb_dnsprefix}"
338 target
="${adb_dnshidedir}"
340 elif [ -n "${dns_passive}" ] && [ "${mode}" = "resume" ]
342 source="${adb_dnshidedir}/${adb_dnsprefix}"
343 target
="${adb_dnsdir}"
346 if [ -n "${status}" ]
348 mv -f "${source}"* "${target}"
350 f_log
"info " "adblock processing ${status}"
355 # f_query: query block lists for certain (sub-)domains
359 local search result cnt
361 local tld
="${domain#*.}"
362 local dns_active
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
364 if [ -z "${dns_active}" ]
366 printf "%s\n" "::: no active block lists found, please start / resume adblock first"
367 elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
369 printf "%s\n" "::: invalid domain input, please submit a specific (sub-)domain, e.g. 'www.abc.xyz'"
372 while [ "${domain}" != "${tld}" ]
374 search
="${domain//./\.}"
375 result
="$(grep -Hm1 "[/\"\.
]${search}[/\"]" "${adb_dnsprefix}"* | awk -F ':|=|/|\"' '{printf(" %-20s : %s\n",$1,$4)}')"
376 printf "%s\n" "::: distinct results for domain '${domain}'"
377 printf "%s\n" "${result:=" no match"}"
384 # f_status: output runtime information
388 local key keylist value
390 if [ -s "${adb_rtfile}" ]
392 local dns_active="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
393 local dns_passive="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
395 if [ -n "${dns_active}" ]
398 elif [ -n "${dns_passive}" ] || [ -z "${dns_active}" ]
400 value="no domains blocked"
402 printf "%s\n" "::: adblock runtime information"
403 printf " %-15s : %s\n" "status" "${value}"
404 json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
406 json_get_keys keylist
407 for key in ${keylist}
409 json_get_var value "${key}"
410 printf " %-15s : %s\n" "${key}" "${value}"
415 # f_log: write to syslog, exit on error
419 local class="${1}" log_msg="${2}"
421 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
423 logger -t "adblock-[${adb_ver}] ${class}" "${log_msg}"
424 if [ "${class}" = "error" ]
426 logger -t "adblock-[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
428 if [ -n "$(ls -dA "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null)" ]
438 # main function for block list processing
442 local src_name src_rset shalla_archive enabled url cnt sum_cnt=0
443 local mem_total="$(awk '/^MemTotal
/ {print int
($2/1000)}' "/proc/meminfo")"
445 f_log "info " "start adblock processing ..."
446 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}"
448 for src_name in ${adb_sources}
450 eval "enabled=\"\${enabled_${src_name}}\""
451 eval "url=\"\${adb_src_${src_name}}\""
452 eval "src_rset=\"\${adb_src_rset_${src_name}}\""
453 adb_dnsfile="${adb_tmpdir}/${adb_dnsprefix}.${src_name}"
460 f_log "debug" "name: ${src_name}, enabled: ${enabled}, url: ${url}, rset: ${src_rset}"
461 if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
469 if [ ${adb_manmode} -eq 1 ] && [ -z "${adb_action}" ]
473 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
480 # download block list
482 if [ "${src_name}" = "blacklist" ]
484 cat "${url}" > "${adb_tmpload}"
486 elif [ "${src_name}" = "shalla" ]
488 shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
489 "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}"
491 if [ ${adb_rc} -eq 0 ]
493 for category in ${adb_src_cat_shalla}
495 tar -xOzf "${shalla_archive}" "BL/${category}/domains" >> "${adb_tmpload}"
497 if [ ${adb_rc} -ne 0 ]
503 rm -f "${shalla_archive}"
504 rm -rf "${adb_tmpdir}/BL"
506 "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}"
510 # check download result and prepare domain output (incl. tld compression, list backup & restore)
512 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
514 awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
515 if [ -s "${adb_tmpfile}" ]
517 awk -F "." '{for(f
=NF
;f
> 1;f--
) printf "%s.", $f;print
$1}' "${adb_tmpfile}" 2>/dev/null | sort -u > "${adb_tmpload}"
518 awk '{if(NR
==1){tld
=$NF};while(getline
){if($NF !~ tld
"\\."){print tld
;tld
=$NF}}print tld
}' "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
519 awk -F "." '{for(f
=NF
;f
> 1;f--
) printf "%s.", $f;print
$1}' "${adb_tmpfile}" 2>/dev/null > "${adb_tmpload}"
520 mv -f "${adb_tmpload}" "${adb_tmpfile}"
529 # remove whitelist domains, final list preparation
531 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
534 if [ ${adb_rc} -ne 0 ]
545 for src_name in $(ls -dASr "${adb_tmpdir}/${adb_dnsprefix}"* 2>/dev/null)
547 if [ ${mem_total} -ge 64 ] || [ ${adb_forcesrt} -eq 1 ]
549 if [ -s "${adb_tmpdir}/blocklist.overall" ]
551 sort "${adb_tmpdir}/blocklist.overall" "${adb_tmpdir}/blocklist.overall" "${src_name}" | uniq -u > "${adb_tmpdir}/tmp.blocklist"
552 mv -f "${adb_tmpdir}/tmp.blocklist" "${src_name}"
554 cat "${src_name}" >> "${adb_tmpdir}/blocklist.overall"
556 cnt="$(wc -l < "${src_name}")"
557 sum_cnt=$((sum_cnt + cnt))
560 # restart the dns backend and export runtime information
562 mv -f "${adb_tmpdir}/${adb_dnsprefix}"* "${adb_dnsdir}" 2>/dev/null
563 chown "${adb_dns}":"${adb_dns}" "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null
569 json_add_object "data"
570 json_add_string "adblock_version" "${adb_ver}"
571 json_add_string "blocked_domains" "${sum_cnt}"
572 json_add_string "fetch_info" "${adb_fetchinfo}"
573 json_add_string "dns_backend" "${adb_dns}"
574 json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
575 json_add_string "system" "${adb_sysver}"
577 json_dump > "${adb_rtfile}"
578 f_log "info " "block lists with overall ${sum_cnt} domains loaded successfully (${adb_sysver})"
580 f_log "error" "dns backend restart with active block lists failed"
584 # handle different adblock actions
587 case "${adb_action}" in