You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
11 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2019-2022 Stan Grishin (stangri@melmac.ca)
  3. # shellcheck disable=SC1091,SC2039,SC3043,SC3060
  4. PKG_VERSION='dev-test'
  5. # shellcheck disable=SC2034
  6. START=80
  7. # shellcheck disable=SC2034
  8. USE_PROCD=1
  9. if type extra_command 1>/dev/null 2>&1; then
  10. extra_command 'version' 'Show version information'
  11. else
  12. # shellcheck disable=SC2034
  13. EXTRA_COMMANDS='version'
  14. fi
  15. readonly packageName='https-dns-proxy'
  16. readonly serviceName="$packageName $PKG_VERSION"
  17. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  18. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  19. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  20. readonly PROG=/usr/sbin/https-dns-proxy
  21. 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'
  22. readonly canaryDomainsMozilla='use-application-dns.net'
  23. readonly canaryDomainsiCloud='mask.icloud.com mask-h2.icloud.com'
  24. canaryDomains=
  25. canary_domains_icloud=
  26. canary_domains_mozilla=
  27. update_dnsmasq_config=
  28. force_dns=
  29. force_dns_port=
  30. str_contains() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
  31. 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; }
  32. is_ipv4() { expr "$1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; }
  33. is_ipv6() { ! is_mac_address "$1" && str_contains "$1" ":"; }
  34. output() {
  35. local msg memmsg logmsg
  36. [ -t 1 ] && printf "%b" "$@"
  37. msg="${1//$serviceName /service }";
  38. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  39. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  40. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  41. logger -t "$packageName" "$(printf "%b" "$logmsg")"
  42. rm -f "$sharedMemoryOutput"
  43. else
  44. printf "%b" "$msg" >> "$sharedMemoryOutput"
  45. fi
  46. }
  47. output_ok() { output "$_OK_"; }
  48. output_okn() { output "${_OK_}\\n"; }
  49. output_fail() { output "$_FAIL_"; }
  50. output_failn() { output "${_FAIL_}\\n"; }
  51. uci_add_list_if_new() {
  52. local PACKAGE="$1"
  53. local CONFIG="$2"
  54. local OPTION="$3"
  55. local VALUE="$4"
  56. local i
  57. [ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
  58. for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
  59. [ "$i" = "$VALUE" ] && return 0
  60. done
  61. uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
  62. }
  63. uci_changes() {
  64. local PACKAGE="$1"
  65. local CONFIG="$2"
  66. local OPTION="$3"
  67. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
  68. }
  69. dnsmasq_restart() { [ -x /etc/init.d/dnsmasq ] || return 0; /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
  70. version() { echo "$PKG_VERSION"; }
  71. xappend() { param="$param $1"; }
  72. append_bool() {
  73. local section="$1"
  74. local option="$2"
  75. local value="$3"
  76. local default="${4:-0}"
  77. local _loctmp
  78. config_get_bool _loctmp "$section" "$option" "$default"
  79. [ "$_loctmp" -ne 0 ] && xappend "$value"
  80. }
  81. append_parm() {
  82. local section="$1"
  83. local option="$2"
  84. local switch="$3"
  85. local default="$4"
  86. local _loctmp
  87. config_get _loctmp "$section" "$option" "$default"
  88. [ -n "$_loctmp" ] && xappend "$switch $_loctmp"
  89. }
  90. append_counter() {
  91. local section="$1"
  92. local option="$2"
  93. local switch="$3"
  94. local default="${4:-0}"
  95. local _loctmp i
  96. config_get _loctmp "$section" "$option" "$default"
  97. # shellcheck disable=SC2086,SC2154
  98. for i in $(seq 1 $_loctmp); do
  99. xappend '-v'
  100. done
  101. }
  102. append_bootstrap() {
  103. local section="$1"
  104. local option="$2"
  105. local switch="$3"
  106. local default="$4"
  107. local _old_ifs="$IFS"
  108. local _loctmp _newtmp i
  109. config_get _loctmp "$section" "$option" "$default"
  110. [ -z "$_loctmp" ] && return 0
  111. IFS=" ,"
  112. for i in $_loctmp; do
  113. if { [ "$ipv6_resolvers_only" -eq 0 ] && is_ipv4 "$i"; } || \
  114. { [ "$ipv6_resolvers_only" -ne 0 ] && is_ipv6 "$i"; }; then
  115. [ -z "$_newtmp" ] && _newtmp="$i" || _newtmp="${_newtmp},${i}"
  116. fi
  117. done
  118. IFS="$_old_ifs"
  119. [ -n "$_newtmp" ] && xappend "$switch $_newtmp"
  120. [ "$ipv6_resolvers_only" -eq 0 ] && xappend '-4'
  121. }
  122. start_instance() {
  123. local cfg="$1" param listen_addr listen_port ipv6_resolvers_only p
  124. config_get_bool ipv6_resolvers_only "$cfg" 'use_ipv6_resolvers_only' '0'
  125. append_parm "$cfg" 'resolver_url' '-r'
  126. append_parm "$cfg" 'polling_interval' '-i'
  127. append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
  128. append_parm "$cfg" 'listen_port' '-p' "$port"
  129. append_parm "$cfg" 'dscp_codepoint' '-c'
  130. append_bootstrap "$cfg" 'bootstrap_dns' '-b' "$DEFAULT_BOOTSTRAP"
  131. append_parm "$cfg" 'user' '-u' 'nobody'
  132. append_parm "$cfg" 'group' '-g' 'nogroup'
  133. append_parm "$cfg" 'proxy_server' '-t'
  134. append_parm "$cfg" 'logfile' '-l'
  135. append_bool "$cfg" 'use_http1' '-x'
  136. append_counter "$cfg" 'verbosity' '-v' '0'
  137. procd_open_instance
  138. # shellcheck disable=SC2086
  139. procd_set_param command $PROG $param
  140. procd_set_param stderr 1
  141. procd_set_param stdout 1
  142. procd_set_param respawn
  143. if [ "$force_dns" -ne 0 ]; then
  144. procd_open_data
  145. json_add_array firewall
  146. for p in $force_dns_port; do
  147. if netstat -tuln | grep 'LISTEN' | grep ":${p}" >/dev/null 2>&1 || [ "$p" = '53' ]; then
  148. json_add_object ''
  149. json_add_string type redirect
  150. json_add_string target DNAT
  151. json_add_string src lan
  152. json_add_string proto 'tcp udp'
  153. json_add_string src_dport "$p"
  154. json_add_string dest_port "$p"
  155. json_add_boolean reflection 0
  156. json_close_object
  157. else
  158. json_add_object ''
  159. json_add_string type rule
  160. json_add_string src lan
  161. json_add_string dest '*'
  162. json_add_string proto 'tcp udp'
  163. json_add_string dest_port "$p"
  164. json_add_string target REJECT
  165. json_close_object
  166. fi
  167. done
  168. json_close_array
  169. procd_close_data
  170. fi
  171. procd_close_instance
  172. if [ "$?" ]; then
  173. config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
  174. config_get listen_port "$cfg" 'listen_port' "$port"
  175. if [ "$update_dnsmasq_config" = '*' ]; then
  176. config_load 'dhcp'
  177. config_foreach dnsmasq_doh_server 'dnsmasq' 'add' "${listen_addr}" "${listen_port}"
  178. elif [ -n "$update_dnsmasq_config" ]; then
  179. for i in $update_dnsmasq_config; do
  180. if [ -n "$(uci_get 'dhcp' "@dnsmasq[$i]")" ]; then
  181. dnsmasq_doh_server "@dnsmasq[$i]" 'add' "${listen_addr}" "${listen_port}"
  182. elif [ -n "$(uci_get 'dhcp' "$i")" ]; then
  183. dnsmasq_doh_server "${i}" 'add' "${listen_addr}" "${listen_port}"
  184. fi
  185. done
  186. fi
  187. output_ok
  188. port="$((port+1))"
  189. force_dns=0
  190. else
  191. output_fail
  192. fi
  193. }
  194. start_service() {
  195. local port=5053
  196. output "Starting $serviceName "
  197. config_load "$packageName"
  198. config_get update_dnsmasq_config 'config' 'update_dnsmasq_config' '*'
  199. config_get_bool canary_domains_icloud 'config' 'canary_domains_icloud' '1'
  200. config_get_bool canary_domains_mozilla 'config' 'canary_domains_mozilla' '1'
  201. config_get_bool force_dns 'config' 'force_dns' '1'
  202. config_get force_dns_port 'config' 'force_dns_port' '53 853'
  203. if [ "$canary_domains_icloud" -ne 0 ]; then
  204. canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
  205. fi
  206. if [ "$canary_domains_mozilla" -ne 0 ]; then
  207. canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
  208. fi
  209. dhcp_backup 'create'
  210. config_load "$packageName"
  211. config_foreach start_instance "$packageName"
  212. if [ -n "$(uci_changes dhcp)" ]; then
  213. uci_commit 'dhcp'
  214. dnsmasq_restart
  215. fi
  216. output "\\n"
  217. }
  218. stop_service() {
  219. local s=0
  220. output "Stopping $serviceName "
  221. config_load "$packageName"
  222. config_get update_dnsmasq_config 'config' 'update_dnsmasq_config' '*'
  223. dhcp_backup 'restore'
  224. if [ -n "$(uci_changes dhcp)" ]; then
  225. uci_commit 'dhcp'
  226. dnsmasq_restart || s=1
  227. fi
  228. # shellcheck disable=SC2015
  229. [ "$s" -eq 0 ] && output_okn || output_failn
  230. }
  231. # shellcheck disable=SC1091
  232. service_triggers() {
  233. local iface
  234. . /lib/functions/network.sh
  235. network_flush_cache
  236. network_find_wan iface
  237. iface="${iface:-wan}"
  238. if [ -n "$iface" ]; then
  239. procd_add_interface_trigger "interface.*" "$iface" "/etc/init.d/${packageName}" restart
  240. fi
  241. procd_add_config_trigger "config.change" "$packageName" "/etc/init.d/${packageName}" restart
  242. }
  243. service_started() { procd_set_config_changed firewall; }
  244. service_stopped() { procd_set_config_changed firewall; }
  245. dnsmasq_doh_server() {
  246. local cfg="$1" param="$2" address="${3:-127.0.0.1}" port="$4" i
  247. case "$param" in
  248. add)
  249. if [ "$force_dns" -ne 0 ]; then
  250. for i in $canaryDomains; do
  251. uci_add_list_if_new 'dhcp' "$cfg" 'server' "/${i}/"
  252. done
  253. fi
  254. case $address in
  255. 0.0.0.0|::ffff:0.0.0.0) address='127.0.0.1';;
  256. ::) address='::1';;
  257. esac
  258. uci_add_list_if_new 'dhcp' "$cfg" 'server' "${address}#${port}"
  259. ;;
  260. remove)
  261. eval "$(ubus call service list "{ 'verbose': true, 'name': '$packageName' }" | jsonfilter -F '# ' -e 'TUPLES=@[*].instances[*].command[4,6]')"
  262. for i in $TUPLES; do
  263. uci_remove_list 'dhcp' "$cfg" 'server' "$i"
  264. done
  265. for i in $canaryDomains; do
  266. uci_remove_list 'dhcp' "$cfg" 'server' "/${i}/"
  267. done
  268. ;;
  269. esac
  270. }
  271. dnsmasq_create_server_backup() {
  272. local cfg="$1" i
  273. [ -n "$(uci_get 'dhcp' "$cfg")" ] || return 1
  274. if [ -z "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" ]; then
  275. if [ -z "$(uci_get 'dhcp' "$cfg" 'noresolv')" ]; then
  276. uci_set 'dhcp' "$cfg" 'doh_backup_noresolv' '-1'
  277. else
  278. uci_set 'dhcp' "$cfg" 'doh_backup_noresolv' "$(uci_get 'dhcp' "$cfg" noresolv)"
  279. fi
  280. uci_set 'dhcp' "$cfg" 'noresolv' 1
  281. fi
  282. if [ -z "$(uci_get 'dhcp' "$cfg" 'doh_backup_server')" ]; then
  283. if [ -z "$(uci_get 'dhcp' "$cfg" 'server')" ]; then
  284. uci_add_list 'dhcp' "$cfg" 'doh_backup_server' ""
  285. fi
  286. for i in $(uci_get 'dhcp' "$cfg" 'server'); do
  287. uci_add_list 'dhcp' "$cfg" 'doh_backup_server' "$i"
  288. if [ "$i" = "$(echo "$i" | tr -d /\#)" ]; then
  289. uci_remove_list 'dhcp' "$cfg" 'server' "$i"
  290. fi
  291. done
  292. fi
  293. return 0
  294. }
  295. dnsmasq_restore_server_backup() {
  296. local cfg="$1" i
  297. [ -n "$(uci_get 'dhcp' "$cfg")" ] || return 0
  298. if [ -n "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" ]; then
  299. if [ "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" = "-1" ]; then
  300. uci_remove 'dhcp' "$cfg" 'noresolv'
  301. else
  302. uci_set 'dhcp' "$cfg" 'noresolv' "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')"
  303. fi
  304. uci_remove 'dhcp' "$cfg" 'doh_backup_noresolv'
  305. fi
  306. if [ -n "$(uci_get 'dhcp' "$cfg" 'doh_backup_server')" ]; then
  307. dnsmasq_doh_server "$cfg" 'remove'
  308. for i in $(uci_get 'dhcp' "$cfg" 'doh_backup_server'); do
  309. uci_add_list_if_new 'dhcp' "$cfg" 'server' "$i"
  310. done
  311. uci_remove 'dhcp' "$cfg" 'doh_backup_server'
  312. fi
  313. }
  314. dhcp_backup() {
  315. local i
  316. config_load 'dhcp'
  317. case "$1" in
  318. create)
  319. if [ "$update_dnsmasq_config" = "*" ]; then
  320. config_foreach dnsmasq_create_server_backup 'dnsmasq'
  321. elif [ -n "$update_dnsmasq_config" ]; then
  322. for i in $update_dnsmasq_config; do
  323. if [ -n "$(uci_get 'dhcp' "@dnsmasq[$i]")" ]; then
  324. dnsmasq_create_server_backup "@dnsmasq[$i]"
  325. elif [ -n "$(uci_get 'dhcp' "$i")" ]; then
  326. dnsmasq_create_server_backup "$i"
  327. fi
  328. done
  329. fi
  330. ;;
  331. restore)
  332. config_foreach dnsmasq_restore_server_backup 'dnsmasq'
  333. ;;
  334. esac
  335. }