2 # Copyright 2022 Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2018,SC2019,SC2039,SC3043,SC3057,SC3060
5 # TechRef: https://openwrt.org/docs/techref/rpcd
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" }'
13 .
/lib
/functions
/network.sh
14 .
/usr
/share
/libubox
/jshn.sh
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"
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
"; }
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
62 printf "%b
" "$
(eval echo "\$$param")"; return;;
70 name="$
(basename "$1")"
71 name="${name:-$packageName}"
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'
78 json_add_boolean 'running' '0'
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!'
95 cmd="uci
-q set ${name}.config.enabled
=1 && uci commit
$name";;
97 cmd="uci
-q set ${name}.config.enabled
=0 && uci commit
$name";;
98 start|stop|reload|restart)
99 cmd="/etc
/init.d
/${name} ${action}";;
101 if [ -n "$cmd" ] && eval "${cmd}" 1>/dev/null 2>&1; then
102 print_json_bool "result
" '1'
104 print_json_bool "result
" '0'
110 name="$
(basename "$1")"
111 name="${name:-$packageName}"
112 local errors ports dns outputFile outputCache outputGzip
114 errors="$
(ubus_get_status errors
)"
115 ports="$
(ubus_get_ports
)"
116 dns="$
(uci
-q get
$packageName.config.dns
)"
119 outputFile="$dnsmasqAddnhostsFile"
120 outputCache="$dnsmasqAddnhostsCache"
121 outputGzip="$dnsmasqAddnhostsGzip"
124 outputFile="$dnsmasqConfFile"
125 outputCache="$dnsmasqConfCache"
126 outputGzip="$dnsmasqConfGzip"
129 outputFile="$dnsmasqIpsetFile"
130 outputCache="$dnsmasqIpsetCache"
131 outputGzip="$dnsmasqIpsetGzip"
134 outputFile="$dnsmasqNftsetFile"
135 outputCache="$dnsmasqNftsetCache"
136 outputGzip="$dnsmasqNftsetGzip"
139 outputFile="$dnsmasqServersFile"
140 outputCache="$dnsmasqServersCache"
141 outputGzip="$dnsmasqServersGzip"
144 outputFile="$unboundFile"
145 outputCache="$unboundCache"
146 outputGzip="$unboundGzip"
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'
157 json_add_boolean 'running' '0'
159 json_add_string 'version' "$
(get_version
"$name")"
160 json_add_array 'errors'
161 for i in $errors; do json_add_string '' "$i"; done
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
169 json_add_boolean 'force_dns_active' '0'
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'
179 json_add_boolean 'outputFileExists' '0'
181 if [ -s "$outputCache" ]; then
182 json_add_boolean 'outputCacheExists' '1'
184 json_add_boolean 'outputCacheExists' '0'
186 if [ -s "$outputGzip" ]; then
187 json_add_boolean 'outputGzipExists' '1'
189 json_add_boolean 'outputGzipExists' '0'
191 json_add_array 'leds'
192 for i in /sys/class/leds/*; do json_add_string '' "$
(basename "$i")"; done
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() {
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'
209 check_dnsmasq_nftset() {
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'
216 get_platform_support() {
218 name="$
(basename "$1")"
219 name="${name:-$packageName}"
221 json_add_object "$name"
223 json_add_boolean 'ipset_installed' '1'
225 json_add_boolean 'ipset_installed' '0'
228 json_add_boolean 'nft_installed' '1'
230 json_add_boolean 'nft_installed' '0'
232 if check_dnsmasq; then
233 json_add_boolean 'dnsmasq_installed' '1'
235 json_add_boolean 'dnsmasq_installed' '0'
237 if check_unbound; then
238 json_add_boolean 'unbound_installed' '1'
240 json_add_boolean 'unbound_installed' '0'
242 if check_dnsmasq_ipset; then
243 json_add_boolean 'dnsmasq_ipset_support' '1'
245 json_add_boolean 'dnsmasq_ipset_support' '0'
247 if check_dnsmasq_nftset; then
248 json_add_boolean 'dnsmasq_nftset_support' '1'
250 json_add_boolean 'dnsmasq_nftset_support' '0'
252 json_add_array 'leds'
253 for i in /sys/class/leds/*; do json_add_string '' "$
(basename "$i")"; done
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_init_list "$name"
291 json_get_var name 'name'
293 get_init_status "$name"
298 json_get_var name 'name'
300 get_platform_support "$name"
305 json_get_var name 'name'
306 json_get_var action 'action'
308 set_init_action "$name" "$action"