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.

167 lines
5.2 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Startup before dnsmasq
  3. START=18
  4. USE_PROCD=1
  5. PROG=/usr/sbin/dnscrypt-proxy
  6. CONFIG_DIR=/var/etc
  7. USER=nobody
  8. boot() {
  9. dnscrypt_boot=1
  10. rc_procd start_service
  11. }
  12. dnscrypt_instance() {
  13. local config_path="$CONFIG_DIR/dnscrypt-proxy-$1.conf"
  14. create_config_file $1 "$config_path"
  15. procd_open_instance
  16. procd_set_param command $PROG "$config_path"
  17. procd_close_instance
  18. }
  19. create_config_file() {
  20. local address port resolver resolvers_list ephemeral_keys client_key log_level syslog syslog_prefix local_cache query_log_file block_ipv6 provider_name provider_key resolver_address
  21. local config_path="$2"
  22. local plugins_support_enabled=$(dnscrypt-proxy --version | grep 'Support for plugins: present' | wc -l)
  23. [ ! -d "$CONFIG_DIR" ] && mkdir -p "$CONFIG_DIR"
  24. [ -f "$config_path" ] && rm "$config_path"
  25. config_get address $1 'address' '127.0.0.1'
  26. config_get port $1 'port' '5353'
  27. config_get resolver $1 'resolver' ''
  28. config_get provider_name $1 'providername' ''
  29. config_get provider_key $1 'providerkey' ''
  30. config_get resolver_address $1 'resolveraddress' ''
  31. config_get resolvers_list $1 'resolvers_list' '/usr/share/dnscrypt-proxy/dnscrypt-resolvers.csv'
  32. config_get client_key $1 'client_key' ''
  33. config_get syslog_prefix $1 'syslog_prefix' 'dnscrypt-proxy'
  34. config_get query_log_file $1 'query_log_file' ''
  35. config_get log_level $1 'log_level' '6'
  36. config_get blacklist $1 'blacklist' ''
  37. config_get_bool syslog $1 'syslog' '1'
  38. config_get_bool ephemeral_keys $1 'ephemeral_keys' '0'
  39. config_get_bool local_cache $1 'local_cache' '0'
  40. config_get_bool block_ipv6 $1 'block_ipv6' '0'
  41. append_param_not_empty "ResolverName" "$resolver" $config_path
  42. append_param "ResolversList" "$resolvers_list" $config_path
  43. append_param_not_empty "ProviderName" "$provider_name" $config_path
  44. append_param_not_empty "ProviderKey" "$provider_key" $config_path
  45. append_param_not_empty "ResolverAddress" "$resolver_address" $config_path
  46. append_param "User" "$USER" $config_path
  47. append_param "LocalAddress" "$address:$port" $config_path
  48. append_param_not_empty "ClientKey" "$client_key" $config_path
  49. append_on_off "EphemeralKeys" $ephemeral_keys $config_path
  50. append_param "LogLevel" "$log_level" $config_path
  51. append_on_off "Syslog" $syslog $config_path
  52. append_param "SyslogPrefix" "$syslog_prefix" $config_path
  53. append_on_off "LocalCache" $local_cache $config_path
  54. append_param_not_empty "QueryLogFile" "$query_log_file" $config_path
  55. if [ $plugins_support_enabled -ne 0 ] && [ $block_ipv6 -ne 0 ]
  56. then
  57. append_yes_no "BlockIPv6" $block_ipv6 $config_path
  58. elif [ $block_ipv6 -ne 0 ]
  59. then
  60. log_ignored_param "block_ipv6"
  61. fi
  62. if [ $plugins_support_enabled -ne 0 ] && [ -n "$blacklist" ]
  63. then
  64. config_list_foreach $1 'blacklist' append_blacklists $config_path
  65. elif [ -n "$blacklist" ]
  66. then
  67. log_ignored_param "blacklist"
  68. fi
  69. }
  70. log_ignored_param() {
  71. local param_name=$1
  72. logger -t dnscrypt-proxy -p user.warn "dnscrypt-proxy plugins support not present, ignoring '$param_name' parameter."
  73. }
  74. append_on_off() {
  75. local param_name=$1
  76. local param_value=$2
  77. local config_path=$3
  78. local value
  79. if [ $param_value -eq 1 ]
  80. then
  81. value="on"
  82. else
  83. value="off"
  84. fi
  85. echo "$param_name $value" >> $config_path
  86. }
  87. append_yes_no() {
  88. local param_name=$1
  89. local param_value=$2
  90. local config_path=$3
  91. local value
  92. if [ $param_value -eq 1 ]
  93. then
  94. value="yes"
  95. else
  96. value="no"
  97. fi
  98. echo "$param_name $value" >> $config_path
  99. }
  100. append_param() {
  101. local param_name=$1
  102. local param_value=$2
  103. local config_path=$3
  104. echo "$param_name $param_value" >> $config_path
  105. }
  106. append_param_not_empty() {
  107. local param_name=$1
  108. local param_value=$2
  109. local config_path=$3
  110. if [ ! -z "$param_value" -a "$param_value" != " " ]
  111. then
  112. append_param "$param_name" "$param_value" "$config_path"
  113. fi
  114. }
  115. append_blacklists() {
  116. local value="$1"
  117. local config_path="$2"
  118. append_param_not_empty "BlackList" "$value" $config_path
  119. }
  120. start_service() {
  121. if [ -n "${dnscrypt_boot}" ]
  122. then
  123. return 0
  124. fi
  125. config_load dnscrypt-proxy
  126. config_foreach dnscrypt_instance dnscrypt-proxy
  127. }
  128. service_triggers() {
  129. local trigger
  130. local triggerlist="$(uci_get dnscrypt-proxy.@global[0].procd_trigger)"
  131. PROCD_RELOAD_DELAY=2000
  132. if [ -n "${triggerlist}" ]
  133. then
  134. for trigger in ${triggerlist}
  135. do
  136. procd_add_interface_trigger "interface.*.up" "${trigger}" /etc/init.d/dnscrypt-proxy reload
  137. done
  138. else
  139. procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/dnscrypt-proxy reload
  140. fi
  141. procd_add_reload_trigger 'dnscrypt-proxy'
  142. }