From 2b9019313b1c1ae024e7c8439c1c4c69284520f1 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Wed, 23 Aug 2023 18:57:24 +0300 Subject: [PATCH] Pull request 1973: 6132 fix hosts stratup Merge in DNS/adguard-home from 6132-fix-hosts-startup to master Updates #6132. Squashed commit of the following: commit 7495e62531f7c0bd775969195da1cbd446f018f7 Merge: c5d99bcef 4b04c620f Author: Eugene Burkov Date: Wed Aug 23 18:50:14 2023 +0300 Merge branch 'master' into 6132-fix-hosts-startup commit c5d99bcefa870ba0d2543158e97b3001f65be459 Author: Eugene Burkov Date: Wed Aug 23 18:01:17 2023 +0300 filtering: fix hosts results commit b7acf266ad73520a0b795c495c8fc75c547ed993 Author: Eugene Burkov Date: Wed Aug 23 17:34:43 2023 +0300 all: revert changes of log of changes commit 293240d5b1277cebd26732c535ad004af76df532 Author: Eugene Burkov Date: Wed Aug 23 17:30:07 2023 +0300 aghnet: imp logs commit d1f7d73477a1a8fed5b1fb8b7f42d1c92acd919c Author: Eugene Burkov Date: Wed Aug 23 17:19:54 2023 +0300 aghnet: impl handle set commit b643793c537fcdd4ba00bae4d7207cb4f1d60d80 Author: Eugene Burkov Date: Wed Aug 23 17:00:05 2023 +0300 aghnet: fix initial refresh --- internal/aghnet/hostscontainer.go | 31 ++++++++++++++++++--------- internal/filtering/dnsrewrite.go | 6 ++---- internal/filtering/dnsrewrite_test.go | 7 +++--- 3 files changed, 26 insertions(+), 18 deletions(-) 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",