From 1356ac26334cd80c1afa1624a6cc5d37302cb6ea Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Thu, 11 Jun 2020 11:11:52 +0300 Subject: [PATCH] - clients: fix incorrect info for clients from /etc/hosts Close #1786 Squashed commit of the following: commit fc662252be38f20c9afda46fcb578efa50c9baf2 Author: Simon Zolin Date: Thu Jun 11 10:57:26 2020 +0300 - clients: fix incorrect info for clients from /etc/hosts --- home/clients.go | 16 +++++++--------- util/auto_hosts.go | 8 ++++---- util/auto_hosts_test.go | 7 +++---- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/home/clients.go b/home/clients.go index 0f25c755..369be2bc 100644 --- a/home/clients.go +++ b/home/clients.go @@ -612,15 +612,13 @@ func (clients *clientsContainer) addFromHostsFile() { _ = clients.rmHosts(ClientSourceHostsFile) n := 0 - for ip, names := range hosts { - for _, name := range names { - ok, err := clients.addHost(ip, name.String(), ClientSourceHostsFile) - if err != nil { - log.Debug("Clients: %s", err) - } - if ok { - n++ - } + for ip, name := range hosts { + ok, err := clients.addHost(ip, name, ClientSourceHostsFile) + if err != nil { + log.Debug("Clients: %s", err) + } + if ok { + n++ } } diff --git a/util/auto_hosts.go b/util/auto_hosts.go index 6e78f1ee..a980f5a2 100644 --- a/util/auto_hosts.go +++ b/util/auto_hosts.go @@ -371,11 +371,11 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) string { return host } -// List - get the hosts table. Thread-safe. -func (a *AutoHosts) List() map[string][]net.IP { - table := make(map[string][]net.IP) +// List - get "IP -> hostname" table. Thread-safe. +func (a *AutoHosts) List() map[string]string { + table := make(map[string]string) a.lock.Lock() - for k, v := range a.table { + for k, v := range a.tableReverse { table[k] = v } a.lock.Unlock() diff --git a/util/auto_hosts_test.go b/util/auto_hosts_test.go index 3871b394..17055e50 100644 --- a/util/auto_hosts_test.go +++ b/util/auto_hosts_test.go @@ -49,10 +49,9 @@ func TestAutoHostsResolution(t *testing.T) { // Test hosts file table := ah.List() - ips, _ = table["host"] - assert.NotNil(t, ips) - assert.Equal(t, 1, len(ips)) - assert.Equal(t, "127.0.0.1", ips[0].String()) + name, ok := table["127.0.0.1"] + assert.True(t, ok) + assert.Equal(t, "host", name) // Test PTR a, _ := dns.ReverseAddr("127.0.0.1")