luci-app-adblock-fast: bugfix: localizable entries in overview
[project/luci.git] / applications / luci-app-adblock-fast / root / usr / libexec / rpcd / luci.adblock-fast
1 #!/bin/sh
2 # Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2018,SC2019,SC2039,SC3043,SC3057,SC3060
4
5 # TechRef: https://openwrt.org/docs/techref/rpcd
6 # TESTS
7 # ubus -v list luci.adblock-fast
8 # ubus -S call luci.adblock-fast getFileUrlFilesizes '{"name": "adblock-fast" }'
9 # ubus -S call luci.adblock-fast getInitList '{"name": "adblock-fast" }'
10 # ubus -S call luci.adblock-fast getInitStatus '{"name": "adblock-fast" }'
11 # ubus -S call luci.adblock-fast getPlatformSupport '{"name": "adblock-fast" }'
12 # ubus -S call luci.adblock-fast setInitAction '{"name": "adblock-fast", "action": "start" }'
13 # ubus -S call luci.adblock-fast setInitAction '{"name": "adblock-fast", "action": "dl" }'
14 # ubus -S call luci.adblock-fast setInitAction '{"name": "adblock-fast", "action": "pause" }'
15 # ubus -S call luci.adblock-fast setInitAction '{"name": "adblock-fast", "action": "stop" }'
16
17 . /lib/functions.sh
18 . /lib/functions/network.sh
19 . /usr/share/libubox/jshn.sh
20
21 readonly packageName="adblock-fast"
22 readonly dnsmasqAddnhostsFile="/var/run/${packageName}/dnsmasq.addnhosts"
23 readonly dnsmasqAddnhostsCache="/var/run/${packageName}/dnsmasq.addnhosts.cache"
24 readonly dnsmasqAddnhostsGzip="${packageName}.dnsmasq.addnhosts.gz"
25 readonly dnsmasqConfFile="/tmp/dnsmasq.d/${packageName}"
26 readonly dnsmasqConfCache="/var/run/${packageName}/dnsmasq.conf.cache"
27 readonly dnsmasqConfGzip="${packageName}.dnsmasq.conf.gz"
28 readonly dnsmasqIpsetFile="/tmp/dnsmasq.d/${packageName}.ipset"
29 readonly dnsmasqIpsetCache="/var/run/${packageName}/dnsmasq.ipset.cache"
30 readonly dnsmasqIpsetGzip="${packageName}.dnsmasq.ipset.gz"
31 readonly dnsmasqNftsetFile="/tmp/dnsmasq.d/${packageName}.nftset"
32 readonly dnsmasqNftsetCache="/var/run/${packageName}/dnsmasq.nftset.cache"
33 readonly dnsmasqNftsetGzip="${packageName}.dnsmasq.nftset.gz"
34 readonly dnsmasqServersFile="/var/run/${packageName}/dnsmasq.servers"
35 readonly dnsmasqServersCache="/var/run/${packageName}/dnsmasq.servers.cache"
36 readonly dnsmasqServersGzip="${packageName}.dnsmasq.servers.gz"
37 readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
38 readonly unboundCache="/var/run/${packageName}/unbound.cache"
39 readonly unboundGzip="${packageName}.unbound.gz"
40 readonly jsonFile="/dev/shm/$packageName-status.json"
41
42 str_contains() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
43 str_contains_word() { echo "$1" | grep -q -w "$2"; }
44 str_to_lower() { echo "$1" | tr 'A-Z' 'a-z'; }
45 str_to_upper() { echo "$1" | tr 'a-z' 'A-Z'; }
46 is_enabled() { uci -q get "${1}.config.enabled"; }
47 get_version() { grep -m1 -A2 -w "^Package: $1$" /usr/lib/opkg/status | sed -n 's/Version: //p'; }
48 print_json_bool() { json_init; json_add_boolean "$1" "$2"; json_dump; json_cleanup; }
49 print_json_int() { json_init; json_add_int "$1" "$2"; json_dump; json_cleanup; }
50 print_json_string() { json_init; json_add_string "$1" "$2"; json_dump; json_cleanup; }
51 logger() { /usr/bin/logger -t "$packageName" "$@"; }
52 ubus_get_status() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@['${packageName}'].instances.main.data.${1}"; }
53 ubus_get_ports() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@['${packageName}'].instances.main.data.firewall.*.dest_port"; }
54 is_present() { command -v "$1" >/dev/null 2>&1; }
55 sanitize_dir() { [ -d "$(readlink -fn "$1")" ] && readlink -fn "$1"; }
56 json() {
57 # shellcheck disable=SC2034
58 local action="$1" param="$2" value="$3" i
59 if [ -s "$jsonFile" ]; then
60 json_load_file "$jsonFile" 2>/dev/null
61 json_select 'data' 2>/dev/null
62 for i in status message error stats reload restart; do
63 json_get_var $i "$i" 2>/dev/null
64 done
65 fi
66 case "$action" in
67 get)
68 case "$param" in
69 *)
70 printf "%b" "$(eval echo "\$$param")"; return;;
71 esac
72 ;;
73 esac
74 }
75
76 get_url_filesize() {
77 local url="$1" size size_command
78 [ -n "$url" ] || { print_json_int 'size' '0'; return 0; }
79 is_present 'curl' || { print_json_int 'size' '0'; return 0; }
80 size_command='curl --silent --insecure --fail --head --request GET'
81 size="$($size_command "$url" | grep -i 'content-length:' | awk '{print $2}'; )"
82 echo "$size"
83 }
84
85 _get_file_url_size() {
86 local url size
87 config_get url "$1" 'url'
88 config_get size "$1" 'size'
89 [ -n "$size" ] || size="$(get_url_filesize "$url")"
90 json_add_object
91 json_add_string 'url' "$url"
92 json_add_int 'size' "$size"
93 json_close_object
94 }
95
96 get_file_url_filesizes() {
97 local name="$1" i
98 json_init
99 json_add_object "$name"
100 json_add_array 'sizes'
101 config_load "$name"
102 config_foreach _get_file_url_size 'file_url'
103 json_close_array
104 json_close_object
105 json_dump
106 json_cleanup
107 }
108
109 get_init_list() {
110 local name
111 name="$(basename "$1")"
112 name="${name:-$packageName}"
113 json_init
114 json_add_object "$name"
115 json_add_boolean 'enabled' "$(is_enabled "$name")"
116 if is_running "$name"; then
117 json_add_boolean 'running' '1'
118 else
119 json_add_boolean 'running' '0'
120 fi
121 json_close_object
122 json_dump
123 json_cleanup
124 }
125
126 set_init_action() {
127 local name action="$2" cmd
128 name="$(basename "$1")"
129 name="${name:-$packageName}"
130 if [ ! -f "/etc/init.d/$name" ]; then
131 print_json_string 'error' 'Init script not found!'
132 return
133 fi
134 case $action in
135 enable)
136 cmd="uci -q set ${name}.config.enabled=1 && uci commit $name";;
137 disable)
138 cmd="uci -q set ${name}.config.enabled=0 && uci commit $name";;
139 start|stop|reload|restart|dl|pause)
140 cmd="/etc/init.d/${name} ${action}";;
141 esac
142 if [ -n "$cmd" ] && eval "${cmd}" >/dev/null 2>&1; then
143 print_json_bool "result" '1'
144 else
145 print_json_bool "result" '0'
146 fi
147 }
148
149 get_init_status() {
150 local name
151 name="$(basename "$1")"
152 name="${name:-$packageName}"
153 local errors warnings ports dns outputFile outputCache outputGzip
154 local i j
155 # shellcheck disable=SC2034
156 local compressed_cache_dir
157 config_load "$name"
158 config_get compressed_cache_dir 'config' 'compressed_cache_dir' '/etc'
159 if [ -n "$(sanitize_dir "$compressed_cache_dir")" ]; then
160 compressed_cache_dir="$(sanitize_dir "$compressed_cache_dir")"
161 else
162 compressed_cache_dir="/etc"
163 fi
164 if [ -n "$(uci -q get $packageName.config.dnsmasq_config_file_url)" ]; then
165 dns="dnsmasq.conf"
166 else
167 dns="$(uci -q get $packageName.config.dns)"
168 fi
169 case "$dns" in
170 dnsmasq.addnhosts)
171 outputFile="$dnsmasqAddnhostsFile"
172 outputCache="$dnsmasqAddnhostsCache"
173 outputGzip="${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
174 ;;
175 dnsmasq.conf)
176 outputFile="$dnsmasqConfFile"
177 outputCache="$dnsmasqConfCache"
178 outputGzip="${compressed_cache_dir}/${dnsmasqConfGzip}"
179 ;;
180 dnsmasq.ipset)
181 outputFile="$dnsmasqIpsetFile"
182 outputCache="$dnsmasqIpsetCache"
183 outputGzip="${compressed_cache_dir}/${dnsmasqIpsetGzip}"
184 ;;
185 dnsmasq.nftset)
186 outputFile="$dnsmasqNftsetFile"
187 outputCache="$dnsmasqNftsetCache"
188 outputGzip="${compressed_cache_dir}/${dnsmasqNftsetGzip}"
189 ;;
190 dnsmasq.servers)
191 outputFile="$dnsmasqServersFile"
192 outputCache="$dnsmasqServersCache"
193 outputGzip="${compressed_cache_dir}/${dnsmasqServersGzip}"
194 ;;
195 unbound.adb_list)
196 outputFile="$unboundFile"
197 outputCache="$unboundCache"
198 outputGzip="${compressed_cache_dir}/${unboundGzip}"
199 ;;
200 esac
201 json_init
202 json_add_object "$name"
203 json_add_boolean 'enabled' "$(is_enabled "$name")"
204 i="$(json 'get' 'status')"
205 j="$(ubus_get_status 'status')"
206 if [ "$i" = 'statusSuccess' ] && [ "$i" != "$j" ]; then
207 i='statusStopped'
208 fi
209 json_add_string 'status' "$i"
210 if [ "$i" = 'statusSuccess' ]; then
211 json_add_boolean 'running' '1'
212 else
213 json_add_boolean 'running' '0'
214 fi
215 json_add_string 'version' "$(get_version "$name")"
216 errors="$(ubus_get_status errors)"
217 json_add_array 'errors'
218 if [ -n "$errors" ]; then
219 for i in $errors; do
220 if str_contains "$i" '|'; then
221 error_extra="${i##*|}"
222 error_id="${i%|*}"
223 else
224 error_id="$i"
225 unset error_extra
226 fi
227 json_add_object
228 json_add_string 'id' "$error_id"
229 json_add_string 'extra' "$error_extra"
230 json_close_object
231 done
232 fi
233 json_close_array
234 warnings="$(ubus_get_status warnings)"
235 json_add_array 'warnings'
236 if [ -n "$warnings" ]; then
237 for i in $warnings; do
238 if str_contains "$i" '|'; then
239 error_extra="${i##*|}"
240 error_id="${i%|*}"
241 else
242 error_id="$i"
243 unset error_extra
244 fi
245 json_add_object
246 json_add_string 'id' "$error_id"
247 json_add_string 'extra' "$error_extra"
248 json_close_object
249 done
250 fi
251 json_close_array
252
253 ports="$(ubus_get_ports)"
254 if [ -n "$ports" ]; then
255 json_add_boolean 'force_dns_active' '1'
256 json_add_array 'force_dns_ports'
257 for i in $ports; do json_add_int '' "$i"; done
258 json_close_array
259 else
260 json_add_boolean 'force_dns_active' '0'
261 fi
262 json_add_int 'entries' "$(ubus_get_status entries)"
263 json_add_string 'dns' "$dns"
264 json_add_string 'outputFile' "$outputFile"
265 json_add_string 'outputCache' "$outputCache"
266 json_add_string 'outputGzip' "$outputGzip"
267 if [ -s "$outputFile" ]; then
268 json_add_boolean 'outputFileExists' '1'
269 else
270 json_add_boolean 'outputFileExists' '0'
271 fi
272 if [ -s "$outputCache" ]; then
273 json_add_boolean 'outputCacheExists' '1'
274 else
275 json_add_boolean 'outputCacheExists' '0'
276 fi
277 if [ -s "$outputGzip" ]; then
278 json_add_boolean 'outputGzipExists' '1'
279 else
280 json_add_boolean 'outputGzipExists' '0'
281 fi
282 json_add_array 'leds'
283 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
284 json_close_array
285 json_close_object
286 json_dump
287 json_cleanup
288 }
289
290 check_ipset() { { command -v ipset && /usr/sbin/ipset help hash:net; } >/dev/null 2>&1; }
291 check_nft() { command -v nft >/dev/null 2>&1; }
292 check_dnsmasq() { command -v dnsmasq >/dev/null 2>&1; }
293 check_unbound() { command -v unbound >/dev/null 2>&1; }
294 check_dnsmasq_ipset() {
295 local o;
296 check_dnsmasq || return 1
297 o="$(dnsmasq -v 2>/dev/null)"
298 check_ipset && ! echo "$o" | grep -q 'no-ipset' && echo "$o" | grep -q 'ipset'
299 }
300 check_dnsmasq_nftset() {
301 local o;
302 check_dnsmasq || return 1
303 o="$(dnsmasq -v 2>/dev/null)"
304 check_nft && ! echo "$o" | grep -q 'no-nftset' && echo "$o" | grep -q 'nftset'
305 }
306
307 get_platform_support() {
308 local name
309 name="$(basename "$1")"
310 name="${name:-$packageName}"
311 json_init
312 json_add_object "$name"
313 if check_ipset; then
314 json_add_boolean 'ipset_installed' '1'
315 else
316 json_add_boolean 'ipset_installed' '0'
317 fi
318 if check_nft; then
319 json_add_boolean 'nft_installed' '1'
320 else
321 json_add_boolean 'nft_installed' '0'
322 fi
323 if check_dnsmasq; then
324 json_add_boolean 'dnsmasq_installed' '1'
325 else
326 json_add_boolean 'dnsmasq_installed' '0'
327 fi
328 if check_unbound; then
329 json_add_boolean 'unbound_installed' '1'
330 else
331 json_add_boolean 'unbound_installed' '0'
332 fi
333 if check_dnsmasq_ipset; then
334 json_add_boolean 'dnsmasq_ipset_support' '1'
335 else
336 json_add_boolean 'dnsmasq_ipset_support' '0'
337 fi
338 if check_dnsmasq_nftset; then
339 json_add_boolean 'dnsmasq_nftset_support' '1'
340 else
341 json_add_boolean 'dnsmasq_nftset_support' '0'
342 fi
343 json_add_array 'leds'
344 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
345 json_close_array
346 json_close_object
347 json_dump
348 json_cleanup
349 }
350
351 case "$1" in
352 list)
353 json_init
354 json_add_object "getFileUrlFilesizes"
355 json_add_string 'name' 'name'
356 json_close_object
357 json_add_object "getInitList"
358 json_add_string 'name' 'name'
359 json_close_object
360 json_add_object "getInitStatus"
361 json_add_string 'name' 'name'
362 json_close_object
363 json_add_object "getPlatformSupport"
364 json_add_string 'name' 'name'
365 json_close_object
366 json_add_object "setInitAction"
367 json_add_string 'name' 'name'
368 json_add_string 'action' 'action'
369 json_close_object
370 json_dump
371 json_cleanup
372 ;;
373 call)
374 case "$2" in
375 getFileUrlFilesizes)
376 read -r input
377 json_load "$input"
378 json_get_var name 'name'
379 json_cleanup
380 get_file_url_filesizes "$name"
381 ;;
382 getInitList)
383 read -r input
384 json_load "$input"
385 json_get_var name 'name'
386 json_cleanup
387 get_init_list "$name"
388 ;;
389 getInitStatus)
390 read -r input
391 json_load "$input"
392 json_get_var name 'name'
393 json_cleanup
394 get_init_status "$name"
395 ;;
396 getPlatformSupport)
397 read -r input
398 json_load "$input"
399 json_get_var name 'name'
400 json_cleanup
401 get_platform_support "$name"
402 ;;
403 setInitAction)
404 read -r input
405 json_load "$input"
406 json_get_var name 'name'
407 json_get_var action 'action'
408 json_cleanup
409 set_init_action "$name" "$action"
410 ;;
411 esac
412 ;;
413 esac