Merge pull request #6642 from vgaetera/dnsmasq-network-select
[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="uci -q set ${name}.config.enabled=1 && uci commit $name";;
77 disable)
78 cmd="uci -q set ${name}.config.enabled=0 && uci commit $name";;
79 start|stop|reload|restart|dl|pause)
80 cmd="/etc/init.d/${name} ${action}";;
81 esac
82 if [ -n "$cmd" ] && eval "${cmd}" >/dev/null 2>&1; then
83 print_json_bool "result" '1'
84 else
85 print_json_bool "result" '0'
86 fi
87 }
88
89 get_init_status() {
90 local name
91 name="$(basename "$1")"
92 name="${name:-$packageName}"
93 local errors warnings ports dns outputFile outputCache outputGzip
94 local i j
95 # shellcheck disable=SC2034
96 local compressed_cache_dir
97 config_load "$name"
98 config_get compressed_cache_dir 'config' 'compressed_cache_dir' '/etc'
99 if [ -n "$(sanitize_dir "$compressed_cache_dir")" ]; then
100 compressed_cache_dir="$(sanitize_dir "$compressed_cache_dir")"
101 else
102 compressed_cache_dir="/etc"
103 fi
104 if [ -n "$(uci -q get $packageName.config.dnsmasq_config_file_url)" ]; then
105 dns="dnsmasq.conf"
106 else
107 dns="$(uci -q get $packageName.config.dns)"
108 fi
109 case "$dns" in
110 dnsmasq.addnhosts)
111 outputFile="$dnsmasqAddnhostsFile"
112 outputCache="$dnsmasqAddnhostsCache"
113 outputGzip="${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
114 ;;
115 dnsmasq.conf)
116 outputFile="$dnsmasqConfFile"
117 outputCache="$dnsmasqConfCache"
118 outputGzip="${compressed_cache_dir}/${dnsmasqConfGzip}"
119 ;;
120 dnsmasq.ipset)
121 outputFile="$dnsmasqIpsetFile"
122 outputCache="$dnsmasqIpsetCache"
123 outputGzip="${compressed_cache_dir}/${dnsmasqIpsetGzip}"
124 ;;
125 dnsmasq.nftset)
126 outputFile="$dnsmasqNftsetFile"
127 outputCache="$dnsmasqNftsetCache"
128 outputGzip="${compressed_cache_dir}/${dnsmasqNftsetGzip}"
129 ;;
130 dnsmasq.servers)
131 outputFile="$dnsmasqServersFile"
132 outputCache="$dnsmasqServersCache"
133 outputGzip="${compressed_cache_dir}/${dnsmasqServersGzip}"
134 ;;
135 unbound.adb_list)
136 outputFile="$unboundFile"
137 outputCache="$unboundCache"
138 outputGzip="${compressed_cache_dir}/${unboundGzip}"
139 ;;
140 esac
141 json_init
142 json_add_object "$name"
143 json_add_boolean 'enabled' "$(is_enabled "$name")"
144 json_add_string 'status' "$(json 'get' 'status')"
145 if is_running "$name"; then
146 json_add_boolean 'running' '1'
147 else
148 json_add_boolean 'running' '0'
149 fi
150 json_add_string 'version' "$(get_version "$name")"
151 errors="$(ubus_get_data errors)"
152 json_add_array 'errors'
153 if [ -n "$errors" ]; then
154 for i in $errors; do
155 if str_contains "$i" '|'; then
156 error_extra="${i##*|}"
157 error_id="${i%|*}"
158 else
159 error_id="$i"
160 unset error_extra
161 fi
162 json_add_object
163 json_add_string 'id' "$error_id"
164 json_add_string 'extra' "$error_extra"
165 json_close_object
166 done
167 fi
168 json_close_array
169 warnings="$(ubus_get_data warnings)"
170 json_add_array 'warnings'
171 if [ -n "$warnings" ]; then
172 for i in $warnings; do
173 if str_contains "$i" '|'; then
174 error_extra="${i##*|}"
175 error_id="${i%|*}"
176 else
177 error_id="$i"
178 unset error_extra
179 fi
180 json_add_object
181 json_add_string 'id' "$error_id"
182 json_add_string 'extra' "$error_extra"
183 json_close_object
184 done
185 fi
186 json_close_array
187
188 ports="$(ubus_get_ports)"
189 if [ -n "$ports" ]; then
190 json_add_boolean 'force_dns_active' '1'
191 json_add_array 'force_dns_ports'
192 for i in $ports; do json_add_int '' "$i"; done
193 json_close_array
194 else
195 json_add_boolean 'force_dns_active' '0'
196 fi
197 json_add_int 'entries' "$(ubus_get_data entries)"
198 json_add_string 'dns' "$dns"
199 json_add_string 'outputFile' "$outputFile"
200 json_add_string 'outputCache' "$outputCache"
201 json_add_string 'outputGzip' "$outputGzip"
202 if [ -s "$outputFile" ]; then
203 json_add_boolean 'outputFileExists' '1'
204 else
205 json_add_boolean 'outputFileExists' '0'
206 fi
207 if [ -s "$outputCache" ]; then
208 json_add_boolean 'outputCacheExists' '1'
209 else
210 json_add_boolean 'outputCacheExists' '0'
211 fi
212 if [ -s "$outputGzip" ]; then
213 json_add_boolean 'outputGzipExists' '1'
214 else
215 json_add_boolean 'outputGzipExists' '0'
216 fi
217 json_add_array 'leds'
218 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
219 json_close_array
220 json_close_object
221 json_dump
222 json_cleanup
223 }
224
225 get_platform_support() {
226 local name
227 name="$(basename "$1")"
228 name="${name:-$packageName}"
229 json_init
230 json_add_object "$name"
231 if check_ipset; then
232 json_add_boolean 'ipset_installed' '1'
233 else
234 json_add_boolean 'ipset_installed' '0'
235 fi
236 if check_nft; then
237 json_add_boolean 'nft_installed' '1'
238 else
239 json_add_boolean 'nft_installed' '0'
240 fi
241 if check_dnsmasq; then
242 json_add_boolean 'dnsmasq_installed' '1'
243 else
244 json_add_boolean 'dnsmasq_installed' '0'
245 fi
246 if check_unbound; then
247 json_add_boolean 'unbound_installed' '1'
248 else
249 json_add_boolean 'unbound_installed' '0'
250 fi
251 if check_dnsmasq_ipset; then
252 json_add_boolean 'dnsmasq_ipset_support' '1'
253 else
254 json_add_boolean 'dnsmasq_ipset_support' '0'
255 fi
256 if check_dnsmasq_nftset; then
257 json_add_boolean 'dnsmasq_nftset_support' '1'
258 else
259 json_add_boolean 'dnsmasq_nftset_support' '0'
260 fi
261 json_add_array 'leds'
262 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
263 json_close_array
264 json_close_object
265 json_dump
266 json_cleanup
267 }
268
269 case "$1" in
270 list)
271 json_init
272 json_add_object "getFileUrlFilesizes"
273 json_add_string 'name' 'name'
274 json_close_object
275 json_add_object "getInitList"
276 json_add_string 'name' 'name'
277 json_close_object
278 json_add_object "getInitStatus"
279 json_add_string 'name' 'name'
280 json_close_object
281 json_add_object "getPlatformSupport"
282 json_add_string 'name' 'name'
283 json_close_object
284 json_add_object "setInitAction"
285 json_add_string 'name' 'name'
286 json_add_string 'action' 'action'
287 json_close_object
288 json_dump
289 json_cleanup
290 ;;
291 call)
292 case "$2" in
293 getFileUrlFilesizes)
294 read -r input
295 json_load "$input"
296 json_get_var name 'name'
297 json_cleanup
298 get_file_url_filesizes "$name"
299 ;;
300 getInitList)
301 read -r input
302 json_load "$input"
303 json_get_var name 'name'
304 json_cleanup
305 get_init_list "$name"
306 ;;
307 getInitStatus)
308 read -r input
309 json_load "$input"
310 json_get_var name 'name'
311 json_cleanup
312 get_init_status "$name"
313 ;;
314 getPlatformSupport)
315 read -r input
316 json_load "$input"
317 json_get_var name 'name'
318 json_cleanup
319 get_platform_support "$name"
320 ;;
321 setInitAction)
322 read -r input
323 json_load "$input"
324 json_get_var name 'name'
325 json_get_var action 'action'
326 json_cleanup
327 set_init_action "$name" "$action"
328 ;;
329 esac
330 ;;
331 esac