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.

184 lines
4.8 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" 'proxy_server' '-t'
  38. append_parm "$cfg" 'logfile' '-l'
  39. append_bool "$cfg" 'use_http1' '-x'
  40. config_get_bool ipv6_resolvers_only "$cfg" 'use_ipv6_resolvers_only' '0'
  41. config_get verbosity "$cfg" 'verbosity' "0"
  42. # shellcheck disable=SC2086,SC2154
  43. for i in $(seq 1 $verbosity); do
  44. xappend "-v"
  45. done
  46. # shellcheck disable=SC2154
  47. if [ "$ipv6_resolvers_only" = 0 ]; then
  48. xappend "-4"
  49. fi
  50. procd_open_instance
  51. # shellcheck disable=SC2086
  52. procd_set_param command ${PROG} ${param}
  53. procd_set_param stderr 1
  54. procd_set_param stdout 1
  55. procd_set_param respawn
  56. procd_close_instance
  57. config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
  58. config_get listen_port "$cfg" 'listen_port' "$p"
  59. if [ "$dnsmasqConfig" = "*" ]; then
  60. config_load 'dhcp'
  61. config_foreach dnsmasq_add_doh_server 'dnsmasq' "${listen_addr}" "${listen_port}"
  62. elif [ -n "$dnsmasqConfig" ]; then
  63. for i in $dnsmasqConfig; do
  64. dnsmasq_add_doh_server "@dnsmasq[${i}]" "${listen_addr}" "${listen_port}"
  65. done
  66. fi
  67. p="$((p+1))"
  68. }
  69. service_triggers() {
  70. procd_add_reload_trigger 'https-dns-proxy'
  71. }
  72. start_service() {
  73. local p=5053
  74. config_load 'https-dns-proxy'
  75. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  76. dhcp_backup 'create'
  77. config_load 'https-dns-proxy'
  78. config_foreach start_instance 'https-dns-proxy'
  79. if [ -n "$(uci -q changes dhcp)" ]; then
  80. uci -q commit dhcp
  81. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  82. fi
  83. }
  84. stop_service() {
  85. config_load 'https-dns-proxy'
  86. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  87. dhcp_backup 'restore'
  88. if [ -n "$(uci -q changes dhcp)" ]; then
  89. uci -q commit dhcp
  90. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  91. fi
  92. }
  93. service_triggers() {
  94. procd_add_reload_trigger 'https-dns-proxy'
  95. }
  96. dnsmasq_add_doh_server() {
  97. local cfg="$1" address="$2" port="$3"
  98. case $address in
  99. 0.0.0.0|::ffff:0.0.0.0) address='127.0.0.1';;
  100. ::) address='::1';;
  101. esac
  102. uci -q del_list "dhcp.${cfg}.server=${address}#${port}"
  103. uci -q add_list "dhcp.${cfg}.server=${address}#${port}"
  104. }
  105. dnsmasq_create_server_backup() {
  106. local cfg="$1"
  107. local i
  108. uci -q get "dhcp.${cfg}" >/dev/null || return 0
  109. if ! uci -q get "dhcp.${cfg}.doh_backup_noresolv" >/dev/null; then
  110. if [ -z "$(uci -q get "dhcp.${cfg}.noresolv")" ]; then
  111. uci -q set "dhcp.${cfg}.noresolv=1"
  112. uci -q set "dhcp.${cfg}.doh_backup_noresolv=-1"
  113. elif [ "$(uci -q get "dhcp.${cfg}.noresolv")" != "1" ]; then
  114. uci -q set "dhcp.${cfg}.noresolv=1"
  115. uci -q set "dhcp.${cfg}.doh_backup_noresolv=0"
  116. fi
  117. fi
  118. if ! uci -q get "dhcp.${cfg}.doh_backup_server" >/dev/null; then
  119. for i in $(uci -q get "dhcp.${cfg}.server"); do
  120. uci -q add_list "dhcp.${cfg}.doh_backup_server=$i"
  121. if [ "$i" = "${i//127.0.0.1}" ] && [ "$i" = "$(echo "$i" | tr -d /)" ]; then
  122. uci -q del_list "dhcp.${cfg}.server=$i"
  123. fi
  124. done
  125. fi
  126. }
  127. dnsmasq_restore_server_backup() {
  128. local cfg="$1"
  129. local i
  130. uci -q get "dhcp.${cfg}" >/dev/null || return 0
  131. if uci -q get "dhcp.${cfg}.doh_backup_noresolv" >/dev/null; then
  132. if [ "$(uci -q get "dhcp.${cfg}.doh_backup_noresolv")" = "0" ]; then
  133. uci -q set "dhcp.${cfg}.noresolv=0"
  134. else
  135. uci -q del "dhcp.${cfg}.noresolv"
  136. fi
  137. uci -q del "dhcp.${cfg}.doh_backup_noresolv"
  138. fi
  139. if uci -q get "dhcp.${cfg}.doh_backup_server" >/dev/null; then
  140. uci -q del "dhcp.${cfg}.server"
  141. for i in $(uci -q get "dhcp.${cfg}.doh_backup_server"); do
  142. uci -q add_list "dhcp.${cfg}.server=$i"
  143. done
  144. uci -q del "dhcp.${cfg}.doh_backup_server"
  145. fi
  146. }
  147. dhcp_backup() {
  148. local i
  149. config_load 'dhcp'
  150. case "$1" in
  151. create)
  152. if [ "$dnsmasqConfig" = "*" ]; then
  153. config_foreach dnsmasq_create_server_backup 'dnsmasq'
  154. elif [ -n "$dnsmasqConfig" ]; then
  155. for i in $dnsmasqConfig; do
  156. dnsmasq_create_server_backup "@dnsmasq[${i}]"
  157. done
  158. fi
  159. ;;
  160. restore)
  161. config_foreach dnsmasq_restore_server_backup 'dnsmasq'
  162. ;;
  163. esac
  164. }