2 # Copyright 2022 Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC2018,SC2019,SC2039,SC3043,SC3057,SC3060
5 # TechRef: https://openwrt.org/docs/techref/rpcd
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" }'
14 readonly pbrFunctionsFile
='/etc/init.d/pbr'
15 if [ -s "$pbrFunctionsFile" ]; then
16 # shellcheck source=../../../../../pbr/files/etc/init.d/pbr
19 print_json_string
'error' "pbr init.d file ($pbrFunctionsFile) not found!"
20 logger
-t pbr
'error' "pbr init.d file ($pbrFunctionsFile) not found!"
25 name
="$(basename "$1")"
26 name
="${name:-$packageName}"
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'
33 json_add_boolean
'running' '0'
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!'
50 cmd
="/etc/init.d/${name} ${action}"
51 cmd
="${cmd} && uci_set ${name} config enabled 1 && uci_commit $name"
54 cmd
="/etc/init.d/${name} ${action}"
55 cmd
="${cmd} && uci_set ${name} config enabled 0 && uci_commit $name"
57 start|stop|reload|restart
)
58 cmd
="/etc/init.d/${name} ${action}"
61 if [ -n "$cmd" ] && eval "$cmd" 1>/dev
/null
2>&1; then
62 print_json_bool
'result' '1'
64 print_json_bool
'result' '0'
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)"
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'
85 json_add_boolean
'running' '0'
87 if is_running_iptables
"$packageName"; then
88 json_add_boolean
'running_iptables' '1'
90 json_add_boolean
'running_iptables' '0'
92 if is_running_nft
"$packageName"; then
93 json_add_boolean
'running_nft' '1'
95 json_add_boolean
'running_nft' '0'
97 if is_running_nft_file
"$packageName"; then
98 json_add_boolean
'running_nft_file' '1'
100 json_add_boolean
'running_nft_file' '0'
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#* }"
115 json_add_string
'id' "$error_id"
116 json_add_string
'extra' "$error_extra"
119 $(echo "$errors" | tr \# \\n)
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#* }"
134 json_add_string
'id' "$warning_id"
135 json_add_string
'extra' "$warning_extra"
138 $(echo "$warnings" | tr \# \\n)
141 if is_greater
"$(opkg_get_version "${name}")" "$(opkg_get_version "luci-app-
${name}")"; then
143 json_add_string
'id' 'warningOutdatedWebUIApp'
144 json_add_string
'extra' "$(opkg_get_version "luci-app-
${name}")"
153 get_platform_support
() {
155 name
="$(basename "$1")"
156 name
="${name:-$packageName}"
158 json_add_object
"$packageName"
160 json_add_boolean
'ipset_installed' '1'
162 json_add_boolean
'ipset_installed' '0'
165 json_add_boolean
'nft_installed' '1'
167 json_add_boolean
'nft_installed' '0'
170 json_add_boolean
'adguardhome_installed' '1'
172 json_add_boolean
'adguardhome_installed' '0'
174 if check_dnsmasq
; then
175 json_add_boolean
'dnsmasq_installed' '1'
177 json_add_boolean
'dnsmasq_installed' '0'
179 if check_unbound
; then
180 json_add_boolean
'unbound_installed' '1'
182 json_add_boolean
'unbound_installed' '0'
184 if check_agh_ipset
; then
185 json_add_boolean
'adguardhome_ipset_support' '1'
187 json_add_boolean
'adguardhome_ipset_support' '0'
189 if check_dnsmasq_ipset
; then
190 json_add_boolean
'dnsmasq_ipset_support' '1'
192 json_add_boolean
'dnsmasq_ipset_support' '0'
194 if check_dnsmasq_nftset
; then
195 json_add_boolean
'dnsmasq_nftset_support' '1'
197 json_add_boolean
'dnsmasq_nftset_support' '0'
204 # shellcheck disable=SC3037
206 echo -en "{\"$packageName\":{\"gateways\":"
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} "; }
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'
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} "
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"
241 [ "$webui_show_ignore_target" -eq "1" ] && ifacesSupported
="$ifacesSupported ignore"
243 json_add_object
"$packageName"
244 json_add_array
'interfaces'
245 for i
in $ifacesSupported; do
246 json_add_string
'' "$i"
257 json_add_object
"getGateways"
258 json_add_string
'name' 'name'
260 json_add_object
"getInitList"
261 json_add_string
'name' 'name'
263 json_add_object
"getInitStatus"
264 json_add_string
'name' 'name'
266 json_add_object
"getInterfaces"
267 json_add_string
'name' 'name'
269 json_add_object
"getPlatformSupport"
270 json_add_string
'name' 'name'
272 json_add_object
"setInitAction"
273 json_add_string
'name' 'name'
274 json_add_string
'action' 'action'
284 json_get_var name
'name'
286 get_gateways
"$packageName"
291 json_get_var name
'name'
293 get_init_list
"$packageName"
298 json_get_var name
'name'
300 get_init_status
"$packageName"
305 json_get_var name
'name'
307 get_supported_interfaces
"$packageName"
312 json_get_var name
'name'
314 get_platform_support
"$packageName"
319 json_get_var name
'name'
320 json_get_var action
'action'
322 set_init_action
"$packageName" "$action"