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"]