1 #!/bin/sh /etc/rc.common
2 # Copyright (c) 2015-2020 Dirk Brenken (dev@brenken.org)
3 # This is free software, licensed under the GNU General Public License v3.
8 EXTRA_COMMANDS
="suspend resume query report list timer status_service"
9 EXTRA_HELP
=" suspend Suspend adblock processing
10 resume Resume adblock processing
11 query <domain> Query active blocklists and backups for a specific domain
12 report [<search>] Print DNS statistics with an optional search parameter
13 list [[<add>|<remove>] [source(s)]] List available adblock sources or add/remove them from config
14 timer <action> <hour> [<minute>] [<weekday>] Set a cron based update interval"
16 adb_init
="/etc/init.d/adblock"
17 adb_script
="/usr/bin/adblock.sh"
18 adb_pidfile
="/var/run/adblock.pid"
20 if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] || \
21 [ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] || \
22 [ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] || \
23 { [ "${action}" = "list" ] && [ -n "${1}" ]; }; }
30 [ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
31 rc_procd start_service
36 if [ "$
("${adb_init}" enabled
; printf "%u" ${?})" -eq 0 ]
38 if [ "${action}" = "boot
" ]
40 if [ -n "$
(uci_get adblock global adb_trigger
)" ]
45 procd_open_instance "adblock
"
46 procd_set_param command "${adb_script}" "${@}"
47 procd_set_param pidfile "${adb_pidfile}"
48 procd_set_param nice "$
(uci_get adblock global adb_nice
"0")"
49 procd_set_param stdout 1
50 procd_set_param stderr 1
57 rc_procd start_service reload
62 rc_procd "${adb_script}" stop
67 rc_procd start_service restart
72 rc_procd start_service suspend
77 rc_procd start_service resume
82 rc_procd "${adb_script}" query "${1}"
87 rc_procd "${adb_script}" report "${1:-"+"}" "${2:-"50"}" "${3:-"true"}" "${4:-"cli"}"
92 local src_archive src_file src_enabled enabled name action="${1}"
94 if [ "${action}" = "add
" ] || [ "${action}" = "remove
" ]
99 if [ "${action}" = "add
" ]
101 if [ -z "$
(uci_get adblock global adb_sources |
grep -Fo "${name}")" ]
103 uci_add_list adblock global adb_sources "${name}"
104 printf "%s
\\n
" "::: adblock
source '${name}' added to config
"
107 if [ -n "$
(uci_get adblock global adb_sources |
grep -Fo "${name}")" ]
109 uci_remove_list adblock global adb_sources "${name}"
110 printf "%s
\\n
" "::: adblock
source '${name}' removed from config
"
114 if [ -n "$
(uci
-q changes adblock
)" ]
119 src_archive="$
(uci_get adblock global adb_srcarc
"/etc/adblock/adblock.sources.gz")"
120 src_file="$
(uci_get adblock global adb_srcfile
"/tmp/adb_sources.json")"
121 src_enabled="$
(uci
-q show adblock.global.adb_sources
)"
122 if [ ! -r "${src_file}" ]
124 if [ -r "${src_archive}" ]
126 zcat "${src_archive}" > "${src_file}"
128 printf "%s
\\n
" "::: adblock
source archive
'${src_archive}' not found
"
131 if [ -r "${src_file}" ]
133 src_enabled="${src_enabled#*=}"
134 src_enabled="${src_enabled//\'}"
135 printf "%s
\\n
" "::: Available adblock sources
"
137 printf "%-25s%-10s%-7s%-21s%s
\\n
" " Name
" "Enabled
" "Size
" "Focus
" "Info URL
"
138 printf "%s
\\n
" " -------------------------------------------------------------------"
139 json_load_file "${src_file}"
140 json_get_keys keylist
141 for key in ${keylist}
144 json_get_var size "size
"
145 json_get_var focus "focus
"
146 json_get_var descurl "descurl
"
147 json_get_var url "url
"
148 json_get_var rule "rule
"
149 if [ -n "${url}" ] && [ -n "${rule}" ]
151 if [ -n "$
(printf "%s" "${src_enabled}" |
grep -Fo "${key}")" ]
157 src_enabled="${src_enabled/${key}}"
158 printf " + %-21s%-10s%-7s%-21s%s
\\n
" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
160 src_enabled="${src_enabled} ${key}"
164 if [ -n "${src_enabled// }" ]
166 printf "%s
\\n
" " ----------------------------------------------"
167 printf "%s
\\n
" " Sources without valid configuration
"
168 printf "%s
\\n
" " ----------------------------------------------"
169 for key in ${src_enabled}
171 printf " - %s
\\n
" "${key:0:20}"
175 printf "%s
\\n
" "::: adblock
source file '${src_file}' not found
"
182 local key keylist value rtfile
184 rtfile="$
(uci_get adblock global adb_rtfile
"/tmp/adb_runtime.json")"
185 if [ -s "${rtfile}" ]
187 printf "%s
\\n
" "::: adblock runtime information
"
188 json_load_file "${rtfile}"
190 json_get_keys keylist
191 for key in ${keylist}
193 json_get_var value "${key}"
194 if [ "${key}" = "active_sources
" ]
196 printf " + %-15s : " "${key}"
199 while json_get_type status "${index}" && [ "${status}" = "object
" ]
201 json_get_values source "${index}"
202 printf "%s
" "${source}"
208 printf " + %-15s : %s
\\n
" "${key}" "${value}"
212 printf "%s
\\n
" "::: no adblock runtime information available
"
218 local action="${1}" hour="${2}" minute="${3:-0}" weekday="${4:-"*"}"
220 hour="${hour//[[:alpha:]]/}"
221 minute="${minute//[[:alpha:]]/}"
222 if [ -n "${action}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] && \
223 [ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] && \
224 [ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]
226 if [ -r "/etc
/crontabs
/root
" ]
228 search="${adb_init//\//\\/}"
229 search="${search//./\\.}"
230 sed -i "/${search}/d
" "/etc
/crontabs
/root
"
232 printf "%02d
%02d
%s
\\n
" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${action}" >> "/etc
/crontabs
/root
"
233 /etc/init.d/cron restart
235 printf "%s
\\n
" "::: the refresh timer could not been updated
"
241 local trigger delay type
243 PROCD_RELOAD_DELAY=$((delay*1000))
244 trigger="$
(uci_get adblock global adb_trigger
)"
245 delay="$
(uci_get adblock global adb_triggerdelay
"2")"
246 type="$
(uci_get adblock global adb_starttype
"start")"
247 if [ -n "${trigger}" ]
249 procd_add_interface_trigger "interface.
*.up
" "${trigger}" "${adb_init}" "${type}"
251 procd_add_reload_trigger "adblock
"