mirror of
https://github.com/novatiq/packages.git
synced 2026-04-30 23:48:39 +01:00
adblock: release 1.1.0
* support more router modes, as long as the firewall and the DNS server
are enabled
* new source sites can be added & changed via awk ruleset in uci config
* source domain count, last update time & overall count will be stored
in uci config
* added 3 new source sites:
ransomware tracker
rolist/easylist
winspy
* switch to minimal inline base64 encoded 1×1 GIF for pixel server
(separate png image no longer needed)
* simplified uci parser
* source download & domain sort optimization
* add whitelist parser with wildcard support
* reduced code size & various cleanups
* updated documentation
Signed-off-by: Dirk Brenken <openwrt@brenken.org>
(cherry picked from commit b9aeb82c95)
This commit is contained in:
committed by
Hannu Nyman
parent
9aa63712b3
commit
bb5f4f6d29
+185
-208
@@ -2,36 +2,10 @@
|
||||
# function library used by adblock-update.sh
|
||||
# written by Dirk Brenken (openwrt@brenken.org)
|
||||
|
||||
#####################################
|
||||
# f_envload: load adblock environment
|
||||
#
|
||||
f_envload()
|
||||
{
|
||||
local cfg_version
|
||||
|
||||
# get version string from default adblock configuration file
|
||||
#
|
||||
cfg_version="$(/sbin/uci -q get adblock.global.adb_cfgver)"
|
||||
cfg_enabled="$(/sbin/uci -q get adblock.global.adb_enabled)"
|
||||
rc=$?
|
||||
if [ $((rc)) -ne 0 ] || [ "${cfg_version}" != "${adb_scriptver%.*}" ]
|
||||
then
|
||||
cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock"
|
||||
rc=$?
|
||||
if [ $((rc)) -eq 0 ]
|
||||
then
|
||||
f_log "new default adblock configuration applied, please check your settings in '/etc/config/adblock'"
|
||||
else
|
||||
f_log "original adblock configuration not found, please (re-)install the adblock package via 'opkg install adblock --force-maintainer'" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
elif [ $((rc)) -eq 0 ] && [ $((cfg_enabled)) -ne 1 ]
|
||||
then
|
||||
rc=-1
|
||||
f_log "adblock is currently disabled, please run 'uci set adblock.global.adb_enabled=1' and 'uci commit adblock' to enable this service"
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# source in openwrt function library
|
||||
#
|
||||
if [ -r "/lib/functions.sh" ]
|
||||
@@ -54,46 +28,22 @@ f_envload()
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# check opkg availability and get list with all installed openwrt packages
|
||||
#
|
||||
if [ -r "/var/lock/opkg.lock" ]
|
||||
then
|
||||
rc=-1
|
||||
f_log "adblock installation finished, 'opkg' currently locked by package installer"
|
||||
f_exit
|
||||
fi
|
||||
pkg_list="$(opkg list-installed)"
|
||||
if [ -z "${pkg_list}" ]
|
||||
then
|
||||
rc=120
|
||||
f_log "empty openwrt package list" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
}
|
||||
|
||||
######################################################
|
||||
# f_envparse: parse adblock config and set environment
|
||||
#
|
||||
f_envparse()
|
||||
{
|
||||
# set initial defaults,
|
||||
# may be overwritten by setting appropriate adblock config options in global section of /etc/config/adblock
|
||||
#
|
||||
adb_wanif="wan"
|
||||
adb_lanif="lan"
|
||||
adb_port="65535"
|
||||
adb_nullport="65535"
|
||||
adb_nullipv4="192.0.2.1"
|
||||
adb_nullipv6="::ffff:c000:0201"
|
||||
adb_blacklist="/etc/adblock/adblock.blacklist"
|
||||
adb_whitelist="/etc/adblock/adblock.whitelist"
|
||||
adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\$1)}"
|
||||
adb_forcedns=1
|
||||
|
||||
# function to read global options by callback
|
||||
# function to parse global section by callback
|
||||
#
|
||||
config_cb()
|
||||
{
|
||||
local type="${1}"
|
||||
local name="${2}"
|
||||
if [ "${type}" = "adblock" ]
|
||||
then
|
||||
option_cb()
|
||||
@@ -107,125 +57,107 @@ f_envparse()
|
||||
fi
|
||||
}
|
||||
|
||||
# function to iterate through config list, read only options in "enabled" sections
|
||||
# function to parse 'service' and 'source' sections
|
||||
#
|
||||
adb_cfglist="adb_backupdir adb_src"
|
||||
unset adb_sources
|
||||
parse_config()
|
||||
{
|
||||
local config="${1}"
|
||||
config_get switch "${config}" "enabled"
|
||||
local value opt section="${1}" options="adb_dir adb_src adb_src_rset adb_src_cat"
|
||||
config_get switch "${section}" "enabled"
|
||||
if [ "${switch}" = "1" ]
|
||||
then
|
||||
for option in ${adb_cfglist}
|
||||
if [ "${section}" != "backup" ]
|
||||
then
|
||||
eval "adb_sources=\"${adb_sources} ${section}\""
|
||||
fi
|
||||
for opt in ${options}
|
||||
do
|
||||
config_get value "${config}" "${option}"
|
||||
config_get value "${section}" "${opt}"
|
||||
if [ -n "${value}" ]
|
||||
then
|
||||
if [ "${option}" = "adb_src" ]
|
||||
then
|
||||
if [ "${config}" = "shalla" ]
|
||||
then
|
||||
categories()
|
||||
{
|
||||
local cat="${1}"
|
||||
adb_cat_shalla="${adb_cat_shalla} ${cat}"
|
||||
}
|
||||
eval "adb_arc_shalla=\"${value}\""
|
||||
config_list_foreach "shalla" "adb_catlist" "categories"
|
||||
else
|
||||
adb_sources="${adb_sources} ${value}"
|
||||
fi
|
||||
else
|
||||
eval "${option}=\"${value}\""
|
||||
fi
|
||||
eval "${opt}_${section}=\"${value}\""
|
||||
fi
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# 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 openwrt packages
|
||||
#
|
||||
pkg_list="$(opkg list-installed)"
|
||||
if [ -z "${pkg_list}" ]
|
||||
then
|
||||
rc=120
|
||||
f_log "empty openwrt package list" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# load adblock config and start parsing functions
|
||||
#
|
||||
config_load adblock
|
||||
config_foreach parse_config service
|
||||
config_foreach parse_config source
|
||||
|
||||
# check 'enabled' & 'version' config options
|
||||
#
|
||||
if [ -z "${adb_enabled}" ] || [ -z "${adb_cfgversion}" ] || [ "${adb_cfgversion}" != "${adb_scriptver%.*}" ]
|
||||
then
|
||||
rc=125
|
||||
f_log "no valid adblock configuration found, please merge latest changes from '/etc/config/adblock.opkg' manually" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
if [ $((adb_enabled)) -ne 1 ]
|
||||
then
|
||||
rc=-1
|
||||
f_log "adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service"
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# set more script defaults (can't be overwritten by adblock config options)
|
||||
#
|
||||
adb_count=0
|
||||
adb_minspace=12000
|
||||
adb_tmpfile="$(mktemp -tu)"
|
||||
adb_tmpdir="$(mktemp -p /tmp -d)"
|
||||
adb_dnsdir="/tmp/dnsmasq.d"
|
||||
adb_dnsprefix="adb_list"
|
||||
adb_prechain_ipv4="prerouting_rule"
|
||||
adb_fwdchain_ipv4="forwarding_rule"
|
||||
adb_outchain_ipv4="output_rule"
|
||||
adb_prechain_ipv6="PREROUTING"
|
||||
adb_fwdchain_ipv6="forwarding_rule"
|
||||
adb_outchain_ipv6="output_rule"
|
||||
adb_fetch="/usr/bin/wget"
|
||||
unset adb_srclist adb_revsrclist adb_errsrclist
|
||||
|
||||
# set adblock source ruleset definitions
|
||||
#
|
||||
rset_core="([A-Za-z0-9_-]+\.){1,}[A-Za-z]+"
|
||||
rset_adaway="awk '\$0 ~/^127\.0\.0\.1[ \t]+${rset_core}/{print tolower(\$2)}'"
|
||||
rset_blacklist="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_disconnect="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_dshield="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_feodo="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_malware="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_malwarelist="awk '\$0 ~/^127\.0\.0\.1[ \t]+${rset_core}/{print tolower(\$2)}'"
|
||||
rset_openphish="awk -F '/' '\$3 ~/^${rset_core}/{print tolower(\$3)}'"
|
||||
rset_palevo="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_ruadlist="awk -F '[|^]' '\$0 ~/^\|\|${rset_core}\^$/{print tolower(\$3)}'"
|
||||
rset_shalla="awk -F '/' '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_spam404="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_sysctl="awk '\$0 ~/^127\.0\.0\.1[ \t]+${rset_core}/{print tolower(\$2)}'"
|
||||
rset_whocares="awk '\$0 ~/^127\.0\.0\.1[ \t]+${rset_core}/{print tolower(\$2)}'"
|
||||
rset_winhelp="awk '\$0 ~/^0\.0\.0\.0[ \t]+${rset_core}/{print tolower(\$2)}'"
|
||||
rset_yoyo="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
rset_zeus="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
|
||||
|
||||
# get logical wan update interfaces (with default route) and their device names
|
||||
#
|
||||
network_find_wan adb_wanif4
|
||||
network_find_wan6 adb_wanif6
|
||||
if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
|
||||
then
|
||||
rc=125
|
||||
f_log "no valid IPv4/IPv6 wan update interface found" "${rc}"
|
||||
f_exit
|
||||
elif [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
|
||||
then
|
||||
rc=125
|
||||
f_log "LAN only (${adb_lanif}) network, no valid IPv4/IPv6 wan update interface found" "${rc}"
|
||||
f_exit
|
||||
else
|
||||
network_get_device adb_wandev4 "${adb_wanif4}"
|
||||
network_get_device adb_wandev6 "${adb_wanif6}"
|
||||
fi
|
||||
|
||||
# get lan ip addresses
|
||||
#
|
||||
network_get_ipaddr adb_ipv4 "${adb_lanif}"
|
||||
network_get_ipaddr6 adb_ipv6 "${adb_lanif}"
|
||||
if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
|
||||
then
|
||||
rc=130
|
||||
f_log "no valid IPv4/IPv6 configuration for given logical LAN interface found (${adb_lanif}), please set 'adb_lanif' manually" "${rc}"
|
||||
rc=135
|
||||
f_log "no valid IPv4/IPv6 configuration found (${adb_lanif}), please set 'adb_lanif' manually" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
}
|
||||
|
||||
#################################################
|
||||
# f_envcheck: check/set environment prerequisites
|
||||
#
|
||||
f_envcheck()
|
||||
{
|
||||
local check
|
||||
|
||||
# check logical update interfaces (with default route)
|
||||
#
|
||||
network_find_wan adb_wanif4
|
||||
network_find_wan6 adb_wanif6
|
||||
if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
|
||||
then
|
||||
adb_wanif4="true"
|
||||
f_log "no valid IPv4/IPv6 interface with default route found, IPv4 mode will be assumed"
|
||||
fi
|
||||
|
||||
# check general package dependencies
|
||||
#
|
||||
f_depend "uhttpd"
|
||||
@@ -252,24 +184,13 @@ f_envcheck()
|
||||
fi
|
||||
fi
|
||||
|
||||
# check ca-certificates package and set wget parms accordingly
|
||||
# check ca-certificates package and set fetch parms accordingly
|
||||
#
|
||||
wget_parm="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=5 --connect-timeout=5 --read-timeout=5"
|
||||
fetch_parm="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=5 --connect-timeout=5 --read-timeout=5"
|
||||
check="$(printf "${pkg_list}" | grep "^ca-certificates -")"
|
||||
if [ -z "${check}" ]
|
||||
then
|
||||
wget_parm="${wget_parm} --no-check-certificate"
|
||||
fi
|
||||
|
||||
# check adblock blacklist/whitelist configuration
|
||||
#
|
||||
if [ ! -r "${adb_blacklist}" ]
|
||||
then
|
||||
f_log "adblock blacklist not found, source will be disabled"
|
||||
fi
|
||||
if [ ! -r "${adb_whitelist}" ]
|
||||
then
|
||||
f_log "adblock whitelist not found, source will be disabled"
|
||||
fetch_parm="${fetch_parm} --no-check-certificate"
|
||||
fi
|
||||
|
||||
# check adblock temp directory
|
||||
@@ -281,7 +202,7 @@ f_envcheck()
|
||||
then
|
||||
if [ $((av_space)) -le 2000 ]
|
||||
then
|
||||
rc=135
|
||||
rc=140
|
||||
f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)" "${rc}"
|
||||
f_exit
|
||||
else
|
||||
@@ -289,12 +210,12 @@ f_envcheck()
|
||||
fi
|
||||
fi
|
||||
else
|
||||
rc=135
|
||||
rc=145
|
||||
f_log "temp directory not found" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# memory check
|
||||
# check memory
|
||||
#
|
||||
mem_total="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc/meminfo")"
|
||||
mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo")"
|
||||
@@ -309,12 +230,12 @@ f_envcheck()
|
||||
|
||||
# check backup configuration
|
||||
#
|
||||
if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
|
||||
if [ -n "${adb_dir_backup}" ] && [ -d "${adb_dir_backup}" ]
|
||||
then
|
||||
f_space "${adb_backupdir}"
|
||||
f_space "${adb_dir_backup}"
|
||||
if [ "${space_ok}" = "false" ]
|
||||
then
|
||||
f_log "not enough free space in '${adb_backupdir}'(avail. ${av_space} kb), backup/restore will be disabled"
|
||||
f_log "not enough free space in '${adb_dir_backup}'(avail. ${av_space} kb), backup/restore will be disabled"
|
||||
backup_ok="false"
|
||||
else
|
||||
f_log "backup/restore will be enabled"
|
||||
@@ -325,17 +246,49 @@ f_envcheck()
|
||||
f_log "backup/restore will be disabled"
|
||||
fi
|
||||
|
||||
# check running dnsmasq instance & set defaults
|
||||
#
|
||||
rc="$(ps | grep -q "[d]nsmasq"; printf ${?})"
|
||||
if [ $((rc)) -eq 0 ]
|
||||
then
|
||||
if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
|
||||
elif [ -n "${adb_wanif4}" ]
|
||||
then
|
||||
adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
|
||||
else
|
||||
adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
|
||||
fi
|
||||
else
|
||||
rc=150
|
||||
f_log "please enable the local dns server to use adblock" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# check running firewall
|
||||
#
|
||||
check="$(/usr/sbin/iptables -vnL | grep -F "DROP")"
|
||||
if [ -z "${check}" ]
|
||||
then
|
||||
rc=155
|
||||
f_log "please enable the local firewall to use adblock" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
|
||||
# check ipv4/iptables configuration
|
||||
#
|
||||
if [ -n "${adb_wanif4}" ] && [ -n "${adb_wandev4}" ]
|
||||
if [ -n "${adb_wanif4}" ]
|
||||
then
|
||||
f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-prerouting" "! -i ${adb_wandev4} -p tcp -d ${adb_nullipv4} -m multiport --dports 80,443 -j REDIRECT --to-ports ${adb_port}"
|
||||
f_firewall "IPv4" "filter" "A" "${adb_fwdchain_ipv4}" "adb-forward" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
|
||||
f_firewall "IPv4" "filter" "A" "${adb_outchain_ipv4}" "adb-output" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
|
||||
f_firewall "IPv4" "nat" "A" "prerouting_rule" "adb-nat" "-p tcp -d ${adb_nullipv4} -m multiport --dports 80,443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
|
||||
f_firewall "IPv4" "filter" "A" "forwarding_rule" "adb-fwd" "-p tcp -d ${adb_nullipv4} -j REJECT --reject-with tcp-reset"
|
||||
f_firewall "IPv4" "filter" "A" "forwarding_rule" "adb-fwd" "-d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
|
||||
f_firewall "IPv4" "filter" "A" "output_rule" "adb-out" "-p tcp -d ${adb_nullipv4} -j REJECT --reject-with tcp-reset"
|
||||
f_firewall "IPv4" "filter" "A" "output_rule" "adb-out" "-d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
|
||||
if [ $((adb_forcedns)) -eq 1 ]
|
||||
then
|
||||
f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p udp --dport 53 -j REDIRECT"
|
||||
f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p tcp --dport 53 -j REDIRECT"
|
||||
f_firewall "IPv4" "nat" "A" "prerouting_rule" "adb-dns" "-p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
|
||||
f_firewall "IPv4" "nat" "A" "prerouting_rule" "adb-dns" "-p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
|
||||
fi
|
||||
if [ "${fw_done}" = "true" ]
|
||||
then
|
||||
@@ -346,15 +299,17 @@ f_envcheck()
|
||||
|
||||
# check ipv6/ip6tables configuration
|
||||
#
|
||||
if [ -n "${adb_wanif6}" ] && [ -n "${adb_wandev6}" ]
|
||||
if [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-prerouting" "! -i ${adb_wandev6} -p tcp -d ${adb_nullipv6} -m multiport --dports 80,443 -j REDIRECT --to-ports ${adb_port}"
|
||||
f_firewall "IPv6" "filter" "A" "${adb_fwdchain_ipv6}" "adb-forward" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
|
||||
f_firewall "IPv6" "filter" "A" "${adb_outchain_ipv6}" "adb-output" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
|
||||
f_firewall "IPv6" "nat" "A" "PREROUTING" "adb-nat" "-p tcp -d ${adb_nullipv6} -m multiport --dports 80,443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
|
||||
f_firewall "IPv6" "filter" "A" "forwarding_rule" "adb-fwd" "-p tcp -d ${adb_nullipv6} -j REJECT --reject-with tcp-reset"
|
||||
f_firewall "IPv6" "filter" "A" "forwarding_rule" "adb-fwd" "-d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
|
||||
f_firewall "IPv6" "filter" "A" "output_rule" "adb-out" "-p tcp -d ${adb_nullipv6} -j REJECT --reject-with tcp-reset"
|
||||
f_firewall "IPv6" "filter" "A" "output_rule" "adb-out" "-d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
|
||||
if [ $((adb_forcedns)) -eq 1 ]
|
||||
then
|
||||
f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p udp --dport 53 -j REDIRECT"
|
||||
f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p tcp --dport 53 -j REDIRECT"
|
||||
f_firewall "IPv6" "nat" "A" "PREROUTING" "adb-dns" "-p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
|
||||
f_firewall "IPv6" "nat" "A" "PREROUTING" "adb-dns" "-p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
|
||||
fi
|
||||
if [ "${fw_done}" = "true" ]
|
||||
then
|
||||
@@ -370,15 +325,14 @@ f_envcheck()
|
||||
then
|
||||
if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}" -p "[${adb_ipv6}]:${adb_port}"
|
||||
uhttpd -h "/www/adblock" -k 0 -N 100 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_nullport}" -p "[${adb_ipv6}]:${adb_nullport}"
|
||||
rc=${?}
|
||||
elif [ -n "${adb_wanif4}" ]
|
||||
then
|
||||
uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}"
|
||||
uhttpd -h "/www/adblock" -k 0 -N 100 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_nullport}"
|
||||
rc=${?}
|
||||
elif [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_port}"
|
||||
else
|
||||
uhttpd -h "/www/adblock" -k 0 -N 100 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_nullport}"
|
||||
rc=${?}
|
||||
fi
|
||||
if [ $((rc)) -eq 0 ]
|
||||
@@ -390,17 +344,11 @@ f_envcheck()
|
||||
fi
|
||||
fi
|
||||
|
||||
# set dnsmasq defaults
|
||||
# check whitelist entries
|
||||
#
|
||||
if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
|
||||
if [ -s "${adb_whitelist}" ]
|
||||
then
|
||||
adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
|
||||
elif [ -n "${adb_wanif4}" ]
|
||||
then
|
||||
adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
|
||||
elif [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
|
||||
awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
|
||||
fi
|
||||
|
||||
# remove no longer used opkg package list
|
||||
@@ -408,7 +356,6 @@ f_envcheck()
|
||||
unset pkg_list
|
||||
}
|
||||
|
||||
######################################
|
||||
# f_depend: check package dependencies
|
||||
#
|
||||
f_depend()
|
||||
@@ -419,13 +366,12 @@ f_depend()
|
||||
check="$(printf "${pkg_list}" | grep "^${package} -")"
|
||||
if [ -z "${check}" ]
|
||||
then
|
||||
rc=140
|
||||
rc=160
|
||||
f_log "package '${package}' not found" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
}
|
||||
|
||||
##############################################
|
||||
# f_firewall: set iptables rules for ipv4/ipv6
|
||||
#
|
||||
f_firewall()
|
||||
@@ -461,12 +407,11 @@ f_firewall()
|
||||
fw_done="true"
|
||||
else
|
||||
f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}"
|
||||
f_restore
|
||||
f_exit
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
##########################################
|
||||
# f_log: log messages to stdout and syslog
|
||||
#
|
||||
f_log()
|
||||
@@ -514,7 +459,49 @@ f_space()
|
||||
fi
|
||||
}
|
||||
|
||||
##################################################################
|
||||
# f_cntconfig: calculate counters in config
|
||||
#
|
||||
f_cntconfig()
|
||||
{
|
||||
local list
|
||||
local src_name
|
||||
local count=0
|
||||
local count_sum=0
|
||||
|
||||
for list in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}."*)
|
||||
do
|
||||
src_name="${list/*./}"
|
||||
count="$(wc -l < "${list}")"
|
||||
if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
count=$((count / 2))
|
||||
fi
|
||||
uci_set "adblock" "${src_name}" "adb_src_count" "${count}"
|
||||
count_sum=$((count_sum + count))
|
||||
done
|
||||
uci_set "adblock" "global" "adb_overall_count" "${count_sum}"
|
||||
}
|
||||
|
||||
# f_rmconfig: remove counters & timestamps in given config sections
|
||||
#
|
||||
f_rmconfig()
|
||||
{
|
||||
local rm_done="${1}"
|
||||
|
||||
for list in ${rm_done}
|
||||
do
|
||||
src_name="${list/*./}"
|
||||
if [ -n "${restore_done}" ]
|
||||
then
|
||||
uci_set "adblock" "${src_name}" "adb_src_timestamp" "list restored"
|
||||
else
|
||||
uci_remove "adblock" "${src_name}" "adb_src_count"
|
||||
uci_remove "adblock" "${src_name}" "adb_src_timestamp"
|
||||
fi
|
||||
done
|
||||
unset restore_done
|
||||
}
|
||||
|
||||
# f_restore: restore last adblock list backups and restart dnsmasq
|
||||
#
|
||||
f_restore()
|
||||
@@ -530,6 +517,7 @@ f_restore()
|
||||
rc=${?}
|
||||
if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
|
||||
then
|
||||
f_rmconfig "${rm_done}"
|
||||
f_log "all bogus adblock lists removed"
|
||||
elif [ $((rc)) -ne 0 ]
|
||||
then
|
||||
@@ -540,20 +528,20 @@ f_restore()
|
||||
|
||||
# restore backups
|
||||
#
|
||||
if [ "${backup_ok}" = "true" ] && [ "$(printf "${adb_backupdir}/${adb_dnsprefix}."*)" != "${adb_backupdir}/${adb_dnsprefix}.*" ]
|
||||
if [ "${backup_ok}" = "true" ]
|
||||
then
|
||||
restore_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec cp -pf "{}" "${adb_dnsdir}" \;)"
|
||||
restore_done="$(find "${adb_dir_backup}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec cp -pf "{}" "${adb_dnsdir}" \;)"
|
||||
rc=${?}
|
||||
if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
|
||||
then
|
||||
f_log "all available backups restored"
|
||||
elif [ $((rc)) -ne 0 ]
|
||||
elif [ $((rc)) -ne 0 ] && [ -n "${restore_done}" ]
|
||||
then
|
||||
f_log "error during restore of adblock lists" "${rc}"
|
||||
f_exit
|
||||
fi
|
||||
else
|
||||
f_log "no backups found, nothing to restore"
|
||||
f_log "backup service disabled, nothing to restore"
|
||||
fi
|
||||
|
||||
# (re-)try dnsmasq restart without bogus adblock lists / with backups
|
||||
@@ -566,32 +554,22 @@ f_restore()
|
||||
if [ $((rc)) -eq 0 ]
|
||||
then
|
||||
rc=0
|
||||
adb_count="$(head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* | wc -l)"
|
||||
if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
adb_count="$((adb_count / 2))"
|
||||
fi
|
||||
f_cntconfig
|
||||
f_log "adblock lists with overall ${adb_count} domains loaded"
|
||||
else
|
||||
rc=145
|
||||
rc=165
|
||||
f_log "dnsmasq restart failed, please check 'logread' output" "${rc}"
|
||||
fi
|
||||
fi
|
||||
f_exit
|
||||
}
|
||||
|
||||
###################################
|
||||
# f_exit: delete (temporary) files,
|
||||
# generate statistics and exit
|
||||
# f_exit: delete (temporary) files, generate statistics and exit
|
||||
#
|
||||
f_exit()
|
||||
{
|
||||
local ipv4_prerouting=0
|
||||
local ipv4_forward=0
|
||||
local ipv4_output=0
|
||||
local ipv6_prerouting=0
|
||||
local ipv6_forward=0
|
||||
local ipv6_output=0
|
||||
local ipv4_adblock=0
|
||||
local ipv6_adblock=0
|
||||
local iptv4="/usr/sbin/iptables"
|
||||
local iptv6="/usr/sbin/ip6tables"
|
||||
|
||||
@@ -612,20 +590,19 @@ f_exit()
|
||||
then
|
||||
if [ -n "${adb_wanif4}" ]
|
||||
then
|
||||
ipv4_prerouting="$(${iptv4} -t nat -vnL | awk '$11 ~ /^adb-prerouting$/ {sum += $1} END {printf sum}')"
|
||||
ipv4_forward="$(${iptv4} -vnL | awk '$11 ~ /^adb-forward$/ {sum += $1} END {printf sum}')"
|
||||
ipv4_output="$(${iptv4} -vnL | awk '$11 ~ /^adb-output$/ {sum += $1} END {printf sum}')"
|
||||
ipv4_adblock="$(${iptv4} -t nat -vnL | awk '$11 ~ /^adb-nat$/ {sum += $1} END {printf sum}')"
|
||||
ipv4_adblock="$((${ipv4_adblock} + $(${iptv4} -vnL | awk '$11 ~ /^adb-(fwd|out)$/ {sum += $1} END {printf sum}')))"
|
||||
fi
|
||||
if [ -n "${adb_wanif6}" ]
|
||||
then
|
||||
ipv6_prerouting="$(${iptv6} -t nat -vnL | awk '$10 ~ /^adb-prerouting$/ {sum += $1} END {printf sum}')"
|
||||
ipv6_forward="$(${iptv6} -vnL | awk '$10 ~ /^adb-forward$/ {sum += $1} END {printf sum}')"
|
||||
ipv6_output="$(${iptv6} -vnL | awk '$10 ~ /^adb-output$/ {sum += $1} END {printf sum}')"
|
||||
ipv6_adblock="$(${iptv6} -t nat -vnL | awk '$10 ~ /^adb-nat$/ {sum += $1} END {printf sum}')"
|
||||
ipv6_adblock="$((${ipv6_adblock} + $(${iptv6} -vnL | awk '$10 ~ /^adb-(fwd|out)$/ {sum += $1} END {printf sum}')))"
|
||||
fi
|
||||
f_log "adblock firewall statistics (IPv4/IPv6):"
|
||||
f_log "${ipv4_prerouting}/${ipv6_prerouting} packets redirected in PREROUTING chain"
|
||||
f_log "${ipv4_forward}/${ipv6_forward} packets rejected in FORWARD chain"
|
||||
f_log "${ipv4_output}/${ipv6_output} packets rejected in OUTPUT chain"
|
||||
if [ -n "$(uci changes adblock)" ]
|
||||
then
|
||||
uci_commit "adblock"
|
||||
fi
|
||||
f_log "firewall statistics (IPv4/IPv6): ${ipv4_adblock}/${ipv6_adblock} ad related packets blocked"
|
||||
f_log "domain adblock processing finished successfully (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
|
||||
elif [ $((rc)) -gt 0 ]
|
||||
then
|
||||
|
||||
Reference in New Issue
Block a user