luci-app-adblock-fast: prepare migration to APK
[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=SC2018,SC2019,SC3043,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 readonly adbFunctionsFile='/etc/init.d/adblock-fast'
18 if [ -s "$adbFunctionsFile" ]; then
19 # shellcheck source=../../../../../adblock-fast/files/etc/init.d/adblock-fast
20 . "$adbFunctionsFile"
21 else
22 logger -t adblock-fast 'error' "adblock-fast init.d file ($adbFunctionsFile) not found!"
23 print_json_string 'error' "adblock-fast init.d file ($adbFunctionsFile) not found!"
24 fi
25
26 get_file_url_filesizes() {
27 _get_file_url_size() {
28 local url size
29 config_get url "$1" 'url'
30 config_get size "$1" 'size'
31 [ -n "$size" ] || size="$(get_url_filesize "$url")"
32 json_add_object
33 json_add_string 'url' "$url"
34 json_add_int 'size' "$size"
35 json_close_object
36 }
37 local name="$1" i
38 json_init
39 json_add_object "$name"
40 json_add_array 'sizes'
41 config_load "$name"
42 config_foreach _get_file_url_size 'file_url'
43 json_close_array
44 json_close_object
45 json_dump
46 json_cleanup
47 }
48
49 get_init_list() {
50 local name
51 name="$(basename "$1")"
52 name="${name:-$packageName}"
53 json_init
54 json_add_object "$name"
55 json_add_boolean 'enabled' "$(is_enabled "$name")"
56 if is_running "$name"; then
57 json_add_boolean 'running' '1'
58 else
59 json_add_boolean 'running' '0'
60 fi
61 json_close_object
62 json_dump
63 json_cleanup
64 }
65
66 set_init_action() {
67 local name action="$2" cmd
68 name="$(basename "$1")"
69 name="${name:-$packageName}"
70 if [ ! -f "/etc/init.d/$name" ]; then
71 print_json_string 'error' 'Init script not found!'
72 return
73 fi
74 case $action in
75 enable)
76 cmd="/etc/init.d/${name} ${action}"
77 cmd="${cmd} && uci_set ${name} config enabled 1 && uci_commit $name"
78 ;;
79 disable)
80 cmd="/etc/init.d/${name} ${action}"
81 cmd="${cmd} && uci_set ${name} config enabled 0 && uci_commit $name"
82 ;;
83 start|stop|reload|restart|dl|pause)
84 cmd="/etc/init.d/${name} ${action}"
85 ;;
86 esac
87 if [ -n "$cmd" ] && eval "$cmd" >/dev/null 2>&1; then
88 print_json_bool "result" '1'
89 else
90 print_json_bool "result" '0'
91 fi
92 }
93
94 get_init_status() {
95 local name
96 name="$(basename "$1")"
97 name="${name:-$packageName}"
98 local errors warnings ports dns outputFile outputCache outputGzip outputConfig
99 local i j
100 # shellcheck disable=SC2034
101 local compressed_cache_dir
102 config_load "$name"
103 config_get compressed_cache_dir 'config' 'compressed_cache_dir' '/etc'
104 compressed_cache_dir="$(sanitize_dir "$compressed_cache_dir")"
105 compressed_cache_dir="${compressed_cache_dir:-/etc}"
106 if [ -n "$(uci_get "$packageName" 'config' 'dnsmasq_config_file_url')" ]; then
107 dns="dnsmasq.conf"
108 else
109 dns="$(uci_get "$packageName" 'config' 'dns' 'dnsmasq.servers')"
110 fi
111
112 dns_set_output_values "$dns"
113
114 json_init
115 json_add_object "$name"
116 json_add_boolean 'enabled' "$(is_enabled "$name")"
117 json_add_string 'status' "$(json 'get' 'status')"
118 if is_running "$name"; then
119 json_add_boolean 'running' '1'
120 else
121 json_add_boolean 'running' '0'
122 fi
123 json_add_string 'version' "$PKG_VERSION"
124 errors="$(ubus_get_data errors)"
125 json_add_array 'errors'
126 if [ -n "$errors" ]; then
127 for i in $errors; do
128 if str_contains "$i" '|'; then
129 error_extra="${i##*|}"
130 error_id="${i%|*}"
131 else
132 error_id="$i"
133 unset error_extra
134 fi
135 json_add_object
136 json_add_string 'id' "$error_id"
137 json_add_string 'extra' "$error_extra"
138 json_close_object
139 done
140 fi
141 json_close_array
142 warnings="$(ubus_get_data warnings)"
143 json_add_array 'warnings'
144 if [ -n "$warnings" ]; then
145 for i in $warnings; do
146 if str_contains "$i" '|'; then
147 error_extra="${i##*|}"
148 error_id="${i%|*}"
149 else
150 error_id="$i"
151 unset error_extra
152 fi
153 json_add_object
154 json_add_string 'id' "$error_id"
155 json_add_string 'extra' "$error_extra"
156 json_close_object
157 done
158 fi
159 json_close_array
160
161 ports="$(ubus_get_ports)"
162 if [ -n "$ports" ]; then
163 json_add_boolean 'force_dns_active' '1'
164 json_add_array 'force_dns_ports'
165 for i in $ports; do json_add_int '' "$i"; done
166 json_close_array
167 else
168 json_add_boolean 'force_dns_active' '0'
169 fi
170 json_add_int 'entries' "$(ubus_get_data entries)"
171 json_add_string 'dns' "$dns"
172 json_add_string 'outputFile' "$outputFile"
173 json_add_string 'outputCache' "$outputCache"
174 json_add_string 'outputGzip' "$outputGzip"
175 if [ -s "$outputFile" ]; then
176 json_add_boolean 'outputFileExists' '1'
177 else
178 json_add_boolean 'outputFileExists' '0'
179 fi
180 if [ -s "$outputCache" ]; then
181 json_add_boolean 'outputCacheExists' '1'
182 else
183 json_add_boolean 'outputCacheExists' '0'
184 fi
185 if [ -s "$outputGzip" ]; then
186 json_add_boolean 'outputGzipExists' '1'
187 else
188 json_add_boolean 'outputGzipExists' '0'
189 fi
190 json_add_array 'leds'
191 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
192 json_close_array
193 json_close_object
194 json_dump
195 json_cleanup
196 }
197
198 get_platform_support() {
199 local name
200 name="$(basename "$1")"
201 name="${name:-$packageName}"
202 json_init
203 json_add_object "$name"
204 if check_ipset; then
205 json_add_boolean 'ipset_installed' '1'
206 else
207 json_add_boolean 'ipset_installed' '0'
208 fi
209 if check_nft; then
210 json_add_boolean 'nft_installed' '1'
211 else
212 json_add_boolean 'nft_installed' '0'
213 fi
214 if check_dnsmasq; then
215 json_add_boolean 'dnsmasq_installed' '1'
216 else
217 json_add_boolean 'dnsmasq_installed' '0'
218 fi
219 if check_dnsmasq_ipset; then
220 json_add_boolean 'dnsmasq_ipset_support' '1'
221 else
222 json_add_boolean 'dnsmasq_ipset_support' '0'
223 fi
224 if check_dnsmasq_nftset; then
225 json_add_boolean 'dnsmasq_nftset_support' '1'
226 else
227 json_add_boolean 'dnsmasq_nftset_support' '0'
228 fi
229 if check_smartdns; then
230 json_add_boolean 'smartdns_installed' '1'
231 else
232 json_add_boolean 'smartdns_installed' '0'
233 fi
234 if check_smartdns_ipset; then
235 json_add_boolean 'smartdns_ipset_support' '1'
236 else
237 json_add_boolean 'smartdns_ipset_support' '0'
238 fi
239 if check_smartdns_nftset; then
240 json_add_boolean 'smartdns_nftset_support' '1'
241 else
242 json_add_boolean 'smartdns_nftset_support' '0'
243 fi
244 if check_unbound; then
245 json_add_boolean 'unbound_installed' '1'
246 else
247 json_add_boolean 'unbound_installed' '0'
248 fi
249 json_add_array 'leds'
250 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
251 json_close_array
252 json_close_object
253 json_dump
254 json_cleanup
255 }
256
257 case "$1" in
258 list)
259 json_init
260 json_add_object "getFileUrlFilesizes"
261 json_add_string 'name' 'name'
262 json_close_object
263 json_add_object "getInitList"
264 json_add_string 'name' 'name'
265 json_close_object
266 json_add_object "getInitStatus"
267 json_add_string 'name' 'name'
268 json_close_object
269 json_add_object "getPlatformSupport"
270 json_add_string 'name' 'name'
271 json_close_object
272 json_add_object "setInitAction"
273 json_add_string 'name' 'name'
274 json_add_string 'action' 'action'
275 json_close_object
276 json_dump
277 json_cleanup
278 ;;
279 call)
280 case "$2" in
281 getFileUrlFilesizes)
282 read -r input
283 json_load "$input"
284 json_get_var name 'name'
285 json_cleanup
286 get_file_url_filesizes "$name"
287 ;;
288 getInitList)
289 read -r input
290 json_load "$input"
291 json_get_var name 'name'
292 json_cleanup
293 get_init_list "$name"
294 ;;
295 getInitStatus)
296 read -r input
297 json_load "$input"
298 json_get_var name 'name'
299 json_cleanup
300 get_init_status "$name"
301 ;;
302 getPlatformSupport)
303 read -r input
304 json_load "$input"
305 json_get_var name 'name'
306 json_cleanup
307 get_platform_support "$name"
308 ;;
309 setInitAction)
310 read -r input
311 json_load "$input"
312 json_get_var name 'name'
313 json_get_var action 'action'
314 json_cleanup
315 set_init_action "$name" "$action"
316 ;;
317 esac
318 ;;
319 esac