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 <E.Burkov@AdGuard.COM>
Date:   Wed Aug 23 18:50:14 2023 +0300

    Merge branch 'master' into 6132-fix-hosts-startup

commit c5d99bcefa870ba0d2543158e97b3001f65be459
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 23 18:01:17 2023 +0300

    filtering: fix hosts results

commit b7acf266ad73520a0b795c495c8fc75c547ed993
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 23 17:34:43 2023 +0300

    all: revert changes of log of changes

commit 293240d5b1277cebd26732c535ad004af76df532
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 23 17:30:07 2023 +0300

    aghnet: imp logs

commit d1f7d73477a1a8fed5b1fb8b7f42d1c92acd919c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 23 17:19:54 2023 +0300

    aghnet: impl handle set

commit b643793c537fcdd4ba00bae4d7207cb4f1d60d80
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 23 17:00:05 2023 +0300

    aghnet: fix initial refresh
This commit is contained in:
Eugene Burkov 2023-08-23 18:57:24 +03:00
parent 4b04c620f2
commit 2b9019313b
3 changed files with 26 additions and 18 deletions

View file

@ -201,7 +201,7 @@ func (hc *HostsContainer) handleEvents() {
} }
if err := hc.refresh(); err != nil { if err := hc.refresh(); err != nil {
log.Error("%s: %s", hostsContainerPrefix, err) log.Error("%s: warning: refreshing: %s", hostsContainerPrefix, err)
} }
case _, ok = <-hc.done: case _, ok = <-hc.done:
// Go on. // Go on.
@ -244,9 +244,7 @@ func (idx *hostsIndex) walk(r io.Reader) (patterns []string, cont bool, err erro
// type check // type check
var _ hostsfile.Set = (*hostsIndex)(nil) var _ hostsfile.Set = (*hostsIndex)(nil)
// Add puts the record for the IP address to the rules builder if needed. // Add implements the [hostsfile.Set] interface for *hostsIndex.
// The first host is considered to be the canonical name for the IP address.
// hosts must have at least one name.
func (idx *hostsIndex) Add(rec *hostsfile.Record) { func (idx *hostsIndex) Add(rec *hostsfile.Record) {
idx.addrs[rec.Addr] = append(idx.addrs[rec.Addr], rec) idx.addrs[rec.Addr] = append(idx.addrs[rec.Addr], rec)
for _, name := range rec.Names { 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]. // equalRecs is an equality function for [*hostsfile.Record].
func equalRecs(a, b *hostsfile.Record) (ok bool) { func equalRecs(a, b *hostsfile.Record) (ok bool) {
return a.Addr == b.Addr && a.Source == b.Source && slices.Equal(a.Names, b.Names) 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...) _, err = aghos.FileWalker(idx.walk).Walk(hc.fsys, hc.patterns...)
if err != nil { if err != nil {
if len(idx.addrs) == 0 { // Don't wrap the error since it's informative enough as is.
return fmt.Errorf("refreshing : %w", err) return err
} else {
log.Debug("%s: refreshing: %s", hostsContainerPrefix, err)
}
} }
// TODO(e.burkov): Serialize updates using time. // TODO(e.burkov): Serialize updates using time.

View file

@ -1,8 +1,6 @@
package filtering package filtering
import ( import (
"net"
"github.com/AdguardTeam/golibs/hostsfile" "github.com/AdguardTeam/golibs/hostsfile"
"github.com/AdguardTeam/urlfilter" "github.com/AdguardTeam/urlfilter"
"github.com/AdguardTeam/urlfilter/rules" "github.com/AdguardTeam/urlfilter/rules"
@ -112,13 +110,13 @@ func appendRewriteResultFromHost(
return vals, resRules return vals, resRules
} }
vals = append(vals, net.IP(rec.Addr.AsSlice())) vals = append(vals, rec.Addr)
case dns.TypeAAAA: case dns.TypeAAAA:
if !rec.Addr.Is6() { if !rec.Addr.Is6() {
return vals, resRules return vals, resRules
} }
vals = append(vals, net.IP(rec.Addr.AsSlice())) vals = append(vals, rec.Addr)
case dns.TypePTR: case dns.TypePTR:
for _, name := range rec.Names { for _, name := range rec.Names {
vals = append(vals, name) vals = append(vals, name)

View file

@ -2,7 +2,6 @@ package filtering
import ( import (
"fmt" "fmt"
"net"
"net/netip" "net/netip"
"path" "path"
"testing" "testing"
@ -265,7 +264,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
Text: "1.2.3.4 v4.host.example", Text: "1.2.3.4 v4.host.example",
FilterListID: SysHostsListID, FilterListID: SysHostsListID,
}}, }},
wantResps: []rules.RRValue{net.IP(addrv4.AsSlice())}, wantResps: []rules.RRValue{addrv4},
}, { }, {
name: "v6", name: "v6",
host: "v6.host.example", host: "v6.host.example",
@ -274,7 +273,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
Text: "::1 v6.host.example", Text: "::1 v6.host.example",
FilterListID: SysHostsListID, FilterListID: SysHostsListID,
}}, }},
wantResps: []rules.RRValue{net.IP(addrv6.AsSlice())}, wantResps: []rules.RRValue{addrv6},
}, { }, {
name: "mapped", name: "mapped",
host: "mapped.host.example", host: "mapped.host.example",
@ -283,7 +282,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
Text: "::ffff:1.2.3.4 mapped.host.example", Text: "::ffff:1.2.3.4 mapped.host.example",
FilterListID: SysHostsListID, FilterListID: SysHostsListID,
}}, }},
wantResps: []rules.RRValue{net.IP(addrMapped.AsSlice())}, wantResps: []rules.RRValue{addrMapped},
}, { }, {
name: "ptr", name: "ptr",
host: "4.3.2.1.in-addr.arpa", host: "4.3.2.1.in-addr.arpa",