luci-app-https-dns-proxy: rewrite in javascript
[project/luci.git] / applications / luci-app-https-dns-proxy / root / usr / libexec / rpcd / luci.https-dns-proxy
1 #!/bin/sh
2 # Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2039,SC3043
4
5 # TechRef: https://openwrt.org/docs/techref/rpcd
6
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" }'
13
14 readonly packageName="https-dns-proxy"
15 readonly providersDir="/usr/share/${packageName}/providers"
16
17 . /lib/functions.sh
18 . /usr/share/libubox/jshn.sh
19
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() { grep -m1 -A2 -w "^Package: $1$" /usr/lib/opkg/status | sed -n 's/Version: //p'; }
23 check_http2() { grep -q 'Provides: libnghttp2' /usr/lib/opkg/status; }
24 check_http3() { grep -q 'Provides: libnghttp3' /usr/lib/opkg/status; }
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; }
28
29 get_init_list() {
30 local name="$1"
31 json_init
32 json_add_object "$name"
33 if is_enabled "$name"; then
34 json_add_boolean 'enabled' '1'
35 else
36 json_add_boolean 'enabled' '0'
37 fi
38 if is_running "$name"; then
39 json_add_boolean 'running' '1'
40 else
41 json_add_boolean 'running' '0'
42 fi
43 json_close_object
44 json_dump
45 json_cleanup
46 }
47
48 get_init_status() {
49 local name
50 local i ports
51 local version
52 name="$(basename "$1")"
53 name="${name:-$packageName}"
54 ports="$(ubus_get_ports)"
55 [ -z "$version" ] && version="$(get_version "$name")"
56 json_init
57 json_add_object "$name"
58 if is_enabled "$name"; then
59 json_add_boolean 'enabled' '1'
60 else
61 json_add_boolean 'enabled' '0'
62 fi
63 if is_running "$name"; then
64 json_add_boolean 'running' '1'
65 else
66 json_add_boolean 'running' '0'
67 fi
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
72 json_close_array
73 else
74 json_add_boolean 'force_dns_active' '0'
75 fi
76 json_add_string 'version' "$version"
77 json_close_array
78 json_close_object
79 json_dump
80 json_cleanup
81 }
82
83 get_platform_support() {
84 local name
85 name="$(basename "$1")"
86 name="${name:-$packageName}"
87 json_init
88 json_add_object "$name"
89 if check_http2; then
90 json_add_boolean 'http2_support' '1'
91 else
92 json_add_boolean 'http2_support' '0'
93 fi
94 if check_http3; then
95 json_add_boolean 'http3_support' '1'
96 else
97 json_add_boolean 'http3_support' '0'
98 fi
99 json_close_object
100 json_dump
101 json_cleanup
102 }
103
104 get_providers() {
105 local f
106 echo '{"https-dns-proxy":['
107 for f in "$providersDir"/*; do
108 cat "$f"
109 echo ','
110 done
111 # echo '{ "title": "Custom", "template": "{option}", "params": { "option": { "type": "text", }, }, },'
112 echo ']}'
113 }
114
115 get_runtime() { ubus call service list "{ 'verbose': true, 'name': '$1' }"; }
116
117 set_init_action() {
118 local name="$1" action="$2" cmd
119 case $action in
120 enable)
121 cmd="/etc/init.d/${name} enable";;
122 disable)
123 cmd="/etc/init.d/${name} disable";;
124 start|stop|restart)
125 cmd="/etc/init.d/${name} ${action}";;
126 esac
127 if [ -n "$cmd" ] && eval "${cmd}" >/dev/null 2>&1; then
128 print_json_bool "result" '1'
129 else
130 print_json_bool "result" '0'
131 fi
132 }
133
134 case "$1" in
135 list)
136 json_init
137 json_add_object "getInitList"
138 json_add_string 'name' "name"
139 json_close_object
140 json_add_object "getInitStatus"
141 json_add_string 'name' 'name'
142 json_close_object
143 json_add_object "getPlatformSupport"
144 json_add_string 'name' 'name'
145 json_close_object
146 json_add_object "getProviders"
147 json_add_string 'name' "name"
148 json_close_object
149 json_add_object "getRuntime"
150 json_add_string 'name' "name"
151 json_close_object
152 json_add_object "setInitAction"
153 json_add_string 'name' "name"
154 json_add_string 'action' "action"
155 json_close_object
156 json_dump
157 json_cleanup
158 ;;
159 call)
160 case "$2" in
161 getInitList)
162 read -r input
163 json_load "$input"
164 json_get_var name "name"
165 json_cleanup
166 get_init_list "$name"
167 ;;
168 getInitStatus)
169 read -r input
170 json_load "$input"
171 json_get_var name 'name'
172 json_cleanup
173 get_init_status "$name"
174 ;;
175 getPlatformSupport)
176 read -r input
177 json_load "$input"
178 json_get_var name 'name'
179 json_cleanup
180 get_platform_support "$name"
181 ;;
182 getProviders)
183 read -r input
184 json_load "$input"
185 json_get_var name "name"
186 json_cleanup
187 get_providers "$name"
188 ;;
189 getRuntime)
190 read -r input
191 json_load "$input"
192 json_get_var name "name"
193 json_cleanup
194 get_runtime "$name"
195 ;;
196 setInitAction)
197 read -r input
198 json_load "$input"
199 json_get_var name "name"
200 json_get_var action "action"
201 json_cleanup
202 set_init_action "$name" "$action"
203 ;;
204 esac
205 ;;
206 esac