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.

185 lines
4.9 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2019 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039
  4. export START=80
  5. export USE_PROCD=1
  6. dnsmasqConfig=''
  7. PROG=/usr/sbin/https-dns-proxy
  8. xappend() { param="$param $1"; }
  9. append_bool() {
  10. local section="$1"
  11. local option="$2"
  12. local value="$3"
  13. local default="$4"
  14. local _loctmp
  15. [ -z "$default" ] && default="0"
  16. config_get_bool _loctmp "$section" "$option" "$default"
  17. [ "$_loctmp" != "0" ] && xappend "$value"
  18. }
  19. append_parm() {
  20. local section="$1"
  21. local option="$2"
  22. local switch="$3"
  23. local default="$4"
  24. local _loctmp
  25. config_get _loctmp "$section" "$option" "$default"
  26. [ -z "$_loctmp" ] && return 0
  27. xappend "$switch $_loctmp"
  28. }
  29. start_instance() {
  30. local cfg="$1" param listen_addr listen_port i
  31. append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
  32. append_parm "$cfg" 'listen_port' '-p' "$p"
  33. append_parm "$cfg" 'bootstrap_dns' '-b'
  34. append_parm "$cfg" 'resolver_url' '-r'
  35. append_parm "$cfg" 'user' '-u' 'nobody'
  36. append_parm "$cfg" 'group' '-g' 'nogroup'
  37. append_parm "$cfg" 'edns_subnet' '-e'
  38. append_parm "$cfg" 'proxy_server' '-t'
  39. append_parm "$cfg" 'logfile' '-l'
  40. append_bool "$cfg" 'use_http1' '-x'
  41. config_get_bool ipv6_resolvers_only "$cfg" 'use_ipv6_resolvers_only' '0'
  42. config_get verbosity "$cfg" 'verbosity' "0"
  43. # shellcheck disable=SC2086,SC2154
  44. for i in $(seq 1 $verbosity); do
  45. xappend "-v"
  46. done
  47. # shellcheck disable=SC2154
  48. if [ "$ipv6_resolvers_only" = 0 ]; then
  49. xappend "-4"
  50. fi
  51. procd_open_instance
  52. # shellcheck disable=SC2086
  53. procd_set_param command ${PROG} ${param}
  54. procd_set_param stderr 1
  55. procd_set_param stdout 1
  56. procd_set_param respawn
  57. procd_close_instance
  58. config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
  59. config_get listen_port "$cfg" 'listen_port' "$p"
  60. if [ "$dnsmasqConfig" = "*" ]; then
  61. config_load 'dhcp'
  62. config_foreach dnsmasq_add_doh_server 'dnsmasq' "${listen_addr}" "${listen_port}"
  63. elif [ -n "$dnsmasqConfig" ]; then
  64. for i in $dnsmasqConfig; do
  65. dnsmasq_add_doh_server "@dnsmasq[${i}]" "${listen_addr}" "${listen_port}"
  66. done
  67. fi
  68. p="$((p+1))"
  69. }
  70. service_triggers() {
  71. procd_add_reload_trigger 'https-dns-proxy'
  72. }
  73. start_service() {
  74. local p=5053
  75. config_load 'https-dns-proxy'
  76. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  77. dhcp_backup 'create'
  78. config_load 'https-dns-proxy'
  79. config_foreach start_instance 'https-dns-proxy'
  80. if [ -n "$(uci -q changes dhcp)" ]; then
  81. uci -q commit dhcp
  82. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  83. fi
  84. }
  85. stop_service() {
  86. config_load 'https-dns-proxy'
  87. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  88. dhcp_backup 'restore'
  89. if [ -n "$(uci -q changes dhcp)" ]; then
  90. uci -q commit dhcp
  91. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  92. fi
  93. }
  94. service_triggers() {
  95. procd_add_reload_trigger 'https-dns-proxy'
  96. }
  97. dnsmasq_add_doh_server() {
  98. local cfg="$1" address="$2" port="$3"
  99. case $address in
  100. 0.0.0.0|::ffff:0.0.0.0) address='127.0.0.1';;
  101. ::) address='::1';;
  102. esac
  103. uci -q del_list "dhcp.${cfg}.server=${address}#${port}"
  104. uci -q add_list "dhcp.${cfg}.server=${address}#${port}"
  105. }
  106. dnsmasq_create_server_backup() {
  107. local cfg="$1"
  108. local i
  109. uci -q get "dhcp.${cfg}" >/dev/null || return 0
  110. if ! uci -q get "dhcp.${cfg}.doh_backup_noresolv" >/dev/null; then
  111. if [ -z "$(uci -q get "dhcp.${cfg}.noresolv")" ]; then
  112. uci -q set "dhcp.${cfg}.noresolv=1"
  113. uci -q set "dhcp.${cfg}.doh_backup_noresolv=-1"
  114. elif [ "$(uci -q get "dhcp.${cfg}.noresolv")" != "1" ]; then
  115. uci -q set "dhcp.${cfg}.noresolv=1"
  116. uci -q set "dhcp.${cfg}.doh_backup_noresolv=0"
  117. fi
  118. fi
  119. if ! uci -q get "dhcp.${cfg}.doh_backup_server" >/dev/null; then
  120. for i in $(uci -q get "dhcp.${cfg}.server"); do
  121. uci -q add_list "dhcp.${cfg}.doh_backup_server=$i"
  122. if [ "$i" = "${i//127.0.0.1}" ] && [ "$i" = "$(echo "$i" | tr -d /)" ]; then
  123. uci -q del_list "dhcp.${cfg}.server=$i"
  124. fi
  125. done
  126. fi
  127. }
  128. dnsmasq_restore_server_backup() {
  129. local cfg="$1"
  130. local i
  131. uci -q get "dhcp.${cfg}" >/dev/null || return 0
  132. if uci -q get "dhcp.${cfg}.doh_backup_noresolv" >/dev/null; then
  133. if [ "$(uci -q get "dhcp.${cfg}.doh_backup_noresolv")" = "0" ]; then
  134. uci -q set "dhcp.${cfg}.noresolv=0"
  135. else
  136. uci -q del "dhcp.${cfg}.noresolv"
  137. fi
  138. uci -q del "dhcp.${cfg}.doh_backup_noresolv"
  139. fi
  140. if uci -q get "dhcp.${cfg}.doh_backup_server" >/dev/null; then
  141. uci -q del "dhcp.${cfg}.server"
  142. for i in $(uci -q get "dhcp.${cfg}.doh_backup_server"); do
  143. uci -q add_list "dhcp.${cfg}.server=$i"
  144. done
  145. uci -q del "dhcp.${cfg}.doh_backup_server"
  146. fi
  147. }
  148. dhcp_backup() {
  149. local i
  150. config_load 'dhcp'
  151. case "$1" in
  152. create)
  153. if [ "$dnsmasqConfig" = "*" ]; then
  154. config_foreach dnsmasq_create_server_backup 'dnsmasq'
  155. elif [ -n "$dnsmasqConfig" ]; then
  156. for i in $dnsmasqConfig; do
  157. dnsmasq_create_server_backup "@dnsmasq[${i}]"
  158. done
  159. fi
  160. ;;
  161. restore)
  162. config_foreach dnsmasq_restore_server_backup 'dnsmasq'
  163. ;;
  164. esac
  165. }