From e7f34cc435f53b80aca328a62e27c6aaa8fe6462 Mon Sep 17 00:00:00 2001
From: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu, 3 Oct 2024 17:55:39 +0300
Subject: [PATCH] home: fix client storage panic

---
 internal/home/clients.go |  6 +++---
 internal/home/home.go    | 16 ++++++++--------
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/internal/home/clients.go b/internal/home/clients.go
index 5a30d6bc..eef52834 100644
--- a/internal/home/clients.go
+++ b/internal/home/clients.go
@@ -92,9 +92,9 @@ func (clients *clientsContainer) Init(
 	// TODO(e.burkov):  The option should probably be returned, since hosts file
 	// currently used not only for clients' information enrichment, but also in
 	// the filtering module and upstream addresses resolution.
-	var hosts client.HostsContainer = etcHosts
-	if !config.Clients.Sources.HostsFile {
-		hosts = nil
+	var hosts client.HostsContainer
+	if config.Clients.Sources.HostsFile && etcHosts != nil {
+		hosts = etcHosts
 	}
 
 	clients.storage, err = client.NewStorage(&client.StorageConfig{
diff --git a/internal/home/home.go b/internal/home/home.go
index df4e4296..56718394 100644
--- a/internal/home/home.go
+++ b/internal/home/home.go
@@ -148,6 +148,14 @@ func setupContext(opts options) (err error) {
 	Context.tlsRoots = aghtls.SystemRootCAs()
 	Context.mux = http.NewServeMux()
 
+	if !opts.noEtcHosts {
+		err = setupHostsContainer()
+		if err != nil {
+			// Don't wrap the error, because it's informative enough as is.
+			return err
+		}
+	}
+
 	if Context.firstRun {
 		log.Info("This is the first time AdGuard Home is launched")
 		checkPermissions()
@@ -168,14 +176,6 @@ func setupContext(opts options) (err error) {
 		os.Exit(0)
 	}
 
-	if !opts.noEtcHosts {
-		err = setupHostsContainer()
-		if err != nil {
-			// Don't wrap the error, because it's informative enough as is.
-			return err
-		}
-	}
-
 	return nil
 }