Merge pull request #2874 from dibdot/adblock

adblock: update 1.2.5
This commit is contained in:
Hannu Nyman 2016-06-19 19:54:59 +03:00 committed by GitHub
commit a179ba8213
4 changed files with 347 additions and 341 deletions

View File

@ -7,7 +7,7 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=adblock PKG_NAME:=adblock
PKG_VERSION:=1.2.1 PKG_VERSION:=1.2.5
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_LICENSE:=GPL-3.0+ PKG_LICENSE:=GPL-3.0+
PKG_MAINTAINER:=Dirk Brenken <dev@brenken.org> PKG_MAINTAINER:=Dirk Brenken <dev@brenken.org>

View File

@ -2,6 +2,29 @@
# function library used by adblock-update.sh # function library used by adblock-update.sh
# written by Dirk Brenken (dev@brenken.org) # written by Dirk Brenken (dev@brenken.org)
# set initial defaults
#
LC_ALL=C
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
adb_lanif="lan"
adb_nullport="65534"
adb_nullportssl="65535"
adb_nullipv4="198.18.0.1"
adb_nullipv6="::ffff:c612:0001"
adb_whitelist="/etc/adblock/adblock.whitelist"
adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
adb_dnsdir="/tmp/dnsmasq.d"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
adb_dnsprefix="adb_list"
adb_count=0
adb_minspace=12000
adb_forcedns=1
adb_fetchttl=5
adb_restricted=0
adb_fetch="$(which wget)"
adb_uci="$(which uci)"
unset adb_revsrclist
# f_envload: load adblock environment # f_envload: load adblock environment
# #
f_envload() f_envload()
@ -10,9 +33,12 @@ f_envload()
# #
if [ -r "/lib/functions.sh" ] if [ -r "/lib/functions.sh" ]
then then
. "/lib/functions.sh" if [ -z "$(type -f config_load)" ]
then
. "/lib/functions.sh"
fi
else else
rc=-1 rc=-10
f_log "system function library not found, please check your installation" f_log "system function library not found, please check your installation"
f_exit f_exit
fi fi
@ -21,28 +47,26 @@ f_envload()
# #
if [ -r "/lib/functions/network.sh" ] if [ -r "/lib/functions/network.sh" ]
then then
. "/lib/functions/network.sh" if [ -z "$(type -f network_get_device)" ]
then
. "/lib/functions/network.sh"
fi
else else
rc=-1 rc=-10
f_log "system network library not found, please check your installation" f_log "system network library not found, please check your installation"
f_exit f_exit
fi fi
# set initial defaults, # check opkg availability
# may be overwritten by setting appropriate adblock config options in global section of /etc/config/adblock
# #
adb_lanif="lan" if [ -r "/var/lock/opkg.lock" ]
adb_nullport="65534" then
adb_nullportssl="65535" rc=-10
adb_nullipv4="198.18.0.1" f_log "adblock installation finished successfully, 'opkg' currently locked by package installer"
adb_nullipv6="::ffff:c612:0001" f_exit
adb_whitelist="/etc/adblock/adblock.whitelist" fi
adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
adb_forcedns=1
adb_fetchttl=5
adb_restricted=0
# function to parse global section by callback # uci function to parse global section by callback
# #
config_cb() config_cb()
{ {
@ -60,69 +84,45 @@ f_envload()
fi fi
} }
# function to parse 'service' and 'source' sections # uci function to parse 'service' and 'source' sections
# #
parse_config() parse_config()
{ {
local value opt section="${1}" options="adb_dir adb_src adb_src_rset adb_src_cat" local value opt section="${1}" options="enabled adb_dir adb_src adb_src_rset adb_src_cat"
config_get switch "${section}" "enabled" if [ "${section}" != "backup" ]
if [ "${switch}" = "1" ]
then then
if [ "${section}" != "backup" ] eval "adb_sources=\"${adb_sources} ${section}\""
then
eval "adb_sources=\"${adb_sources} ${section}\""
fi
for opt in ${options}
do
config_get value "${section}" "${opt}"
if [ -n "${value}" ]
then
eval "${opt}_${section}=\"${value}\""
fi
done
fi fi
for opt in ${options}
do
config_get value "${section}" "${opt}"
if [ -n "${value}" ]
then
eval "${opt}_${section}=\"${value}\""
fi
done
} }
# check opkg availability
#
if [ -r "/var/lock/opkg.lock" ]
then
rc=-1
f_log "adblock installation finished successfully, 'opkg' currently locked by package installer"
f_exit
fi
# get list with all installed packages
#
pkg_list="$(opkg list-installed)"
if [ -z "${pkg_list}" ]
then
rc=-1
f_log "empty package list, please check your installation"
f_exit
fi
# load adblock config and start parsing functions # load adblock config and start parsing functions
# #
config_load adblock config_load adblock
config_foreach parse_config service config_foreach parse_config service
config_foreach parse_config source config_foreach parse_config source
}
# set more script defaults (can't be overwritten by adblock config options) # f_envcheck: check/set environment prerequisites
#
f_envcheck()
{
local check
# check restricted mode
# #
adb_minspace=12000 if [ "${adb_restricted}" = "1" ]
adb_tmpfile="$(mktemp -tu)" then
adb_tmpdir="$(mktemp -p /tmp -d)" adb_uci="$(which true)"
adb_dnsdir="/tmp/dnsmasq.d" restricted_ok="true"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden" fi
adb_dnsprefix="adb_list"
adb_iptv4="$(which iptables)"
adb_iptv6="$(which ip6tables)"
adb_uhttpd="$(which uhttpd)"
adb_fetch="$(which wget)"
adb_uci="$(which uci)"
adb_date="$(which date)"
unset adb_srclist adb_revsrclist
# check 'enabled' & 'version' config options # check 'enabled' & 'version' config options
# #
@ -135,13 +135,25 @@ f_envload()
then then
outdate_ok="true" outdate_ok="true"
fi fi
if [ $((adb_enabled)) -ne 1 ] if [ "${adb_enabled}" != "1" ]
then then
rc=-1 rc=-1
f_log "adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service" f_log "adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service"
f_exit f_exit
fi fi
# get list with all installed packages
#
pkg_list="$(opkg list-installed)"
if [ -z "${pkg_list}" ]
then
rc=-1
f_log "empty 'opkg' package list, please check your installation"
f_exit
fi
adb_sysver="$(printf "${pkg_list}" | grep "^base-files -")"
adb_sysver="${adb_sysver##*-}"
# get lan ip addresses # get lan ip addresses
# #
network_get_ipaddr adb_ipv4 "${adb_lanif}" network_get_ipaddr adb_ipv4 "${adb_lanif}"
@ -175,14 +187,14 @@ f_envload()
[ "$(${adb_uci} -q get uhttpd.main.listen_https | grep -Fo "443")" = "443" ] [ "$(${adb_uci} -q get uhttpd.main.listen_https | grep -Fo "443")" = "443" ]
then then
rc=-1 rc=-1
f_log "AP mode detected, set local LuCI instance to ports <> 80/443" f_log "AP mode detected, please set local LuCI instance to ports <> 80/443"
f_exit f_exit
elif [ -z "$(pgrep -f "dnsmasq")" ] elif [ -z "$(pgrep -f "dnsmasq")" ]
then then
rc=-1 rc=-1
f_log "please enable the local dnsmasq instance to use adblock" f_log "please enable the local dnsmasq instance to use adblock"
f_exit f_exit
elif [ -z "$(${adb_iptv4} -w -vnL | grep -Fo "DROP")" ] elif [ -z "$(iptables -w -vnL | grep -Fo "DROP")" ]
then then
rc=-1 rc=-1
f_log "please enable the local firewall to use adblock" f_log "please enable the local firewall to use adblock"
@ -191,30 +203,23 @@ f_envload()
apmode_ok="true" apmode_ok="true"
fi fi
else else
apmode_ok="false"
check="$(${adb_uci} -q get bcp38.@bcp38[0].enabled)" check="$(${adb_uci} -q get bcp38.@bcp38[0].enabled)"
if [ $((check)) -eq 1 ] if [ "${check}" = "1" ]
then then
check="$(${adb_uci} -q get bcp38.@bcp38[0].match | grep -Fo "${adb_nullipv4%.*}")" check="$(${adb_uci} -q get bcp38.@bcp38[0].match | grep -Fo "${adb_nullipv4%.*}")"
if [ -n "${check}" ] if [ -n "${check}" ]
then then
rc=-1 rc=-1
f_log "please whitelist '${adb_nullipv4}' in your bcp38 configuration to use your adblock null-ip" f_log "please whitelist '${adb_nullipv4}' in your bcp38 configuration to use adblock"
f_exit f_exit
fi fi
fi fi
fi fi
# get system release level # start normal processing/logging
# #
adb_sysver="$(printf "${pkg_list}" | grep "^base-files -")" f_log "domain adblock processing started (${adb_scriptver}, ${adb_sysver}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
adb_sysver="${adb_sysver##*-}"
}
# f_envcheck: check/set environment prerequisites
#
f_envcheck()
{
local check
# log partially outdated config # log partially outdated config
# #
@ -230,11 +235,10 @@ f_envcheck()
f_log "AP mode enabled" f_log "AP mode enabled"
fi fi
# set & log restricted mode # log restricted mode
# #
if [ $((adb_restricted)) -eq 1 ] if [ "${restricted_ok}" = "true" ]
then then
adb_uci="$(which true)"
f_log "Restricted mode enabled" f_log "Restricted mode enabled"
fi fi
@ -286,6 +290,8 @@ f_envcheck()
# check adblock temp directory # check adblock temp directory
# #
adb_tmpfile="$(mktemp -tu)"
adb_tmpdir="$(mktemp -p /tmp -d)"
if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ] if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
then then
f_space "${adb_tmpdir}" f_space "${adb_tmpdir}"
@ -294,15 +300,15 @@ f_envcheck()
if [ $((av_space)) -le 2000 ] if [ $((av_space)) -le 2000 ]
then then
rc=105 rc=105
f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)" "${rc}" f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)"
f_exit f_exit
else else
f_log "not enough free space to handle all adblock list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)" f_log "not enough free space to handle all block list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
fi fi
fi fi
else else
rc=110 rc=110
f_log "temp directory not found" "${rc}" f_log "temp directory not found"
f_exit f_exit
fi fi
@ -321,7 +327,7 @@ f_envcheck()
# check backup configuration # check backup configuration
# #
if [ -n "${adb_dir_backup}" ] && [ -d "${adb_dir_backup}" ] if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
then then
f_space "${adb_dir_backup}" f_space "${adb_dir_backup}"
if [ "${space_ok}" = "false" ] if [ "${space_ok}" = "false" ]
@ -353,38 +359,44 @@ f_envcheck()
# #
if [ -n "${adb_wanif4}" ] if [ -n "${adb_wanif4}" ]
then then
check="$(${adb_iptv4} -w -vnL | grep -Fo "adb-")" check="$(iptables -w -t nat -vnL | grep -Fo "adb-")"
if [ -z "${check}" ] if [ -z "${check}" ]
then then
if [ $((adb_forcedns)) -eq 1 ] && [ -n "${adb_landev4}" ] if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev4}" ]
then then
f_firewall "IPv4" "nat" "prerouting_rule" "prerouting_rule" "0" "dns" "-i ${adb_landev4} -p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53" f_firewall "IPv4" "nat" "prerouting_rule" "prerouting_rule" "0" "dns" "-i ${adb_landev4} -p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
f_firewall "IPv4" "nat" "prerouting_rule" "prerouting_rule" "0" "dns" "-i ${adb_landev4} -p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53" f_firewall "IPv4" "nat" "prerouting_rule" "prerouting_rule" "0" "dns" "-i ${adb_landev4} -p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
fi fi
f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}" f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullportssl}" f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullportssl}"
f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset" if [ "${apmode_ok}" = "false" ]
f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp-host-unreachable" then
f_firewall "IPv4" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset" f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
f_firewall "IPv4" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp-host-unreachable" f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp-host-unreachable"
f_firewall "IPv4" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
f_firewall "IPv4" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp-host-unreachable"
fi
fi fi
fi fi
if [ -n "${adb_wanif6}" ] if [ -n "${adb_wanif6}" ]
then then
check="$(${adb_iptv6} -w -vnL | grep -Fo "adb-")" check="$(ip6tables -w -t nat -vnL | grep -Fo "adb-")"
if [ -z "${check}" ] if [ -z "${check}" ]
then then
if [ $((adb_forcedns)) -eq 1 ] && [ -n "${adb_landev6}" ] if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev6}" ]
then then
f_firewall "IPv6" "nat" "PREROUTING" "PREROUTING" "0" "dns" "-i ${adb_landev6} -p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53" f_firewall "IPv6" "nat" "PREROUTING" "PREROUTING" "0" "dns" "-i ${adb_landev6} -p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
f_firewall "IPv6" "nat" "PREROUTING" "PREROUTING" "0" "dns" "-i ${adb_landev6} -p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53" f_firewall "IPv6" "nat" "PREROUTING" "PREROUTING" "0" "dns" "-i ${adb_landev6} -p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
fi fi
f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}" f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullportssl}" f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullportssl}"
f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset" if [ "${apmode_ok}" = "false" ]
f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp6-addr-unreachable" then
f_firewall "IPv6" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset" f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
f_firewall "IPv6" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp6-addr-unreachable" f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp6-addr-unreachable"
f_firewall "IPv6" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
f_firewall "IPv6" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp6-addr-unreachable"
fi
fi fi
fi fi
if [ "${fw_done}" = "true" ] if [ "${fw_done}" = "true" ]
@ -424,7 +436,7 @@ f_envcheck()
awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist" awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
fi fi
# remove no longer used opkg package list # remove temporary package list
# #
unset pkg_list unset pkg_list
} }
@ -440,7 +452,7 @@ f_depend()
if [ -z "${check}" ] if [ -z "${check}" ]
then then
rc=115 rc=115
f_log "package '${package}' not found" "${rc}" f_log "package '${package}' not found"
f_exit f_exit
fi fi
} }
@ -449,7 +461,7 @@ f_depend()
# #
f_firewall() f_firewall()
{ {
local ipt="${adb_iptv4}" local ipt="iptables"
local nullip="${adb_nullipv4}" local nullip="${adb_nullipv4}"
local proto="${1}" local proto="${1}"
local table="${2}" local table="${2}"
@ -463,7 +475,7 @@ f_firewall()
# #
if [ "${proto}" = "IPv6" ] if [ "${proto}" = "IPv6" ]
then then
ipt="${adb_iptv6}" ipt="ip6tables"
nullip="${adb_nullipv6}" nullip="${adb_nullipv6}"
fi fi
@ -493,7 +505,7 @@ f_firewall()
then then
fw_done="true" fw_done="true"
else else
f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}" f_log "failed to initialize volatile ${proto} firewall rule '${notes}'"
f_exit f_exit
fi fi
fi fi
@ -506,48 +518,17 @@ f_uhttpd()
local realm="${1}" local realm="${1}"
local timeout="${2}" local timeout="${2}"
local ports="${3}" local ports="${3}"
"${adb_uhttpd}" -h "/www/adblock" -N 25 -T "${timeout}" -r "${realm}" -k 0 -t 0 -R -D -S -E "/index.html" ${ports} uhttpd -h "/www/adblock" -N 25 -T "${timeout}" -r "${realm}" -k 0 -t 0 -R -D -S -E "/index.html" ${ports}
rc=${?} rc=${?}
if [ $((rc)) -eq 0 ] if [ $((rc)) -eq 0 ]
then then
uhttpd_done="true" uhttpd_done="true"
else else
f_log "failed to initialize volatile uhttpd instance (${realm})" "${rc}" f_log "failed to initialize volatile uhttpd instance (${realm})"
f_exit f_exit
fi fi
} }
# f_log: log messages to stdout and syslog
#
f_log()
{
local log_parm
local log_msg="${1}"
local log_rc="${2}"
local class="info "
# check for terminal session
#
if [ -t 1 ]
then
log_parm="-s"
fi
# log to different output devices and set log class accordingly
#
if [ -n "${log_msg}" ]
then
if [ $((log_rc)) -gt 0 ]
then
class="error"
log_rc=", rc: ${log_rc}"
log_msg="${log_msg}${log_rc}"
fi
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
fi
}
################################################
# f_space: check mount points/space requirements # f_space: check mount points/space requirements
# #
f_space() f_space()
@ -570,7 +551,6 @@ f_cntconfig()
{ {
local src_name local src_name
local count=0 local count=0
local count_sum=0
for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*) for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
do do
@ -581,44 +561,130 @@ f_cntconfig()
count=$((count / 2)) count=$((count / 2))
fi fi
"${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}" "${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}"
count_sum=$((count_sum + count)) adb_count=$((adb_count + count))
done done
"${adb_uci}" -q set "adblock.global.adb_overall_count=${count_sum}" "${adb_uci}" -q set "adblock.global.adb_overall_count=${adb_count}"
} }
# f_rmconfig: remove counters & timestamps in given config sections # f_rmconfig: remove volatile config entries
# #
f_rmconfig() f_rmconfig()
{ {
local src_name local opt
local rm_done="${1}" local options="adb_src_timestamp adb_src_count"
local section="${1}"
for src_name in ${rm_done} rm_cfg="${adb_lastrun}"
do if [ -n "${rm_cfg}" ]
src_name="${src_name#*.}" then
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_count" "${adb_uci}" -q delete "adblock.global.adb_overall_count"
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_timestamp" "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
done "${adb_uci}" -q delete "adblock.global.adb_percentage"
"${adb_uci}" -q delete "adblock.global.adb_lastrun"
for opt in ${options}
do
"${adb_uci}" -q delete "adblock.${section}.${opt}"
done
fi
} }
# f_exit: delete (temporary) files, generate statistics and exit # f_rmdns: remove dns block lists and backups
#
f_rmdns()
{
rm_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
if [ -n "${rm_dns}" ]
then
rm -rf "${adb_dnshidedir}"
if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
then
rm -f "${adb_dir_backup}/${adb_dnsprefix}"*.gz
fi
/etc/init.d/dnsmasq restart
fi
}
# f_rmuhttpd: remove uhttpd instances
#
f_rmuhttpd()
{
rm_uhttpd="$(pgrep -f "uhttpd -h /www/adblock")"
if [ -n "${rm_uhttpd}" ]
then
for pid in ${rm_uhttpd}
do
kill -9 "${pid}"
done
fi
}
# f_rmfirewall: remove firewall rulsets
#
f_rmfirewall()
{
rm_fw="$(iptables -w -t nat -vnL | grep -Fo "adb-")"
if [ -n "${rm_fw}" ]
then
iptables-save -t nat | grep -Fv -- "adb-" | iptables-restore
iptables-save -t filter | grep -Fv -- "adb-" | iptables-restore
if [ -n "$(lsmod | grep -Fo "ip6table_nat")" ]
then
ip6tables-save -t nat | grep -Fv -- "adb-" | ip6tables-restore
ip6tables-save -t filter | grep -Fv -- "adb-" | ip6tables-restore
fi
fi
}
# f_log: log messages to stdout and syslog
#
f_log()
{
local log_parm
local log_msg="${1}"
local class="info "
local lastrun="$(date "+%d.%m.%Y %H:%M:%S")"
# check for terminal session
#
if [ -t 1 ]
then
log_parm="-s"
fi
# log to different output devices and set log class accordingly
#
if [ -n "${log_msg}" ]
then
if [ $((rc)) -gt 0 ]
then
class="error"
fi
logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
# clean exit on error
#
if [ $((rc)) -eq -1 ] || [ $((rc)) -gt 0 ]
then
f_rmdns
f_rmuhttpd
f_rmfirewall
config_foreach f_rmconfig source
"${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun} => runtime error, please check the log!"
"${adb_uci}" -q commit "adblock"
fi
fi
}
# f_exit: delete temporary files, generate statistics and exit
# #
f_exit() f_exit()
{ {
local ipv4_blk=0 ipv4_all=0 ipv4_pct=0 local ipv4_blk=0 ipv4_all=0 ipv4_pct=0
local ipv6_blk=0 ipv6_all=0 ipv6_pct=0 local ipv6_blk=0 ipv6_all=0 ipv6_pct=0
local lastrun="$(${adb_date} "+%d.%m.%Y %H:%M:%S")" local lastrun="$(date "+%d.%m.%Y %H:%M:%S")"
# delete temporary files & directories rm -f "${adb_tmpfile}"
# rm -rf "${adb_tmpdir}"
if [ -f "${adb_tmpfile}" ]
then
rm -f "${adb_tmpfile}"
fi
if [ -d "${adb_tmpdir}" ]
then
rm -rf "${adb_tmpdir}"
fi
# final log message and iptables statistics # final log message and iptables statistics
# #
@ -626,8 +692,8 @@ f_exit()
then then
if [ -n "${adb_wanif4}" ] if [ -n "${adb_wanif4}" ]
then then
ipv4_blk="$(${adb_iptv4} -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')" ipv4_blk="$(iptables -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
ipv4_all="$(${adb_iptv4} -t nat -vnL PREROUTING | awk '$3 ~ /^prerouting_rule$/ {sum += $1} END {printf sum}')" ipv4_all="$(iptables -t nat -vnL PREROUTING | awk '$3 ~ /^prerouting_rule$/ {sum += $1} END {printf sum}')"
if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ] if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ]
then then
ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')" ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')"
@ -635,8 +701,8 @@ f_exit()
fi fi
if [ -n "${adb_wanif6}" ] if [ -n "${adb_wanif6}" ]
then then
ipv6_blk="$(${adb_iptv6} -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')" ipv6_blk="$(ip6tables -t nat -vnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
ipv6_all="$(${adb_iptv6} -t nat -vnL PREROUTING | awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')" ipv6_all="$(ip6tables -t nat -vnL PREROUTING | awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')"
if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ] if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ]
then then
ipv6_pct="$(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')" ipv6_pct="$(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')"
@ -649,7 +715,6 @@ f_exit()
f_log "domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})" f_log "domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
elif [ $((rc)) -gt 0 ] elif [ $((rc)) -gt 0 ]
then then
"${adb_uci}" -q revert "adblock"
f_log "domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})" f_log "domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
else else
rc=0 rc=0

View File

@ -6,88 +6,82 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# set the C locale # prepare environment
#
LC_ALL=C
# set pid & logger
# #
adb_pid="${$}" adb_pid="${$}"
adb_pidfile="/var/run/adblock.pid" adb_pidfile="/var/run/adblock.pid"
adb_log="$(which logger)" adb_scriptver="1.2.5"
adb_mincfgver="2.2"
adb_scriptdir="${0%/*}"
if [ -r "${adb_pidfile}" ] if [ -r "${adb_pidfile}" ]
then then
rc=255 rc=255
"${adb_log}" -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))"
exit ${rc} exit ${rc}
else else
printf "${adb_pid}" > "${adb_pidfile}" printf "${adb_pid}" > "${adb_pidfile}"
fi if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
then
# get current directory and set script/config version if [ -z "$(type -f f_envload)" ]
# then
adb_scriptdir="${0%/*}" . "${adb_scriptdir}/adblock-helper.sh"
adb_scriptver="1.2.1" f_envload
adb_mincfgver="2.2" fi
else
# source in adblock function library rc=254
# logger -s -t "adblock[${adb_pid}] error" "adblock function library not found"
if [ -r "${adb_scriptdir}/adblock-helper.sh" ] rm -f "${adb_pidfile}"
then exit ${rc}
. "${adb_scriptdir}/adblock-helper.sh" fi
else
rc=254
"${adb_log}" -s -t "adblock[${adb_pid}] error" "adblock function library not found"
rm -f "${adb_pidfile}"
exit ${rc}
fi fi
# call trap function on error signals (HUP, INT, QUIT, BUS, SEGV, TERM) # call trap function on error signals (HUP, INT, QUIT, BUS, SEGV, TERM)
# #
trap "rc=250; f_log 'error signal received/trapped' '${rc}'; f_exit" 1 2 3 10 11 15 trap "rc=250; f_log 'error signal received/trapped'; f_exit" 1 2 3 10 11 15
# load environment
#
f_envload
# start logging
#
f_log "domain adblock processing started (${adb_scriptver}, ${adb_sysver}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
# check environment # check environment
# #
f_envcheck f_envcheck
# loop through active adblock domain sources, # main loop for all block list sources
# download sources, prepare output and store all extracted domains in temp file
# #
for src_name in ${adb_sources} for src_name in ${adb_sources}
do do
# check disabled sources
#
eval "enabled=\"\${enabled_${src_name}}\""
if [ "${enabled}" = "0" ]
then
if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
then
rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
then
rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
fi
rm_done="true"
f_log "=> disabled source '${src_name}' removed"
fi
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_timestamp"
continue
fi
f_log "=> processing source '${src_name}'"
eval "url=\"\${adb_src_${src_name}}\"" eval "url=\"\${adb_src_${src_name}}\""
eval "src_rset=\"\${adb_src_rset_${src_name}}\"" eval "src_rset=\"\${adb_src_rset_${src_name}}\""
eval "list_time=\"\${CONFIG_${src_name}_adb_src_timestamp}\""
adb_dnsfile="${adb_dnsdir}/${adb_dnsprefix}.${src_name}" adb_dnsfile="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
list_time="$(${adb_uci} -q get "adblock.${src_name}.adb_src_timestamp")"
f_log "=> processing adblock source '${src_name}'"
# check 'url' and 'src_rset' values # check 'url' and 'src_rset' values
# #
if [ -z "${url}" ] || [ -z "${src_rset}" ] if [ -z "${url}" ] || [ -z "${src_rset}" ]
then then
"${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=broken config" "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=broken config"
f_log " broken source configuration, check 'adb_src' and 'adb_src_rset' in config" f_log " broken source configuration, skipped"
continue continue
fi fi
# prepare find statement with active adblock list sources
#
if [ -z "${adb_srclist}" ]
then
adb_srclist="! -name ${adb_dnsprefix}.${src_name}*"
else
adb_srclist="${adb_srclist} -a ! -name ${adb_dnsprefix}.${src_name}*"
fi
# download only block list with newer/updated timestamp # download only block list with newer/updated timestamp
# #
if [ "${src_name}" = "blacklist" ] if [ "${src_name}" = "blacklist" ]
@ -99,7 +93,7 @@ do
if [ -z "${url_time}" ] if [ -z "${url_time}" ]
then then
url_time="$(date)" url_time="$(date)"
f_log " no online timestamp received" f_log " no online timestamp"
fi fi
if [ -z "${list_time}" ] || [ "${list_time}" != "${url_time}" ] || [ ! -r "${adb_dnsfile}" ] ||\ if [ -z "${list_time}" ] || [ "${list_time}" != "${url_time}" ] || [ ! -r "${adb_dnsfile}" ] ||\
([ "${backup_ok}" = "true" ] && [ ! -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]) ([ "${backup_ok}" = "true" ] && [ ! -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ])
@ -136,7 +130,7 @@ do
fi fi
rc=${?} rc=${?}
else else
f_log " source doesn't change, no update required" f_log " source doesn't change, skipped"
continue continue
fi fi
@ -154,8 +148,9 @@ do
unset tmp_domains unset tmp_domains
elif [ $((rc)) -eq 0 ] && [ -z "${tmp_domains}" ] elif [ $((rc)) -eq 0 ] && [ -z "${tmp_domains}" ]
then then
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
"${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty download" "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty download"
f_log " empty source download finished" f_log " empty source download, skipped"
continue continue
else else
rc=0 rc=0
@ -164,10 +159,11 @@ do
gunzip -cf "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}" gunzip -cf "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
count="$(wc -l < "${adb_tmpfile}")" count="$(wc -l < "${adb_tmpfile}")"
"${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored" "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored"
f_log " source download failed, list restored (${count} entries)" f_log " source download failed, restored (${count} entries)"
else else
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
"${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=download failed" "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=download failed"
f_log " source download failed" f_log " source download failed, skipped"
continue continue
fi fi
fi fi
@ -185,7 +181,7 @@ do
fi fi
rc=${?} rc=${?}
# finish domain processing, prepare find statement with revised adblock list source # finish domain processing, prepare find statement with revised block list source
# #
if [ $((rc)) -eq 0 ] if [ $((rc)) -eq 0 ]
then then
@ -203,7 +199,9 @@ do
then then
rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
fi fi
f_log " domain merging failed, list removed" "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
"${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=domain merging failed"
f_log " domain merging failed, skipped"
continue continue
fi fi
else else
@ -212,39 +210,18 @@ do
then then
rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
fi fi
"${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
"${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty domain input" "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty domain input"
f_log " empty domain input received, list removed" f_log " empty domain input, skipped"
continue continue
fi fi
done done
# remove disabled adblock lists and their backups # make separate block lists entries unique
#
if [ -n "${adb_srclist}" ]
then
rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_srclist} \) -print -exec rm -f "{}" \;)"
if [ "${backup_ok}" = "true" ] && [ -n "${rm_done}" ]
then
find "${adb_dir_backup}" -maxdepth 1 -type f \( ${adb_srclist} \) -exec rm -f "{}" \;
fi
else
rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
if [ "${backup_ok}" = "true" ]
then
find "${adb_dir_backup}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -exec rm -f "{}" \;
fi
fi
if [ -n "${rm_done}" ]
then
f_rmconfig "${rm_done}"
f_log "disabled adblock lists removed"
fi
# make separate adblock lists entries unique
# #
if [ "${mem_ok}" = "true" ] && [ -n "${adb_revsrclist}" ] if [ "${mem_ok}" = "true" ] && [ -n "${adb_revsrclist}" ]
then then
f_log "remove duplicates in separate adblock lists" f_log "remove duplicates in separate block lists"
# generate a unique overall block list # generate a unique overall block list
# #
@ -269,42 +246,43 @@ then
rm -f "${adb_tmpdir}/blocklist.overall" rm -f "${adb_tmpdir}/blocklist.overall"
fi fi
# restart & check dnsmasq with newly generated set of adblock lists # restart & check dnsmasq with newly generated set of block lists
# #
f_cntconfig if [ -n "${adb_revsrclist}" ] || [ "${rm_done}" = "true" ]
adb_count="$(${adb_uci} -q get "adblock.global.adb_overall_count")"
if [ -n "${adb_revsrclist}" ] || [ -n "${rm_done}" ]
then then
"${adb_uci}" -q set "adblock.global.adb_dnstoggle=on" "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
sleep 1 sleep 1
check="$(pgrep -f "dnsmasq")" check="$(pgrep -f "dnsmasq")"
if [ -n "${check}" ] if [ -n "${check}" ]
then then
f_log "adblock lists with overall ${adb_count} domains loaded" dns_ok="true"
else else
f_log "dnsmasq restart failed, retry without newly generated block lists" f_log "dnsmasq restart failed, retry without newly generated block lists"
rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \;)" rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \;)"
if [ -n "${rm_done}" ] if [ -n "${rm_done}" ]
then then
f_log "bogus adblock lists removed"
f_rmconfig "${rm_done}"
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
sleep 1 sleep 1
check="$(pgrep -f "dnsmasq")" check="$(pgrep -f "dnsmasq")"
if [ -n "${check}" ] if [ -n "${check}" ]
then then
f_cntconfig dns_ok="true"
f_log "adblock lists with overall ${adb_count} domains loaded"
else
rc=100
f_log "dnsmasq restart failed, please check 'logread' output" "${rc}"
f_exit
fi fi
fi fi
fi fi
if [ "${dns_ok}" = "true" ]
then
f_cntconfig
f_log "block lists with overall ${adb_count} domains loaded"
else
rc=100
f_log "dnsmasq restart finally failed, please check 'logread' output"
f_exit
fi
else else
f_log "adblock lists with overall ${adb_count} domains are still valid, no update required" f_cntconfig
f_log "block lists with overall ${adb_count} domains are still valid, no update required"
fi fi
# remove temporary files and exit # remove temporary files and exit

View File

@ -2,49 +2,35 @@
# #
START=99 START=99
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
EXTRA_COMMANDS="toggle cfgup" EXTRA_COMMANDS="toggle cfgup"
EXTRA_HELP=" toggle Toggle adblocking 'on' or 'off' EXTRA_HELP=" toggle Toggle adblocking 'on' or 'off'
cfgup Update the adblock configuration file" cfgup Update the adblock configuration file"
adb_debug=0 adb_debug=0
adb_pid="${$}"
adb_script="/usr/bin/adblock-update.sh"
adb_helper="/usr/bin/adblock-helper.sh"
adb_pidfile="/var/run/adblock.pid"
bg_parm="&"
unset log_parm
if [ $((adb_debug)) -eq 0 ] if [ $((adb_debug)) -eq 0 ]
then then
exec 2>/dev/null exec 2>/dev/null
fi fi
adb_pid="${$}"
adb_script="/usr/bin/adblock-update.sh"
adb_dnsdir="/tmp/dnsmasq.d"
adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
adb_dnsprefix="adb_list"
adb_pidfile="/var/run/adblock.pid"
adb_log="$(which logger)"
adb_uci="$(which uci)"
if [ -t 1 ] if [ -t 1 ]
then then
log_parm="-s" log_parm="-s"
unset bg_parm unset bg_parm
else
unset log_parm
bg_parm="&"
fi fi
if [ -r "${adb_pidfile}" ] if [ -r "${adb_pidfile}" ]
then then
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1 logger ${log_parm} -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1
exit 255 exit 255
fi fi
rm_config()
{
local value opt section="${1}" options="adb_src_timestamp adb_src_count"
for opt in ${options}
do
"${adb_uci}" -q delete "adblock.${section}.${opt}"
done
}
boot() boot()
{ {
return 0 return 0
@ -58,92 +44,68 @@ start()
restart() restart()
{ {
restart="true"
stop stop
start start
} }
reload() reload()
{ {
restart="true" reload="true"
stop stop
start start
} }
stop() stop()
{ {
cfg_check="$(${adb_uci} -q get "adblock.global.adb_overall_count")" . "${adb_helper}"
if [ -n "${cfg_check}" ] && [ -z "${restart}" ] f_envload
f_rmdns
f_rmuhttpd
config_foreach f_rmconfig source
if [ -n "$(${adb_uci} -q changes adblock)" ]
then then
. "/lib/functions.sh" "${adb_uci}" -q commit adblock
config_load adblock
config_foreach rm_config source
"${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
"${adb_uci}" -q delete "adblock.global.adb_overall_count"
"${adb_uci}" -q delete "adblock.global.adb_percentage"
"${adb_uci}" -q delete "adblock.global.adb_lastrun"
"${adb_uci}" -q commit "adblock"
fi fi
fw_check="$(iptables -w -vnL | grep -Fo "adb-")" if [ -z "${reload}" ]
if [ -n "${fw_check}" ] && [ -z "${restart}" ]
then then
iptables-save -t nat | grep -Fv -- "adb-" | iptables-restore f_rmfirewall
iptables-save -t filter | grep -Fv -- "adb-" | iptables-restore
if [ -n "$(lsmod | grep -F "ip6table_nat")" ]
then
ip6tables-save -t nat | grep -Fv -- "adb-" | ip6tables-restore
ip6tables-save -t filter | grep -Fv -- "adb-" | ip6tables-restore
fi
fi fi
if [ -d "${adb_dnshidedir}" ] if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "${rm_cfg}" ]
then then
find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -exec mv -f "{}" "${adb_dnsdir}" \; logger ${log_parm} -t "adblock[${adb_pid}] info " "all adblock related services stopped" 2>&1
fi
dns_check="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
if [ -n "${dns_check}" ]
then
rm -rf "${adb_dnshidedir}"
/etc/init.d/dnsmasq restart
fi
www_check="$(pgrep -f "uhttpd -h /www/adblock")"
if [ -n "${www_check}" ]
then
for pid in ${www_check}
do
kill -9 "${pid}"
done
fi
if [ -n "${cfg_check}" ] || [ -n "${fw_check}" ] || [ -n "${dns_check}" ] || [ -n "${www_check}" ]
then
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] info " "all adblock related services stopped" 2>&1
fi fi
return 0 return 0
} }
toggle() toggle()
{ {
if [ "$(${adb_uci} -q get "adblock.global.adb_restricted")" = "1" ] . "${adb_helper}"
then
adb_uci="$(which true)"
fi
if [ -d "${adb_dnshidedir}" ] if [ -d "${adb_dnshidedir}" ]
then then
list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)" list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)" list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
if [ -n "${list_dns}" ] if [ -n "${list_dns}" ]
then then
mv -f "${adb_dnsdir}/${adb_dnsprefix}"* "${adb_dnshidedir}" source="${adb_dnsdir}/${adb_dnsprefix}"
"${adb_uci}" -q set "adblock.global.adb_dnstoggle=off" target="${adb_dnshidedir}"
"${adb_uci}" -q commit "adblock" pos="off"
/etc/init.d/dnsmasq restart
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] info " "toggle for adblocking switched 'off'" 2>&1
elif [ -n "${list_dnshide}" ] elif [ -n "${list_dnshide}" ]
then then
mv -f "${adb_dnshidedir}/${adb_dnsprefix}"* "${adb_dnsdir}" source="${adb_dnshidedir}/${adb_dnsprefix}"
"${adb_uci}" -q set "adblock.global.adb_dnstoggle=on" target="${adb_dnsdir}"
pos="on"
fi
if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ]
then
if [ "$(${adb_uci} -q get adblock.global.adb_restricted)" = "1" ]
then
adb_uci="$(which true)"
fi
mv -f "${source}"* "${target}"
"${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}"
"${adb_uci}" -q commit "adblock" "${adb_uci}" -q commit "adblock"
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] info " "toggle for adblocking switched 'on'" 2>&1 logger ${log_parm} -t "adblock[${adb_pid}] info " "toggle for adblock switched '${pos}'" 2>&1
fi fi
fi fi
return 0 return 0
@ -156,8 +118,9 @@ cfgup()
rc=$? rc=$?
if [ $((rc)) -eq 0 ] if [ $((rc)) -eq 0 ]
then then
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] info " "default adblock configuration applied, please check the settings in '/etc/config/adblock'" 2>&1 logger ${log_parm} -t "adblock[${adb_pid}] info " "default adblock configuration applied, please check the settings in '/etc/config/adblock'" 2>&1
else else
"${adb_log}" ${log_parm} -t "adblock[${adb_pid}] info " "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'" 2>&1 logger ${log_parm} -t "adblock[${adb_pid}] info " "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'" 2>&1
fi fi
return 0
} }