dnsforward -- initialize all dnsfilter settings at start and reconfigure

This commit is contained in:
Eugene Bujak 2018-11-30 13:51:15 +03:00
parent 2281b60ebb
commit 0502ef6cc7
1 changed files with 22 additions and 5 deletions

View File

@ -178,7 +178,12 @@ func (s *Server) Start(config *ServerConfig) error {
if s.dnsFilter == nil { if s.dnsFilter == nil {
log.Printf("Creating dnsfilter") log.Printf("Creating dnsfilter")
s.dnsFilter = dnsfilter.New(nil) dnsFilterConfig := &config.Config
s.dnsFilter = dnsfilter.New(dnsFilterConfig)
// add rules only if they are enabled
if s.FilteringEnabled {
s.dnsFilter.AddRules(s.Filters)
}
} }
go s.packetLoop() go s.packetLoop()
@ -308,7 +313,7 @@ func (s *Server) reconfigureUpstreams(new ServerConfig) {
s.Upstreams = new.Upstreams s.Upstreams = new.Upstreams
} }
func (s *Server) reconfigureFilters(new ServerConfig) { func (s *Server) reconfigureFiltering(new ServerConfig) {
newFilters := new.Filters newFilters := new.Filters
if len(newFilters) == 0 { if len(newFilters) == 0 {
newFilters = defaultValues.Filters newFilters = defaultValues.Filters
@ -317,11 +322,22 @@ func (s *Server) reconfigureFilters(new ServerConfig) {
if len(oldFilters) == 0 { if len(oldFilters) == 0 {
oldFilters = defaultValues.Filters oldFilters = defaultValues.Filters
} }
if reflect.DeepEqual(newFilters, oldFilters) {
// they're exactly the same, do nothing needUpdate := false
if !reflect.DeepEqual(newFilters, oldFilters) {
needUpdate = true
}
if !reflect.DeepEqual(new.FilteringConfig, s.FilteringConfig) {
needUpdate = true
}
if !needUpdate {
// nothing to do, everything is same
return return
} }
// TODO: instead of creating new dnsfilter, change existing one's settings and filters
dnsFilter := dnsfilter.New(&new.Config) // sets safebrowsing, safesearch and parental dnsFilter := dnsfilter.New(&new.Config) // sets safebrowsing, safesearch and parental
// add rules only if they are enabled // add rules only if they are enabled
@ -332,6 +348,7 @@ func (s *Server) reconfigureFilters(new ServerConfig) {
s.Lock() s.Lock()
oldDnsFilter := s.dnsFilter oldDnsFilter := s.dnsFilter
s.dnsFilter = dnsFilter s.dnsFilter = dnsFilter
s.FilteringConfig = new.FilteringConfig
s.Unlock() s.Unlock()
oldDnsFilter.Destroy() oldDnsFilter.Destroy()
@ -340,7 +357,7 @@ func (s *Server) reconfigureFilters(new ServerConfig) {
func (s *Server) Reconfigure(new ServerConfig) error { func (s *Server) Reconfigure(new ServerConfig) error {
s.reconfigureBlockedResponseTTL(new) s.reconfigureBlockedResponseTTL(new)
s.reconfigureUpstreams(new) s.reconfigureUpstreams(new)
s.reconfigureFilters(new) s.reconfigureFiltering(new)
err := s.reconfigureListenAddr(new) err := s.reconfigureListenAddr(new)
if err != nil { if err != nil {