1 #!/bin/sh /etc/rc.common
2 # Copyright 2019-2022 Stan Grishin (stangri@melmac.ca)
3 # shellcheck disable=SC1091,SC2039,SC3043,SC3060
6 # shellcheck disable=SC2034
8 # shellcheck disable=SC2034
11 if type extra_command
1>/dev
/null
2>&1; then
12 extra_command
'version' 'Show version information'
14 # shellcheck disable=SC2034
15 EXTRA_COMMANDS
='version'
18 readonly packageName
='https-dns-proxy'
19 readonly serviceName
="$packageName $PKG_VERSION"
20 readonly sharedMemoryOutput
="/dev/shm/$packageName-output"
21 readonly _OK_
='\033[0;32m\xe2\x9c\x93\033[0m'
22 readonly _FAIL_
='\033[0;31m\xe2\x9c\x97\033[0m'
23 readonly PROG
=/usr
/sbin
/https-dns-proxy
24 readonly DEFAULT_BOOTSTRAP
='1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001,8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844'
25 readonly canaryDomainsMozilla
='use-application-dns.net'
26 readonly canaryDomainsiCloud
='mask.icloud.com mask-h2.icloud.com'
28 canary_domains_icloud
=
29 canary_domains_mozilla
=
30 update_dnsmasq_config
=
34 str_contains
() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
35 is_mac_address
() { expr "$1" : '[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]$' >/dev
/null
; }
36 is_ipv4
() { expr "$1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev
/null
; }
37 is_ipv6
() { ! is_mac_address
"$1" && str_contains
"$1" ":"; }
39 local msg memmsg logmsg
40 [ -t 1 ] && printf "%b" "$@"
41 msg
="${1//$serviceName /service }";
42 if [ "$(printf "%b
" "$msg" | wc -l)" -gt 0 ]; then
43 [ -s "$sharedMemoryOutput" ] && memmsg
="$(cat "$sharedMemoryOutput")"
44 logmsg
="$(printf "%b
" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
45 logger
-t "$packageName" "$(printf "%b
" "$logmsg")"
46 rm -f "$sharedMemoryOutput"
48 printf "%b" "$msg" >> "$sharedMemoryOutput"
51 output_ok
() { output
"$_OK_"; }
52 output_okn
() { output
"${_OK_}\\n"; }
53 output_fail
() { output
"$_FAIL_"; }
54 output_failn
() { output
"${_FAIL_}\\n"; }
55 uci_add_list_if_new
() {
61 [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] ||
return 1
62 for i
in $
(uci_get
"$PACKAGE" "$CONFIG" "$OPTION"); do
63 [ "$i" = "$VALUE" ] && return 0
65 uci_add_list
"$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
71 /sbin
/uci
${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
74 dnsmasq_restart() { [ -x /etc/init.d/dnsmasq ] || return 0; /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
76 version() { echo "$PKG_VERSION"; }
78 xappend() { param="$param $1"; }
84 local default="${4:-0}"
86 config_get_bool _loctmp "$section" "$option" "$default"
87 [ "$_loctmp" -ne 0 ] && xappend "$value"
96 config_get _loctmp "$section" "$option" "$default"
97 [ -n "$_loctmp" ] && xappend "$switch $_loctmp"
104 local default="${4:-0}"
106 config_get _loctmp "$section" "$option" "$default"
107 # shellcheck disable=SC2086,SC2154
108 for i in $(seq 1 $_loctmp); do
118 local _old_ifs="$IFS"
119 local _loctmp _newtmp i
120 config_get _loctmp "$section" "$option" "$default"
121 [ -z "$_loctmp" ] && return 0
123 for i in $_loctmp; do
124 if { [ "$ipv6_resolvers_only" -eq 0 ] && is_ipv4 "$i"; } || \
125 { [ "$ipv6_resolvers_only" -ne 0 ] && is_ipv6 "$i"; }; then
126 [ -z "$_newtmp" ] && _newtmp="$i" || _newtmp="${_newtmp},${i}"
130 [ -n "$_newtmp" ] && xappend "$switch $_newtmp"
131 [ "$ipv6_resolvers_only" -eq 0 ] && xappend '-4'
135 local cfg="$1" param listen_addr listen_port ipv6_resolvers_only p
136 config_get_bool ipv6_resolvers_only "$cfg" 'use_ipv6_resolvers_only' '0'
137 append_parm "$cfg" 'resolver_url' '-r'
138 append_parm "$cfg" 'polling_interval' '-i'
139 append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
140 append_parm "$cfg" 'listen_port' '-p' "$port"
141 append_parm "$cfg" 'dscp_codepoint' '-c'
142 append_bootstrap "$cfg" 'bootstrap_dns' '-b' "$DEFAULT_BOOTSTRAP"
143 append_parm "$cfg" 'user' '-u' 'nobody'
144 append_parm "$cfg" 'group' '-g' 'nogroup'
145 append_parm "$cfg" 'proxy_server' '-t'
146 append_parm "$cfg" 'logfile' '-l'
147 append_bool "$cfg" 'use_http1' '-x'
148 append_counter "$cfg" 'verbosity' '-v' '0'
151 # shellcheck disable=SC2086
152 procd_set_param command $PROG $param
153 procd_set_param stderr 1
154 procd_set_param stdout 1
155 procd_set_param respawn
156 if [ "$force_dns" -ne 0 ]; then
158 json_add_array firewall
159 for p in $force_dns_port; do
160 if netstat -tuln | grep 'LISTEN' | grep ":${p}" >/dev/null 2>&1 || [ "$p" = '53' ]; then
162 json_add_string type redirect
163 json_add_string target DNAT
164 json_add_string src lan
165 json_add_string proto 'tcp udp'
166 json_add_string src_dport "$p"
167 json_add_string dest_port "$p"
168 json_add_boolean reflection 0
172 json_add_string type rule
173 json_add_string src lan
174 json_add_string dest '*'
175 json_add_string proto 'tcp udp'
176 json_add_string dest_port "$p"
177 json_add_string target REJECT
187 config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
188 config_get listen_port "$cfg" 'listen_port' "$port"
189 if [ "$update_dnsmasq_config" = '*' ]; then
191 config_foreach dnsmasq_doh_server 'dnsmasq' 'add' "${listen_addr}" "${listen_port}"
192 elif [ -n "$update_dnsmasq_config" ]; then
193 for i in $update_dnsmasq_config; do
194 if [ -n "$
(uci_get
'dhcp' "@dnsmasq[$i]")" ]; then
195 dnsmasq_doh_server "@dnsmasq
[$i]" 'add' "${listen_addr}" "${listen_port}"
196 elif [ -n "$
(uci_get
'dhcp' "$i")" ]; then
197 dnsmasq_doh_server "${i}" 'add' "${listen_addr}" "${listen_port}"
211 output "Starting
$serviceName "
212 config_load "$packageName"
213 config_get update_dnsmasq_config 'config' 'update_dnsmasq_config' '*'
214 config_get_bool canary_domains_icloud 'config' 'canary_domains_icloud' '1'
215 config_get_bool canary_domains_mozilla 'config' 'canary_domains_mozilla' '1'
216 config_get_bool force_dns 'config' 'force_dns' '1'
217 config_get force_dns_port 'config' 'force_dns_port' '53 853'
218 if [ "$canary_domains_icloud" -ne 0 ]; then
219 canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
221 if [ "$canary_domains_mozilla" -ne 0 ]; then
222 canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
225 config_load "$packageName"
226 config_foreach start_instance "$packageName"
227 if [ -n "$
(uci_changes dhcp
)" ]; then
236 output "Stopping
$serviceName "
237 config_load "$packageName"
238 config_get update_dnsmasq_config 'config' 'update_dnsmasq_config' '*'
239 dhcp_backup 'restore'
240 if [ -n "$
(uci_changes dhcp
)" ]; then
242 dnsmasq_restart || s=1
244 # shellcheck disable=SC2015
245 [ "$s" -eq 0 ] && output_okn || output_failn
248 # shellcheck disable=SC1091
251 . /lib/functions/network.sh
253 network_find_wan iface
254 iface="${iface:-wan}"
255 if [ -n "$iface" ]; then
256 procd_add_interface_trigger "interface.
*" "$iface" "/etc
/init.d
/${packageName}" restart
258 procd_add_config_trigger "config.change
" "$packageName" "/etc
/init.d
/${packageName}" restart
261 service_started() { procd_set_config_changed firewall; }
262 service_stopped() { procd_set_config_changed firewall; }
264 dnsmasq_doh_server() {
265 local cfg="$1" param="$2" address="${3:-127.0.0.1}" port="$4" i
268 if [ "$force_dns" -ne 0 ]; then
269 for i in $canaryDomains; do
270 uci_add_list_if_new 'dhcp' "$cfg" 'server' "/${i}/"
274 0.0.0.0|::ffff:0.0.0.0) address='127.0.0.1';;
277 uci_add_list_if_new 'dhcp' "$cfg" 'server' "${address}#${port}"
280 eval "$(ubus call service list "{ 'verbose': true
, 'name': '$packageName' }" | jsonfilter -F '# ' -e 'TUPLES=@[*].instances[*].command[4,6]')"
282 uci_remove_list
'dhcp' "$cfg" 'server' "$i"
284 for i
in $canaryDomains; do
285 uci_remove_list
'dhcp' "$cfg" 'server' "/${i}/"
291 dnsmasq_create_server_backup
() {
293 [ -n "$(uci_get 'dhcp' "$cfg")" ] ||
return 1
294 if [ -z "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" ]; then
295 if [ -z "$(uci_get 'dhcp' "$cfg" 'noresolv')" ]; then
296 uci_set
'dhcp' "$cfg" 'doh_backup_noresolv' '-1'
298 uci_set
'dhcp' "$cfg" 'doh_backup_noresolv' "$(uci_get 'dhcp' "$cfg" noresolv)"
300 uci_set
'dhcp' "$cfg" 'noresolv' 1
302 if [ -z "$(uci_get 'dhcp' "$cfg" 'doh_backup_server')" ]; then
303 if [ -z "$(uci_get 'dhcp' "$cfg" 'server')" ]; then
304 uci_add_list
'dhcp' "$cfg" 'doh_backup_server' ""
306 for i
in $
(uci_get
'dhcp' "$cfg" 'server'); do
307 uci_add_list
'dhcp' "$cfg" 'doh_backup_server' "$i"
308 if [ "$i" = "$(echo "$i" | tr -d /\#)" ]; then
309 uci_remove_list
'dhcp' "$cfg" 'server' "$i"
316 dnsmasq_restore_server_backup
() {
318 [ -n "$(uci_get 'dhcp' "$cfg")" ] ||
return 0
319 if [ -n "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" ]; then
320 if [ "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" = "-1" ]; then
321 uci_remove
'dhcp' "$cfg" 'noresolv'
323 uci_set
'dhcp' "$cfg" 'noresolv' "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')"
325 uci_remove
'dhcp' "$cfg" 'doh_backup_noresolv'
327 if [ -n "$(uci_get 'dhcp' "$cfg" 'doh_backup_server')" ]; then
328 dnsmasq_doh_server
"$cfg" 'remove'
329 for i
in $
(uci_get
'dhcp' "$cfg" 'doh_backup_server'); do
330 uci_add_list_if_new
'dhcp' "$cfg" 'server' "$i"
332 uci_remove
'dhcp' "$cfg" 'doh_backup_server'
341 if [ "$update_dnsmasq_config" = "*" ]; then
342 config_foreach dnsmasq_create_server_backup
'dnsmasq'
343 elif [ -n "$update_dnsmasq_config" ]; then
344 for i
in $update_dnsmasq_config; do
345 if [ -n "$(uci_get 'dhcp' "@dnsmasq
[$i]")" ]; then
346 dnsmasq_create_server_backup
"@dnsmasq[$i]"
347 elif [ -n "$(uci_get 'dhcp' "$i")" ]; then
348 dnsmasq_create_server_backup
"$i"
354 config_foreach dnsmasq_restore_server_backup
'dnsmasq'