2 # function library used by adblock-update.sh
3 # written by Dirk Brenken (dev@brenken.org)
8 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
12 adb_nullportssl
="65535"
13 adb_nullipv4
="198.18.0.1"
14 adb_nullipv6
="::ffff:c612:0001"
15 adb_whitelist
="/etc/adblock/adblock.whitelist"
16 adb_whitelist_rset
="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
17 adb_dnsdir
="/tmp/dnsmasq.d"
18 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
19 adb_dnsprefix
="adb_list"
25 adb_uci
="$(which uci)"
27 # f_envload: load adblock environment
31 # source in system function library
33 if [ -r "/lib/functions.sh" ]
38 f_log
"system function library not found, please check your installation"
42 # source in system network library
44 if [ -r "/lib/functions/network.sh" ]
46 .
"/lib/functions/network.sh"
49 f_log
"system network library not found, please check your installation"
53 # check opkg availability
55 if [ -f "/var/lock/opkg.lock" ]
58 f_log
"adblock installation finished successfully, 'opkg' currently locked by package installer"
62 # uci function to parse global section by callback
67 if [ "${type}" = "adblock" ]
73 eval "${option}=\"${value}\""
80 # uci function to parse 'service' and 'source' sections
84 local value opt section
="${1}" options
="enabled adb_dir adb_src adb_src_rset adb_src_cat"
85 if [ "${section}" != "backup" ]
87 eval "adb_sources=\"${adb_sources} ${section}\""
91 config_get value
"${section}" "${opt}"
94 eval "${opt}_${section}=\"${value}\""
99 # load adblock config and start parsing functions
102 config_foreach parse_config service
103 config_foreach parse_config
source
107 network_get_ipaddr adb_ipv4
"${adb_lanif}"
108 network_get_ipaddr6 adb_ipv6
"${adb_lanif}"
109 network_get_device adb_landev
"${adb_lanif}"
110 network_find_wan adb_wanif4
111 network_find_wan6 adb_wanif6
114 # f_envcheck: check/set environment prerequisites
120 # check 'enabled' & 'version' config options
122 if [ -z "${adb_enabled}" ] || [ -z "${adb_cfgver}" ] || [ "${adb_cfgver%%.*}" != "${adb_mincfgver%%.*}" ]
125 f_log
"outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
127 elif [ "${adb_cfgver#*.}" != "${adb_mincfgver#*.}" ]
131 if [ "${adb_enabled}" != "1" ]
134 f_log
"adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service"
138 # get list with all installed packages
140 pkg_list
="$(opkg list-installed)"
141 if [ -z "${pkg_list}" ]
144 f_log
"empty 'opkg' package list, please check your installation"
147 adb_sysver
="$(printf "${pkg_list}" | grep "^base-files
-")"
148 adb_sysver
="${adb_sysver##*-}"
150 # get lan ip addresses
152 if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
155 f_log
"no valid IPv4/IPv6 configuration found (${adb_lanif}), please set 'adb_lanif' manually"
159 # check logical update interfaces (with default route)
161 if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
163 adb_wanif4
="${adb_lanif}"
168 if [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
170 adb_nullipv4
="${adb_ipv4}"
171 adb_nullipv6
="${adb_ipv6}"
172 if [ -n "$(${adb_uci} -q get uhttpd.main.listen_http | grep -o ":80$
")" ] ||
173 [ -n "$(${adb_uci} -q get uhttpd.main.listen_https | grep -o ":443$
")" ]
176 f_log
"AP mode detected, please set local LuCI instance to ports <> 80/443"
183 check
="$(${adb_uci} -q get bcp38.@bcp38[0].enabled)"
184 if [ "${check}" = "1" ]
186 if [ -n "$(${adb_uci} -q get bcp38.@bcp38[0].match | grep -Fo "${adb_nullipv4%.*}")" ]
189 f_log
"please whitelist '${adb_nullipv4}' in your bcp38 configuration to use adblock"
195 # check general package dependencies
200 f_depend
"iptables -"
201 f_depend
"kmod-ipt-nat -"
202 f_depend
"firewall -"
205 # check ipv6 related package dependencies
207 if [ -n "${adb_wanif6}" ]
209 f_depend
"ip6tables -" "true"
210 if [ "${package_ok}" = "false" ]
212 f_log
"package 'ip6tables' not found, IPv6 support will be disabled"
215 f_depend
"kmod-ipt-nat6 -" "true"
216 if [ "${package_ok}" = "false" ]
218 f_log
"package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
224 # check uclient-fetch/wget dependencies
226 f_depend
"uclient-fetch -" "true"
227 if [ "${package_ok}" = "true" ]
229 f_depend
"libustream-polarssl -" "true"
230 if [ "${package_ok}" = "false" ]
232 f_depend
"libustream-\(mbedtls\|openssl\|cyassl\) -" "true"
233 if [ "${package_ok}" = "true" ]
235 adb_fetch
="$(which uclient-fetch)"
236 fetch_parm
="-q --timeout=${adb_fetchttl}"
237 response_parm
="--spider"
241 if [ -z "${adb_fetch}" ]
243 f_depend
"wget -" "true"
244 if [ "${package_ok}" = "true" ]
246 adb_fetch
="$(which wget)"
247 fetch_parm
="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=${adb_fetchttl} --connect-timeout=${adb_fetchttl} --read-timeout=${adb_fetchttl}"
248 response_parm
="--spider --server-response"
251 f_log
"please install 'uclient-fetch' or 'wget' with ssl support to use adblock"
256 # check ca-certificate package and set fetch parm accordingly
258 f_depend
"ca-certificates -" "true"
259 if [ "${package_ok}" = "false" ]
261 fetch_parm
="${fetch_parm} --no-check-certificate"
264 # start normal processing/logging
266 f_log
"domain adblock processing started (${adb_scriptver}, ${adb_sysver}, $(/bin/date "+%d.
%m.
%Y
%H
:%M
:%S
"))"
268 # log partially outdated config
270 if [ "${outdated_ok}" = "true" ]
272 f_log
"partially outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
277 if [ "${apmode_ok}" = "true" ]
279 f_log
"AP mode enabled"
282 # set/log restricted mode
284 if [ "${adb_restricted}" = "1" ]
286 adb_uci
="$(which true)"
287 f_log
"Restricted mode enabled"
290 # check dns hideout directory
292 if [ -d "${adb_dnshidedir}" ]
294 mv_done
="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec mv -f "{}" "${adb_dnsdir}" \;)"
296 mkdir
-p -m 660 "${adb_dnshidedir}"
299 # check adblock temp directory
301 adb_tmpfile
="$(mktemp -tu)"
302 adb_tmpdir
="$(mktemp -p /tmp -d)"
303 if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
305 f_space
"${adb_tmpdir}"
306 if [ "${space_ok}" = "false" ]
308 if [ $
((av_space
)) -le 2000 ]
311 f_log
"not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)"
314 f_log
"not enough free space to handle all block list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
319 f_log
"temp directory not found"
325 mem_total
="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc
/meminfo
")"
326 mem_free
="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc
/meminfo
")"
327 mem_swap
="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc
/meminfo
")"
328 if [ $
((mem_total
)) -le 64000 ] && [ $
((mem_swap
)) -eq 0 ]
331 f_log
"not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
336 # check backup configuration
338 if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
340 f_space
"${adb_dir_backup}"
341 if [ "${space_ok}" = "false" ]
343 f_log
"not enough free space in '${adb_dir_backup}'(avail. ${av_space} kb), backup/restore will be disabled"
346 f_log
"backup/restore will be enabled"
351 f_log
"backup/restore will be disabled"
354 # set dnsmasq defaults
356 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
358 adb_dnsformat
="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
359 elif [ -n "${adb_wanif4}" ]
361 adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print
\"address
=/\"\
$0\"/\"ipv4
}'"
363 adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print
\"address
=/\"\
$0\"/\"ipv6
}'"
366 # check volatile iptables configuration
368 if [ -n "${adb_wanif4}" ]
370 if [ "${apmode_ok}" = "false" ]
372 if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
374 f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
375 f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
377 f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
378 f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp-host-unreachable"
379 f_firewall "IPv4" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
380 f_firewall "IPv4" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp-host-unreachable"
382 f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
383 f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullportssl}"
385 if [ -n "${adb_wanif6}" ]
387 if [ "${apmode_ok}" = "false" ]
389 if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
391 f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
392 f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
394 f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
395 f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp6-addr-unreachable"
396 f_firewall "IPv6" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
397 f_firewall "IPv6" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp6-addr-unreachable"
399 f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
400 f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullportssl}"
402 if [ "${firewall_ok}" = "true" ]
404 f_log "created volatile firewall rulesets"
407 # check volatile uhttpd instance configuration
409 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
411 f_uhttpd "adbIPv4+6_80" "1" "-p ${adb_ipv4}:${adb_nullport} -p [${adb_ipv6}]:${adb_nullport}"
412 f_uhttpd "adbIPv4+6_443" "0" "-p ${adb_ipv4}:${adb_nullportssl} -p [${adb_ipv6}]:${adb_nullportssl}"
413 elif [ -n "${adb_wanif4}" ]
415 f_uhttpd "adbIPv4_80" "1" "-p ${adb_ipv4}:${adb_nullport}"
416 f_uhttpd "adbIPv4_443" "0" "-p ${adb_ipv4}:${adb_nullportssl}"
418 f_uhttpd "adbIPv6_80" "1" "-p [${adb_ipv6}]:${adb_nullport}"
419 f_uhttpd "adbIPv6_443" "0" "-p [${adb_ipv6}]:${adb_nullportssl}"
421 if [ "${uhttpd_ok}" = "true" ]
423 f_log "created volatile uhttpd instances"
426 # check whitelist entries
428 if [ -s "${adb_whitelist}" ]
430 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
433 # remove temporary package list
438 # f_depend: check package dependencies
444 local check_only="${2}"
447 check="$(printf "${pkg_list}" | grep "^${package}")"
448 if [ "${check_only}" = "true" ] && [ -z "${check}" ]
451 elif [ -z "${check}" ]
454 f_log "package '${package}' not found"
459 # f_firewall: set iptables rules for ipv4/ipv6
464 local nullip="${adb_nullipv4}"
470 local notes="adb-${6}"
473 # select appropriate iptables executable for IPv6
475 if [ "${proto}" = "IPv6" ]
478 nullip="${adb_nullipv6}"
481 # check whether iptables chain already exist
483 rc="$("${ipt}" -w -t "${table}" -nL "${chain}" >/dev/null 2>&1; printf ${?})"
486 "${ipt}" -w -t "${table}" -N "${chain}"
487 "${ipt}" -w -t "${table}" -A "${chain}" -m comment --comment "${notes}" -j RETURN
488 if [ "${chain}" = "adb-dns" ]
490 "${ipt}" -w -t "${table}" -A "${chsrc}" -i "${adb_landev}+" -m comment --comment "${notes}" -j "${chain}"
492 "${ipt}" -w -t "${table}" -A "${chsrc}" -d "${nullip}" -m comment --comment "${notes}" -j "${chain}"
497 f_log "failed to initialize volatile ${proto} firewall chain '${chain}'"
502 # check whether iptables rule already exist
504 rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
507 "${ipt}" -w -t "${table}" -I "${chain}" "${chpos}" -m comment --comment "${notes}" ${rules}
513 f_log "failed to initialize volatile ${proto} firewall rule '${notes}'"
519 # f_uhttpd: start uhttpd instances
528 check="$(pgrep -f "uhttpd -h /www/adblock -N 25 -T ${timeout} -r ${realm}")"
531 uhttpd -h "/www/adblock" -N 25 -T "${timeout}" -r "${realm}" -k 0 -t 0 -R -D -S -E "/index.html" ${ports}
537 f_log "failed to initialize volatile uhttpd instance (${realm})"
543 # f_space: check mount points/space requirements
552 av_space="$(df "${mp}" | tail -n1 | awk '{printf $4}')"
553 if [ $((av_space)) -lt $((adb_minspace)) ]
560 # f_cntconfig: calculate counters in config
567 for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
569 count="$(wc -l < "${src_name}")"
570 src_name="${src_name##*.}"
571 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
575 "${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}"
576 adb_count=$((adb_count + count))
578 "${adb_uci}" -q set "adblock.global.adb_overall_count=${adb_count}"
581 # f_rmconfig: remove volatile config entries
586 local options="adb_src_timestamp adb_src_count"
589 "${adb_uci}" -q delete "adblock.global.adb_overall_count"
590 "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
591 "${adb_uci}" -q delete "adblock.global.adb_percentage"
592 "${adb_uci}" -q delete "adblock.global.adb_lastrun"
593 for opt in ${options}
595 "${adb_uci}" -q delete "adblock.${section}.${opt}"
599 # f_rmdns: remove dns block lists and backups
603 rm_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
604 if [ -n "${rm_dns}" ]
606 rm -rf "${adb_dnshidedir}"
607 if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
609 rm -f "${adb_dir_backup}/${adb_dnsprefix}"*.gz
611 /etc/init.d/dnsmasq restart
615 # f_rmuhttpd: remove uhttpd instances
619 rm_uhttpd="$(pgrep -f "uhttpd -h /www/adblock")"
620 if [ -n "${rm_uhttpd}" ]
622 for pid in ${rm_uhttpd}
629 # f_rmfirewall: remove firewall rulsets
633 rm_fw="$(iptables -w -t nat -vnL | grep -Fo "adb-")"
636 iptables-save -t nat | grep -Fv -- "adb-" | iptables-restore
637 iptables-save -t filter | grep -Fv -- "adb-" | iptables-restore
638 if [ -n "$(lsmod | grep -Fo "ip6table_nat")" ]
640 ip6tables-save -t nat | grep -Fv -- "adb-" | ip6tables-restore
641 ip6tables-save -t filter | grep -Fv -- "adb-" | ip6tables-restore
646 # f_log: log messages to stdout and syslog
654 # check for terminal session
661 # log to different output devices and set log class accordingly
663 if [ -n "${log_msg}" ]
669 logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
673 # f_statistics: adblock runtime statistics
676 local ipv4_blk=0 ipv4_all=0 ipv4_pct=0
677 local ipv6_blk=0 ipv6_all=0 ipv6_pct=0
679 if [ -n "${adb_wanif4}" ]
681 ipv4_blk="$(iptables -t nat -vxnL adb-nat | awk '$3 ~
/^DNAT$
/ {sum += $1} END
{printf sum}')"
682 ipv4_all="$(iptables -t nat -vxnL PREROUTING | awk '$3 ~
/^
(delegate_prerouting|prerouting_rule
)$
/ {sum += $1} END
{printf sum}')"
683 if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ]
685 ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')"
686 elif [ $((ipv4_all)) -lt $((ipv4_blk)) ]
688 iptables -t nat -Z adb-nat
691 if [ -n "${adb_wanif6}" ]
693 ipv6_blk="$
(ip6tables
-t nat
-vxnL adb-nat |
awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
694 ipv6_all="$
(ip6tables
-t nat
-vxnL PREROUTING |
awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')"
695 if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ]
697 ipv6_pct="$
(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')"
698 elif [ $
((ipv6_all
)) -lt $
((ipv6_blk
)) ]
700 ip6tables
-t nat
-Z adb-nat
703 "${adb_uci}" -q set "adblock.global.adb_percentage=${ipv4_pct}%/${ipv6_pct}%"
704 f_log
"firewall statistics (IPv4/IPv6): ${ipv4_pct}%/${ipv6_pct}% of all packets in prerouting chain are ad related & blocked"
707 # f_exit: delete temporary files, generate statistics and exit
711 local lastrun
="$(date "+%d.
%m.
%Y
%H
:%M
:%S
")"
713 if [ "${adb_restricted}" = "1" ]
715 adb_uci
="$(which true)"
718 # delete temp files & directories
720 rm -f "${adb_tmpfile}"
721 rm -rf "${adb_tmpdir}"
725 if [ $
((rc
)) -lt 0 ] ||
[ $
((rc
)) -gt 0 ]
730 config_foreach f_rmconfig
source
731 if [ $
((rc
)) -eq -1 ]
733 "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun} => runtime error, please check the log!"
737 # final log message and iptables statistics
742 "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun}"
743 f_log
"domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
744 elif [ $
((rc
)) -gt 0 ]
746 f_log
"domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
750 "${adb_uci}" -q commit
"adblock"
751 rm -f "${adb_pidfile}"