summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-https-dns-proxy/root/usr/libexec/rpcd/luci.https-dns-proxy
blob: 7c59f90c38ba81e3422114d3ed51c588b5de7388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/sh
# Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
# shellcheck disable=SC1091,SC2039,SC3043

# TechRef: https://openwrt.org/docs/techref/rpcd

# ubus -v list luci.https-dns-proxy
# ubus -S call luci.https-dns-proxy getInitList '{"name": "https-dns-proxy" }'
# ubus -S call luci.https-dns-proxy getInitStatus '{"name": "https-dns-proxy" }'
# ubus -S call luci.https-dns-proxy getPlatformSupport '{"name": "https-dns-proxy" }'
# ubus -S call luci.https-dns-proxy getProviders '{"name": "https-dns-proxy" }'
# ubus -S call luci.https-dns-proxy getRuntime '{"name": "https-dns-proxy" }'

readonly packageName="https-dns-proxy"
readonly providersDir="/usr/share/${packageName}/providers"

. /lib/functions.sh
. /usr/share/libubox/jshn.sh

is_enabled() { "/etc/init.d/${1}" enabled; }
is_running() { [ "$(ubus call service list "{ 'name': '$1' }" | jsonfilter -q -e "@['$1'].instances[*].running" | uniq)" = 'true' ]; }
get_version() { grep -m1 -A2 -w "^Package: $1$" /usr/lib/opkg/status | sed -n 's/Version: //p'; }
check_http2() { curl --version | grep -q 'nghttp2'; }
check_http3() { curl --version | grep -q 'nghttp3'; }
ubus_get_ports() { ubus call service list "{ 'name': '$packageName' }" | jsonfilter -e "@['${packageName}'].instances[*].data.firewall.*.dest_port"; }
logger() { /usr/bin/logger -t "$packageName" "$@"; }
print_json_bool() { json_init; json_add_boolean "$1" "$2"; json_dump; json_cleanup; }

get_init_list() {
	local name="$1"
	json_init
	json_add_object "$name"
	if is_enabled "$name"; then
		json_add_boolean 'enabled' '1'
	else
		json_add_boolean 'enabled' '0'
	fi
	if is_running "$name"; then
		json_add_boolean 'running' '1'
	else
		json_add_boolean 'running' '0'
	fi
	json_close_object
	json_dump
	json_cleanup
}

get_init_status() {
	local name
	local i ports
	local version
	name="$(basename "$1")"
	name="${name:-$packageName}" 
	ports="$(ubus_get_ports)"
	[ -z "$version" ] && version="$(get_version "$name")"
	json_init
	json_add_object  "$name"
	if is_enabled "$name"; then
		json_add_boolean 'enabled' '1'
	else
		json_add_boolean 'enabled' '0'
	fi
	if is_running "$name"; then
		json_add_boolean 'running' '1'
	else
		json_add_boolean 'running' '0'
	fi
	if [ -n "$ports" ]; then
		json_add_boolean 'force_dns_active' '1'
		json_add_array 'force_dns_ports'
			for i in $ports; do json_add_int '' "$i"; done
		json_close_array
	else
		json_add_boolean 'force_dns_active' '0'
	fi
	json_add_string 'version' "$version"
	json_close_array
	json_close_object
	json_dump
	json_cleanup
}

get_platform_support() {
	local name
	name="$(basename "$1")"
	name="${name:-$packageName}" 
	json_init
	json_add_object "$name"
	if check_http2; then
		json_add_boolean 'http2_support' '1'
	else
		json_add_boolean 'http2_support' '0'
	fi
	if check_http3; then
		json_add_boolean 'http3_support' '1'
	else
		json_add_boolean 'http3_support' '0'
	fi
	json_close_object
	json_dump
	json_cleanup
}

get_providers() {
	local f
	echo '{"https-dns-proxy":['
	for f in "$providersDir"/*; do
		cat "$f"
		echo ','
	done
#	echo '{ "title": "Custom", "template": "{option}", "params": { "option": { "type": "text", }, }, },'
	echo ']}'
}

get_runtime() { ubus call service list "{ 'verbose': true, 'name': '$1' }"; }

set_init_action() {
	local name="$1" action="$2" cmd
	case $action in
		enable)
			cmd="/etc/init.d/${name} enable";;
		disable)
			cmd="/etc/init.d/${name} disable";;
		start|stop|restart)
			cmd="/etc/init.d/${name} ${action}";;
	esac
	if [ -n "$cmd" ] && eval "${cmd}" >/dev/null 2>&1; then
		print_json_bool "result" '1'
	else
		print_json_bool "result" '0'
	fi
}

case "$1" in
	list)
		json_init
		json_add_object "getInitList"
			json_add_string 'name' "name"
		json_close_object
		json_add_object "getInitStatus"
			json_add_string 'name' 'name'
		json_close_object
		json_add_object "getPlatformSupport"
			json_add_string 'name' 'name'
		json_close_object
		json_add_object "getProviders"
			json_add_string 'name' "name"
		json_close_object
		json_add_object "getRuntime"
			json_add_string 'name' "name"
		json_close_object
		json_add_object "setInitAction"
			json_add_string 'name' "name"
			json_add_string 'action' "action"
		json_close_object
		json_dump
		json_cleanup
		;;
	call)
		case "$2" in
			getInitList)
				read -r input
				json_load "$input"
				json_get_var name "name"
				json_cleanup
				get_init_list "$name"
				;;
			getInitStatus)
				read -r input
				json_load "$input"
				json_get_var name 'name'
				json_cleanup
				get_init_status "$name"
				;;
			getPlatformSupport)
				read -r input
				json_load "$input"
				json_get_var name 'name'
				json_cleanup
				get_platform_support "$name"
				;;
			getProviders)
				read -r input
				json_load "$input"
				json_get_var name "name"
				json_cleanup
				get_providers "$name"
				;;
			getRuntime)
				read -r input
				json_load "$input"
				json_get_var name "name"
				json_cleanup
				get_runtime "$name"
				;;
			setInitAction)
				read -r input
				json_load "$input"
				json_get_var name "name"
				json_get_var action "action"
				json_cleanup
				set_init_action "$name" "$action"
				;;
		esac
	;;
esac