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.

161 lines
4.0 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. append_match() {
  30. local section="$1"
  31. local option="$2"
  32. local value="$3"
  33. local match="$4"
  34. local _loctmp
  35. config_get_bool _loctmp "$section" "$option"
  36. [ "$_loctmp" = "$match" ] && xappend "$value"
  37. }
  38. start_instance() {
  39. local cfg="$1" param listen_addr listen_port i
  40. append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
  41. append_parm "$cfg" 'listen_port' '-p' "$p"
  42. append_parm "$cfg" 'bootstrap_dns' '-b'
  43. append_parm "$cfg" 'resolver_url' '-r'
  44. append_parm "$cfg" 'user' '-u' 'nobody'
  45. append_parm "$cfg" 'group' '-g' 'nogroup'
  46. append_parm "$cfg" 'edns_subnet' '-e'
  47. append_parm "$cfg" 'proxy_server' '-t'
  48. append_parm "$cfg" 'logfile' '-l'
  49. append_bool "$cfg" 'use_http1' '-x'
  50. append_match "$cfg" 'verbosity' '-v' '1'
  51. append_match "$cfg" 'verbosity' '-vv' '2'
  52. append_match "$cfg" 'verbosity' '-vvv' '3'
  53. append_match "$cfg" 'verbosity' '-vvvv' '4'
  54. procd_open_instance
  55. # shellcheck disable=SC2086
  56. procd_set_param command ${PROG} -4 ${param}
  57. procd_set_param respawn
  58. procd_close_instance
  59. config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
  60. config_get listen_port "$cfg" 'listen_port' "$p"
  61. if [ "$dnsmasqConfig" = "*" ]; then
  62. config_load 'dhcp'
  63. config_foreach dnsmasq_add_doh_server 'dnsmasq' "${listen_addr}#${listen_port}"
  64. elif [ -n "$dnsmasqConfig" ]; then
  65. for i in $dnsmasqConfig; do
  66. dnsmasq_add_doh_server "@dnsmasq[${i}]" "${listen_addr}#${listen_port}"
  67. done
  68. fi
  69. p="$((p+1))"
  70. }
  71. service_triggers() {
  72. procd_add_reload_trigger 'https-dns-proxy'
  73. }
  74. start_service() {
  75. local p=5053
  76. config_load 'https-dns-proxy'
  77. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  78. dhcp_backup 'create'
  79. config_load 'https-dns-proxy'
  80. config_foreach start_instance 'https-dns-proxy'
  81. if [ -n "$(uci -q changes dhcp)" ]; then
  82. uci -q commit dhcp
  83. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  84. fi
  85. }
  86. stop_service() {
  87. config_load 'https-dns-proxy'
  88. config_get dnsmasqConfig 'config' 'update_dnsmasq_config' '*'
  89. dhcp_backup 'restore'
  90. if [ -n "$(uci -q changes dhcp)" ]; then
  91. uci -q commit dhcp
  92. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  93. fi
  94. }
  95. service_triggers() {
  96. procd_add_reload_trigger 'https-dns-proxy'
  97. }
  98. dnsmasq_add_doh_server() {
  99. local cfg="$1" value="$2"
  100. uci -q del_list dhcp."$cfg".server="$value"
  101. uci -q add_list dhcp."$cfg".server="$value"
  102. }
  103. dnsmasq_create_server_backup() {
  104. local cfg="$1"
  105. local i
  106. uci -q get "dhcp.$cfg.doh_backup_server" >/dev/null && return 0
  107. for i in $(uci -q get "dhcp.$cfg.server"); do
  108. uci -q add_list dhcp."$cfg".doh_backup_server="$i"
  109. if [ "$i" = "${i//127.0.0.1}" ] && [ "$i" = "$(echo "$i" | tr -d /)" ]; then
  110. uci -q del_list dhcp."$cfg".server="$i"
  111. fi
  112. done
  113. }
  114. dnsmasq_restore_server_backup() {
  115. local cfg="$1"
  116. local i
  117. if uci -q get "dhcp.$cfg.doh_backup_server" >/dev/null; then
  118. uci -q del "dhcp.$cfg.server"
  119. for i in $(uci -q get "dhcp.$cfg.doh_backup_server"); do
  120. uci -q add_list dhcp."$cfg".server="$i"
  121. done
  122. uci -q del "dhcp.$cfg.doh_backup_server"
  123. fi
  124. }
  125. dhcp_backup() {
  126. local i
  127. config_load 'dhcp'
  128. case "$1" in
  129. create)
  130. if [ "$dnsmasqConfig" = "*" ]; then
  131. config_foreach dnsmasq_create_server_backup 'dnsmasq'
  132. elif [ -n "$dnsmasqConfig" ]; then
  133. for i in $dnsmasqConfig; do
  134. dnsmasq_create_server_backup "@dnsmasq[${i}]"
  135. done
  136. fi
  137. ;;
  138. restore)
  139. config_foreach dnsmasq_restore_server_backup 'dnsmasq'
  140. ;;
  141. esac
  142. }