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.

166 lines
5.2 KiB

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