2 # Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC2018,SC2019,SC3043,SC3060
5 # TechRef: https://openwrt.org/docs/techref/rpcd
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" }'
17 readonly adbFunctionsFile
='/etc/init.d/adblock-fast'
18 if [ -s "$adbFunctionsFile" ]; then
19 # shellcheck source=../../../../../adblock-fast/files/etc/init.d/adblock-fast
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!"
26 get_file_url_filesizes
() {
27 _get_file_url_size
() {
29 config_get url
"$1" 'url'
30 config_get size
"$1" 'size'
31 [ -n "$size" ] || size
="$(get_url_filesize "$url")"
33 json_add_string
'url' "$url"
34 json_add_int
'size' "$size"
39 json_add_object
"$name"
40 json_add_array
'sizes'
42 config_foreach _get_file_url_size
'file_url'
51 name
="$(basename "$1")"
52 name
="${name:-$packageName}"
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'
59 json_add_boolean
'running' '0'
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!'
76 cmd
="/etc/init.d/${name} ${action}"
77 cmd
="${cmd} && uci_set ${name} config enabled 1 && uci_commit $name"
80 cmd
="/etc/init.d/${name} ${action}"
81 cmd
="${cmd} && uci_set ${name} config enabled 0 && uci_commit $name"
83 start|stop|reload|restart|dl|pause
)
84 cmd
="/etc/init.d/${name} ${action}"
87 if [ -n "$cmd" ] && eval "$cmd" >/dev
/null
2>&1; then
88 print_json_bool
"result" '1'
90 print_json_bool
"result" '0'
96 name
="$(basename "$1")"
97 name
="${name:-$packageName}"
98 local errors warnings ports dns outputFile outputCache outputGzip outputConfig
100 # shellcheck disable=SC2034
101 local compressed_cache_dir
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
109 dns
="$(uci_get "$packageName" 'config' 'dns' 'dnsmasq.servers')"
112 dns_set_output_values
"$dns"
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'
121 json_add_boolean
'running' '0'
123 json_add_string
'version' "$PKG_VERSION"
124 errors
="$(ubus_get_data errors)"
125 json_add_array
'errors'
126 if [ -n "$errors" ]; then
128 if str_contains
"$i" '|'; then
129 error_extra
="${i##*|}"
136 json_add_string
'id' "$error_id"
137 json_add_string
'extra' "$error_extra"
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##*|}"
154 json_add_string
'id' "$error_id"
155 json_add_string
'extra' "$error_extra"
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
168 json_add_boolean
'force_dns_active' '0'
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'
178 json_add_boolean
'outputFileExists' '0'
180 if [ -s "$outputCache" ]; then
181 json_add_boolean
'outputCacheExists' '1'
183 json_add_boolean
'outputCacheExists' '0'
185 if [ -s "$outputGzip" ]; then
186 json_add_boolean
'outputGzipExists' '1'
188 json_add_boolean
'outputGzipExists' '0'
190 json_add_array
'leds'
191 for i
in /sys
/class
/leds
/*; do json_add_string
'' "$(basename "$i")"; done
198 get_platform_support
() {
200 name
="$(basename "$1")"
201 name
="${name:-$packageName}"
203 json_add_object
"$name"
205 json_add_boolean
'ipset_installed' '1'
207 json_add_boolean
'ipset_installed' '0'
210 json_add_boolean
'nft_installed' '1'
212 json_add_boolean
'nft_installed' '0'
214 if check_dnsmasq
; then
215 json_add_boolean
'dnsmasq_installed' '1'
217 json_add_boolean
'dnsmasq_installed' '0'
219 if check_dnsmasq_ipset
; then
220 json_add_boolean
'dnsmasq_ipset_support' '1'
222 json_add_boolean
'dnsmasq_ipset_support' '0'
224 if check_dnsmasq_nftset
; then
225 json_add_boolean
'dnsmasq_nftset_support' '1'
227 json_add_boolean
'dnsmasq_nftset_support' '0'
229 if check_smartdns
; then
230 json_add_boolean
'smartdns_installed' '1'
232 json_add_boolean
'smartdns_installed' '0'
234 if check_smartdns_ipset
; then
235 json_add_boolean
'smartdns_ipset_support' '1'
237 json_add_boolean
'smartdns_ipset_support' '0'
239 if check_smartdns_nftset
; then
240 json_add_boolean
'smartdns_nftset_support' '1'
242 json_add_boolean
'smartdns_nftset_support' '0'
244 if check_unbound
; then
245 json_add_boolean
'unbound_installed' '1'
247 json_add_boolean
'unbound_installed' '0'
249 json_add_array
'leds'
250 for i
in /sys
/class
/leds
/*; do json_add_string
'' "$(basename "$i")"; done
260 json_add_object
"getFileUrlFilesizes"
261 json_add_string
'name' 'name'
263 json_add_object
"getInitList"
264 json_add_string
'name' 'name'
266 json_add_object
"getInitStatus"
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_file_url_filesizes
"$name"
291 json_get_var name
'name'
293 get_init_list
"$name"
298 json_get_var name
'name'
300 get_init_status
"$name"
305 json_get_var name
'name'
307 get_platform_support
"$name"
312 json_get_var name
'name'
313 json_get_var action
'action'
315 set_init_action
"$name" "$action"