luci-app-pbr: update to 1.1.4-5
[project/luci.git] / applications / luci-app-pbr / root / usr / libexec / rpcd / luci.pbr
1 #!/bin/sh
2 # Copyright 2022 Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC2018,SC2019,SC2039,SC3043,SC3057,SC3060
4
5 # TechRef: https://openwrt.org/docs/techref/rpcd
6 # TESTS
7 # ubus -v list luci.pbr
8 # ubus -S call luci.pbr getInitList '{"name": "pbr" }'
9 # ubus -S call luci.pbr getInitStatus '{"name": "pbr" }'
10 # ubus -S call luci.pbr getPlatformSupport '{"name": "pbr" }'
11 # ubus -S call luci.pbr getGateways '{"name": "pbr" }'
12 # ubus -S call luci.pbr getInterfaces '{"name": "pbr" }'
13
14 readonly pbrFunctionsFile='/etc/init.d/pbr'
15 if [ -s "$pbrFunctionsFile" ]; then
16 # shellcheck source=../../../../../pbr/files/etc/init.d/pbr
17 . "$pbrFunctionsFile"
18 else
19 print_json_string 'error' "pbr init.d file ($pbrFunctionsFile) not found!"
20 logger -t pbr 'error' "pbr init.d file ($pbrFunctionsFile) not found!"
21 fi
22
23 get_init_list() {
24 local name
25 name="$(basename "$1")"
26 name="${name:-$packageName}"
27 json_init
28 json_add_object "$packageName"
29 json_add_boolean 'enabled' "$(is_enabled "$packageName")"
30 if is_running "$packageName"; then
31 json_add_boolean 'running' '1'
32 else
33 json_add_boolean 'running' '0'
34 fi
35 json_close_object
36 json_dump
37 json_cleanup
38 }
39
40 set_init_action() {
41 local name action="$2" cmd
42 name="$(basename "$1")"
43 name="${name:-$packageName}"
44 if [ ! -f "/etc/init.d/$packageName" ]; then
45 print_json_string 'error' 'Init script not found!'
46 return
47 fi
48 case $action in
49 enable)
50 cmd="/etc/init.d/${name} ${action}"
51 cmd="${cmd} && uci_set ${name} config enabled 1 && uci_commit $name"
52 ;;
53 disable)
54 cmd="/etc/init.d/${name} ${action}"
55 cmd="${cmd} && uci_set ${name} config enabled 0 && uci_commit $name"
56 ;;
57 start|stop|reload|restart)
58 cmd="/etc/init.d/${name} ${action}"
59 ;;
60 esac
61 if [ -n "$cmd" ] && eval "$cmd" 1>/dev/null 2>&1; then
62 print_json_bool 'result' '1'
63 else
64 print_json_bool 'result' '0'
65 fi
66 }
67
68 get_init_status() {
69 local name
70 name="$(basename "$1")"
71 name="${name:-$packageName}"
72 local version gateways warnings errors
73 [ -z "$version" ] && version="$(opkg_get_version "${name}")"
74 [ -z "$version" ] && version="$(opkg_get_version "${name}-iptables")"
75 [ -z "$version" ] && version="$(opkg_get_version "${name}-netifd")"
76 gateways="$(ubus_get_status gateways | sed "s|\\\n|<br />|g;s|\(\\\033[^<]*\)|✓|g;")"
77 warnings="$(ubus_get_status warnings)"
78 errors="$(ubus_get_status errors)"
79 json_init
80 json_add_object "$packageName"
81 json_add_boolean 'enabled' "$(is_enabled "$packageName")"
82 if is_running "$packageName"; then
83 json_add_boolean 'running' '1'
84 else
85 json_add_boolean 'running' '0'
86 fi
87 if is_running_iptables "$packageName"; then
88 json_add_boolean 'running_iptables' '1'
89 else
90 json_add_boolean 'running_iptables' '0'
91 fi
92 if is_running_nft "$packageName"; then
93 json_add_boolean 'running_nft' '1'
94 else
95 json_add_boolean 'running_nft' '0'
96 fi
97 if is_running_nft_file "$packageName"; then
98 json_add_boolean 'running_nft_file' '1'
99 else
100 json_add_boolean 'running_nft_file' '0'
101 fi
102 json_add_string 'version' "$version"
103 json_add_string 'gateways' "$gateways"
104 json_add_array 'errors'
105 if [ -n "$errors" ]; then
106 while read -r line; do
107 if str_contains "$line" ' '; then
108 error_id="${line% *}"
109 error_extra="${line#* }"
110 else
111 error_id="$line"
112 unset error_extra
113 fi
114 json_add_object
115 json_add_string 'id' "$error_id"
116 json_add_string 'extra' "$error_extra"
117 json_close_object
118 done <<EOF
119 $(echo "$errors" | tr \# \\n)
120 EOF
121 fi
122 json_close_array
123 json_add_array 'warnings'
124 if [ -n "$warnings" ]; then
125 while read -r line; do
126 if str_contains "$line" ' '; then
127 warning_id="${line% *}"
128 warning_extra="${line#* }"
129 else
130 warning_id="$line"
131 unset warning_extra
132 fi
133 json_add_object
134 json_add_string 'id' "$warning_id"
135 json_add_string 'extra' "$warning_extra"
136 json_close_object
137 done <<EOF
138 $(echo "$warnings" | tr \# \\n)
139 EOF
140 fi
141 if is_greater "$(opkg_get_version "${name}")" "$(opkg_get_version "luci-app-${name}")"; then
142 json_add_object
143 json_add_string 'id' 'warningOutdatedWebUIApp'
144 json_add_string 'extra' "$(opkg_get_version "luci-app-${name}")"
145 json_close_object
146 fi
147 json_close_array
148 json_close_object
149 json_dump
150 json_cleanup
151 }
152
153 get_platform_support() {
154 local name
155 name="$(basename "$1")"
156 name="${name:-$packageName}"
157 json_init
158 json_add_object "$packageName"
159 if check_ipset; then
160 json_add_boolean 'ipset_installed' '1'
161 else
162 json_add_boolean 'ipset_installed' '0'
163 fi
164 if check_nft; then
165 json_add_boolean 'nft_installed' '1'
166 else
167 json_add_boolean 'nft_installed' '0'
168 fi
169 if check_agh; then
170 json_add_boolean 'adguardhome_installed' '1'
171 else
172 json_add_boolean 'adguardhome_installed' '0'
173 fi
174 if check_dnsmasq; then
175 json_add_boolean 'dnsmasq_installed' '1'
176 else
177 json_add_boolean 'dnsmasq_installed' '0'
178 fi
179 if check_unbound; then
180 json_add_boolean 'unbound_installed' '1'
181 else
182 json_add_boolean 'unbound_installed' '0'
183 fi
184 if check_agh_ipset; then
185 json_add_boolean 'adguardhome_ipset_support' '1'
186 else
187 json_add_boolean 'adguardhome_ipset_support' '0'
188 fi
189 if check_dnsmasq_ipset; then
190 json_add_boolean 'dnsmasq_ipset_support' '1'
191 else
192 json_add_boolean 'dnsmasq_ipset_support' '0'
193 fi
194 if check_dnsmasq_nftset; then
195 json_add_boolean 'dnsmasq_nftset_support' '1'
196 else
197 json_add_boolean 'dnsmasq_nftset_support' '0'
198 fi
199 json_close_object
200 json_dump
201 json_cleanup
202 }
203
204 # shellcheck disable=SC3037
205 get_gateways() {
206 echo -en "{\"$packageName\":{\"gateways\":"
207 ubus_get_gateways
208 echo -en "}}"
209 }
210
211 get_supported_interfaces() {
212 _find_firewall_wan_zone() { [ "$(uci_get 'firewall' "$1" 'name')" = "wan" ] && firewallWanZone="$1"; }
213 _build_ifaces_all() { ifacesAll="${ifacesAll}${1} "; }
214 _build_ifaces_supported() { is_supported_interface "$1" && ! str_contains "$ifacesSupported" "$1" && ifacesSupported="${ifacesSupported}${1} "; }
215 local i
216 local firewallWanZone
217 local ifacesAll ifacesSupported
218 local webui_show_ignore_target
219 local ignored_interface supported_interface
220 local wanIface4 wanIface6
221 config_load "$packageName"
222 config_get_bool webui_show_ignore_target 'config' 'webui_show_ignore_target' '0'
223 config_get ignored_interface 'config' 'ignored_interface'
224 config_get supported_interface 'config' 'supported_interface'
225 local i
226 config_load 'network'
227 config_foreach _build_ifaces_all 'interface'
228 pbr_find_iface wanIface4 'wan'
229 pbr_find_iface wanIface6 'wan6'
230 config_load 'firewall'
231 config_foreach _find_firewall_wan_zone 'zone'
232 for i in $(uci_get 'firewall' "$firewallWanZone" 'network'); do
233 is_supported_interface "$i" && ! str_contains "$ifacesSupported" "$1" && ifacesSupported="${ifacesSupported}${i} "
234 done
235 config_load 'network'
236 config_foreach _build_ifaces_supported 'interface'
237 is_tor_running && ifacesSupported="$ifacesSupported tor"
238 for i in $supported_interface; do
239 is_xray "$i" && ifacesSupported="$ifacesSupported $i"
240 done
241 [ "$webui_show_ignore_target" -eq "1" ] && ifacesSupported="$ifacesSupported ignore"
242 json_init
243 json_add_object "$packageName"
244 json_add_array 'interfaces'
245 for i in $ifacesSupported; do
246 json_add_string '' "$i"
247 done
248 json_close_array
249 json_close_object
250 json_dump
251 json_cleanup
252 }
253
254 case "$1" in
255 list)
256 json_init
257 json_add_object "getGateways"
258 json_add_string 'name' 'name'
259 json_close_object
260 json_add_object "getInitList"
261 json_add_string 'name' 'name'
262 json_close_object
263 json_add_object "getInitStatus"
264 json_add_string 'name' 'name'
265 json_close_object
266 json_add_object "getInterfaces"
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 getGateways)
282 read -r input
283 json_load "$input"
284 json_get_var name 'name'
285 json_cleanup
286 get_gateways "$packageName"
287 ;;
288 getInitList)
289 read -r input
290 json_load "$input"
291 json_get_var name 'name'
292 json_cleanup
293 get_init_list "$packageName"
294 ;;
295 getInitStatus)
296 read -r input
297 json_load "$input"
298 json_get_var name 'name'
299 json_cleanup
300 get_init_status "$packageName"
301 ;;
302 getInterfaces)
303 read -r input
304 json_load "$input"
305 json_get_var name 'name'
306 json_cleanup
307 get_supported_interfaces "$packageName"
308 ;;
309 getPlatformSupport)
310 read -r input
311 json_load "$input"
312 json_get_var name 'name'
313 json_cleanup
314 get_platform_support "$packageName"
315 ;;
316 setInitAction)
317 read -r input
318 json_load "$input"
319 json_get_var name 'name'
320 json_get_var action 'action'
321 json_cleanup
322 set_init_action "$packageName" "$action"
323 ;;
324 esac
325 ;;
326 esac