Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/networks/usernet/dnshosts/dnshosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,30 @@ func (z zoneHost) name() string {
if i < 0 {
return string(z)
}
return string(z)[i+1:] + "."
return string(z)[z.zoneIndex()+1:] + "."
}

func (z zoneHost) recordName() string {
i := z.dotIndex()
if i < 0 {
return ""
}
return string(z)[:i]

j := z.zoneIndex()
if j < 0 {
return ""
}

return string(z)[:j]
}

func (z zoneHost) dotIndex() int {
return strings.LastIndex(string(z), ".")
}

func (z zoneHost) zoneIndex() int {
parts := strings.Split(string(z), ".")
host := strings.Join(parts[:len(parts)-1], ".")

return strings.LastIndex(host, ".")
}
27 changes: 15 additions & 12 deletions pkg/networks/usernet/dnshosts/dnshosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func Test_zoneHost(t *testing.T) {
}{
{}, // test for empty value as well
{host: "sample", want: val{name: "sample"}},
{host: "another.sample", want: val{name: "sample.", recordName: "another"}},
{host: "another.sample.com", want: val{name: "com.", recordName: "another.sample"}},
{host: "a.c", want: val{name: "c.", recordName: "a"}},
{host: "a.b.c.d", want: val{name: "d.", recordName: "a.b.c"}},
{host: "another.sample", want: val{name: "another.sample."}},
{host: "another.sample.com", want: val{name: "sample.com.", recordName: "another"}},
{host: "a.c", want: val{name: "a.c."}},
{host: "a.b.c.d", want: val{name: "c.d.", recordName: "a.b"}},
}
for i, tt := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
Expand Down Expand Up @@ -148,23 +148,26 @@ func TestExtractZones(t *testing.T) {
{
wantZones: []types.Zone{
{
Name: "ae.",
Name: "google.ae.",
DefaultIP: net.ParseIP("8.8.4.4"),
},
{
Name: "google.com.",
DefaultIP: net.ParseIP("8.8.4.4"),
Records: []types.Record{
{Name: "google", IP: net.ParseIP("8.8.4.4")},
{Name: "local", IP: net.ParseIP("8.8.8.8")},
},
},
{
Name: "com.",
Name: "docker.internal.",
Records: []types.Record{
{Name: "google", IP: net.ParseIP("8.8.4.4")},
{Name: "local.google", IP: net.ParseIP("8.8.8.8")},
{Name: "host", IP: net.ParseIP("192.168.5.2")},
},
},
{
Name: "internal.",
Name: "lima.internal.",
Records: []types.Record{
{Name: "host.docker", IP: net.ParseIP("192.168.5.2")},
{Name: "host.lima", IP: net.ParseIP("192.168.5.2")},
{Name: "host", IP: net.ParseIP("192.168.5.2")},
},
},
{
Expand Down
Loading