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')"
19 adb_whitelist
="/etc/adblock/adblock.whitelist"
20 adb_whitelist_rset
="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
21 adb_fetch
="/usr/bin/wget"
22 adb_fetchparm
="--no-config --quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
23 adb_dnslist
="dnsmasq unbound"
24 adb_dnsprefix
="adb_list"
25 adb_rtfile
="/tmp/adb_runtime.json"
27 # f_envload: load adblock environment
33 # source in system library
35 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
38 .
"/usr/share/libubox/jshn.sh"
40 f_log
"error" "system libraries not found"
43 # set dns backend environment
45 while [ ${cnt} -le 20 ]
47 for dns
in ${adb_dnslist}
49 dns_up
="$(ubus -S call service list "{\"name
\":\"${dns}\"}" | jsonfilter -l1 -e "@.
${dns}.instances.
*.running
")"
50 if [ "${dns_up}" = "true" ]
55 adb_dnsdir
="/tmp/dnsmasq.d"
56 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
57 adb_dnsformat
="awk '{print \"local=/\"\$0\"/\"}'"
62 adb_dnsdir
="/var/lib/unbound"
63 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
64 adb_dnsformat
="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
73 if [ -z "${adb_dns}" ]
75 f_log
"error" "no active/supported DNS backend found"
78 # parse global section by callback
83 if [ "${type}" = "adblock" ]
89 eval "${option}=\"${value}\""
96 # parse 'source' section
100 local value opt section
="${1}" options
="enabled adb_src adb_src_rset adb_src_cat"
101 eval "adb_sources=\"${adb_sources} ${section}\""
102 for opt
in ${options}
104 config_get value
"${section}" "${opt}"
107 eval "${opt}_${section}=\"${value}\""
112 # load adblock config
115 config_foreach parse_config
source
118 # f_envcheck: check/set environment prerequisites
124 # check 'enabled' option
126 if [ ${adb_enabled} -ne 1 ]
128 if [ -n "$(ls -dA "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null)" ]
133 f_log
"info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
137 # check fetch utility
140 if [ -x "${adb_fetch}" ]
142 if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
144 adb_fetchparm
="--no-config --quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
145 elif [ "$(readlink -fn "/bin
/wget
")" = "/bin/busybox" ] ||
[ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ]
147 adb_fetch
="/bin/busybox"
148 adb_fetchparm
="-q -O"
153 if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin
/wget
")" = "/bin/uclient-fetch" ]
155 adb_fetch
="/bin/uclient-fetch"
156 if [ -f "/lib/libustream-ssl.so" ]
158 adb_fetchparm
="-q --timeout=10 --no-check-certificate -O"
159 ssl_lib
="libustream-ssl"
161 adb_fetchparm
="-q --timeout=10 -O"
164 if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
166 f_log
"error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
168 adb_fetchinfo
="${adb_fetch##*/} (${ssl_lib})"
170 # create dns hideout directory
172 if [ ! -d "${adb_dnshidedir}" ]
174 mkdir
-p -m 660 "${adb_dnshidedir}"
175 chown
-R "${adb_dns}":"${adb_dns}" "${adb_dnshidedir}" 2>/dev
/null
177 rm -f "${adb_dnshidedir}/${adb_dnsprefix}"*
180 # create adblock temp file/directory
182 adb_tmpload
="$(mktemp -tu)"
183 adb_tmpfile
="$(mktemp -tu)"
184 adb_tmpdir
="$(mktemp -p /tmp -d)"
186 # prepare whitelist entries
188 if [ -s "${adb_whitelist}" ]
190 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
194 # f_rmtemp: remove temporary files & directories
198 if [ -d "${adb_tmpdir}" ]
200 rm -f "${adb_tmpload}"
201 rm -f "${adb_tmpfile}"
202 rm -rf "${adb_tmpdir}"
206 # f_rmdns: remove dns related files & directories
210 if [ -n "${adb_dns}" ]
212 rm -f "${adb_dnsdir}/${adb_dnsprefix}"*
213 rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
214 rm -rf "${adb_dnshidedir}"
219 # f_dnsrestart: restart the dns backend
225 "/etc/init.d/${adb_dns}" restart
>/dev
/null
2>&1
226 while [ ${cnt} -le 10 ]
228 adb_dnsup
="$(ubus -S call service list "{\"name
\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@.
${adb_dns}.instances.
*.running
")"
229 if [ "${adb_dnsup}" = "true" ]
238 # f_list: backup/restore/remove block lists
242 local mode
="${1}" in_rc
="${adb_rc}" cnt
=0
246 cnt
="$(wc -l < "${adb_tmpfile}")"
247 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
249 gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
254 if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
256 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
257 if [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
259 gunzip
-cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
265 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
266 if [ -d "${adb_backupdir}" ]
268 rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
273 f_log
"debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
276 # f_switch: suspend/resume adblock processing
280 if [ -d "${adb_dnshidedir}" ]
282 local source target status mode
="${1}"
283 local dns_active
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
284 local dns_passive
="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
286 if [ -n "${dns_active}" ] && [ "${mode}" = "suspend" ]
288 source="${adb_dnsdir}/${adb_dnsprefix}"
289 target
="${adb_dnshidedir}"
291 elif [ -n "${dns_passive}" ] && [ "${mode}" = "resume" ]
293 source="${adb_dnshidedir}/${adb_dnsprefix}"
294 target
="${adb_dnsdir}"
297 if [ -n "${status}" ]
299 mv -f "${source}"* "${target}"
301 f_log
"info " "adblock processing ${status}"
306 # f_query: query block lists for certain (sub-)domains
310 local search result cnt
312 local tld
="${domain#*.}"
313 local dns_active
="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
315 if [ -z "${dns_active}" ]
317 printf "%s\n" "::: no active block lists found, please start / resume adblock first"
318 elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
320 printf "%s\n" "::: invalid domain input, please submit a specific (sub-)domain, e.g. 'www.abc.xyz'"
323 while [ "${domain}" != "${tld}" ]
325 search
="${domain//./\.}"
326 result
="$(grep -Hm1 "[/\"\.
]${search}[/\"]" "${adb_dnsprefix}"* | awk -F ':|=|/|\"' '{printf(" %-20s : %s\n",$1,$4)}')"
327 printf "%s\n" "::: distinct results for domain '${domain}'"
328 printf "%s\n" "${result:=" no match"}"
335 # f_status: output runtime information
339 local key keylist value
341 if [ -s "${adb_rtfile}" ]
343 local dns_active="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
344 local dns_passive="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
346 if [ -n "${dns_active}" ]
349 elif [ -n "${dns_passive}" ] || [ -z "${dns_active}" ]
351 value="no domains blocked"
353 printf "%s\n" "::: adblock runtime information"
354 printf " %-15s : %s\n" "status" "${value}"
355 json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
357 json_get_keys keylist
358 for key in ${keylist}
360 json_get_var value ${key}
361 printf " %-15s : %s\n" "${key}" "${value}"
366 # f_log: write to syslog, exit on error
370 local class="${1}" log_msg="${2}"
372 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
374 logger -t "adblock-[${adb_ver}] ${class}" "${log_msg}"
375 if [ "${class}" = "error" ]
377 logger -t "adblock-[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
379 if [ -n "$(ls -dA "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null)" ]
389 # main function for block list processing
393 local enabled url cnt sum_cnt=0 mem_total=0
394 local src_name src_rset shalla_archive
395 mem_total="$(awk '$1 ~
/^MemTotal
/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
397 f_log "info " "start adblock processing ..."
399 for src_name in ${adb_sources}
401 eval "enabled=\"\${enabled_${src_name}}\""
402 eval "url=\"\${adb_src_${src_name}}\""
403 eval "src_rset=\"\${adb_src_rset_${src_name}}\""
404 adb_dnsfile="${adb_tmpdir}/${adb_dnsprefix}.${src_name}"
411 if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
417 # download block list
419 f_log "debug" "name: ${src_name}, enabled: ${enabled}, backup: ${adb_backup}, dns: ${adb_dns}, fetch: ${adb_fetchinfo}, memory: ${mem_total}"
420 if [ "${src_name}" = "blacklist" ]
422 cat "${url}" 2>/dev/null > "${adb_tmpload}"
424 elif [ "${src_name}" = "shalla" ]
426 shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
427 "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}" 2>/dev/null
429 if [ ${adb_rc} -eq 0 ]
431 for category in ${adb_src_cat_shalla}
433 tar -xOzf "${shalla_archive}" BL/${category}/domains >> "${adb_tmpload}"
435 if [ ${adb_rc} -ne 0 ]
441 rm -f "${shalla_archive}"
442 rm -rf "${adb_tmpdir}/BL"
444 "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}" 2>/dev/null
448 # check download result and prepare domain output (incl. tld compression, list backup & restore)
450 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
452 awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
453 if [ -s "${adb_tmpfile}" ]
455 awk -F "." '{for(f
=NF
;f
> 1;f--
) printf "%s.", $f;print
$1}' "${adb_tmpfile}" 2>/dev/null | sort -u > "${adb_tmpload}"
456 awk '{if(NR
==1){tld
=$NF};while(getline
){if($NF !~ tld
"\\."){print tld
;tld
=$NF}}print tld
}' "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
457 awk -F "." '{for(f
=NF
;f
> 1;f--
) printf "%s.", $f;print
$1}' "${adb_tmpfile}" 2>/dev/null > "${adb_tmpload}"
458 mv -f "${adb_tmpload}" "${adb_tmpfile}"
467 # remove whitelist domains, final list preparation
469 if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
471 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
473 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpfile}" 2>/dev/null | eval "${adb_dnsformat}" > "${adb_dnsfile}"
475 cat "${adb_tmpfile}" 2>/dev/null | eval "${adb_dnsformat}" > "${adb_dnsfile}"
478 if [ ${adb_rc} -ne 0 ]
489 for src_name in $(ls -dASr "${adb_tmpdir}/${adb_dnsprefix}"* 2>/dev/null)
491 if [ ${mem_total} -ge 64000 ]
493 if [ -s "${adb_tmpdir}/blocklist.overall" ]
495 sort "${adb_tmpdir}/blocklist.overall" "${adb_tmpdir}/blocklist.overall" "${src_name}" | uniq -u > "${adb_tmpdir}/tmp.blocklist"
496 mv -f "${adb_tmpdir}/tmp.blocklist" "${src_name}"
498 cat "${src_name}" >> "${adb_tmpdir}/blocklist.overall"
500 cnt="$(wc -l < "${src_name}")"
501 sum_cnt=$((sum_cnt + cnt))
504 # restart the dns backend and export runtime information
506 mv -f "${adb_tmpdir}/${adb_dnsprefix}"* "${adb_dnsdir}" 2>/dev/null
507 chown "${adb_dns}":"${adb_dns}" "${adb_dnsdir}/${adb_dnsprefix}"* 2>/dev/null
510 if [ "${adb_dnsup}" = "true" ]
513 json_add_object "data"
514 json_add_string "adblock_version" "${adb_ver}"
515 json_add_string "blocked_domains" "${sum_cnt}"
516 json_add_string "fetch_info" "${adb_fetchinfo}"
517 json_add_string "dns_backend" "${adb_dns}"
518 json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
519 json_add_string "system" "${adb_sysver}"
521 json_dump > "${adb_rtfile}"
522 f_log "info " "block lists with overall ${sum_cnt} domains loaded successfully (${adb_sysver})"
524 f_log "error" "dns backend restart with active block lists failed"
528 # handle different adblock actions