luci-app-https-dns-proxy: prepare migration to APK
[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() { /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; }
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|disable|start|stop|restart)
121 cmd="/etc/init.d/${name} ${action}"
122 ;;
123 esac
124 if [ -n "$cmd" ] && eval "$cmd" >/dev/null 2>&1; then
125 print_json_bool "result" '1'
126 else
127 print_json_bool "result" '0'
128 fi
129 }
130
131 case "$1" in
132 list)
133 json_init
134 json_add_object "getInitList"
135 json_add_string 'name' "name"
136 json_close_object
137 json_add_object "getInitStatus"
138 json_add_string 'name' 'name'
139 json_close_object
140 json_add_object "getPlatformSupport"
141 json_add_string 'name' 'name'
142 json_close_object
143 json_add_object "getProviders"
144 json_add_string 'name' "name"
145 json_close_object
146 json_add_object "getRuntime"
147 json_add_string 'name' "name"
148 json_close_object
149 json_add_object "setInitAction"
150 json_add_string 'name' "name"
151 json_add_string 'action' "action"
152 json_close_object
153 json_dump
154 json_cleanup
155 ;;
156 call)
157 case "$2" in
158 getInitList)
159 read -r input
160 json_load "$input"
161 json_get_var name "name"
162 json_cleanup
163 get_init_list "$name"
164 ;;
165 getInitStatus)
166 read -r input
167 json_load "$input"
168 json_get_var name 'name'
169 json_cleanup
170 get_init_status "$name"
171 ;;
172 getPlatformSupport)
173 read -r input
174 json_load "$input"
175 json_get_var name 'name'
176 json_cleanup
177 get_platform_support "$name"
178 ;;
179 getProviders)
180 read -r input
181 json_load "$input"
182 json_get_var name "name"
183 json_cleanup
184 get_providers "$name"
185 ;;
186 getRuntime)
187 read -r input
188 json_load "$input"
189 json_get_var name "name"
190 json_cleanup
191 get_runtime "$name"
192 ;;
193 setInitAction)
194 read -r input
195 json_load "$input"
196 json_get_var name "name"
197 json_get_var action "action"
198 json_cleanup
199 set_init_action "$name" "$action"
200 ;;
201 esac
202 ;;
203 esac