bluez-tools: Add package bluezetools
[feed/packages.git] / net / adblock / files / adblock.init
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.
4
5 # set (s)hellcheck exceptions
6 # shellcheck disable=1091,2010,2016,2034,2039,2059,2086,2091,2129,2143,2154,2181,2183,2188
7
8 START=30
9 USE_PROCD=1
10
11 if [ -n "$(type -t extra_command)" ]
12 then
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"
20 else
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"
30 fi
31
32 adb_init="/etc/init.d/adblock"
33 adb_script="/usr/bin/adblock.sh"
34 adb_pidfile="/var/run/adblock.pid"
35
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}" ]; }; }
40 then
41 exit 0
42 fi
43
44 version()
45 {
46 rc_procd "${adb_script}" version
47 }
48
49 boot()
50 {
51 [ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
52 rc_procd start_service
53 }
54
55 start_service()
56 {
57 if [ "$("${adb_init}" enabled; printf "%u" ${?})" -eq 0 ]
58 then
59 if [ "${action}" = "boot" ]
60 then
61 if [ -n "$(uci_get adblock global adb_trigger)" ]
62 then
63 return 0
64 fi
65 fi
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
72 procd_close_instance
73 fi
74 }
75
76 reload_service()
77 {
78 rc_procd start_service reload
79 }
80
81 stop_service()
82 {
83 rc_procd "${adb_script}" stop
84 }
85
86 restart()
87 {
88 rc_procd start_service restart
89 }
90
91 suspend()
92 {
93 rc_procd start_service suspend
94 }
95
96 resume()
97 {
98 rc_procd start_service resume
99 }
100
101 query()
102 {
103 rc_procd "${adb_script}" query "${1}"
104 }
105
106 report()
107 {
108 rc_procd "${adb_script}" report "${1:-"cli"}" "${2}" "${3}"
109 }
110
111 list()
112 {
113 local src_archive src_file src_enabled enabled name sha_list utc_list action="${1}"
114
115 if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]
116 then
117 shift
118 for name in "${@}"
119 do
120 case "${action}" in
121 "add")
122 if [ -z "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
123 then
124 uci_add_list adblock global adb_sources "${name}"
125 printf "%s\n" "::: adblock source '${name}' added to config"
126 fi
127 ;;
128 "remove")
129 if [ -n "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
130 then
131 uci_remove_list adblock global adb_sources "${name}"
132 printf "%s\n" "::: adblock source '${name}' removed from config"
133 fi
134 ;;
135 "add_sha")
136 if [ -z "$(uci_get adblock global adb_sha_sources | grep -Fo "${name}")" ]
137 then
138 uci_add_list adblock global adb_sha_sources "${name}"
139 printf "%s\n" "::: adblock shallalist '${name}' added to config"
140 fi
141 ;;
142 "remove_sha")
143 if [ -n "$(uci_get adblock global adb_sha_sources | grep -Fo "${name}")" ]
144 then
145 uci_remove_list adblock global adb_sha_sources "${name}"
146 printf "%s\n" "::: adblock shallalist '${name}' removed from config"
147 fi
148 ;;
149 "add_utc")
150 if [ -z "$(uci_get adblock global adb_utc_sources | grep -Fo "${name}")" ]
151 then
152 uci_add_list adblock global adb_utc_sources "${name}"
153 printf "%s\n" "::: adblock utcapitole '${name}' added to config"
154 fi
155 ;;
156 "remove_utc")
157 if [ -n "$(uci_get adblock global adb_utc_sources | grep -Fo "${name}")" ]
158 then
159 uci_remove_list adblock global adb_utc_sources "${name}"
160 printf "%s\n" "::: adblock utcapitole '${name}' removed from config"
161 fi
162 ;;
163 "add_eng")
164 if [ -z "$(uci_get adblock global adb_eng_sources | grep -Fo "${name}")" ]
165 then
166 uci_add_list adblock global adb_eng_sources "${name}"
167 printf "%s\n" "::: adblock energized '${name}' added to config"
168 fi
169 ;;
170 "remove_eng")
171 if [ -n "$(uci_get adblock global adb_eng_sources | grep -Fo "${name}")" ]
172 then
173 uci_remove_list adblock global adb_eng_sources "${name}"
174 printf "%s\n" "::: adblock energized '${name}' removed from config"
175 fi
176 ;;
177 "add_stb")
178 if [ -z "$(uci_get adblock global adb_stb_sources | grep -Fo "${name}")" ]
179 then
180 uci_add_list adblock global adb_stb_sources "${name}"
181 printf "%s\n" "::: adblock stevenblack '${name}' added to config"
182 fi
183 ;;
184 "remove_stb")
185 if [ -n "$(uci_get adblock global adb_stb_sources | grep -Fo "${name}")" ]
186 then
187 uci_remove_list adblock global adb_stb_sources "${name}"
188 printf "%s\n" "::: adblock stevenblack '${name}' removed from config"
189 fi
190 ;;
191 esac
192 done
193 if [ -n "$(uci -q changes adblock)" ]
194 then
195 uci_commit adblock
196 "${adb_init}" start
197 fi
198 else
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}" ]
203 then
204 zcat "${src_archive}" > "${src_file}"
205 else
206 printf "%s\n" "::: adblock source archive '${src_archive}' not found"
207 fi
208 if [ -r "${src_file}" ]
209 then
210 src_enabled="${src_enabled#*=}"
211 src_enabled="${src_enabled//\'}"
212 printf "%s\n" "::: Available adblock sources"
213 printf "%s\n" ":::"
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}
219 do
220 json_select "${key}"
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}" ]
227 then
228 if [ -n "$(printf "%s" "${src_enabled}" | grep -Fo "${key}")" ]
229 then
230 enabled="x"
231 else
232 enabled=" "
233 fi
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}"
236 else
237 src_enabled="${src_enabled} ${key}"
238 fi
239 json_select ..
240 done
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// /, }"
250
251 if [ -n "${src_enabled// }" ]
252 then
253 printf "%s\n" " ---------------------------------------------------------------------------"
254 printf "%s\n" " Sources without valid configuration"
255 printf "%s\n" " ---------------------------------------------------------------------------"
256 for key in ${src_enabled}
257 do
258 printf " - %s\n" "${key:0:20}"
259 done
260 fi
261 else
262 printf "%s\n" "::: adblock source file '${src_file}' not found"
263 fi
264 fi
265 }
266
267 status()
268 {
269 status_service
270 }
271
272 status_service()
273 {
274 local key keylist value idxval values rtfile
275
276 rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
277
278 json_load_file "${rtfile}" >/dev/null 2>&1
279 json_get_keys keylist
280 if [ -n "${keylist}" ]
281 then
282 printf "%s\n" "::: adblock runtime information"
283 for key in ${keylist}
284 do
285 json_get_var value "${key}" >/dev/null 2>&1
286 if [ "${key%_*}" = "active" ]
287 then
288 printf " + %-15s : " "${key}"
289 json_select "${key}" >/dev/null 2>&1
290 values=""
291 index=1
292 while json_get_type type "${index}" && [ "${type}" = "object" ]
293 do
294 json_get_values idxval "${index}" >/dev/null 2>&1
295 if [ "${index}" = "1" ]
296 then
297 values="${idxval}"
298 else
299 values="${values}, ${idxval}"
300 fi
301 index=$((index+1))
302 done
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:-"-"}"
305 json_select ".."
306 else
307 printf " + %-15s : %s\n" "${key}" "${value:-"-"}"
308 fi
309 done
310 else
311 printf "%s\n" "::: no adblock runtime information available"
312 fi
313 }
314
315 timer()
316 {
317 local cron_file cron_content cron_lineno action="${1:-"list"}" cron_tasks="${2}" hour="${3}" minute="${4:-0}" weekday="${5:-"*"}"
318
319 cron_file="/etc/crontabs/root"
320
321 if [ -s "${cron_file}" ] && [ "${action}" = "list" ]
322 then
323 awk '{print NR "> " $0}' "${cron_file}"
324 elif [ "${action}" = "add" ]
325 then
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 ]
331 then
332 printf "%02d %02d %s\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${cron_tasks}" >> "${cron_file}"
333 /etc/init.d/cron restart
334 fi
335 elif [ -s "${cron_file}" ] && [ "${action}" = "remove" ]
336 then
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}" ]
341 then
342 printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" > "${cron_file}"
343 /etc/init.d/cron restart
344 fi
345 fi
346 }
347
348 service_triggers()
349 {
350 local iface delay
351
352 iface="$(uci_get adblock global adb_trigger)"
353 delay="$(uci_get adblock global adb_triggerdelay "5")"
354 PROCD_RELOAD_DELAY=$((delay*1000))
355
356 if [ -z "${iface}" ]
357 then
358 . "/lib/functions/network.sh"
359 network_find_wan iface
360 if [ -n "${iface}" ]
361 then
362 uci_set adblock global adb_trigger "${iface}"
363 uci_commit "adblock"
364 fi
365 fi
366 if [ -n "${iface}" ]
367 then
368 procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start"
369 fi
370 procd_add_reload_trigger "adblock"
371 }