2 # Copyright 2023 MOSSDeF, 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.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" }'
18 .
/lib
/functions
/network.sh
19 .
/usr
/share
/libubox
/jshn.sh
21 readonly packageName
="adblock-fast"
22 readonly dnsmasqAddnhostsFile
="/var/run/${packageName}/dnsmasq.addnhosts"
23 readonly dnsmasqAddnhostsCache
="/var/run/${packageName}/dnsmasq.addnhosts.cache"
24 readonly dnsmasqAddnhostsGzip
="${packageName}.dnsmasq.addnhosts.gz"
25 readonly dnsmasqConfFile
="/tmp/dnsmasq.d/${packageName}"
26 readonly dnsmasqConfCache
="/var/run/${packageName}/dnsmasq.conf.cache"
27 readonly dnsmasqConfGzip
="${packageName}.dnsmasq.conf.gz"
28 readonly dnsmasqIpsetFile
="/tmp/dnsmasq.d/${packageName}.ipset"
29 readonly dnsmasqIpsetCache
="/var/run/${packageName}/dnsmasq.ipset.cache"
30 readonly dnsmasqIpsetGzip
="${packageName}.dnsmasq.ipset.gz"
31 readonly dnsmasqNftsetFile
="/tmp/dnsmasq.d/${packageName}.nftset"
32 readonly dnsmasqNftsetCache
="/var/run/${packageName}/dnsmasq.nftset.cache"
33 readonly dnsmasqNftsetGzip
="${packageName}.dnsmasq.nftset.gz"
34 readonly dnsmasqServersFile
="/var/run/${packageName}/dnsmasq.servers"
35 readonly dnsmasqServersCache
="/var/run/${packageName}/dnsmasq.servers.cache"
36 readonly dnsmasqServersGzip
="${packageName}.dnsmasq.servers.gz"
37 readonly unboundFile
="/var/lib/unbound/adb_list.${packageName}"
38 readonly unboundCache
="/var/run/${packageName}/unbound.cache"
39 readonly unboundGzip
="${packageName}.unbound.gz"
40 readonly jsonFile
="/dev/shm/$packageName-status.json"
42 str_contains
() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
43 str_contains_word
() { echo "$1" |
grep -q -w "$2"; }
44 str_to_lower
() { echo "$1" |
tr 'A-Z' 'a-z'; }
45 str_to_upper
() { echo "$1" |
tr 'a-z' 'A-Z'; }
46 is_enabled
() { uci
-q get
"${1}.config.enabled"; }
47 get_version
() { grep -m1 -A2 -w "^Package: $1$" /usr
/lib
/opkg
/status |
sed -n 's/Version: //p'; }
48 print_json_bool
() { json_init
; json_add_boolean
"$1" "$2"; json_dump
; json_cleanup
; }
49 print_json_int
() { json_init
; json_add_int
"$1" "$2"; json_dump
; json_cleanup
; }
50 print_json_string
() { json_init
; json_add_string
"$1" "$2"; json_dump
; json_cleanup
; }
51 logger
() { /usr
/bin
/logger
-t "$packageName" "$@"; }
52 ubus_get_status
() { ubus call service list
"{ 'name': '$packageName' }" | jsonfilter
-e "@['${packageName}'].instances.main.data.${1}"; }
53 ubus_get_ports() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@
['${packageName}'].instances.main.data.firewall.
*.dest_port
"; }
54 is_present() { command -v "$1" >/dev/null 2>&1; }
55 sanitize_dir() { [ -d "$
(readlink
-fn "$1")" ] && readlink -fn "$1"; }
57 # shellcheck disable=SC2034
58 local action="$1" param="$2" value="$3" i
59 if [ -s "$jsonFile" ]; then
60 json_load_file "$jsonFile" 2>/dev/null
61 json_select 'data' 2>/dev/null
62 for i in status message error stats reload restart; do
63 json_get_var $i "$i" 2>/dev/null
70 printf "%b
" "$
(eval echo "\$$param")"; return;;
77 local url="$1" size size_command
78 [ -n "$url" ] || { print_json_int 'size' '0'; return 0; }
79 is_present 'curl' || { print_json_int 'size' '0'; return 0; }
80 size_command='curl --silent --insecure --fail --head --request GET'
81 size="$
($size_command "$url" |
grep -i 'content-length:' |
awk '{print $2}'; )"
85 _get_file_url_size() {
87 config_get url "$1" 'url'
88 config_get size "$1" 'size'
89 [ -n "$size" ] || size="$
(get_url_filesize
"$url")"
91 json_add_string 'url' "$url"
92 json_add_int 'size' "$size"
96 get_file_url_filesizes() {
99 json_add_object "$name"
100 json_add_array 'sizes'
102 config_foreach _get_file_url_size 'file_url'
111 name="$
(basename "$1")"
112 name="${name:-$packageName}"
114 json_add_object "$name"
115 json_add_boolean 'enabled' "$
(is_enabled
"$name")"
116 if is_running "$name"; then
117 json_add_boolean 'running' '1'
119 json_add_boolean 'running' '0'
127 local name action="$2" cmd
128 name="$
(basename "$1")"
129 name="${name:-$packageName}"
130 if [ ! -f "/etc
/init.d
/$name" ]; then
131 print_json_string 'error' 'Init script not found!'
136 cmd="uci
-q set ${name}.config.enabled
=1 && uci commit
$name";;
138 cmd="uci
-q set ${name}.config.enabled
=0 && uci commit
$name";;
139 start|stop|reload|restart|dl|pause)
140 cmd="/etc
/init.d
/${name} ${action}";;
142 if [ -n "$cmd" ] && eval "${cmd}" >/dev/null 2>&1; then
143 print_json_bool "result
" '1'
145 print_json_bool "result
" '0'
151 name="$
(basename "$1")"
152 name="${name:-$packageName}"
153 local errors warnings ports dns outputFile outputCache outputGzip
155 # shellcheck disable=SC2034
156 local compressed_cache_dir
158 config_get compressed_cache_dir 'config' 'compressed_cache_dir' '/etc'
159 if [ -n "$
(sanitize_dir
"$compressed_cache_dir")" ]; then
160 compressed_cache_dir="$
(sanitize_dir
"$compressed_cache_dir")"
162 compressed_cache_dir="/etc
"
164 if [ -n "$
(uci
-q get
$packageName.config.dnsmasq_config_file_url
)" ]; then
167 dns="$
(uci
-q get
$packageName.config.dns
)"
171 outputFile="$dnsmasqAddnhostsFile"
172 outputCache="$dnsmasqAddnhostsCache"
173 outputGzip="${compressed_cache_dir}/${dnsmasqAddnhostsGzip}"
176 outputFile="$dnsmasqConfFile"
177 outputCache="$dnsmasqConfCache"
178 outputGzip="${compressed_cache_dir}/${dnsmasqConfGzip}"
181 outputFile="$dnsmasqIpsetFile"
182 outputCache="$dnsmasqIpsetCache"
183 outputGzip="${compressed_cache_dir}/${dnsmasqIpsetGzip}"
186 outputFile="$dnsmasqNftsetFile"
187 outputCache="$dnsmasqNftsetCache"
188 outputGzip="${compressed_cache_dir}/${dnsmasqNftsetGzip}"
191 outputFile="$dnsmasqServersFile"
192 outputCache="$dnsmasqServersCache"
193 outputGzip="${compressed_cache_dir}/${dnsmasqServersGzip}"
196 outputFile="$unboundFile"
197 outputCache="$unboundCache"
198 outputGzip="${compressed_cache_dir}/${unboundGzip}"
202 json_add_object "$name"
203 json_add_boolean 'enabled' "$
(is_enabled
"$name")"
204 i="$
(json
'get' 'status')"
205 j="$
(ubus_get_status
'status')"
206 if [ "$i" = 'statusSuccess' ] && [ "$i" != "$j" ]; then
209 json_add_string 'status' "$i"
210 if [ "$i" = 'statusSuccess' ]; then
211 json_add_boolean 'running' '1'
213 json_add_boolean 'running' '0'
215 json_add_string 'version' "$
(get_version
"$name")"
216 errors="$
(ubus_get_status errors
)"
217 json_add_array 'errors'
218 if [ -n "$errors" ]; then
220 if str_contains "$i" '|'; then
221 error_extra="${i##*|}"
228 json_add_string 'id' "$error_id"
229 json_add_string 'extra' "$error_extra"
234 warnings="$
(ubus_get_status warnings
)"
235 json_add_array 'warnings'
236 if [ -n "$warnings" ]; then
237 for i in $warnings; do
238 if str_contains "$i" '|'; then
239 error_extra="${i##*|}"
246 json_add_string 'id' "$error_id"
247 json_add_string 'extra' "$error_extra"
253 ports="$
(ubus_get_ports
)"
254 if [ -n "$ports" ]; then
255 json_add_boolean 'force_dns_active' '1'
256 json_add_array 'force_dns_ports'
257 for i in $ports; do json_add_int '' "$i"; done
260 json_add_boolean 'force_dns_active' '0'
262 json_add_int 'entries' "$
(ubus_get_status entries
)"
263 json_add_string 'dns' "$dns"
264 json_add_string 'outputFile' "$outputFile"
265 json_add_string 'outputCache' "$outputCache"
266 json_add_string 'outputGzip' "$outputGzip"
267 if [ -s "$outputFile" ]; then
268 json_add_boolean 'outputFileExists' '1'
270 json_add_boolean 'outputFileExists' '0'
272 if [ -s "$outputCache" ]; then
273 json_add_boolean 'outputCacheExists' '1'
275 json_add_boolean 'outputCacheExists' '0'
277 if [ -s "$outputGzip" ]; then
278 json_add_boolean 'outputGzipExists' '1'
280 json_add_boolean 'outputGzipExists' '0'
282 json_add_array 'leds'
283 for i in /sys/class/leds/*; do json_add_string '' "$
(basename "$i")"; done
290 check_ipset() { { command -v ipset && /usr/sbin/ipset help hash:net; } >/dev/null 2>&1; }
291 check_nft() { command -v nft >/dev/null 2>&1; }
292 check_dnsmasq() { command -v dnsmasq >/dev/null 2>&1; }
293 check_unbound() { command -v unbound >/dev/null 2>&1; }
294 check_dnsmasq_ipset() {
296 check_dnsmasq || return 1
297 o="$
(dnsmasq
-v 2>/dev
/null
)"
298 check_ipset && ! echo "$o" | grep -q 'no-ipset' && echo "$o" | grep -q 'ipset'
300 check_dnsmasq_nftset() {
302 check_dnsmasq || return 1
303 o="$
(dnsmasq
-v 2>/dev
/null
)"
304 check_nft && ! echo "$o" | grep -q 'no-nftset' && echo "$o" | grep -q 'nftset'
307 get_platform_support() {
309 name="$
(basename "$1")"
310 name="${name:-$packageName}"
312 json_add_object "$name"
314 json_add_boolean 'ipset_installed' '1'
316 json_add_boolean 'ipset_installed' '0'
319 json_add_boolean 'nft_installed' '1'
321 json_add_boolean 'nft_installed' '0'
323 if check_dnsmasq; then
324 json_add_boolean 'dnsmasq_installed' '1'
326 json_add_boolean 'dnsmasq_installed' '0'
328 if check_unbound; then
329 json_add_boolean 'unbound_installed' '1'
331 json_add_boolean 'unbound_installed' '0'
333 if check_dnsmasq_ipset; then
334 json_add_boolean 'dnsmasq_ipset_support' '1'
336 json_add_boolean 'dnsmasq_ipset_support' '0'
338 if check_dnsmasq_nftset; then
339 json_add_boolean 'dnsmasq_nftset_support' '1'
341 json_add_boolean 'dnsmasq_nftset_support' '0'
343 json_add_array 'leds'
344 for i in /sys/class/leds/*; do json_add_string '' "$
(basename "$i")"; done
354 json_add_object "getFileUrlFilesizes
"
355 json_add_string 'name' 'name'
357 json_add_object "getInitList
"
358 json_add_string 'name' 'name'
360 json_add_object "getInitStatus
"
361 json_add_string 'name' 'name'
363 json_add_object "getPlatformSupport
"
364 json_add_string 'name' 'name'
366 json_add_object "setInitAction
"
367 json_add_string 'name' 'name'
368 json_add_string 'action' 'action'
378 json_get_var name 'name'
380 get_file_url_filesizes "$name"
385 json_get_var name 'name'
387 get_init_list "$name"
392 json_get_var name 'name'
394 get_init_status "$name"
399 json_get_var name 'name'
401 get_platform_support "$name"
406 json_get_var name 'name'
407 json_get_var action 'action'
409 set_init_action "$name" "$action"