2 # function library used by adblock-update.sh
3 # written by Dirk Brenken (dev@brenken.org)
8 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
11 adb_nullportssl
="65535"
12 adb_nullipv4
="198.18.0.1"
13 adb_nullipv6
="::ffff:c612:0001"
14 adb_whitelist
="/etc/adblock/adblock.whitelist"
15 adb_whitelist_rset
="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
16 adb_dnsdir
="/tmp/dnsmasq.d"
17 adb_dnshidedir
="${adb_dnsdir}/.adb_hidden"
18 adb_dnsprefix
="adb_list"
24 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 [ -r "/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
113 # set restricted mode
115 if [ "${adb_restricted}" = "1" ]
117 adb_uci
="$(which true)"
122 # f_envcheck: check/set environment prerequisites
128 # check 'enabled' & 'version' config options
130 if [ -z "${adb_enabled}" ] || [ -z "${adb_cfgver}" ] || [ "${adb_cfgver%%.*}" != "${adb_mincfgver%%.*}" ]
133 f_log
"outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
135 elif [ "${adb_cfgver#*.}" != "${adb_mincfgver#*.}" ]
139 if [ "${adb_enabled}" != "1" ]
142 f_log
"adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service"
146 # get list with all installed packages
148 pkg_list
="$(opkg list-installed)"
149 if [ -z "${pkg_list}" ]
152 f_log
"empty 'opkg' package list, please check your installation"
155 adb_sysver
="$(printf "${pkg_list}" | grep "^base-files
-")"
156 adb_sysver
="${adb_sysver##*-}"
158 # get lan ip addresses
160 if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
163 f_log
"no valid IPv4/IPv6 configuration found (${adb_lanif}), please set 'adb_lanif' manually"
167 # check logical update interfaces (with default route)
169 if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
171 adb_wanif4
="${adb_lanif}"
176 if [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
178 adb_nullipv4
="${adb_ipv4}"
179 adb_nullipv6
="${adb_ipv6}"
180 if [ "$(${adb_uci} -q get uhttpd.main.listen_http | grep -Fo "80")" = "80" ] ||
181 [ "$(${adb_uci} -q get uhttpd.main.listen_https | grep -Fo "443")" = "443" ]
184 f_log
"AP mode detected, please set local LuCI instance to ports <> 80/443"
186 elif [ -z "$(pgrep -f "dnsmasq
")" ]
189 f_log
"please enable the local dnsmasq instance to use adblock"
191 elif [ ! -f "/var/run/fw3.state" ]
194 f_log
"please enable the local firewall to use adblock"
201 check
="$(${adb_uci} -q get bcp38.@bcp38[0].enabled)"
202 if [ "${check}" = "1" ]
204 check
="$(${adb_uci} -q get bcp38.@bcp38[0].match | grep -Fo "${adb_nullipv4%.*}")"
208 f_log
"please whitelist '${adb_nullipv4}' in your bcp38 configuration to use adblock"
214 # check general package dependencies
220 f_depend
"kmod-ipt-nat"
222 # check ipv6 related package dependencies
224 if [ -n "${adb_wanif6}" ]
226 f_depend
"ip6tables" "true"
227 if [ "${package_ok}" = "false" ]
229 f_log
"package 'ip6tables' not found, IPv6 support will be disabled"
232 f_depend
"kmod-ipt-nat6" "true"
233 if [ "${package_ok}" = "false" ]
235 f_log
"package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
241 # check uclient-fetch/wget dependencies
243 f_depend
"uclient-fetch" "true"
244 if [ "${package_ok}" = "true" ]
246 f_depend
"libustream-polarssl" "true"
247 if [ "${package_ok}" = "false" ]
249 adb_fetch
="$(which uclient-fetch)"
250 fetch_parm
="-q --timeout=${adb_fetchttl}"
251 response_parm
="--spider"
254 if [ -z "${adb_fetch}" ]
256 f_depend
"wget" "true"
257 if [ "${package_ok}" = "true" ]
259 adb_fetch
="$(which wget)"
260 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}"
261 response_parm
="--spider --server-response"
264 f_log
"please install 'uclient-fetch' or 'wget' with ssl support to use adblock"
269 # check ca-certificate package and set fetch parm accordingly
271 f_depend
"ca-certificates" "true"
272 if [ "${package_ok}" = "false" ]
274 fetch_parm
="${fetch_parm} --no-check-certificate"
277 # start normal processing/logging
279 f_log
"domain adblock processing started (${adb_scriptver}, ${adb_sysver}, $(/bin/date "+%d.
%m.
%Y
%H
:%M
:%S
"))"
281 # log partially outdated config
283 if [ "${outdated_ok}" = "true" ]
285 f_log
"partially outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
290 if [ "${apmode_ok}" = "true" ]
292 f_log
"AP mode enabled"
295 # log restricted mode
297 if [ "${restricted_ok}" = "true" ]
299 f_log
"Restricted mode enabled"
302 # check dns hideout directory
304 if [ -d "${adb_dnshidedir}" ]
306 mv_done
="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec mv -f "{}" "${adb_dnsdir}" \;)"
308 mkdir
-p -m 660 "${adb_dnshidedir}"
311 # check adblock temp directory
313 adb_tmpfile
="$(mktemp -tu)"
314 adb_tmpdir
="$(mktemp -p /tmp -d)"
315 if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
317 f_space
"${adb_tmpdir}"
318 if [ "${space_ok}" = "false" ]
320 if [ $
((av_space
)) -le 2000 ]
323 f_log
"not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)"
326 f_log
"not enough free space to handle all block list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
331 f_log
"temp directory not found"
337 mem_total
="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc
/meminfo
")"
338 mem_free
="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc
/meminfo
")"
339 mem_swap
="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc
/meminfo
")"
340 if [ $
((mem_total
)) -le 64000 ] && [ $
((mem_swap
)) -eq 0 ]
343 f_log
"not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
348 # check backup configuration
350 if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
352 f_space
"${adb_dir_backup}"
353 if [ "${space_ok}" = "false" ]
355 f_log
"not enough free space in '${adb_dir_backup}'(avail. ${av_space} kb), backup/restore will be disabled"
358 f_log
"backup/restore will be enabled"
363 f_log
"backup/restore will be disabled"
366 # set dnsmasq defaults
368 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
370 adb_dnsformat
="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
371 elif [ -n "${adb_wanif4}" ]
373 adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print
\"address
=/\"\
$0\"/\"ipv4
}'"
375 adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print
\"address
=/\"\
$0\"/\"ipv6
}'"
378 # check volatile iptables configuration
380 if [ -n "${adb_wanif4}" ]
382 if [ "${apmode_ok}" = "false" ]
384 if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
386 f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
387 f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
389 f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
390 f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp-host-unreachable"
391 f_firewall "IPv4" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
392 f_firewall "IPv4" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp-host-unreachable"
394 f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
395 f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullportssl}"
397 if [ -n "${adb_wanif6}" ]
399 if [ "${apmode_ok}" = "false" ]
401 if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
403 f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
404 f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
406 f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
407 f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp6-addr-unreachable"
408 f_firewall "IPv6" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
409 f_firewall "IPv6" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp6-addr-unreachable"
411 f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
412 f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullportssl}"
414 if [ "${firewall_ok}" = "true" ]
416 f_log "created volatile firewall rulesets"
419 # check volatile uhttpd instance configuration
421 check="$(pgrep -f "uhttpd -h /www/adblock")"
424 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
426 f_uhttpd "adbIPv4+6_80" "1" "-p ${adb_ipv4}:${adb_nullport} -p [${adb_ipv6}]:${adb_nullport}"
427 f_uhttpd "adbIPv4+6_443" "0" "-p ${adb_ipv4}:${adb_nullportssl} -p [${adb_ipv6}]:${adb_nullportssl}"
428 elif [ -n "${adb_wanif4}" ]
430 f_uhttpd "adbIPv4_80" "1" "-p ${adb_ipv4}:${adb_nullport}"
431 f_uhttpd "adbIPv4_443" "0" "-p ${adb_ipv4}:${adb_nullportssl}"
433 f_uhttpd "adbIPv6_80" "1" "-p [${adb_ipv6}]:${adb_nullport}"
434 f_uhttpd "adbIPv6_443" "0" "-p [${adb_ipv6}]:${adb_nullportssl}"
436 if [ "${uhttpd_ok}" = "true" ]
438 f_log "created volatile uhttpd instances"
442 # check whitelist entries
444 if [ -s "${adb_whitelist}" ]
446 awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
449 # remove temporary package list
454 # f_depend: check package dependencies
460 local check_only="${2}"
463 check="$(printf "${pkg_list}" | grep "^${package} -")"
464 if [ "${check_only}" = "true" ] && [ -z "${check}" ]
467 elif [ -z "${check}" ]
471 f_log "package '${package}' not found"
476 # f_firewall: set iptables rules for ipv4/ipv6
481 local nullip="${adb_nullipv4}"
487 local notes="adb-${6}"
491 # select appropriate iptables executable for IPv6
493 if [ "${proto}" = "IPv6" ]
496 nullip="${adb_nullipv6}"
499 # check whether iptables chain already exist
501 rc="$("${ipt}" -w -t "${table}" -nL "${chain}" >/dev/null 2>&1; printf ${?})"
504 "${ipt}" -w -t "${table}" -N "${chain}"
505 "${ipt}" -w -t "${table}" -A "${chain}" -m comment --comment "${notes}" -j RETURN
506 if [ "${chain}" = "adb-dns" ]
508 "${ipt}" -w -t "${table}" -A "${chsrc}" -i "${adb_landev}+" -m comment --comment "${notes}" -j "${chain}"
510 "${ipt}" -w -t "${table}" -A "${chsrc}" -d "${nullip}" -m comment --comment "${notes}" -j "${chain}"
515 f_log "failed to initialize volatile ${proto} firewall chain '${chain}'"
520 # check whether iptables rule already exist
522 rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
525 "${ipt}" -w -t "${table}" -I "${chain}" "${chpos}" -m comment --comment "${notes}" ${rules}
530 f_log "failed to initialize volatile ${proto} firewall rule '${notes}'"
536 # f_uhttpd: start uhttpd instances
545 uhttpd -h "/www/adblock" -N 25 -T "${timeout}" -r "${realm}" -k 0 -t 0 -R -D -S -E "/index.html" ${ports}
550 f_log "failed to initialize volatile uhttpd instance (${realm})"
555 # f_space: check mount points/space requirements
564 av_space="$(df "${mp}" | tail -n1 | awk '{printf $4}')"
565 if [ $((av_space)) -lt $((adb_minspace)) ]
574 # f_cntconfig: calculate counters in config
581 for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
583 count="$(wc -l < "${src_name}")"
584 src_name="${src_name##*.}"
585 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
589 "${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}"
590 adb_count=$((adb_count + count))
592 "${adb_uci}" -q set "adblock.global.adb_overall_count=${adb_count}"
595 # f_rmconfig: remove volatile config entries
600 local options="adb_src_timestamp adb_src_count"
603 rm_cfg="${adb_lastrun}"
604 if [ -n "${rm_cfg}" ]
606 "${adb_uci}" -q delete "adblock.global.adb_overall_count"
607 "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
608 "${adb_uci}" -q delete "adblock.global.adb_percentage"
609 "${adb_uci}" -q delete "adblock.global.adb_lastrun"
610 for opt in ${options}
612 "${adb_uci}" -q delete "adblock.${section}.${opt}"
617 # f_rmdns: remove dns block lists and backups
621 rm_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
622 if [ -n "${rm_dns}" ]
624 rm -rf "${adb_dnshidedir}"
625 if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
627 rm -f "${adb_dir_backup}/${adb_dnsprefix}"*.gz
629 /etc/init.d/dnsmasq restart
633 # f_rmuhttpd: remove uhttpd instances
637 rm_uhttpd="$(pgrep -f "uhttpd -h /www/adblock")"
638 if [ -n "${rm_uhttpd}" ]
640 for pid in ${rm_uhttpd}
647 # f_rmfirewall: remove firewall rulsets
651 rm_fw="$(iptables -w -t nat -vnL | grep -Fo "adb-")"
654 iptables-save -t nat | grep -Fv -- "adb-" | iptables-restore
655 iptables-save -t filter | grep -Fv -- "adb-" | iptables-restore
656 if [ -n "$(lsmod | grep -Fo "ip6table_nat")" ]
658 ip6tables-save -t nat | grep -Fv -- "adb-" | ip6tables-restore
659 ip6tables-save -t filter | grep -Fv -- "adb-" | ip6tables-restore
664 # f_log: log messages to stdout and syslog
671 local lastrun="$(date "+%d.%m.%Y %H:%M:%S")"
673 # check for terminal session
680 # log to different output devices and set log class accordingly
682 if [ -n "${log_msg}" ]
688 logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
690 # clean exit on error
692 if [ $((rc)) -eq -1 ] || [ $((rc)) -gt 0 ]
697 config_foreach f_rmconfig source
698 "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun} => runtime error, please check the log!"
699 "${adb_uci}" -q commit "adblock"
704 # f_statistics: adblock runtime statistics
707 local ipv4_blk=0 ipv4_all=0 ipv4_pct=0
708 local ipv6_blk=0 ipv6_all=0 ipv6_pct=0
710 if [ -n "${adb_wanif4}" ]
712 ipv4_blk="$(iptables -t nat -vnL adb-nat | awk '$3 ~
/^DNAT$
/ {sum += $1} END
{printf sum}')"
713 ipv4_all="$(iptables -t nat -vnL PREROUTING | awk '$3 ~
/^prerouting_rule$
/ {sum += $1} END
{printf sum}')"
714 if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ]
716 ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')"
719 if [ -n "${adb_wanif6}" ]
721 ipv6_blk="$
(ip6tables
-t nat
-vnL adb-nat |
awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
722 ipv6_all="$
(ip6tables
-t nat
-vnL PREROUTING |
awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')"
723 if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ]
725 ipv6_pct="$
(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')"
728 "${adb_uci}" -q set "adblock.global.adb_percentage=${ipv4_pct}%/${ipv6_pct}%"
729 f_log
"firewall statistics (IPv4/IPv6): ${ipv4_pct}%/${ipv6_pct}% of all packets in prerouting chain are ad related & blocked"
732 # f_exit: delete temporary files, generate statistics and exit
736 local lastrun
="$(date "+%d.
%m.
%Y
%H
:%M
:%S
")"
738 rm -f "${adb_tmpfile}"
739 rm -rf "${adb_tmpdir}"
741 # final log message and iptables statistics
746 "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun}"
747 "${adb_uci}" -q commit
"adblock"
748 f_log
"domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
749 elif [ $
((rc
)) -gt 0 ]
751 f_log
"domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
755 rm -f "${adb_pidfile}"