From 33ae359cc1d892d82f4e8fc84b92e1d47e8361d2 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 16:53:23 +0300 Subject: [PATCH] *: change initDNSServer method, add getDataDir --- home/config.go | 5 +++++ home/control_install.go | 6 ++---- home/dns.go | 6 ++++-- home/dns_test.go | 2 +- home/filter.go | 2 +- home/home.go | 3 +-- 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/home/config.go b/home/config.go index e2c52302..68e5fc00 100644 --- a/home/config.go +++ b/home/config.go @@ -239,6 +239,11 @@ func (c *configuration) getConfigFilename() string { return configFile } +// getDataDir returns path to the directory where we store databases and filters +func (c *configuration) getDataDir() string { + return filepath.Join(c.ourWorkingDir, dataDir) +} + // getLogSettings reads logging settings from the config file. // we do it in a separate method in order to configure logger before the actual configuration is parsed and applied. func getLogSettings() logSettings { diff --git a/home/control_install.go b/home/control_install.go index 822f0c0b..7e9e315f 100644 --- a/home/control_install.go +++ b/home/control_install.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "os/exec" - "path/filepath" "strconv" "github.com/AdguardTeam/golibs/log" @@ -236,8 +235,7 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) { config.DNS.BindHost = newSettings.DNS.IP config.DNS.Port = newSettings.DNS.Port - dnsBaseDir := filepath.Join(config.ourWorkingDir, dataDir) - initDNSServer(dnsBaseDir) + initDNSServer() err = startDNSServer() if err != nil { @@ -263,7 +261,7 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) { // until all requests are finished, and _we_ are inside a request right now, so it will block indefinitely if restartHTTP { go func() { - config.httpServer.Shutdown(context.TODO()) + _ = config.httpServer.Shutdown(context.TODO()) }() } diff --git a/home/dns.go b/home/dns.go index 9ea4db47..2a08db3b 100644 --- a/home/dns.go +++ b/home/dns.go @@ -29,7 +29,9 @@ func onConfigModified() { // initDNSServer creates an instance of the dnsforward.Server // Please note that we must do it even if we don't start it // so that we had access to the query log and the stats -func initDNSServer(baseDir string) { +func initDNSServer() { + baseDir := config.getDataDir() + err := os.MkdirAll(baseDir, 0755) if err != nil { log.Fatalf("Cannot create DNS data dir at %s: %s", baseDir, err) @@ -52,7 +54,7 @@ func initDNSServer(baseDir string) { config.queryLog = querylog.New(conf) config.dnsServer = dnsforward.NewServer(config.stats, config.queryLog) - sessFilename := filepath.Join(config.ourWorkingDir, "data/sessions.db") + sessFilename := filepath.Join(baseDir, "sessions.db") config.auth = InitAuth(sessFilename, config.Users) config.Users = nil diff --git a/home/dns_test.go b/home/dns_test.go index 67d7b3ed..49ff3212 100644 --- a/home/dns_test.go +++ b/home/dns_test.go @@ -6,7 +6,7 @@ import ( func TestResolveRDNS(t *testing.T) { config.DNS.BindHost = "1.1.1.1" - initDNSServer(".") + initDNSServer() if r := config.dnsctx.rdns.resolve("1.1.1.1"); r != "one.one.one.one" { t.Errorf("resolveRDNS(): %s", r) } diff --git a/home/filter.go b/home/filter.go index 8bf902a2..80e448ea 100644 --- a/home/filter.go +++ b/home/filter.go @@ -448,7 +448,7 @@ func (filter *filter) unload() { // Path to the filter contents func (filter *filter) Path() string { - return filepath.Join(config.ourWorkingDir, dataDir, filterDir, strconv.FormatInt(filter.ID, 10)+".txt") + return filepath.Join(config.getDataDir(), filterDir, strconv.FormatInt(filter.ID, 10)+".txt") } // LastTimeUpdated returns the time when the filter was last time updated diff --git a/home/home.go b/home/home.go index 5f2d4fc0..80f9e860 100644 --- a/home/home.go +++ b/home/home.go @@ -142,8 +142,7 @@ func run(args options) { log.Fatal(err) } - dnsBaseDir := filepath.Join(config.ourWorkingDir, dataDir) - initDNSServer(dnsBaseDir) + initDNSServer() err = startDNSServer() if err != nil {