*: change initDNSServer method, add getDataDir

This commit is contained in:
Andrey Meshkov 2019-10-02 16:53:23 +03:00
parent 9b23acf6da
commit 33ae359cc1
6 changed files with 14 additions and 10 deletions

View File

@ -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 {

View File

@ -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())
}()
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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 {