prometheus-node-exporter-lua: add dawn exporter
[feed/packages.git] / net / adblock / files / adblock.init
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.
4
5 START=30
6 USE_PROCD=1
7
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"
15
16 adb_init="/etc/init.d/adblock"
17 adb_script="/usr/bin/adblock.sh"
18 adb_pidfile="/var/run/adblock.pid"
19
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}" ]; }; }
24 then
25 exit 0
26 fi
27
28 boot()
29 {
30 [ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
31 rc_procd start_service
32 }
33
34 start_service()
35 {
36 if [ "$("${adb_init}" enabled; printf "%u" ${?})" -eq 0 ]
37 then
38 if [ "${action}" = "boot" ]
39 then
40 if [ -n "$(uci_get adblock global adb_trigger)" ]
41 then
42 return 0
43 fi
44 fi
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
51 procd_close_instance
52 fi
53 }
54
55 reload_service()
56 {
57 rc_procd start_service reload
58 }
59
60 stop_service()
61 {
62 rc_procd "${adb_script}" stop
63 }
64
65 restart()
66 {
67 rc_procd start_service restart
68 }
69
70 suspend()
71 {
72 rc_procd start_service suspend
73 }
74
75 resume()
76 {
77 rc_procd start_service resume
78 }
79
80 query()
81 {
82 rc_procd "${adb_script}" query "${1}"
83 }
84
85 report()
86 {
87 rc_procd "${adb_script}" report "${1:-"+"}" "${2:-"50"}" "${3:-"true"}" "${4:-"cli"}"
88 }
89
90 list()
91 {
92 local src_archive src_file src_enabled enabled name action="${1}"
93
94 if [ "${action}" = "add" ] || [ "${action}" = "remove" ]
95 then
96 shift
97 for name in "${@}"
98 do
99 if [ "${action}" = "add" ]
100 then
101 if [ -z "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
102 then
103 uci_add_list adblock global adb_sources "${name}"
104 printf "%s\\n" "::: adblock source '${name}' added to config"
105 fi
106 else
107 if [ -n "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
108 then
109 uci_remove_list adblock global adb_sources "${name}"
110 printf "%s\\n" "::: adblock source '${name}' removed from config"
111 fi
112 fi
113 done
114 if [ -n "$(uci -q changes adblock)" ]
115 then
116 uci_commit adblock
117 fi
118 else
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}" ]
123 then
124 if [ -r "${src_archive}" ]
125 then
126 zcat "${src_archive}" > "${src_file}"
127 else
128 printf "%s\\n" "::: adblock source archive '${src_archive}' not found"
129 fi
130 fi
131 if [ -r "${src_file}" ]
132 then
133 src_enabled="${src_enabled#*=}"
134 src_enabled="${src_enabled//\'}"
135 printf "%s\\n" "::: Available adblock sources"
136 printf "%s\\n" ":::"
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}
142 do
143 json_select "${key}"
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}" ]
150 then
151 if [ -n "$(printf "%s" "${src_enabled}" | grep -Fo "${key}")" ]
152 then
153 enabled="x"
154 else
155 enabled=" "
156 fi
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}"
159 else
160 src_enabled="${src_enabled} ${key}"
161 fi
162 json_select ..
163 done
164 if [ -n "${src_enabled// }" ]
165 then
166 printf "%s\\n" " ----------------------------------------------"
167 printf "%s\\n" " Sources without valid configuration"
168 printf "%s\\n" " ----------------------------------------------"
169 for key in ${src_enabled}
170 do
171 printf " - %s\\n" "${key:0:20}"
172 done
173 fi
174 else
175 printf "%s\\n" "::: adblock source file '${src_file}' not found"
176 fi
177 fi
178 }
179
180 status_service()
181 {
182 local key keylist value rtfile
183
184 rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"
185 if [ -s "${rtfile}" ]
186 then
187 printf "%s\\n" "::: adblock runtime information"
188 json_load_file "${rtfile}"
189 json_select data
190 json_get_keys keylist
191 for key in ${keylist}
192 do
193 json_get_var value "${key}"
194 if [ "${key}" = "active_sources" ]
195 then
196 printf " + %-15s : " "${key}"
197 json_select "${key}"
198 index=1
199 while json_get_type status "${index}" && [ "${status}" = "object" ]
200 do
201 json_get_values source "${index}"
202 printf "%s " "${source}"
203 index=$((index+1))
204 done
205 printf "\\n"
206 json_select ".."
207 else
208 printf " + %-15s : %s\\n" "${key}" "${value}"
209 fi
210 done
211 else
212 printf "%s\\n" "::: no adblock runtime information available"
213 fi
214 }
215
216 timer()
217 {
218 local action="${1}" hour="${2}" minute="${3:-0}" weekday="${4:-"*"}"
219
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 ]
225 then
226 if [ -r "/etc/crontabs/root" ]
227 then
228 search="${adb_init//\//\\/}"
229 search="${search//./\\.}"
230 sed -i "/${search}/d" "/etc/crontabs/root"
231 fi
232 printf "%02d %02d %s\\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${action}" >> "/etc/crontabs/root"
233 /etc/init.d/cron restart
234 else
235 printf "%s\\n" "::: the refresh timer could not been updated"
236 fi
237 }
238
239 service_triggers()
240 {
241 local trigger delay type
242
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}" ]
248 then
249 procd_add_interface_trigger "interface.*.up" "${trigger}" "${adb_init}" "${type}"
250 fi
251 procd_add_reload_trigger "adblock"
252 }