diff --git a/dnsforward/dnsforward.go b/dnsforward/dnsforward.go index df4f4d68..9f2ddd0d 100644 --- a/dnsforward/dnsforward.go +++ b/dnsforward/dnsforward.go @@ -7,6 +7,8 @@ import ( "sync" "time" + "github.com/pkg/errors" + "github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/dnsproxy/proxy" @@ -97,21 +99,19 @@ func (s *Server) startInternal(config *ServerConfig) error { s.ServerConfig = *config } - if s.dnsFilter == nil { - log.Printf("Creating dnsfilter") - s.dnsFilter = dnsfilter.New(&s.Config) - // add rules only if they are enabled - if s.FilteringEnabled { - // TODO: Handle error - s.dnsFilter.AddRules(s.Filters) - } + if s.dnsFilter != nil || s.dnsProxy != nil { + return errors.New("DNS server is already started") + } + + err := s.initDNSFilter() + if err != nil { + return err } log.Printf("Loading stats from querylog") - err := fillStatsFromQueryLog() + err = fillStatsFromQueryLog() if err != nil { - log.Printf("Failed to load stats from querylog: %s", err) - return err + return errorx.Decorate(err, "failed to load stats from querylog") } once.Do(func() { @@ -139,12 +139,23 @@ func (s *Server) startInternal(config *ServerConfig) error { proxyConfig.Upstreams = defaultValues.Upstreams } - // TODO: Don't let call Start the second time - // Initialize the DNS proxy + // Initialize and start the DNS proxy s.dnsProxy = &proxy.Proxy{Config: proxyConfig} + return s.dnsProxy.Start() +} - err = s.dnsProxy.Start() - return err +// Initializes the DNS filter +func (s *Server) initDNSFilter() error { + log.Printf("Creating dnsfilter") + s.dnsFilter = dnsfilter.New(&s.Config) + // add rules only if they are enabled + if s.FilteringEnabled { + err := s.dnsFilter.AddRules(s.Filters) + if err != nil { + return errorx.Decorate(err, "could not initialize dnsfilter") + } + } + return nil } // Stop stops the DNS server diff --git a/dnsforward/querylog.go b/dnsforward/querylog.go index 82c06f6a..7058a3b9 100644 --- a/dnsforward/querylog.go +++ b/dnsforward/querylog.go @@ -3,7 +3,6 @@ package dnsforward import ( "encoding/json" "fmt" - "log" "net/http" "strconv" "strings" @@ -12,6 +11,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/miekg/dns" + log "github.com/sirupsen/logrus" ) const ( diff --git a/dnsforward/querylog_file.go b/dnsforward/querylog_file.go index 19097baa..43a93093 100644 --- a/dnsforward/querylog_file.go +++ b/dnsforward/querylog_file.go @@ -5,11 +5,12 @@ import ( "compress/gzip" "encoding/json" "fmt" - "log" "os" "sync" "time" + log "github.com/sirupsen/logrus" + "github.com/go-test/deep" ) diff --git a/dnsforward/querylog_top.go b/dnsforward/querylog_top.go index b78dea79..26c896fa 100644 --- a/dnsforward/querylog_top.go +++ b/dnsforward/querylog_top.go @@ -3,7 +3,6 @@ package dnsforward import ( "bytes" "fmt" - "log" "net/http" "os" "path" @@ -14,6 +13,8 @@ import ( "sync" "time" + log "github.com/sirupsen/logrus" + "github.com/bluele/gcache" "github.com/miekg/dns" ) diff --git a/dnsforward/stats.go b/dnsforward/stats.go index 9cfe5f58..2befcad2 100644 --- a/dnsforward/stats.go +++ b/dnsforward/stats.go @@ -3,11 +3,12 @@ package dnsforward import ( "encoding/json" "fmt" - "log" "net/http" "sync" "time" + log "github.com/sirupsen/logrus" + "github.com/AdguardTeam/AdGuardHome/dnsfilter" ) diff --git a/go.mod b/go.mod index 7567227d..22246242 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/joomcode/errorx v0.1.0 github.com/miekg/dns v1.1.1 github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/pkg/errors v0.8.0 github.com/shirou/gopsutil v2.18.10+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect github.com/sirupsen/logrus v1.2.0