* dnsforward: refactor code for default DNS servers logic

This commit is contained in:
Simon Zolin 2019-10-31 12:32:14 +03:00
parent 8bf75b54a4
commit 9b8cccdfcf
4 changed files with 39 additions and 65 deletions

View File

@ -29,6 +29,12 @@ const (
parentalBlockHost = "family-block.dns.adguard.com"
)
var defaultDNS = []string{
"https://1.1.1.1/dns-query",
"https://1.0.0.1/dns-query",
}
var defaultBootstrap = []string{"1.1.1.1", "1.0.0.1"}
// Server is the main way to start a DNS server.
//
// Example:
@ -59,6 +65,11 @@ func NewServer(dnsFilter *dnsfilter.Dnsfilter, stats stats.Stats, queryLog query
s.dnsFilter = dnsFilter
s.stats = stats
s.queryLog = queryLog
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" {
// Use plain DNS on MIPS, encryption is too slow
defaultDNS = []string{"1.1.1.1", "1.0.0.1"}
}
return s
}
@ -149,19 +160,6 @@ var defaultValues = ServerConfig{
FilteringConfig: FilteringConfig{BlockedResponseTTL: 3600},
}
func init() {
defaultDNS := []string{"8.8.8.8:53", "8.8.4.4:53"}
defaultUpstreams := make([]upstream.Upstream, 0)
for _, addr := range defaultDNS {
u, err := upstream.AddressToUpstream(addr, upstream.Options{Timeout: DefaultTimeout})
if err == nil {
defaultUpstreams = append(defaultUpstreams, u)
}
}
defaultValues.Upstreams = defaultUpstreams
}
// Start starts the DNS server
func (s *Server) Start(config *ServerConfig) error {
s.Lock()
@ -177,20 +175,34 @@ func (s *Server) startInternal(config *ServerConfig) error {
if config != nil {
s.conf = *config
upstreamConfig, err := proxy.ParseUpstreamsConfig(s.conf.UpstreamDNS, s.conf.BootstrapDNS, DefaultTimeout)
if err != nil {
return fmt.Errorf("DNS: proxy.ParseUpstreamsConfig: %s", err)
}
s.conf.Upstreams = upstreamConfig.Upstreams
s.conf.DomainsReservedUpstreams = upstreamConfig.DomainReservedUpstreams
}
if len(s.conf.UpstreamDNS) == 0 {
s.conf.UpstreamDNS = defaultDNS
}
if len(s.conf.BootstrapDNS) == 0 {
s.conf.BootstrapDNS = defaultBootstrap
}
upstreamConfig, err := proxy.ParseUpstreamsConfig(s.conf.UpstreamDNS, s.conf.BootstrapDNS, DefaultTimeout)
if err != nil {
return fmt.Errorf("DNS: proxy.ParseUpstreamsConfig: %s", err)
}
s.conf.Upstreams = upstreamConfig.Upstreams
s.conf.DomainsReservedUpstreams = upstreamConfig.DomainReservedUpstreams
if len(s.conf.ParentalBlockHost) == 0 {
s.conf.ParentalBlockHost = parentalBlockHost
}
if len(s.conf.SafeBrowsingBlockHost) == 0 {
s.conf.SafeBrowsingBlockHost = safeBrowsingBlockHost
}
if s.conf.UDPListenAddr == nil {
s.conf.UDPListenAddr = defaultValues.UDPListenAddr
}
if s.conf.TCPListenAddr == nil {
s.conf.TCPListenAddr = defaultValues.TCPListenAddr
}
proxyConfig := proxy.Config{
UDPListenAddr: s.conf.UDPListenAddr,
@ -208,7 +220,7 @@ func (s *Server) startInternal(config *ServerConfig) error {
}
s.access = &accessCtx{}
err := s.access.Init(s.conf.AllowedClients, s.conf.DisallowedClients, s.conf.BlockedHosts)
err = s.access.Init(s.conf.AllowedClients, s.conf.DisallowedClients, s.conf.BlockedHosts)
if err != nil {
return err
}
@ -225,16 +237,8 @@ func (s *Server) startInternal(config *ServerConfig) error {
}
}
if proxyConfig.UDPListenAddr == nil {
proxyConfig.UDPListenAddr = defaultValues.UDPListenAddr
}
if proxyConfig.TCPListenAddr == nil {
proxyConfig.TCPListenAddr = defaultValues.TCPListenAddr
}
if len(proxyConfig.Upstreams) == 0 {
proxyConfig.Upstreams = defaultValues.Upstreams
log.Fatal("len(proxyConfig.Upstreams) == 0")
}
if !s.webRegistered && s.conf.HTTPRegister != nil {
@ -300,6 +304,7 @@ func (s *Server) Reconfigure2(newconf FilteringConfig) error {
return nil
}
// Reconfigure applies the new configuration to the DNS server
func (s *Server) Reconfigure(config *ServerConfig) error {
s.Lock()
defer s.Unlock()

View File

@ -5,7 +5,6 @@ import (
"fmt"
"net"
"net/http"
"runtime"
"strconv"
"strings"
@ -15,12 +14,6 @@ import (
"github.com/miekg/dns"
)
var defaultDNS = []string{
"https://1.1.1.1/dns-query",
"https://1.0.0.1/dns-query",
}
var defaultBootstrap = []string{"1.1.1.1", "1.0.0.1"}
func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) {
text := fmt.Sprintf(format, args...)
log.Info("DNS: %s %s: %s", r.Method, r.URL, text)
@ -58,14 +51,7 @@ func (s *Server) handleSetUpstreamConfig(w http.ResponseWriter, r *http.Request)
}
newconf := FilteringConfig{}
newconf.UpstreamDNS = defaultDNS
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" {
// Use plain DNS on MIPS, encryption is too slow
newconf.UpstreamDNS = []string{"1.1.1.1", "1.0.0.1"}
}
if len(req.Upstreams) != 0 {
newconf.UpstreamDNS = req.Upstreams
}
newconf.UpstreamDNS = req.Upstreams
// bootstrap servers are plain DNS only
for _, host := range req.BootstrapDNS {
@ -74,10 +60,7 @@ func (s *Server) handleSetUpstreamConfig(w http.ResponseWriter, r *http.Request)
return
}
}
newconf.BootstrapDNS = defaultBootstrap
if len(req.BootstrapDNS) != 0 {
newconf.BootstrapDNS = req.BootstrapDNS
}
newconf.BootstrapDNS = req.BootstrapDNS
newconf.AllServers = req.AllServers
err = s.Reconfigure2(newconf)

View File

@ -16,8 +16,8 @@ import (
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/likexian/gokit/assert"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
const (
@ -391,7 +391,7 @@ func createTestServer(t *testing.T) *Server {
s := NewServer(f, nil, nil)
s.conf.UDPListenAddr = &net.UDPAddr{Port: 0}
s.conf.TCPListenAddr = &net.TCPAddr{Port: 0}
s.conf.UpstreamDNS = []string{"8.8.8.8:53", "8.8.4.4:53"}
s.conf.FilteringConfig.ProtectionEnabled = true
return s
}

View File

@ -5,7 +5,6 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"sync"
"time"
@ -115,12 +114,6 @@ type dnsConfig struct {
DnsfilterConf dnsfilter.Config `yaml:",inline"`
}
var defaultDNS = []string{
"https://1.1.1.1/dns-query",
"https://1.0.0.1/dns-query",
}
var defaultBootstrap = []string{"1.1.1.1", "1.0.0.1"}
type tlsConfigSettings struct {
Enabled bool `yaml:"enabled" json:"enabled"` // Enabled is the encryption (DOT/DOH/HTTPS) status
ServerName string `yaml:"server_name" json:"server_name,omitempty"` // ServerName is the hostname of your HTTPS/TLS server
@ -205,13 +198,6 @@ func initConfig() {
config.WebSessionTTLHours = 30 * 24
config.DNS.UpstreamDNS = defaultDNS
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" {
// Use plain DNS on MIPS, encryption is too slow
config.DNS.UpstreamDNS = []string{"1.1.1.1", "1.0.0.1"}
}
config.DNS.BootstrapDNS = defaultBootstrap
config.DNS.CacheSize = 4 * 1024 * 1024
config.DNS.DnsfilterConf.SafeBrowsingCacheSize = 1 * 1024 * 1024
config.DNS.DnsfilterConf.SafeSearchCacheSize = 1 * 1024 * 1024