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
="uci -q set ${name}.config.enabled=1 && uci commit $name";;
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}";;
82 if [ -n "$cmd" ] && eval "${cmd}" >/dev
/null
2>&1; then
83 print_json_bool
"result" '1'
85 print_json_bool
"result" '0'
91 name
="$(basename "$1")"
92 name
="${name:-$packageName}"
93 local errors warnings ports dns outputFile outputCache outputGzip
95 # shellcheck disable=SC2034
96 local compressed_cache_dir
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")"
102 compressed_cache_dir
="/etc"
104 if [ -n "$(uci -q get $packageName.config.dnsmasq_config_file_url)" ]; then
107 dns
="$(uci -q get $packageName.config.dns)"
111 outputFile
="$dnsmasqAddnhostsFile"
112 outputCache
="$dnsmasqAddnhostsCache"
113 outputGzip
="${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
116 outputFile
="$dnsmasqConfFile"
117 outputCache
="$dnsmasqConfCache"
118 outputGzip
="${compressed_cache_dir}/${dnsmasqConfGzip}"
121 outputFile
="$dnsmasqIpsetFile"
122 outputCache
="$dnsmasqIpsetCache"
123 outputGzip
="${compressed_cache_dir}/${dnsmasqIpsetGzip}"
126 outputFile
="$dnsmasqNftsetFile"
127 outputCache
="$dnsmasqNftsetCache"
128 outputGzip
="${compressed_cache_dir}/${dnsmasqNftsetGzip}"
131 outputFile
="$dnsmasqServersFile"
132 outputCache
="$dnsmasqServersCache"
133 outputGzip
="${compressed_cache_dir}/${dnsmasqServersGzip}"
136 outputFile
="$unboundFile"
137 outputCache
="$unboundCache"
138 outputGzip
="${compressed_cache_dir}/${unboundGzip}"
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'
148 json_add_boolean
'running' '0'
150 json_add_string
'version' "$(get_version "$name")"
151 errors
="$(ubus_get_data errors)"
152 json_add_array
'errors'
153 if [ -n "$errors" ]; then
155 if str_contains
"$i" '|'; then
156 error_extra
="${i##*|}"
163 json_add_string
'id' "$error_id"
164 json_add_string
'extra' "$error_extra"
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##*|}"
181 json_add_string
'id' "$error_id"
182 json_add_string
'extra' "$error_extra"
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
195 json_add_boolean
'force_dns_active' '0'
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'
205 json_add_boolean
'outputFileExists' '0'
207 if [ -s "$outputCache" ]; then
208 json_add_boolean
'outputCacheExists' '1'
210 json_add_boolean
'outputCacheExists' '0'
212 if [ -s "$outputGzip" ]; then
213 json_add_boolean
'outputGzipExists' '1'
215 json_add_boolean
'outputGzipExists' '0'
217 json_add_array
'leds'
218 for i
in /sys
/class
/leds
/*; do json_add_string
'' "$(basename "$i")"; done
225 get_platform_support
() {
227 name
="$(basename "$1")"
228 name
="${name:-$packageName}"
230 json_add_object
"$name"
232 json_add_boolean
'ipset_installed' '1'
234 json_add_boolean
'ipset_installed' '0'
237 json_add_boolean
'nft_installed' '1'
239 json_add_boolean
'nft_installed' '0'
241 if check_dnsmasq
; then
242 json_add_boolean
'dnsmasq_installed' '1'
244 json_add_boolean
'dnsmasq_installed' '0'
246 if check_unbound
; then
247 json_add_boolean
'unbound_installed' '1'
249 json_add_boolean
'unbound_installed' '0'
251 if check_dnsmasq_ipset
; then
252 json_add_boolean
'dnsmasq_ipset_support' '1'
254 json_add_boolean
'dnsmasq_ipset_support' '0'
256 if check_dnsmasq_nftset
; then
257 json_add_boolean
'dnsmasq_nftset_support' '1'
259 json_add_boolean
'dnsmasq_nftset_support' '0'
261 json_add_array
'leds'
262 for i
in /sys
/class
/leds
/*; do json_add_string
'' "$(basename "$i")"; done
272 json_add_object
"getFileUrlFilesizes"
273 json_add_string
'name' 'name'
275 json_add_object
"getInitList"
276 json_add_string
'name' 'name'
278 json_add_object
"getInitStatus"
279 json_add_string
'name' 'name'
281 json_add_object
"getPlatformSupport"
282 json_add_string
'name' 'name'
284 json_add_object
"setInitAction"
285 json_add_string
'name' 'name'
286 json_add_string
'action' 'action'
296 json_get_var name
'name'
298 get_file_url_filesizes
"$name"
303 json_get_var name
'name'
305 get_init_list
"$name"
310 json_get_var name
'name'
312 get_init_status
"$name"
317 json_get_var name
'name'
319 get_platform_support
"$name"
324 json_get_var name
'name'
325 json_get_var action
'action'
327 set_init_action
"$name" "$action"