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.

144 lines
3.3 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. PROG=/usr/sbin/https-dns-proxy
  7. xappend() { param="$param $1"; }
  8. append_bool() {
  9. local section="$1"
  10. local option="$2"
  11. local value="$3"
  12. local default="$4"
  13. local _loctmp
  14. [ -z "$default" ] && default="0"
  15. config_get_bool _loctmp "$section" "$option" "$default"
  16. [ "$_loctmp" != "0" ] && xappend "$value"
  17. }
  18. append_parm() {
  19. local section="$1"
  20. local option="$2"
  21. local switch="$3"
  22. local default="$4"
  23. local _loctmp
  24. config_get _loctmp "$section" "$option" "$default"
  25. [ -z "$_loctmp" ] && return 0
  26. xappend "$switch $_loctmp"
  27. }
  28. start_instance() {
  29. local cfg="$1" param listen_addr listen_port
  30. append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
  31. append_parm "$cfg" 'listen_port' '-p' "$p"
  32. append_parm "$cfg" 'bootstrap_dns' '-b'
  33. append_parm "$cfg" 'resolver_url' '-r'
  34. append_parm "$cfg" 'user' '-u' 'nobody'
  35. append_parm "$cfg" 'group' '-g' 'nogroup'
  36. append_parm "$cfg" 'edns_subnet' '-e'
  37. append_parm "$cfg" 'proxy_server' '-t'
  38. append_parm "$cfg" 'logfile' '-l'
  39. append_bool "$cfg" 'use_http1' '-x'
  40. append_bool "$cfg" 'ipv4_resolvers' '-4'
  41. config_get verbosity "$cfg" 'verbosity' "0"
  42. for i in $(seq 1 $verbosity); do
  43. xappend "-v"
  44. done
  45. procd_open_instance
  46. # shellcheck disable=SC2086
  47. procd_set_param command ${PROG} ${param}
  48. procd_set_param stderr 1
  49. procd_set_param stdout 1
  50. procd_set_param respawn
  51. procd_close_instance
  52. config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
  53. config_get listen_port "$cfg" 'listen_port' "$p"
  54. # Don't add the any address to dnsmasq
  55. case $listen_addr in
  56. 0.0.0.0|::ffff:0.0.0.0)
  57. listen_addr='127.0.0.1'
  58. ;;
  59. ::)
  60. listen_addr='::1'
  61. ;;
  62. esac
  63. config_load 'dhcp'
  64. # shellcheck disable=SC2154
  65. config_foreach dnsmasq_add_doh_server 'dnsmasq' "${listen_addr}#${listen_port}"
  66. p="$((p+1))"
  67. }
  68. service_triggers() {
  69. procd_add_reload_trigger 'https-dns-proxy'
  70. }
  71. start_service() {
  72. local p=5053
  73. dhcp_backup 'create'
  74. config_load 'https-dns-proxy'
  75. config_foreach start_instance 'https-dns-proxy'
  76. if [ -z "$(uci -q get dhcp.@dnsmasq[0].server)" ]; then
  77. dhcp_backup 'restore'
  78. fi
  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. dhcp_backup 'restore'
  86. if [ -n "$(uci -q changes dhcp)" ]; then
  87. uci -q commit dhcp
  88. [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq restart >/dev/null 2>&1
  89. fi
  90. }
  91. service_triggers() {
  92. procd_add_reload_trigger 'https-dns-proxy'
  93. }
  94. dnsmasq_add_doh_server() {
  95. local cfg="$1" value="$2"
  96. uci -q add_list dhcp."$cfg".server="$value"
  97. }
  98. dnsmasq_create_server_backup() {
  99. local cfg="$1" i
  100. if ! uci -q get "dhcp.$cfg.doh_backup_server" >/dev/null; then
  101. for i in $(uci -q get "dhcp.$cfg.server"); do
  102. uci -q add_list dhcp."$cfg".doh_backup_server="$i"
  103. done
  104. fi
  105. uci -q del "dhcp.$cfg.server"
  106. }
  107. dnsmasq_restore_server_backup() {
  108. local cfg="$1" i
  109. if uci -q get "dhcp.$cfg.doh_backup_server" >/dev/null; then
  110. uci -q del "dhcp.$cfg.server"
  111. for i in $(uci -q get "dhcp.$cfg.doh_backup_server"); do
  112. uci -q add_list dhcp."$cfg".server="$i"
  113. done
  114. fi
  115. }
  116. dhcp_backup() {
  117. config_load 'dhcp'
  118. case "$1" in
  119. create)
  120. config_foreach dnsmasq_create_server_backup 'dnsmasq';;
  121. restore)
  122. config_foreach dnsmasq_restore_server_backup 'dnsmasq';;
  123. esac
  124. }