From 27006f58c506b3dd365a1dea3a9e42f41d5c2a6e Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 16 Jul 2019 12:55:18 +0300 Subject: [PATCH 1/3] - filters: windows: fix update procedure We couldn't write filter files on Windows due to "file is being used" error. --- home/filter.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/home/filter.go b/home/filter.go index c01ce55b..4e509866 100644 --- a/home/filter.go +++ b/home/filter.go @@ -170,12 +170,15 @@ func periodicallyRefreshFilters() { // Algorithm: // . Get the list of filters to be updated // . For each filter run the download and checksum check operation -// . If filter data hasn't changed, set new update time -// . If filter data has changed, parse it, save it on disk, set new update time +// . Stop server +// . For each filter: +// . If filter data hasn't changed, just set new update time on file +// . If filter data has changed, save it on disk // . Apply changes to the current configuration -// . Restart server +// . Start server func refreshFiltersIfNecessary(force bool) int { var updateFilters []filter + var updateFlags []bool // 'true' if filter data has changed if config.firstRun { return 0 @@ -210,21 +213,28 @@ func refreshFiltersIfNecessary(force bool) int { log.Printf("Failed to update filter %s: %s\n", uf.URL, err) continue } + uf.LastUpdated = time.Now() + updateFlags = append(updateFlags, updated) + } + + isRunning := isRunning() + _ = dnsServer.Stop() + + for i := range updateFilters { + uf := &updateFilters[i] + updated := updateFlags[i] if updated { // Saving it to the filters dir now - err = uf.save() + err := uf.save() if err != nil { log.Printf("Failed to save the updated filter %d: %s", uf.ID, err) continue } - } else { - mtime := time.Now() - e := os.Chtimes(uf.Path(), mtime, mtime) + e := os.Chtimes(uf.Path(), uf.LastUpdated, uf.LastUpdated) if e != nil { log.Error("os.Chtimes(): %v", e) } - uf.LastUpdated = mtime } config.Lock() @@ -249,7 +259,7 @@ func refreshFiltersIfNecessary(force bool) int { config.Unlock() } - if updateCount > 0 && isRunning() { + if updateCount > 0 && isRunning { err := reconfigureDNSServer() if err != nil { msg := fmt.Sprintf("SHOULD NOT HAPPEN: cannot reconfigure DNS server with the new filters: %s", err) From 94f3bf44d7d8f4761957e40882b8ae11475dd9da Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 16 Jul 2019 12:55:47 +0300 Subject: [PATCH 2/3] - filters: start DNS server after filter has been removed --- home/dns.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/home/dns.go b/home/dns.go index ae9b8f42..52f57202 100644 --- a/home/dns.go +++ b/home/dns.go @@ -256,10 +256,6 @@ func startDNSServer() error { } func reconfigureDNSServer() error { - if !isRunning() { - return nil - } - config, err := generateServerConfig() if err != nil { return errorx.Decorate(err, "Couldn't start forwarding DNS server") From 2c91de73af78a032c076dbad6dcdb85e22e82536 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 16 Jul 2019 12:55:55 +0300 Subject: [PATCH 3/3] * minor --- home/filter.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/home/filter.go b/home/filter.go index 4e509866..2a56a847 100644 --- a/home/filter.go +++ b/home/filter.go @@ -262,8 +262,7 @@ func refreshFiltersIfNecessary(force bool) int { if updateCount > 0 && isRunning { err := reconfigureDNSServer() if err != nil { - msg := fmt.Sprintf("SHOULD NOT HAPPEN: cannot reconfigure DNS server with the new filters: %s", err) - panic(msg) + log.Error("cannot reconfigure DNS server with the new filters: %s", err) } } return updateCount