diff --git a/internal/aghnet/hostscontainer.go b/internal/aghnet/hostscontainer.go index 79f89594..0d9a4bcc 100644 --- a/internal/aghnet/hostscontainer.go +++ b/internal/aghnet/hostscontainer.go @@ -201,7 +201,7 @@ func (hc *HostsContainer) handleEvents() { } if err := hc.refresh(); err != nil { - log.Error("%s: %s", hostsContainerPrefix, err) + log.Error("%s: warning: refreshing: %s", hostsContainerPrefix, err) } case _, ok = <-hc.done: // Go on. @@ -244,9 +244,7 @@ func (idx *hostsIndex) walk(r io.Reader) (patterns []string, cont bool, err erro // type check var _ hostsfile.Set = (*hostsIndex)(nil) -// Add puts the record for the IP address to the rules builder if needed. -// The first host is considered to be the canonical name for the IP address. -// hosts must have at least one name. +// Add implements the [hostsfile.Set] interface for *hostsIndex. func (idx *hostsIndex) Add(rec *hostsfile.Record) { idx.addrs[rec.Addr] = append(idx.addrs[rec.Addr], rec) for _, name := range rec.Names { @@ -254,6 +252,23 @@ func (idx *hostsIndex) Add(rec *hostsfile.Record) { } } +// type check +var _ hostsfile.HandleSet = (*hostsIndex)(nil) + +// HandleInvalid implements the [hostsfile.HandleSet] interface for *hostsIndex. +func (idx *hostsIndex) HandleInvalid(src string, _ []byte, err error) { + lineErr := &hostsfile.LineError{} + if !errors.As(err, &lineErr) { + // Must not happen if idx passed to [hostsfile.Parse]. + return + } else if errors.Is(lineErr, hostsfile.ErrEmptyLine) { + // Ignore empty lines. + return + } + + log.Info("%s: warning: parsing %q: %s", hostsContainerPrefix, src, lineErr) +} + // equalRecs is an equality function for [*hostsfile.Record]. func equalRecs(a, b *hostsfile.Record) (ok bool) { return a.Addr == b.Addr && a.Source == b.Source && slices.Equal(a.Names, b.Names) @@ -291,13 +306,9 @@ func (hc *HostsContainer) refresh() (err error) { } _, err = aghos.FileWalker(idx.walk).Walk(hc.fsys, hc.patterns...) - if err != nil { - if len(idx.addrs) == 0 { - return fmt.Errorf("refreshing : %w", err) - } else { - log.Debug("%s: refreshing: %s", hostsContainerPrefix, err) - } + // Don't wrap the error since it's informative enough as is. + return err } // TODO(e.burkov): Serialize updates using time. diff --git a/internal/filtering/dnsrewrite.go b/internal/filtering/dnsrewrite.go index edfc8b91..3fd6e778 100644 --- a/internal/filtering/dnsrewrite.go +++ b/internal/filtering/dnsrewrite.go @@ -1,8 +1,6 @@ package filtering import ( - "net" - "github.com/AdguardTeam/golibs/hostsfile" "github.com/AdguardTeam/urlfilter" "github.com/AdguardTeam/urlfilter/rules" @@ -112,13 +110,13 @@ func appendRewriteResultFromHost( return vals, resRules } - vals = append(vals, net.IP(rec.Addr.AsSlice())) + vals = append(vals, rec.Addr) case dns.TypeAAAA: if !rec.Addr.Is6() { return vals, resRules } - vals = append(vals, net.IP(rec.Addr.AsSlice())) + vals = append(vals, rec.Addr) case dns.TypePTR: for _, name := range rec.Names { vals = append(vals, name) diff --git a/internal/filtering/dnsrewrite_test.go b/internal/filtering/dnsrewrite_test.go index ee88cb09..98853c95 100644 --- a/internal/filtering/dnsrewrite_test.go +++ b/internal/filtering/dnsrewrite_test.go @@ -2,7 +2,6 @@ package filtering import ( "fmt" - "net" "net/netip" "path" "testing" @@ -265,7 +264,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) { Text: "1.2.3.4 v4.host.example", FilterListID: SysHostsListID, }}, - wantResps: []rules.RRValue{net.IP(addrv4.AsSlice())}, + wantResps: []rules.RRValue{addrv4}, }, { name: "v6", host: "v6.host.example", @@ -274,7 +273,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) { Text: "::1 v6.host.example", FilterListID: SysHostsListID, }}, - wantResps: []rules.RRValue{net.IP(addrv6.AsSlice())}, + wantResps: []rules.RRValue{addrv6}, }, { name: "mapped", host: "mapped.host.example", @@ -283,7 +282,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) { Text: "::ffff:1.2.3.4 mapped.host.example", FilterListID: SysHostsListID, }}, - wantResps: []rules.RRValue{net.IP(addrMapped.AsSlice())}, + wantResps: []rules.RRValue{addrMapped}, }, { name: "ptr", host: "4.3.2.1.in-addr.arpa",