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.

115 lines
2.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2021 Tianling Shen <cnsztl@immortalwrt.org>
  3. USE_PROCD=1
  4. START=90
  5. CONF="dnsproxy"
  6. PROG="/usr/bin/dnsproxy"
  7. is_enabled() {
  8. local enabled
  9. config_get enabled "$1" "$2" "0"
  10. if [ "$enabled" -eq "1" ]; then
  11. return 0
  12. else
  13. return 1
  14. fi
  15. }
  16. is_empty() {
  17. local empty
  18. config_get empty "$1" "$2" $3
  19. if [ -z "$empty" ]; then
  20. return 0
  21. else
  22. return 1
  23. fi
  24. }
  25. append_param() {
  26. procd_append_param command "$1" $2
  27. }
  28. append_param_arg() {
  29. local value
  30. config_get value "$1" "$2" $4
  31. [ -n "$value" ] && append_param "$3" "$value"
  32. }
  33. append_param_bool() {
  34. is_enabled "$1" "$2" && append_param "--${2//_/-}"
  35. }
  36. load_config_arg() {
  37. append_param_bool "$1" "all_servers"
  38. append_param_bool "$1" "fastest_addr"
  39. append_param_bool "$1" "insecure"
  40. append_param_bool "$1" "ipv6_disabled"
  41. append_param_bool "$1" "refuse_any"
  42. append_param_bool "$1" "verbose"
  43. }
  44. load_config_list() {
  45. is_empty "bogus_nxdomain" "ip_addr" || config_list_foreach "bogus_nxdomain" "ip_addr" "append_param '--bogus-nxdomain'"
  46. for i in "bootstrap" "fallback" "upstream"; do
  47. is_empty "servers" "$i" || config_list_foreach "servers" "$i" "append_param '--$i'"
  48. done
  49. }
  50. load_config_param() {
  51. append_param_arg "global" "listen_addr" "--listen" "127.0.0.1"
  52. append_param_arg "global" "listen_port" "--port" "5353"
  53. append_param_arg "global" "log_file" "--output"
  54. append_param_arg "global" "max_go_routines" "--max-go-routines"
  55. append_param_arg "global" "rate_limit" "--ratelimit"
  56. append_param_arg "global" "udp_buf_size" "--udp-buf-size"
  57. is_enabled "cache" "enabled" && {
  58. append_param "--cache"
  59. append_param_bool "cache" "cache_optimistic"
  60. append_param_arg "cache" "size" "--cache-size"
  61. append_param_arg "cache" "min_ttl" "--cache-min-ttl"
  62. append_param_arg "cache" "max_ttl" "--cache-max-ttl"
  63. }
  64. is_enabled "dns64" "enabled" && {
  65. append_param "--dns64"
  66. append_param_arg "dns64" "dns64_prefix" "--dns64-prefix"
  67. }
  68. is_enabled "edns" "enabled" && {
  69. append_param "--edns"
  70. append_param_arg "edns" "edns_addr" "--edns-addr"
  71. }
  72. }
  73. start_service() {
  74. config_load "$CONF"
  75. is_enabled "global" "enabled" || exit 1
  76. procd_open_instance "$CONF"
  77. procd_set_param command "$PROG"
  78. load_config_arg "global"
  79. load_config_list
  80. load_config_param
  81. procd_set_param respawn
  82. procd_set_param stdout 1
  83. procd_set_param stderr 1
  84. procd_set_param user dnsproxy
  85. procd_close_instance
  86. }
  87. reload_service() {
  88. stop
  89. start
  90. }
  91. service_triggers() {
  92. procd_add_reload_trigger "$CONF"
  93. }