From 4870da7f94e040fea63f2ff99bf1b79ee5da457e Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Wed, 17 Jun 2020 19:36:35 +0300 Subject: [PATCH] * auto-hosts: support '#' comments after ip and hosts Close #1807 Squashed commit of the following: commit 9d3e2809df056354bb6195abf8106616c1e214b1 Author: Simon Zolin Date: Wed Jun 17 19:23:15 2020 +0300 improve commit 9b8c7104560c9c80f848f15818d49a065a38e498 Author: Simon Zolin Date: Wed Jun 17 11:07:56 2020 +0300 * auto-hosts: support '#' comments after ip and hosts --- util/auto_hosts.go | 10 ++++++++++ util/auto_hosts_test.go | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/util/auto_hosts.go b/util/auto_hosts.go index a980f5a2..f36ee1e7 100644 --- a/util/auto_hosts.go +++ b/util/auto_hosts.go @@ -172,8 +172,18 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string]string, if len(host) == 0 { break } + sharp := strings.IndexByte(host, '#') + if sharp == 0 { + break // skip the rest of the line after # + } else if sharp > 0 { + host = host[:sharp] + } + a.updateTable(table, host, ipAddr) a.updateTableRev(tableRev, host, ipAddr) + if sharp >= 0 { + break // skip the rest of the line after # + } } } } diff --git a/util/auto_hosts_test.go b/util/auto_hosts_test.go index 17055e50..322e7e9c 100644 --- a/util/auto_hosts_test.go +++ b/util/auto_hosts_test.go @@ -29,8 +29,8 @@ func TestAutoHostsResolution(t *testing.T) { defer func() { _ = os.Remove(f.Name()) }() defer f.Close() - _, _ = f.WriteString(" 127.0.0.1 host localhost \n") - _, _ = f.WriteString(" ::1 localhost \n") + _, _ = f.WriteString(" 127.0.0.1 host localhost # comment \n") + _, _ = f.WriteString(" ::1 localhost#comment \n") ah.Init(f.Name()) @@ -47,6 +47,10 @@ func TestAutoHostsResolution(t *testing.T) { ips = ah.Process("newhost", dns.TypeA) assert.Nil(t, ips) + // Unknown host (comment) + ips = ah.Process("comment", dns.TypeA) + assert.Nil(t, ips) + // Test hosts file table := ah.List() name, ok := table["127.0.0.1"]