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.

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