@ -2,14 +2,13 @@
# Copyright (c) 2015-2022 Dirk Brenken (dev@brenken.org)
# This is free software, licensed under the GNU General Public License v3.
# set (s)hellcheck except io ns
# shellcheck disable=1091,2010,2016,2034,2039,2059,2086,2091,2129,2143,2154,2181,2183,2188
# di sabl e (s)hellcheck in relea se
# shellcheck disable=all
START=30
USE_PROCD=1
if [ -n "$(type -t extra_command)" ]
then
if [ -n "$(type -t extra_command)" ]; then
extra_command "suspend" "Suspend adblock processing"
extra_command "resume" "Resume adblock processing"
extra_command "query" "<domain> Query active blocklists and backups for a specific domain"
@ -33,35 +32,26 @@ adb_init="/etc/init.d/adblock"
adb_script="/usr/bin/adblock.sh"
adb_pidfile="/var/run/adblock.pid"
if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] || \
[ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] || \
[ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] || \
{ [ "${action}" = "list" ] && [ -n "${1}" ]; }; }
then
if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] ||
[ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] ||
[ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] ||
{ [ "${action}" = "list" ] && [ -n "${1}" ]; }; }; then
exit 0
fi
version()
{
version() {
rc_procd "${adb_script}" version
}
boot()
{
[ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
boot() {
[ -s "${adb_pidfile}" ] && : >"${adb_pidfile}"
rc_procd start_service
}
start_service()
{
if [ "$("${adb_init}" enabled; printf "%u" ${?})" -eq 0 ]
then
if [ "${action}" = "boot" ]
then
if [ -n "$(uci_get adblock global adb_trigger)" ]
then
return 0
fi
start_service() {
if "${adb_init}" enabled; then
if [ "${action}" = "boot" ]; then
[ -n "$(uci_get adblock global adb_trigger)" ] && return 0
fi
procd_open_instance "adblock"
procd_set_param command "${adb_script}" "${@}"
@ -73,140 +63,99 @@ start_service()
fi
}
reload_service()
{
reload_service() {
rc_procd start_service reload
}
stop_service()
{
stop_service() {
rc_procd "${adb_script}" stop
}
restart()
{
restart() {
rc_procd start_service restart
}
suspend()
{
suspend() {
rc_procd start_service suspend
}
resume()
{
resume() {
rc_procd start_service resume
}
query()
{
query() {
rc_procd "${adb_script}" query "${1}"
}
report()
{
report() {
rc_procd "${adb_script}" report "${1:-"cli"}" "${2}" "${3}"
}
list()
{
local src_archive src_file src_enabled enabled name sha_list utc_list action="${1}"
list() {
local src_archive src_file src_enabled enabled name utc_list size focus descurl action="${1}"
if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]
then
if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]; then
shift
for name in "${@}"
do
for name in "${@}"; do
case "${action}" in
"add")
if [ -z "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
then
if ! uci_get adblock global adb_sources | grep -q "${name}"; then
uci_add_list adblock global adb_sources "${name}"
printf "%s\n" "::: adblock source '${name}' added to config"
fi
;;
;;
"remove")
if [ -n "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
then
if uci_get adblock global adb_sources | grep -q "${name}"; then
uci_remove_list adblock global adb_sources "${name}"
printf "%s\n" "::: adblock source '${name}' removed from config"
fi
;;
"add_sha")
if [ -z "$(uci_get adblock global adb_sha_sources | grep -Fo "${name}")" ]
then
uci_add_list adblock global adb_sha_sources "${name}"
printf "%s\n" "::: adblock shallalist '${name}' added to config"
fi
;;
"remove_sha")
if [ -n "$(uci_get adblock global adb_sha_sources | grep -Fo "${name}")" ]
then
uci_remove_list adblock global adb_sha_sources "${name}"
printf "%s\n" "::: adblock shallalist '${name}' removed from config"
fi
;;
;;
"add_utc")
if [ -z "$(uci_get adblock global adb_utc_sources | grep -Fo "${name}")" ]
then
if ! uci_get adblock global adb_utc_sources | grep -q "${name}"; then
uci_add_list adblock global adb_utc_sources "${name}"
printf "%s\n" "::: adblock utcapitole '${name}' added to config"
fi
;;
;;
"remove_utc")
if [ -n "$(uci_get adblock global adb_utc_sources | grep -Fo "${name}")" ]
then
if uci_get adblock global adb_utc_sources | grep -q "${name}"; then
uci_remove_list adblock global adb_utc_sources "${name}"
printf "%s\n" "::: adblock utcapitole '${name}' removed from config"
fi
;;
;;
"add_eng")
if [ -z "$(uci_get adblock global adb_eng_sources | grep -Fo "${name}")" ]
then
if ! uci_get adblock global adb_eng_sources | grep -q "${name}"; then
uci_add_list adblock global adb_eng_sources "${name}"
printf "%s\n" "::: adblock energized '${name}' added to config"
fi
;;
;;
"remove_eng")
if [ -n "$(uci_get adblock global adb_eng_sources | grep -Fo "${name}")" ]
then
if uci_get adblock global adb_eng_sources | grep -q "${name}"; then
uci_remove_list adblock global adb_eng_sources "${name}"
printf "%s\n" "::: adblock energized '${name}' removed from config"
fi
;;
;;
"add_stb")
if [ -z "$(uci_get adblock global adb_stb_sources | grep -Fo "${name}")" ]
then
if ! uci_get adblock global adb_stb_sources | grep -q "${name}"; then
uci_add_list adblock global adb_stb_sources "${name}"
printf "%s\n" "::: adblock stevenblack '${name}' added to config"
fi
;;
;;
"remove_stb")
if [ -n "$(uci_get adblock global adb_stb_sources | grep -Fo "${name}")" ]
then
if uci_get adblock global adb_stb_sources | grep -q "${name}"; then
uci_remove_list adblock global adb_stb_sources "${name}"
printf "%s\n" "::: adblock stevenblack '${name}' removed from config"
fi
;;
;;
esac
done
if [ -n "$(uci -q changes adblock)" ]
then
uci_commit adblock
"${adb_init}" start
fi
[ -n "$(uci -q changes adblock)" ] && { uci_commit adblock; "${adb_init}" start; }
else
src_archive="$(uci_get adblock global adb_srcarc "/etc/adblock/adblock.sources.gz")"
src_file="$(uci_get adblock global adb_srcfile "/tmp/adb_sources.json")"
src_enabled="$(uci -q show adblock.global.adb_sources)"
if [ -r "${src_archive}" ]
then
zcat "${src_archive}" > "${src_file}"
else
printf "%s\n" "::: adblock source archive '${src_archive}' not found"
fi
if [ -r "${src_file}" ]
then
[ -r "${src_archive}" ] && zcat "${src_archive}" >"${src_file}" || printf "%s\n" "::: adblock source archive '${src_archive}' not found"
if [ -r "${src_file}" ]; then
src_enabled="${src_enabled#*=}"
src_enabled="${src_enabled//\'}"
printf "%s\n" "::: Available adblock sources"
@ -215,18 +164,15 @@ list()
printf "%s\n" " -------------------------------------------------------------------"
json_load_file "${src_file}"
json_get_keys keylist
for key in ${keylist}
do
for key in ${keylist}; do
json_select "${key}"
json_get_var size "size"
json_get_var focus "focus"
json_get_var descurl "descurl"
json_get_var url "url"
json_get_var rule "rule"
if [ -n "${url}" ] && [ -n "${rule}" ]
then
if [ -n "$(printf "%s" "${src_enabled}" | grep -Fo "${key}")" ]
then
if [ -n "${url}" ] && [ -n "${rule}" ]; then
if printf "%s" "${src_enabled}" | grep -q "${key}"; then
enabled="x"
else
enabled=" "
@ -238,23 +184,19 @@ list()
fi
json_select ..
done
sha_list="$(uci_get adblock global adb_sha_sources "-")"
utc_list="$(uci_get adblock global adb_utc_sources "-")"
eng_list="$(uci_get adblock global adb_eng_sources "-")"
stb_list="$(uci_get adblock global adb_stb_sources "-")"
printf "%s\n" " ---------------------------------------------------------------------------"
printf " * %s\n" "Configured shallalist categories: ${sha_list// /, }"
printf " * %s\n" "Configured utcapitole categories: ${utc_list// /, }"
printf " * %s\n" "Configured energized variants: ${eng_list// /, }"
printf " * %s\n" "Configured stevenblack variants: ${stb_list// /, }"
if [ -n "${src_enabled// }" ]
then
if [ -n "${src_enabled// }" ]; then
printf "%s\n" " ---------------------------------------------------------------------------"
printf "%s\n" " Sources without valid configuration"
printf "%s\n" " Sources with in valid configuration"
printf "%s\n" " ---------------------------------------------------------------------------"
for key in ${src_enabled}
do
for key in ${src_enabled}; do
printf " - %s\n" "${key:0:20}"
done
fi
@ -264,41 +206,34 @@ list()
fi
}
status()
{
status() {
status_service
}
status_service()
{
local key keylist value idxval values rtfile
status_service() {
local key keylist value idxval values type rtfile
rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
json_load_file "${rtfile}" >/dev/null 2>&1
json_get_keys keylist
if [ -n "${keylist}" ]
then
if [ -n "${keylist}" ]; then
printf "%s\n" "::: adblock runtime information"
for key in ${keylist}
do
for key in ${keylist}; do
json_get_var value "${key}" >/dev/null 2>&1
if [ "${key%_*}" = "active" ]
then
if [ "${key%_*}" = "active" ]; then
printf " + %-15s : " "${key}"
json_select "${key}" >/dev/null 2>&1
values=""
index=1
while json_get_type type "${index}" && [ "${type}" = "object" ]
do
index="1"
while json_get_type type "${index}" && [ "${type}" = "object" ]; do
json_get_values idxval "${index}" >/dev/null 2>&1
if [ "${index}" = "1" ]
then
if [ "${index}" = "1" ]; then
values="${idxval}"
else
values="${values}, ${idxval}"
fi
index=$((index+1))
index=" $((index + 1))"
done
values="$(printf "%s" "${values}" | awk '{NR=1;max=98;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max)}else{printf"%-22s%s\n","",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
printf "%s\n" "${values:-"-"}"
@ -312,50 +247,40 @@ status_service()
fi
}
timer()
{
timer() {
local cron_file cron_content cron_lineno action="${1:-"list"}" cron_tasks="${2}" hour="${3}" minute="${4:-0}" weekday="${5:-"*"}"
cron_file="/etc/crontabs/root"
if [ -s "${cron_file}" ] && [ "${action}" = "list" ]
then
if [ -s "${cron_file}" ] && [ "${action}" = "list" ]; then
awk '{print NR "> " $0}' "${cron_file}"
elif [ "${action}" = "add" ]
then
elif [ -x "/etc/init.d/cron" ] && [ "${action}" = "add" ]; then
hour="${hour//[[:alpha:]]/}"
minute="${minute//[[:alpha:]]/}"
if [ -n "${cron_tasks}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] && \
[ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] && \
[ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]
then
printf "%02d %02d %s\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${cron_tasks}" >> "${cron_file}"
if [ -n "${cron_tasks}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] &&
[ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] &&
[ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]; then
printf "%02d %02d %s\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${cron_tasks}" >>"${cron_file}"
/etc/init.d/cron restart
fi
elif [ -s "${cron_file}" ] && [ "${action}" = "remove" ]
then
elif [ -x "/etc/init.d/cron" ] && [ -s "${cron_file}" ] && [ "${action}" = "remove" ]; then
cron_tasks="${cron_tasks//[[:alpha:]]/}"
cron_lineno="$(awk 'END{print NR}' "${cron_file}")"
cron_content="$(awk '{print $0}' "${cron_file}")"
if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]
then
printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" > "${cron_file}"
if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]; then
printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" >"${cron_file}"
/etc/init.d/cron restart
fi
fi
}
service_triggers()
{
service_triggers() {
local iface delay
iface="$(uci_get adblock global adb_trigger)"
delay="$(uci_get adblock global adb_triggerdelay "5")"
PROCD_RELOAD_DELAY=$((delay*1000))
PROCD_RELOAD_DELAY=" $((delay * 1000))"
if [ -n "${iface}" ]
then
procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start"
fi
[ -n "${iface}" ] && procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start"
procd_add_reload_trigger "adblock"
}