2016-02-15 19:25:21 +08:00
#!/bin/sh /etc/rc.common
2022-03-08 04:25:31 +08:00
# Copyright (c) 2015-2022 Dirk Brenken (dev@brenken.org)
2020-03-28 03:26:39 +08:00
# This is free software, licensed under the GNU General Public License v3.
2016-02-15 19:25:21 +08:00
2022-09-11 00:42:14 +08:00
# disable (s)hellcheck in release
# shellcheck disable=all
2020-10-18 17:05:22 +08:00
2017-09-09 23:35:42 +08:00
START=30
2016-12-20 03:11:25 +08:00
USE_PROCD=1
2016-02-15 19:25:21 +08:00
2022-09-11 00:42:14 +08:00
if [ -n "$(type -t extra_command)" ]; then
2021-02-27 03:52:55 +08:00
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"
2022-10-18 18:44:31 +08:00
extra_command "report" "[[<cli>|<mail>|<gen>|<json>] [<top_count>] [<res_count>] [<search>]] Print DNS statistics with an optional search parameter"
2021-02-27 03:52:55 +08:00
extra_command "list" "[<add>|<add_sha>|<add_utc>|<add_eng>|<add_stb>|<remove>|<remove_sha>|<remove_utc>|<remove_eng>|<remove_stb>] <source(s)> List/Edit available sources"
extra_command "timer" "[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
else
EXTRA_COMMANDS="status suspend resume query report list timer version"
EXTRA_HELP=" status Service status
suspend Suspend adblock processing
resume Resume adblock processing
query <domain> Query active blocklists and backups for a specific domain
2021-04-17 15:41:17 +08:00
report [[<cli>|<mail>|<gen>|<json>] [<count>] [<search>]] Print DNS statistics with an optional search parameter
2021-02-27 03:52:55 +08:00
list [<add>|<add_sha>|<add_utc>|<add_eng>|<add_stb>|<remove>|<remove_sha>|<remove_utc>|<remove_eng>|<remove_stb>] <source(s)> List/Edit available sources
2022-09-11 18:45:51 +08:00
timer [<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
2021-02-27 03:52:55 +08:00
fi
2016-02-15 19:25:21 +08:00
2017-03-26 04:49:44 +08:00
adb_init="/etc/init.d/adblock"
2016-12-20 03:11:25 +08:00
adb_script="/usr/bin/adblock.sh"
2018-01-11 02:33:42 +08:00
adb_pidfile="/var/run/adblock.pid"
2016-07-31 23:19:33 +08:00
2022-09-11 00:42:14 +08:00
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
2019-11-17 00:36:08 +08:00
exit 0
2019-10-31 19:34:48 +08:00
fi
2022-09-11 00:42:14 +08:00
boot() {
[ -s "${adb_pidfile}" ] && : >"${adb_pidfile}"
2018-09-01 14:35:35 +08:00
rc_procd start_service
2016-02-15 19:25:21 +08:00
}
2022-09-11 00:42:14 +08:00
start_service() {
if "${adb_init}" enabled; then
if [ "${action}" = "boot" ]; then
[ -n "$(uci_get adblock global adb_trigger)" ] && return 0
2018-09-01 14:35:35 +08:00
fi
procd_open_instance "adblock"
procd_set_param command "${adb_script}" "${@}"
procd_set_param pidfile "${adb_pidfile}"
2020-03-28 03:26:39 +08:00
procd_set_param nice "$(uci_get adblock global adb_nice "0")"
2018-09-01 14:35:35 +08:00
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
fi
2016-02-15 19:25:21 +08:00
}
2022-09-11 00:42:14 +08:00
reload_service() {
2018-09-01 14:35:35 +08:00
rc_procd start_service reload
2017-05-08 17:19:57 +08:00
}
2022-09-11 00:42:14 +08:00
stop_service() {
2018-09-01 14:35:35 +08:00
rc_procd "${adb_script}" stop
2016-02-15 19:25:21 +08:00
}
2016-06-03 21:14:53 +08:00
2022-09-11 00:42:14 +08:00
restart() {
2018-09-01 14:35:35 +08:00
rc_procd start_service restart
2017-02-05 04:32:50 +08:00
}
2022-09-11 00:42:14 +08:00
suspend() {
2019-08-15 20:02:30 +08:00
rc_procd start_service suspend
2016-06-03 21:14:53 +08:00
}
2022-09-11 00:42:14 +08:00
resume() {
2019-08-15 20:02:30 +08:00
rc_procd start_service resume
2016-06-22 05:14:29 +08:00
}
2022-09-11 00:42:14 +08:00
query() {
2018-09-01 14:35:35 +08:00
rc_procd "${adb_script}" query "${1}"
2016-12-23 14:15:11 +08:00
}
2022-09-11 00:42:14 +08:00
report() {
2022-10-18 18:44:31 +08:00
rc_procd "${adb_script}" report "${1:-"cli"}" "${2}" "${3}" "${4}"
2020-03-28 03:26:39 +08:00
}
2022-09-11 00:42:14 +08:00
list() {
local src_archive src_file src_enabled enabled name utc_list size focus descurl action="${1}"
2020-03-28 03:26:39 +08:00
2022-09-11 00:42:14 +08:00
if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]; then
2020-03-28 03:26:39 +08:00
shift
2022-09-11 00:42:14 +08:00
for name in "${@}"; do
2021-02-27 03:52:55 +08:00
case "${action}" in
"add")
2022-09-11 00:42:14 +08:00
if ! uci_get adblock global adb_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_add_list adblock global adb_sources "${name}"
printf "%s\n" "::: adblock source '${name}' added to config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"remove")
2022-09-11 00:42:14 +08:00
if uci_get adblock global adb_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_remove_list adblock global adb_sources "${name}"
printf "%s\n" "::: adblock source '${name}' removed from config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"add_utc")
2022-09-11 00:42:14 +08:00
if ! uci_get adblock global adb_utc_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_add_list adblock global adb_utc_sources "${name}"
printf "%s\n" "::: adblock utcapitole '${name}' added to config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"remove_utc")
2022-09-11 00:42:14 +08:00
if uci_get adblock global adb_utc_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_remove_list adblock global adb_utc_sources "${name}"
printf "%s\n" "::: adblock utcapitole '${name}' removed from config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"add_eng")
2022-09-11 00:42:14 +08:00
if ! uci_get adblock global adb_eng_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_add_list adblock global adb_eng_sources "${name}"
printf "%s\n" "::: adblock energized '${name}' added to config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"remove_eng")
2022-09-11 00:42:14 +08:00
if uci_get adblock global adb_eng_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_remove_list adblock global adb_eng_sources "${name}"
printf "%s\n" "::: adblock energized '${name}' removed from config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"add_stb")
2022-09-11 00:42:14 +08:00
if ! uci_get adblock global adb_stb_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_add_list adblock global adb_stb_sources "${name}"
printf "%s\n" "::: adblock stevenblack '${name}' added to config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
"remove_stb")
2022-09-11 00:42:14 +08:00
if uci_get adblock global adb_stb_sources | grep -q "${name}"; then
2021-02-27 03:52:55 +08:00
uci_remove_list adblock global adb_stb_sources "${name}"
printf "%s\n" "::: adblock stevenblack '${name}' removed from config"
fi
2022-09-11 00:42:14 +08:00
;;
2021-02-27 03:52:55 +08:00
esac
2020-03-28 03:26:39 +08:00
done
2022-09-11 00:42:14 +08:00
[ -n "$(uci -q changes adblock)" ] && { uci_commit adblock; "${adb_init}" start; }
2020-03-28 03:26:39 +08:00
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)"
2022-09-11 00:42:14 +08:00
[ -r "${src_archive}" ] && zcat "${src_archive}" >"${src_file}" || printf "%s\n" "::: adblock source archive '${src_archive}' not found"
if [ -r "${src_file}" ]; then
2020-03-28 03:26:39 +08:00
src_enabled="${src_enabled#*=}"
src_enabled="${src_enabled//\'}"
2021-02-27 03:52:55 +08:00
printf "%s\n" "::: Available adblock sources"
printf "%s\n" ":::"
printf "%-25s%-10s%-7s%-21s%s\n" " Name" "Enabled" "Size" "Focus" "Info URL"
printf "%s\n" " -------------------------------------------------------------------"
2020-03-28 03:26:39 +08:00
json_load_file "${src_file}"
json_get_keys keylist
2022-09-11 00:42:14 +08:00
for key in ${keylist}; do
2020-03-28 03:26:39 +08:00
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"
2022-09-11 00:42:14 +08:00
if [ -n "${url}" ] && [ -n "${rule}" ]; then
if printf "%s" "${src_enabled}" | grep -q "${key}"; then
2020-03-28 03:26:39 +08:00
enabled="x"
else
enabled=" "
fi
src_enabled="${src_enabled/${key}}"
2021-02-27 03:52:55 +08:00
printf " + %-21s%-10s%-7s%-21s%s\n" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
2020-03-28 03:26:39 +08:00
else
src_enabled="${src_enabled} ${key}"
fi
json_select ..
done
2021-02-27 03:52:55 +08:00
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 utcapitole categories: ${utc_list// /, }"
printf " * %s\n" "Configured energized variants: ${eng_list// /, }"
printf " * %s\n" "Configured stevenblack variants: ${stb_list// /, }"
2022-09-11 00:42:14 +08:00
if [ -n "${src_enabled// }" ]; then
2021-02-27 03:52:55 +08:00
printf "%s\n" " ---------------------------------------------------------------------------"
2022-09-11 00:42:14 +08:00
printf "%s\n" " Sources with invalid configuration"
2021-02-27 03:52:55 +08:00
printf "%s\n" " ---------------------------------------------------------------------------"
2022-09-11 00:42:14 +08:00
for key in ${src_enabled}; do
2021-02-27 03:52:55 +08:00
printf " - %s\n" "${key:0:20}"
2020-03-28 03:26:39 +08:00
done
fi
else
2021-02-27 03:52:55 +08:00
printf "%s\n" "::: adblock source file '${src_file}' not found"
2020-03-28 03:26:39 +08:00
fi
fi
2018-12-20 00:32:29 +08:00
}
2022-09-11 00:42:14 +08:00
status() {
2021-02-27 03:52:55 +08:00
status_service
}
2022-09-11 00:42:14 +08:00
status_service() {
local key keylist value idxval values type rtfile
2018-01-06 01:54:45 +08:00
2020-03-28 03:26:39 +08:00
rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
2021-02-27 03:52:55 +08:00
json_load_file "${rtfile}" >/dev/null 2>&1
json_get_keys keylist
2022-09-11 00:42:14 +08:00
if [ -n "${keylist}" ]; then
2021-02-27 03:52:55 +08:00
printf "%s\n" "::: adblock runtime information"
2022-09-11 00:42:14 +08:00
for key in ${keylist}; do
2021-02-27 03:52:55 +08:00
json_get_var value "${key}" >/dev/null 2>&1
2022-09-11 00:42:14 +08:00
if [ "${key%_*}" = "active" ]; then
2020-03-28 03:26:39 +08:00
printf " + %-15s : " "${key}"
2021-02-27 03:52:55 +08:00
json_select "${key}" >/dev/null 2>&1
values=""
2022-09-11 00:42:14 +08:00
index="1"
while json_get_type type "${index}" && [ "${type}" = "object" ]; do
2021-02-27 03:52:55 +08:00
json_get_values idxval "${index}" >/dev/null 2>&1
2022-09-11 00:42:14 +08:00
if [ "${index}" = "1" ]; then
2021-02-27 03:52:55 +08:00
values="${idxval}"
else
values="${values}, ${idxval}"
fi
2022-09-11 00:42:14 +08:00
index="$((index + 1))"
2020-03-28 03:26:39 +08:00
done
2021-02-27 03:52:55 +08:00
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:-"-"}"
2020-03-28 03:26:39 +08:00
json_select ".."
else
2021-02-27 03:52:55 +08:00
printf " + %-15s : %s\n" "${key}" "${value:-"-"}"
2020-03-28 03:26:39 +08:00
fi
2018-09-01 14:35:35 +08:00
done
else
2021-02-27 03:52:55 +08:00
printf "%s\n" "::: no adblock runtime information available"
2018-09-01 14:35:35 +08:00
fi
2017-04-11 03:15:34 +08:00
}
2022-09-11 00:42:14 +08:00
timer() {
2021-02-27 03:52:55 +08:00
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"
2016-08-25 19:41:15 +08:00
2022-09-11 00:42:14 +08:00
if [ -s "${cron_file}" ] && [ "${action}" = "list" ]; then
2021-02-27 03:52:55 +08:00
awk '{print NR "> " $0}' "${cron_file}"
2022-09-11 00:42:14 +08:00
elif [ -x "/etc/init.d/cron" ] && [ "${action}" = "add" ]; then
2021-02-27 03:52:55 +08:00
hour="${hour//[[:alpha:]]/}"
minute="${minute//[[:alpha:]]/}"
2022-09-11 00:42:14 +08:00
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}"
2021-02-27 03:52:55 +08:00
/etc/init.d/cron restart
fi
2022-09-11 00:42:14 +08:00
elif [ -x "/etc/init.d/cron" ] && [ -s "${cron_file}" ] && [ "${action}" = "remove" ]; then
2021-02-27 03:52:55 +08:00
cron_tasks="${cron_tasks//[[:alpha:]]/}"
cron_lineno="$(awk 'END{print NR}' "${cron_file}")"
cron_content="$(awk '{print $0}' "${cron_file}")"
2022-09-11 00:42:14 +08:00
if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]; then
printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" >"${cron_file}"
2021-02-27 03:52:55 +08:00
/etc/init.d/cron restart
2020-03-28 03:26:39 +08:00
fi
2019-10-31 19:34:48 +08:00
fi
2020-03-28 03:26:39 +08:00
}
2019-10-31 19:34:48 +08:00
2022-09-11 00:42:14 +08:00
service_triggers() {
2021-02-27 03:52:55 +08:00
local iface delay
2020-03-28 03:26:39 +08:00
2021-02-27 03:52:55 +08:00
iface="$(uci_get adblock global adb_trigger)"
delay="$(uci_get adblock global adb_triggerdelay "5")"
2022-09-11 00:42:14 +08:00
PROCD_RELOAD_DELAY="$((delay * 1000))"
2021-02-27 03:52:55 +08:00
2022-09-11 00:42:14 +08:00
[ -n "${iface}" ] && procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start"
2018-09-01 14:35:35 +08:00
procd_add_reload_trigger "adblock"
2016-10-08 01:00:44 +08:00
}