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')"
21 adb_whitelist
="/etc/adblock/adblock.whitelist"
22 adb_whitelist_rset
="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
23 adb_fetch
="/usr/bin/wget"
24 adb_fetchparm
="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
25 adb_dnslist
="dnsmasq unbound"
26 adb_dnsprefix
="adb_list"
27 adb_rtfile
="/tmp/adb_runtime.json"
32 # f_envload: load adblock environment
36 local services dns_up cnt
=0
38 # source in system library
40 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
43 .
"/usr/share/libubox/jshn.sh"
45 f_log
"error" "system libraries not found"
48 # parse global section by callback
53 if [ "${type}" = "adblock" ]
59 eval "${option}=\"${value}\""
66 # parse 'source' sections
70 local value opt section
="${1}" options
="enabled adb_src adb_src_rset adb_src_cat"
71 eval "adb_sources=\"${adb_sources} ${section}\""
74 config_get value
"${section}" "${opt}"
77 eval "${opt}_${section}=\"${value}\""
85 config_foreach parse_config
source
87 # set dns backend environment
89 while [ ${cnt} -le 20 ]
91 services
="$(ubus -S call service list 2>/dev/null)"
92 if [ -n "${services}" ]
94 for dns
in ${adb_dnslist}
96 dns_up
="$(printf "%s
" "${services}" | jsonfilter -l1 -e "@.
${dns}.instances.
*.running
")"
97 if [ "${dns_up}" = "true" ]
102 adb_dnsdir
="${adb_dnsdir:="/tmp/dnsmasq.d"}"
103 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
104 adb_dnsformat
="awk '{print \"local=/\"\$0\"/\"}'"
109 adb_dnsdir
="${adb_dnsdir:="/var/lib/unbound"}"
110 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
111 adb_dnsformat
="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
121 if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
123 f_log
"error" "no active/supported DNS backend found"
126 # force dns to local resolver
128 if [ ${adb_forcedns} -eq 1 ] && [ -z "$(uci -q get firewall.adblock_dns)" ]
130 uci
-q set firewall.adblock_dns
="redirect"
131 uci
-q set firewall.adblock_dns.name
="Adblock DNS"
132 uci
-q set firewall.adblock_dns.src
="lan"
133 uci
-q set firewall.adblock_dns.proto
="tcp udp"
134 uci
-q set firewall.adblock_dns.src_dport
="53"
135 uci
-q set firewall.adblock_dns.dest_port
="53"
136 uci
-q set firewall.adblock_dns.target
="DNAT"
137 elif [ ${adb_forcedns} -eq 0 ] && [ -n "$(uci -q get firewall.adblock_dns)" ]
139 uci
-q delete firewall.adblock_dns
141 if [ -n "$(uci -q changes firewall)" ]
143 uci
-q commit firewall
144 if [ $
(/etc
/init.d
/firewall enabled
; printf "%u" ${?}) -eq 0 ]
146 /etc
/init.d
/firewall reload
>/dev
/null
2>&1
151 # f_envcheck: check/set environment prerequisites
157 # check 'enabled' option
159 if [ ${adb_enabled} -ne 1 ]
161 if [ -n "$(ls -dA "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null)" ]
166 f_log
"info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
170 # check fetch utility
173 if [ -x "${adb_fetch}" ]
175 if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
177 adb_fetchparm
="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
178 elif [ "$(readlink -fn "/bin
/wget
")" = "/bin/busybox" ] ||
[ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ]
180 adb_fetch
="/bin/busybox"
181 adb_fetchparm
="-q -O"
186 if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin
/wget
")" = "/bin/uclient-fetch" ]
188 adb_fetch
="/bin/uclient-fetch"
189 if [ -f "/lib/libustream-ssl.so" ]
191 adb_fetchparm
="-q --timeout=10 --no-check-certificate -O"
192 ssl_lib
="libustream-ssl"
194 adb_fetchparm
="-q --timeout=10 -O"
197 if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
199 f_log
"error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
201 adb_fetchinfo
="${adb_fetch##*/} (${ssl_lib})"
203 # create dns hideout directory
205 if [ ! -d "${adb_dnshidedir}" ]
207 mkdir
-p -m 660 "${adb_dnshidedir}"
208 chown
-R "${adb_dns}":"${adb_dns}" "${adb_dnshidedir}" 2>/dev
/null
210 rm -f "${adb_dnshidedir}/${adb_dnsprefix}"*
213 # create adblock temp file/directory
215 adb_tmpload
="$(mktemp -tu)"
216 adb_tmpfile
="$(mktemp -tu)"
217 adb_tmpdir
="$(mktemp -p /tmp -d)"
219 # prepare whitelist entries
221 if [ -s "${adb_whitelist}" ]
223 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
227 # f_rmtemp: remove temporary files & directories
231 if [ -d "${adb_tmpdir}" ]
233 rm -f "${adb_tmpload}"
234 rm -f "${adb_tmpfile}"
235 rm -rf "${adb_tmpdir}"
239 # f_rmdns: remove dns related files & directories
243 if [ -n "${adb_dns}" ]
245 rm -f "${adb_dnsdir}/${adb_dnsprefix}"*
246 rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
247 rm -rf "${adb_dnshidedir}"
252 # f_dnsrestart: restart the dns backend
258 "/etc/init.d/${adb_dns}" restart
>/dev
/null
2>&1
259 while [ ${cnt} -le 10 ]
261 adb_dnsup
="$(ubus -S call service list "{\"name
\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@.
${adb_dns}.instances.
*.running
")"
262 if [ "${adb_dnsup}" = "true" ]
271 # f_list: backup/restore/remove block lists
275 local mode
="${1}" in_rc
="${adb_rc}" cnt
=0
279 cnt
="$(wc -l < "${adb_tmpfile}")"
280 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
282 gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
287 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
289 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
290 if [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
292 gunzip
-cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
298 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
299 if [ -d "${adb_backupdir}" ]
301 rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
306 f_log
"debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
309 # f_switch: suspend/resume adblock processing
313 if [ -d "${adb_dnshidedir}" ]
315 local source target status mode
="${1}"
316 local dns_active
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
317 local dns_passive
="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
319 if [ -n "${dns_active}" ] && [ "${mode}" = "suspend" ]
321 source="${adb_dnsdir}/${adb_dnsprefix}"
322 target
="${adb_dnshidedir}"
324 elif [ -n "${dns_passive}" ] && [ "${mode}" = "resume" ]
326 source="${adb_dnshidedir}/${adb_dnsprefix}"
327 target
="${adb_dnsdir}"
330 if [ -n "${status}" ]
332 mv -f "${source}"* "${target}"
334 f_log
"info " "adblock processing ${status}"
339 # f_query: query block lists for certain (sub-)domains
343 local search result cnt
345 local tld
="${domain#*.}"
346 local dns_active
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
348 if [ -z "${dns_active}" ]
350 printf "%s\n" "::: no active block lists found, please start / resume adblock first"
351 elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
353 printf "%s\n" "::: invalid domain input, please submit a specific (sub-)domain, e.g. 'www.abc.xyz'"
356 while [ "${domain}" != "${tld}" ]
358 search
="${domain//./\.}"
359 result
="$(grep -Hm1 "[/\"\.
]${search}[/\"]" "${adb_dnsprefix}"* | awk -F ':|=|/|\"' '{printf(" %-20s : %s\n",$1,$4)}')"
360 printf "%s\n" "::: distinct results for domain '${domain}'"
361 printf "%s\n" "${result:=" no match"}"
368 # f_status: output runtime information
372 local key keylist value
374 if [ -s "${adb_rtfile}" ]
376 local dns_active="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
377 local dns_passive="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
379 if [ -n "${dns_active}" ]
382 elif [ -n "${dns_passive}" ] || [ -z "${dns_active}" ]
384 value="no domains blocked"
386 printf "%s\n" "::: adblock runtime information"
387 printf " %-15s : %s\n" "status" "${value}"
388 json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
390 json_get_keys keylist
391 for key in ${keylist}
393 json_get_var value "${key}"
394 printf " %-15s : %s\n" "${key}" "${value}"
399 # f_log: write to syslog, exit on error
403 local class="${1}" log_msg="${2}"
405 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
407 logger -t "adblock-[${adb_ver}] ${class}" "${log_msg}"
408 if [ "${class}" = "error" ]
410 logger -t "adblock-[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
412 if [ -n "$(ls -dA "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null)" ]
422 # main function for block list processing
426 local enabled url cnt sum_cnt=0 mem_total=0
427 local src_name src_rset shalla_archive
428 mem_total="$(awk '$1 ~
/^MemTotal
/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
430 f_log "info " "start adblock processing ..."
431 f_log "debug" "action: ${adb_action}, backup: ${adb_backup}, dns: ${adb_dns}, fetch: ${adb_fetchinfo}, memory: ${mem_total}, force srt/dns: ${adb_forcesrt}/${adb_forcedns}"
433 for src_name in ${adb_sources}
435 eval "enabled=\"\${enabled_${src_name}}\""
436 eval "url=\"\${adb_src_${src_name}}\""
437 eval "src_rset=\"\${adb_src_rset_${src_name}}\""
438 adb_dnsfile="${adb_tmpdir}/${adb_dnsprefix}.${src_name}"
445 f_log "debug" "name: ${src_name}, enabled: ${enabled}, url: ${url}, rset: ${src_rset}"
446 if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
452 # download block list
454 if [ "${src_name}" = "blacklist" ]
456 cat "${url}" 2>/dev/null > "${adb_tmpload}"
458 elif [ "${src_name}" = "shalla" ]
460 shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
461 "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}" 2>/dev/null
463 if [ ${adb_rc} -eq 0 ]
465 for category in ${adb_src_cat_shalla}
467 tar -xOzf "${shalla_archive}" "BL/${category}/domains" >> "${adb_tmpload}"
469 if [ ${adb_rc} -ne 0 ]
475 rm -f "${shalla_archive}"
476 rm -rf "${adb_tmpdir}/BL"
478 "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}" 2>/dev/null
482 # check download result and prepare domain output (incl. tld compression, list backup & restore)
484 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
486 awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
487 if [ -s "${adb_tmpfile}" ]
489 awk -F "." '{for(f
=NF
;f
> 1;f--
) printf "%s.", $f;print
$1}' "${adb_tmpfile}" 2>/dev/null | sort -u > "${adb_tmpload}"
490 awk '{if(NR
==1){tld
=$NF};while(getline
){if($NF !~ tld
"\\."){print tld
;tld
=$NF}}print tld
}' "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
491 awk -F "." '{for(f
=NF
;f
> 1;f--
) printf "%s.", $f;print
$1}' "${adb_tmpfile}" 2>/dev/null > "${adb_tmpload}"
492 mv -f "${adb_tmpload}" "${adb_tmpfile}"
501 # remove whitelist domains, final list preparation
503 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
505 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
507 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpfile}" 2>/dev/null | eval "${adb_dnsformat}" > "${adb_dnsfile}"
509 eval "${adb_dnsformat}" "${adb_tmpfile}" > "${adb_dnsfile}"
512 if [ ${adb_rc} -ne 0 ]
523 for src_name in $(ls -dASr "${adb_tmpdir}/${adb_dnsprefix}"* 2>/dev/null)
525 if [ ${mem_total} -ge 64000 ] || [ ${adb_forcesrt} -eq 1 ]
527 if [ -s "${adb_tmpdir}/blocklist.overall" ]
529 sort "${adb_tmpdir}/blocklist.overall" "${adb_tmpdir}/blocklist.overall" "${src_name}" | uniq -u > "${adb_tmpdir}/tmp.blocklist"
530 mv -f "${adb_tmpdir}/tmp.blocklist" "${src_name}"
532 cat "${src_name}" >> "${adb_tmpdir}/blocklist.overall"
534 cnt="$(wc -l < "${src_name}")"
535 sum_cnt=$((sum_cnt + cnt))
538 # restart the dns backend and export runtime information
540 mv -f "${adb_tmpdir}/${adb_dnsprefix}"* "${adb_dnsdir}" 2>/dev/null
541 chown "${adb_dns}":"${adb_dns}" "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null
544 if [ "${adb_dnsup}" = "true" ]
547 json_add_object "data"
548 json_add_string "adblock_version" "${adb_ver}"
549 json_add_string "blocked_domains" "${sum_cnt}"
550 json_add_string "fetch_info" "${adb_fetchinfo}"
551 json_add_string "dns_backend" "${adb_dns}"
552 json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
553 json_add_string "system" "${adb_sysver}"
555 json_dump > "${adb_rtfile}"
556 f_log "info " "block lists with overall ${sum_cnt} domains loaded successfully (${adb_sysver})"
558 f_log "error" "dns backend restart with active block lists failed"
562 # handle different adblock actions
565 case "${adb_action}" in