2 # Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2039,SC3043
5 # TechRef: https://openwrt.org/docs/techref/rpcd
7 # ubus -v list luci.https-dns-proxy
8 # ubus -S call luci.https-dns-proxy getInitList '{"name": "https-dns-proxy" }'
9 # ubus -S call luci.https-dns-proxy getInitStatus '{"name": "https-dns-proxy" }'
10 # ubus -S call luci.https-dns-proxy getPlatformSupport '{"name": "https-dns-proxy" }'
11 # ubus -S call luci.https-dns-proxy getProviders '{"name": "https-dns-proxy" }'
12 # ubus -S call luci.https-dns-proxy getRuntime '{"name": "https-dns-proxy" }'
14 readonly packageName
="https-dns-proxy"
15 readonly providersDir
="/usr/share/${packageName}/providers"
18 .
/usr
/share
/libubox
/jshn.sh
20 is_enabled
() { "/etc/init.d/${1}" enabled
; }
21 is_running
() { [ "$(ubus call service list "{ 'name': '$1' }" | jsonfilter -q -e "@
['$1'].instances
[*].running
" | uniq)" = 'true' ]; }
22 get_version
() { /usr
/sbin
/https-dns-proxy
-V; }
23 check_http2
() { curl
--version |
grep -q 'nghttp2'; }
24 check_http3
() { curl
--version |
grep -q 'nghttp3'; }
25 ubus_get_ports
() { ubus call service list
"{ 'name': '$packageName' }" | jsonfilter
-e "@['${packageName}'].instances[*].data.firewall.*.dest_port"; }
26 logger
() { /usr
/bin
/logger
-t "$packageName" "$@"; }
27 print_json_bool
() { json_init
; json_add_boolean
"$1" "$2"; json_dump
; json_cleanup
; }
32 json_add_object
"$name"
33 if is_enabled
"$name"; then
34 json_add_boolean
'enabled' '1'
36 json_add_boolean
'enabled' '0'
38 if is_running
"$name"; then
39 json_add_boolean
'running' '1'
41 json_add_boolean
'running' '0'
52 name
="$(basename "$1")"
53 name
="${name:-$packageName}"
54 ports
="$(ubus_get_ports)"
55 [ -z "$version" ] && version
="$(get_version "$name")"
57 json_add_object
"$name"
58 if is_enabled
"$name"; then
59 json_add_boolean
'enabled' '1'
61 json_add_boolean
'enabled' '0'
63 if is_running
"$name"; then
64 json_add_boolean
'running' '1'
66 json_add_boolean
'running' '0'
68 if [ -n "$ports" ]; then
69 json_add_boolean
'force_dns_active' '1'
70 json_add_array
'force_dns_ports'
71 for i
in $ports; do json_add_int
'' "$i"; done
74 json_add_boolean
'force_dns_active' '0'
76 json_add_string
'version' "$version"
83 get_platform_support
() {
85 name
="$(basename "$1")"
86 name
="${name:-$packageName}"
88 json_add_object
"$name"
90 json_add_boolean
'http2_support' '1'
92 json_add_boolean
'http2_support' '0'
95 json_add_boolean
'http3_support' '1'
97 json_add_boolean
'http3_support' '0'
106 echo '{"https-dns-proxy":['
107 for f
in "$providersDir"/*; do
111 # echo '{ "title": "Custom", "template": "{option}", "params": { "option": { "type": "text", }, }, },'
115 get_runtime
() { ubus call service list
"{ 'verbose': true, 'name': '$1' }"; }
118 local name
="$1" action
="$2" cmd
120 enable|disable|start|stop|restart
)
121 cmd
="/etc/init.d/${name} ${action}"
124 if [ -n "$cmd" ] && eval "$cmd" >/dev
/null
2>&1; then
125 print_json_bool
"result" '1'
127 print_json_bool
"result" '0'
134 json_add_object
"getInitList"
135 json_add_string
'name' "name"
137 json_add_object
"getInitStatus"
138 json_add_string
'name' 'name'
140 json_add_object
"getPlatformSupport"
141 json_add_string
'name' 'name'
143 json_add_object
"getProviders"
144 json_add_string
'name' "name"
146 json_add_object
"getRuntime"
147 json_add_string
'name' "name"
149 json_add_object
"setInitAction"
150 json_add_string
'name' "name"
151 json_add_string
'action' "action"
161 json_get_var name
"name"
163 get_init_list
"$name"
168 json_get_var name
'name'
170 get_init_status
"$name"
175 json_get_var name
'name'
177 get_platform_support
"$name"
182 json_get_var name
"name"
184 get_providers
"$name"
189 json_get_var name
"name"
196 json_get_var name
"name"
197 json_get_var action
"action"
199 set_init_action
"$name" "$action"