Skip to content

Commit

Permalink
* auto-hosts: support '#' comments after ip and hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
szolin committed Jun 17, 2020
1 parent ec6ca93 commit 9b8c710
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion util/auto_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string]string,
}
for i := 1; i != len(fields); i++ {
host := fields[i]
if len(host) == 0 {
if len(host) == 0 || host[0] == '#' {
break
}
a.updateTable(table, host, ipAddr)
Expand Down
6 changes: 5 additions & 1 deletion util/auto_hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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(" 127.0.0.1 host localhost # comment \n")
_, _ = f.WriteString(" ::1 localhost \n")

ah.Init(f.Name())
Expand All @@ -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"]
Expand Down

0 comments on commit 9b8c710

Please sign in to comment.