1 #!/bin/sh /etc/rc.common
2 # Copyright (c) 2015-2021 Dirk Brenken (dev@brenken.org)
3 # This is free software, licensed under the GNU General Public License v3.
5 # set (s)hellcheck exceptions
6 # shellcheck disable=1091,2010,2016,2034,2039,2059,2086,2091,2129,2143,2154,2181,2183,2188
11 if [ -n "$(type -t extra_command)" ]
13 extra_command
"suspend" "Suspend adblock processing"
14 extra_command
"resume" "Resume adblock processing"
15 extra_command
"query" "<domain> Query active blocklists and backups for a specific domain"
16 extra_command
"report" "[[<cli>|<mail>|<gen>|<json>] [<count>] [<search>]] Print DNS statistics with an optional search parameter"
17 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"
18 extra_command
"timer" "[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
19 extra_command
"version" "Print version information"
21 EXTRA_COMMANDS
="status suspend resume query report list timer version"
22 EXTRA_HELP
=" status Service status
23 suspend Suspend adblock processing
24 resume Resume adblock processing
25 query <domain> Query active blocklists and backups for a specific domain
26 report [[<cli>|<mail>|<gen>|<json>] [<count>] [<search>]] Print DNS statistics with an optional search parameter
27 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
28 timer [<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals
29 version Print version information"
32 adb_init
="/etc/init.d/adblock"
33 adb_script
="/usr/bin/adblock.sh"
34 adb_pidfile
="/var/run/adblock.pid"
36 if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] || \
37 [ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] || \
38 [ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] || \
39 { [ "${action}" = "list" ] && [ -n "${1}" ]; }; }
46 rc_procd "${adb_script}" version
51 [ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
52 rc_procd start_service
57 if [ "$
("${adb_init}" enabled
; printf "%u" ${?})" -eq 0 ]
59 if [ "${action}" = "boot
" ]
61 if [ -n "$
(uci_get adblock global adb_trigger
)" ]
66 procd_open_instance "adblock
"
67 procd_set_param command "${adb_script}" "${@}"
68 procd_set_param pidfile "${adb_pidfile}"
69 procd_set_param nice "$
(uci_get adblock global adb_nice
"0")"
70 procd_set_param stdout 1
71 procd_set_param stderr 1
78 rc_procd start_service reload
83 rc_procd "${adb_script}" stop
88 rc_procd start_service restart
93 rc_procd start_service suspend
98 rc_procd start_service resume
103 rc_procd "${adb_script}" query "${1}"
108 rc_procd "${adb_script}" report "${1:-"cli"}" "${2}" "${3}"
113 local src_archive src_file src_enabled enabled name sha_list utc_list action="${1}"
115 if [ "${action%_*}" = "add
" ] || [ "${action%_*}" = "remove
" ]
122 if [ -z "$
(uci_get adblock global adb_sources |
grep -Fo "${name}")" ]
124 uci_add_list adblock global adb_sources "${name}"
125 printf "%s
\n" "::: adblock
source '${name}' added to config
"
129 if [ -n "$
(uci_get adblock global adb_sources |
grep -Fo "${name}")" ]
131 uci_remove_list adblock global adb_sources "${name}"
132 printf "%s
\n" "::: adblock
source '${name}' removed from config
"
136 if [ -z "$
(uci_get adblock global adb_sha_sources |
grep -Fo "${name}")" ]
138 uci_add_list adblock global adb_sha_sources "${name}"
139 printf "%s
\n" "::: adblock shallalist
'${name}' added to config
"
143 if [ -n "$
(uci_get adblock global adb_sha_sources |
grep -Fo "${name}")" ]
145 uci_remove_list adblock global adb_sha_sources "${name}"
146 printf "%s
\n" "::: adblock shallalist
'${name}' removed from config
"
150 if [ -z "$
(uci_get adblock global adb_utc_sources |
grep -Fo "${name}")" ]
152 uci_add_list adblock global adb_utc_sources "${name}"
153 printf "%s
\n" "::: adblock utcapitole
'${name}' added to config
"
157 if [ -n "$
(uci_get adblock global adb_utc_sources |
grep -Fo "${name}")" ]
159 uci_remove_list adblock global adb_utc_sources "${name}"
160 printf "%s
\n" "::: adblock utcapitole
'${name}' removed from config
"
164 if [ -z "$
(uci_get adblock global adb_eng_sources |
grep -Fo "${name}")" ]
166 uci_add_list adblock global adb_eng_sources "${name}"
167 printf "%s
\n" "::: adblock energized
'${name}' added to config
"
171 if [ -n "$
(uci_get adblock global adb_eng_sources |
grep -Fo "${name}")" ]
173 uci_remove_list adblock global adb_eng_sources "${name}"
174 printf "%s
\n" "::: adblock energized
'${name}' removed from config
"
178 if [ -z "$
(uci_get adblock global adb_stb_sources |
grep -Fo "${name}")" ]
180 uci_add_list adblock global adb_stb_sources "${name}"
181 printf "%s
\n" "::: adblock stevenblack
'${name}' added to config
"
185 if [ -n "$
(uci_get adblock global adb_stb_sources |
grep -Fo "${name}")" ]
187 uci_remove_list adblock global adb_stb_sources "${name}"
188 printf "%s
\n" "::: adblock stevenblack
'${name}' removed from config
"
193 if [ -n "$
(uci
-q changes adblock
)" ]
199 src_archive="$
(uci_get adblock global adb_srcarc
"/etc/adblock/adblock.sources.gz")"
200 src_file="$
(uci_get adblock global adb_srcfile
"/tmp/adb_sources.json")"
201 src_enabled="$
(uci
-q show adblock.global.adb_sources
)"
202 if [ -r "${src_archive}" ]
204 zcat "${src_archive}" > "${src_file}"
206 printf "%s
\n" "::: adblock
source archive
'${src_archive}' not found
"
208 if [ -r "${src_file}" ]
210 src_enabled="${src_enabled#*=}"
211 src_enabled="${src_enabled//\'}"
212 printf "%s
\n" "::: Available adblock sources
"
214 printf "%-25s%-10s%-7s%-21s%s
\n" " Name
" "Enabled
" "Size
" "Focus
" "Info URL
"
215 printf "%s
\n" " -------------------------------------------------------------------"
216 json_load_file "${src_file}"
217 json_get_keys keylist
218 for key in ${keylist}
221 json_get_var size "size
"
222 json_get_var focus "focus
"
223 json_get_var descurl "descurl
"
224 json_get_var url "url
"
225 json_get_var rule "rule
"
226 if [ -n "${url}" ] && [ -n "${rule}" ]
228 if [ -n "$
(printf "%s" "${src_enabled}" |
grep -Fo "${key}")" ]
234 src_enabled="${src_enabled/${key}}"
235 printf " + %-21s%-10s%-7s%-21s%s
\n" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
237 src_enabled="${src_enabled} ${key}"
241 sha_list="$
(uci_get adblock global adb_sha_sources
"-")"
242 utc_list="$
(uci_get adblock global adb_utc_sources
"-")"
243 eng_list="$
(uci_get adblock global adb_eng_sources
"-")"
244 stb_list="$
(uci_get adblock global adb_stb_sources
"-")"
245 printf "%s
\n" " ---------------------------------------------------------------------------"
246 printf " * %s
\n" "Configured shallalist categories
: ${sha_list// /, }"
247 printf " * %s
\n" "Configured utcapitole categories
: ${utc_list// /, }"
248 printf " * %s
\n" "Configured energized variants
: ${eng_list// /, }"
249 printf " * %s
\n" "Configured stevenblack variants
: ${stb_list// /, }"
251 if [ -n "${src_enabled// }" ]
253 printf "%s
\n" " ---------------------------------------------------------------------------"
254 printf "%s
\n" " Sources without valid configuration
"
255 printf "%s
\n" " ---------------------------------------------------------------------------"
256 for key in ${src_enabled}
258 printf " - %s
\n" "${key:0:20}"
262 printf "%s
\n" "::: adblock
source file '${src_file}' not found
"
274 local key keylist value idxval values rtfile
276 rtfile="$
(uci_get adblock global adb_rtfile
"/tmp/adb_runtime.json")"
278 json_load_file "${rtfile}" >/dev/null 2>&1
279 json_get_keys keylist
280 if [ -n "${keylist}" ]
282 printf "%s
\n" "::: adblock runtime information
"
283 for key in ${keylist}
285 json_get_var value "${key}" >/dev/null 2>&1
286 if [ "${key%_*}" = "active
" ]
288 printf " + %-15s : " "${key}"
289 json_select "${key}" >/dev/null 2>&1
292 while json_get_type type "${index}" && [ "${type}" = "object
" ]
294 json_get_values idxval "${index}" >/dev/null 2>&1
295 if [ "${index}" = "1" ]
299 values="${values}, ${idxval}"
303 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}')"
304 printf "%s
\n" "${values:-"-"}"
307 printf " + %-15s : %s
\n" "${key}" "${value:-"-"}"
311 printf "%s
\n" "::: no adblock runtime information available
"
317 local cron_file cron_content cron_lineno action="${1:-"list"}" cron_tasks="${2}" hour="${3}" minute="${4:-0}" weekday="${5:-"*"}"
319 cron_file="/etc
/crontabs
/root
"
321 if [ -s "${cron_file}" ] && [ "${action}" = "list
" ]
323 awk '{print NR "> " $0}' "${cron_file}"
324 elif [ "${action}" = "add
" ]
326 hour="${hour//[[:alpha:]]/}"
327 minute="${minute//[[:alpha:]]/}"
328 if [ -n "${cron_tasks}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] && \
329 [ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] && \
330 [ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]
332 printf "%02d
%02d
%s
\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${cron_tasks}" >> "${cron_file}"
333 /etc/init.d/cron restart
335 elif [ -s "${cron_file}" ] && [ "${action}" = "remove
" ]
337 cron_tasks="${cron_tasks//[[:alpha:]]/}"
338 cron_lineno="$
(awk 'END{print NR}' "${cron_file}")"
339 cron_content="$
(awk '{print $0}' "${cron_file}")"
340 if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]
342 printf "%s
\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" > "${cron_file}"
343 /etc/init.d/cron restart
352 iface="$
(uci_get adblock global adb_trigger
)"
353 delay="$
(uci_get adblock global adb_triggerdelay
"5")"
354 PROCD_RELOAD_DELAY=$((delay*1000))
358 . "/lib
/functions
/network.sh
"
359 network_find_wan iface
362 uci_set adblock global adb_trigger "${iface}"
368 procd_add_interface_trigger "interface.
*.up
" "${iface}" "${adb_init}" "start
"
370 procd_add_reload_trigger "adblock
"