luci-app-simple-adblock: convert to js
[project/luci.git] / applications / luci-app-simple-adblock / root / usr / libexec / rpcd / luci.simple-adblock
1 #!/bin/sh
2 # Copyright 2022 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.simple-adblock
8 # ubus -S call luci.simple-adblock getInitList '{"name": "simple-adblock" }'
9 # ubus -S call luci.simple-adblock getInitStatus '{"name": "simple-adblock" }'
10 # ubus -S call luci.simple-adblock getPlatformSupport '{"name": "simple-adblock" }'
11
12 . /lib/functions.sh
13 . /lib/functions/network.sh
14 . /usr/share/libubox/jshn.sh
15
16 readonly packageName="simple-adblock"
17 readonly dnsmasqAddnhostsFile="/var/run/${packageName}/dnsmasq.addnhosts"
18 readonly dnsmasqAddnhostsCache="/var/run/${packageName}/dnsmasq.addnhosts.cache"
19 readonly dnsmasqAddnhostsGzip="/etc/${packageName}.dnsmasq.addnhosts.gz"
20 readonly dnsmasqConfFile="/tmp/dnsmasq.d/${packageName}"
21 readonly dnsmasqConfCache="/var/run/${packageName}/dnsmasq.conf.cache"
22 readonly dnsmasqConfGzip="/etc/${packageName}.dnsmasq.conf.gz"
23 readonly dnsmasqIpsetFile="/tmp/dnsmasq.d/${packageName}.ipset"
24 readonly dnsmasqIpsetCache="/var/run/${packageName}/dnsmasq.ipset.cache"
25 readonly dnsmasqIpsetGzip="/etc/${packageName}.dnsmasq.ipset.gz"
26 readonly dnsmasqNftsetFile="/tmp/dnsmasq.d/${packageName}.nftset"
27 readonly dnsmasqNftsetCache="/var/run/${packageName}/dnsmasq.nftset.cache"
28 readonly dnsmasqNftsetGzip="/etc/${packageName}.dnsmasq.nftset.gz"
29 readonly dnsmasqServersFile="/var/run/${packageName}/dnsmasq.servers"
30 readonly dnsmasqServersCache="/var/run/${packageName}/dnsmasq.servers.cache"
31 readonly dnsmasqServersGzip="/etc/${packageName}.dnsmasq.servers.gz"
32 readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
33 readonly unboundCache="/var/run/${packageName}/unbound.cache"
34 readonly unboundGzip="/etc/${packageName}.unbound.gz"
35 readonly jsonFile="/var/run/${packageName}/${packageName}.json"
36
37 str_contains() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
38 str_contains_word() { echo "$1" | grep -q -w "$2"; }
39 str_to_lower() { echo "$1" | tr 'A-Z' 'a-z'; }
40 str_to_upper() { echo "$1" | tr 'a-z' 'A-Z'; }
41 is_enabled() { uci -q get "${1}.config.enabled"; }
42 get_version() { grep -m1 -A2 -w "^Package: $1$" /usr/lib/opkg/status | sed -n 's/Version: //p'; }
43 print_json_bool() { json_init; json_add_boolean "$1" "$2"; json_dump; json_cleanup; }
44 print_json_string() { json_init; json_add_string "$1" "$2"; json_dump; json_cleanup; }
45 logger() { /usr/bin/logger -t "$packageName" "$@"; }
46 ubus_get_status() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@['${packageName}'].instances.main.data.${1}"; }
47 ubus_get_ports() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@['${packageName}'].instances.main.data.firewall.*.dest_port"; }
48 json() {
49 # shellcheck disable=SC2034
50 local action="$1" param="$2" value="$3" i
51 if [ -s "$jsonFile" ]; then
52 json_load_file "$jsonFile" 2>/dev/null
53 json_select 'data' 2>/dev/null
54 for i in status message error stats reload restart; do
55 json_get_var $i "$i" 2>/dev/null
56 done
57 fi
58 case "$action" in
59 get)
60 case "$param" in
61 *)
62 printf "%b" "$(eval echo "\$$param")"; return;;
63 esac
64 ;;
65 esac
66 }
67
68 get_init_list() {
69 local name
70 name="$(basename "$1")"
71 name="${name:-$packageName}"
72 json_init
73 json_add_object "$name"
74 json_add_boolean 'enabled' "$(is_enabled "$name")"
75 if is_running "$name"; then
76 json_add_boolean 'running' '1'
77 else
78 json_add_boolean 'running' '0'
79 fi
80 json_close_object
81 json_dump
82 json_cleanup
83 }
84
85 set_init_action() {
86 local name action="$2" cmd
87 name="$(basename "$1")"
88 name="${name:-$packageName}"
89 if [ ! -f "/etc/init.d/$name" ]; then
90 print_json_string 'error' 'Init script not found!'
91 return
92 fi
93 case $action in
94 enable)
95 cmd="uci -q set ${name}.config.enabled=1 && uci commit $name";;
96 disable)
97 cmd="uci -q set ${name}.config.enabled=0 && uci commit $name";;
98 start|stop|reload|restart)
99 cmd="/etc/init.d/${name} ${action}";;
100 esac
101 if [ -n "$cmd" ] && eval "${cmd}" 1>/dev/null 2>&1; then
102 print_json_bool "result" '1'
103 else
104 print_json_bool "result" '0'
105 fi
106 }
107
108 get_init_status() {
109 local name
110 name="$(basename "$1")"
111 name="${name:-$packageName}"
112 local errors ports dns outputFile outputCache outputGzip
113 local i
114 errors="$(ubus_get_status errors)"
115 ports="$(ubus_get_ports)"
116 dns="$(uci -q get $packageName.config.dns)"
117 case "$dns" in
118 dnsmasq.addnhosts)
119 outputFile="$dnsmasqAddnhostsFile"
120 outputCache="$dnsmasqAddnhostsCache"
121 outputGzip="$dnsmasqAddnhostsGzip"
122 ;;
123 dnsmasq.conf)
124 outputFile="$dnsmasqConfFile"
125 outputCache="$dnsmasqConfCache"
126 outputGzip="$dnsmasqConfGzip"
127 ;;
128 dnsmasq.ipset)
129 outputFile="$dnsmasqIpsetFile"
130 outputCache="$dnsmasqIpsetCache"
131 outputGzip="$dnsmasqIpsetGzip"
132 ;;
133 dnsmasq.nftset)
134 outputFile="$dnsmasqNftsetFile"
135 outputCache="$dnsmasqNftsetCache"
136 outputGzip="$dnsmasqNftsetGzip"
137 ;;
138 dnsmasq.servers)
139 outputFile="$dnsmasqServersFile"
140 outputCache="$dnsmasqServersCache"
141 outputGzip="$dnsmasqServersGzip"
142 ;;
143 unbound.adb_list)
144 outputFile="$unboundFile"
145 outputCache="$unboundCache"
146 outputGzip="$unboundGzip"
147 ;;
148 esac
149 json_init
150 json_add_object "$name"
151 json_add_boolean 'enabled' "$(is_enabled "$name")"
152 i="$(json 'get' 'status')"
153 json_add_string 'status' "$i"
154 if [ "$i" = 'statusSuccess' ]; then
155 json_add_boolean 'running' '1'
156 else
157 json_add_boolean 'running' '0'
158 fi
159 json_add_string 'version' "$(get_version "$name")"
160 json_add_array 'errors'
161 for i in $errors; do json_add_string '' "$i"; done
162 json_close_array
163 if [ -n "$ports" ]; then
164 json_add_boolean 'force_dns_active' '1'
165 json_add_array 'force_dns_ports'
166 for i in $ports; do json_add_int '' "$i"; done
167 json_close_array
168 else
169 json_add_boolean 'force_dns_active' '0'
170 fi
171 json_add_int 'entries' "$(ubus_get_status entries)"
172 json_add_string 'dns' "$dns"
173 json_add_string 'outputFile' "$outputFile"
174 json_add_string 'outputCache' "$outputCache"
175 json_add_string 'outputGzip' "$outputGzip"
176 if [ -s "$outputFile" ]; then
177 json_add_boolean 'outputFileExists' '1'
178 else
179 json_add_boolean 'outputFileExists' '0'
180 fi
181 if [ -s "$outputCache" ]; then
182 json_add_boolean 'outputCacheExists' '1'
183 else
184 json_add_boolean 'outputCacheExists' '0'
185 fi
186 if [ -s "$outputGzip" ]; then
187 json_add_boolean 'outputGzipExists' '1'
188 else
189 json_add_boolean 'outputGzipExists' '0'
190 fi
191 json_add_array 'leds'
192 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
193 json_close_array
194 json_close_object
195 json_dump
196 json_cleanup
197 }
198
199 check_ipset() { { command -v ipset && /usr/sbin/ipset help hash:net; } >/dev/null 2>&1; }
200 check_nft() { command -v nft >/dev/null 2>&1; }
201 check_dnsmasq() { command -v dnsmasq >/dev/null 2>&1; }
202 check_unbound() { command -v unbound >/dev/null 2>&1; }
203 check_dnsmasq_ipset() {
204 local o;
205 check_dnsmasq || return 1
206 o="$(dnsmasq -v 2>/dev/null)"
207 check_ipset && ! echo "$o" | grep -q 'no-ipset' && echo "$o" | grep -q 'ipset'
208 }
209 check_dnsmasq_nftset() {
210 local o;
211 check_dnsmasq || return 1
212 o="$(dnsmasq -v 2>/dev/null)"
213 check_nft && ! echo "$o" | grep -q 'no-nftset' && echo "$o" | grep -q 'nftset'
214 }
215
216 get_platform_support() {
217 local name
218 name="$(basename "$1")"
219 name="${name:-$packageName}"
220 json_init
221 json_add_object "$name"
222 if check_ipset; then
223 json_add_boolean 'ipset_installed' '1'
224 else
225 json_add_boolean 'ipset_installed' '0'
226 fi
227 if check_nft; then
228 json_add_boolean 'nft_installed' '1'
229 else
230 json_add_boolean 'nft_installed' '0'
231 fi
232 if check_dnsmasq; then
233 json_add_boolean 'dnsmasq_installed' '1'
234 else
235 json_add_boolean 'dnsmasq_installed' '0'
236 fi
237 if check_unbound; then
238 json_add_boolean 'unbound_installed' '1'
239 else
240 json_add_boolean 'unbound_installed' '0'
241 fi
242 if check_dnsmasq_ipset; then
243 json_add_boolean 'dnsmasq_ipset_support' '1'
244 else
245 json_add_boolean 'dnsmasq_ipset_support' '0'
246 fi
247 if check_dnsmasq_nftset; then
248 json_add_boolean 'dnsmasq_nftset_support' '1'
249 else
250 json_add_boolean 'dnsmasq_nftset_support' '0'
251 fi
252 json_add_array 'leds'
253 for i in /sys/class/leds/*; do json_add_string '' "$(basename "$i")"; done
254 json_close_array
255 json_close_object
256 json_dump
257 json_cleanup
258 }
259
260 case "$1" in
261 list)
262 json_init
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 getInitList)
282 read -r input
283 json_load "$input"
284 json_get_var name 'name'
285 json_cleanup
286 get_init_list "$name"
287 ;;
288 getInitStatus)
289 read -r input
290 json_load "$input"
291 json_get_var name 'name'
292 json_cleanup
293 get_init_status "$name"
294 ;;
295 getPlatformSupport)
296 read -r input
297 json_load "$input"
298 json_get_var name 'name'
299 json_cleanup
300 get_platform_support "$name"
301 ;;
302 setInitAction)
303 read -r input
304 json_load "$input"
305 json_get_var name 'name'
306 json_get_var action 'action'
307 json_cleanup
308 set_init_action "$name" "$action"
309 ;;
310 esac
311 ;;
312 esac