2016-02-15 19:25:21 +08:00
|
|
|
#!/bin/sh /etc/rc.common
|
2024-06-27 13:47:10 +08:00
|
|
|
# Copyright (c) 2015-2024 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
|
|
|
|
2024-06-27 13:47:10 +08:00
|
|
|
# (s)hellcheck exceptions
|
2022-09-11 00:42:14 +08:00
|
|
|
# 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-10-20 17:46:09 +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"
|
|
|
|
extra_command "report" "[[<cli>|<mail>|<gen>|<json>] [<top_count>] [<res_count>] [<search>]] Print DNS statistics with an optional search parameter"
|
2024-08-09 22:59:49 +08:00
|
|
|
extra_command "list" "List available sources"
|
2022-10-20 17:46:09 +08:00
|
|
|
extra_command "timer" "[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
|
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
|
|
|
|
2024-06-27 13:47:10 +08:00
|
|
|
[ "${action}" = "boot" ] && "${adb_init}" running && exit 0
|
|
|
|
[ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] || [ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] || [ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] || { [ "${action}" = "list" ] && [ -n "${1}" ]; }; } && exit 1
|
2019-10-31 19:34:48 +08:00
|
|
|
|
2022-09-11 00:42:14 +08:00
|
|
|
boot() {
|
2024-06-27 13:47:10 +08:00
|
|
|
: >"${adb_pidfile}"
|
2024-07-01 01:30:11 +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")"
|
2024-11-24 05:15:00 +08:00
|
|
|
procd_set_param stdout 0
|
|
|
|
procd_set_param stderr 0
|
2018-09-01 14:35:35 +08:00
|
|
|
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() {
|
2025-02-18 04:58:24 +08:00
|
|
|
rc_procd "${adb_script}" stop
|
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() {
|
2024-08-09 22:59:49 +08:00
|
|
|
local src_archive src_file src_enabled enabled name utc_list size focus descurl
|
|
|
|
|
|
|
|
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)"
|
|
|
|
[ -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"
|
|
|
|
printf "%s\n" ":::"
|
|
|
|
printf "%-25s%-10s%-7s%-21s%s\n" " Name" "Enabled" "Size" "Focus" "Info URL"
|
|
|
|
printf "%s\n" " -------------------------------------------------------------------"
|
|
|
|
json_load_file "${src_file}"
|
|
|
|
json_get_keys keylist
|
|
|
|
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 printf "%s" "${src_enabled}" | grep -q "${key}"; then
|
|
|
|
enabled="x"
|
2020-03-28 03:26:39 +08:00
|
|
|
else
|
2024-08-09 22:59:49 +08:00
|
|
|
enabled=" "
|
2020-03-28 03:26:39 +08:00
|
|
|
fi
|
2024-08-09 22:59:49 +08:00
|
|
|
src_enabled="${src_enabled/${key}}"
|
|
|
|
printf " + %-21s%-10s%-7s%-21s%s\n" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
|
|
|
|
else
|
|
|
|
src_enabled="${src_enabled} ${key}"
|
2020-03-28 03:26:39 +08:00
|
|
|
fi
|
2024-08-09 22:59:49 +08:00
|
|
|
json_select ..
|
|
|
|
done
|
|
|
|
utc_list="$(uci_get adblock global adb_utc_sources "-")"
|
|
|
|
hag_list="$(uci_get adblock global adb_hag_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 hagezi variants: ${hag_list// /, }"
|
|
|
|
printf " * %s\n" "Configured stevenblack variants: ${stb_list// /, }"
|
|
|
|
|
|
|
|
if [ -n "${src_enabled// }" ]; then
|
|
|
|
printf "%s\n" " ---------------------------------------------------------------------------"
|
|
|
|
printf "%s\n" " Sources with invalid configuration"
|
|
|
|
printf "%s\n" " ---------------------------------------------------------------------------"
|
|
|
|
for key in ${src_enabled}; do
|
|
|
|
printf " - %s\n" "${key:0:20}"
|
|
|
|
done
|
2020-03-28 03:26:39 +08:00
|
|
|
fi
|
2024-08-09 22:59:49 +08:00
|
|
|
else
|
|
|
|
printf "%s\n" "::: adblock source file '${src_file}' not found"
|
2020-03-28 03:26:39 +08:00
|
|
|
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() {
|
2024-10-11 01:00:14 +08:00
|
|
|
local key keylist value values 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")"
|
2024-10-11 01:00:14 +08:00
|
|
|
json_init
|
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
|
2024-10-11 01:00:14 +08:00
|
|
|
if [ "${key}" = "active_sources" ]; then
|
|
|
|
json_get_values values "${key}" >/dev/null 2>&1
|
|
|
|
value="${values// /, }"
|
2020-03-28 03:26:39 +08:00
|
|
|
fi
|
2024-10-11 01:00:14 +08:00
|
|
|
printf " + %-15s : %s\n" "${key}" "${value:-"-"}"
|
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")"
|
|
|
|
|
2024-08-11 15:31:29 +08:00
|
|
|
PROCD_RELOAD_DELAY="$((delay * 1000))"
|
2024-11-24 05:15:00 +08:00
|
|
|
[ -n "${iface}" ] && procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" start
|
2024-08-11 15:31:29 +08:00
|
|
|
|
|
|
|
PROCD_RELOAD_DELAY="$((2 * 1000))"
|
2018-09-01 14:35:35 +08:00
|
|
|
procd_add_reload_trigger "adblock"
|
2016-10-08 01:00:44 +08:00
|
|
|
}
|