2021-10-14 19:39:21 +03:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
2023-11-16 14:05:10 +03:00
|
|
|
"context"
|
2021-10-14 19:39:21 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
2022-10-25 15:08:12 +03:00
|
|
|
"net/netip"
|
2021-10-14 19:39:21 +03:00
|
|
|
"path"
|
2023-11-16 14:05:10 +03:00
|
|
|
"strings"
|
2023-08-22 16:45:11 +03:00
|
|
|
"sync/atomic"
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2023-11-16 14:05:10 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-10-14 19:39:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2023-08-22 16:45:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
2021-10-14 19:39:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2022-10-25 15:08:12 +03:00
|
|
|
"golang.org/x/exp/maps"
|
2023-08-22 16:45:11 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2021-10-14 19:39:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultHostsPaths returns the slice of paths default for the operating system
|
|
|
|
// to files and directories which are containing the hosts database. The result
|
2021-11-16 16:16:38 +03:00
|
|
|
// is intended to be used within fs.FS so the initial slash is omitted.
|
2021-10-14 19:39:21 +03:00
|
|
|
func DefaultHostsPaths() (paths []string) {
|
|
|
|
return defaultHostsPaths()
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// MatchAddr returns the records for the IP address.
|
|
|
|
func (hc *HostsContainer) MatchAddr(ip netip.Addr) (recs []*hostsfile.Record) {
|
|
|
|
cur := hc.current.Load()
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
2021-11-23 18:01:48 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
return cur.addrs[ip]
|
2021-11-23 18:01:48 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// MatchName returns the records for the hostname.
|
|
|
|
func (hc *HostsContainer) MatchName(name string) (recs []*hostsfile.Record) {
|
|
|
|
cur := hc.current.Load()
|
|
|
|
if cur != nil {
|
|
|
|
recs = cur.names[name]
|
|
|
|
}
|
2021-11-23 18:01:48 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
return recs
|
2021-11-23 18:01:48 +03:00
|
|
|
}
|
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
// hostsContainerPrefix is a prefix for logging and wrapping errors in
|
2021-10-14 19:39:21 +03:00
|
|
|
// HostsContainer's methods.
|
2023-03-28 13:27:46 +03:00
|
|
|
const hostsContainerPrefix = "hosts container"
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// Hosts is a map of IP addresses to the records, as it primarily stored in the
|
|
|
|
// [HostsContainer]. It should not be accessed for writing since it may be read
|
|
|
|
// concurrently, users should clone it before modifying.
|
|
|
|
//
|
|
|
|
// The order of records for each address is preserved from original files, but
|
|
|
|
// the order of the addresses, being a map key, is not.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Probably, this should be a sorted slice of records.
|
|
|
|
type Hosts map[netip.Addr][]*hostsfile.Record
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
// HostsContainer stores the relevant hosts database provided by the OS and
|
|
|
|
// processes both A/AAAA and PTR DNS requests for those.
|
|
|
|
type HostsContainer struct {
|
2021-11-17 17:21:10 +03:00
|
|
|
// done is the channel to sign closing the container.
|
|
|
|
done chan struct{}
|
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
// updates is the channel for receiving updated hosts.
|
2023-08-22 16:45:11 +03:00
|
|
|
updates chan Hosts
|
2021-12-16 15:58:54 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// current is the last set of hosts parsed.
|
|
|
|
current atomic.Pointer[hostsIndex]
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
// fsys is the working file system to read hosts files from.
|
|
|
|
fsys fs.FS
|
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
// watcher tracks the changes in specified files and directories.
|
|
|
|
watcher aghos.FSWatcher
|
2021-12-16 15:58:54 +03:00
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
// patterns stores specified paths in the fs.Glob-compatible form.
|
|
|
|
patterns []string
|
2022-10-25 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
2021-11-17 17:21:10 +03:00
|
|
|
// ErrNoHostsPaths is returned when there are no valid paths to watch passed to
|
|
|
|
// the HostsContainer.
|
|
|
|
const ErrNoHostsPaths errors.Error = "no valid paths to hosts files provided"
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
// NewHostsContainer creates a container of hosts, that watches the paths with
|
2021-11-26 18:25:43 +03:00
|
|
|
// w. listID is used as an identifier of the underlying rules list. paths
|
|
|
|
// shouldn't be empty and each of paths should locate either a file or a
|
|
|
|
// directory in fsys. fsys and w must be non-nil.
|
2021-10-14 19:39:21 +03:00
|
|
|
func NewHostsContainer(
|
|
|
|
fsys fs.FS,
|
|
|
|
w aghos.FSWatcher,
|
|
|
|
paths ...string,
|
|
|
|
) (hc *HostsContainer, err error) {
|
2023-03-28 13:27:46 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "%s: %w", hostsContainerPrefix) }()
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
if len(paths) == 0 {
|
2021-11-17 17:21:10 +03:00
|
|
|
return nil, ErrNoHostsPaths
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2021-11-17 17:21:10 +03:00
|
|
|
var patterns []string
|
|
|
|
patterns, err = pathsToPatterns(fsys, paths)
|
2021-10-14 19:39:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-17 17:21:10 +03:00
|
|
|
} else if len(patterns) == 0 {
|
|
|
|
return nil, ErrNoHostsPaths
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hc = &HostsContainer{
|
2021-11-17 17:21:10 +03:00
|
|
|
done: make(chan struct{}, 1),
|
2023-08-22 16:45:11 +03:00
|
|
|
updates: make(chan Hosts, 1),
|
2021-10-14 19:39:21 +03:00
|
|
|
fsys: fsys,
|
2023-03-28 13:27:46 +03:00
|
|
|
watcher: w,
|
2021-10-14 19:39:21 +03:00
|
|
|
patterns: patterns,
|
|
|
|
}
|
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
log.Debug("%s: starting", hostsContainerPrefix)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
// Load initially.
|
|
|
|
if err = hc.refresh(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
2021-11-17 17:21:10 +03:00
|
|
|
if err = w.Add(p); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return nil, fmt.Errorf("adding path: %w", err)
|
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
log.Debug("%s: %s is expected to exist but doesn't", hostsContainerPrefix, p)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go hc.handleEvents()
|
|
|
|
|
|
|
|
return hc, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
// Close implements the [io.Closer] interface for *HostsContainer. It closes
|
|
|
|
// both itself and its [aghos.FSWatcher]. Close must only be called once.
|
2021-10-14 19:39:21 +03:00
|
|
|
func (hc *HostsContainer) Close() (err error) {
|
2023-03-28 13:27:46 +03:00
|
|
|
log.Debug("%s: closing", hostsContainerPrefix)
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
err = errors.Annotate(hc.watcher.Close(), "closing fs watcher: %w")
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
// Go on and close the container either way.
|
2021-11-17 17:21:10 +03:00
|
|
|
close(hc.done)
|
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
return err
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
// Upd returns the channel into which the updates are sent.
|
2023-08-22 16:45:11 +03:00
|
|
|
func (hc *HostsContainer) Upd() (updates <-chan Hosts) {
|
2021-10-14 19:39:21 +03:00
|
|
|
return hc.updates
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathsToPatterns converts paths into patterns compatible with fs.Glob.
|
|
|
|
func pathsToPatterns(fsys fs.FS, paths []string) (patterns []string, err error) {
|
|
|
|
for i, p := range paths {
|
|
|
|
var fi fs.FileInfo
|
2021-11-17 17:21:10 +03:00
|
|
|
fi, err = fs.Stat(fsys, p)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't put a filename here since it's already added by fs.Stat.
|
|
|
|
return nil, fmt.Errorf("path at index %d: %w", i, err)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
p = path.Join(p, "*")
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns = append(patterns, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:14:59 +03:00
|
|
|
// handleEvents concurrently handles the file system events. It closes the
|
|
|
|
// update channel of HostsContainer when finishes. It's used to be called
|
|
|
|
// within a separate goroutine.
|
2021-10-14 19:39:21 +03:00
|
|
|
func (hc *HostsContainer) handleEvents() {
|
2023-03-28 13:27:46 +03:00
|
|
|
defer log.OnPanic(fmt.Sprintf("%s: handling events", hostsContainerPrefix))
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
defer close(hc.updates)
|
|
|
|
|
2023-03-28 13:27:46 +03:00
|
|
|
ok, eventsCh := true, hc.watcher.Events()
|
2021-11-17 17:21:10 +03:00
|
|
|
for ok {
|
|
|
|
select {
|
|
|
|
case _, ok = <-eventsCh:
|
|
|
|
if !ok {
|
2023-03-28 13:27:46 +03:00
|
|
|
log.Debug("%s: watcher closed the events channel", hostsContainerPrefix)
|
2021-11-17 17:21:10 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hc.refresh(); err != nil {
|
2023-08-23 18:57:24 +03:00
|
|
|
log.Error("%s: warning: refreshing: %s", hostsContainerPrefix, err)
|
2021-11-17 17:21:10 +03:00
|
|
|
}
|
|
|
|
case _, ok = <-hc.done:
|
|
|
|
// Go on.
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// sendUpd tries to send the parsed data to the ch.
|
|
|
|
func (hc *HostsContainer) sendUpd(recs Hosts) {
|
|
|
|
log.Debug("%s: sending upd", hostsContainerPrefix)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
ch := hc.updates
|
|
|
|
select {
|
|
|
|
case ch <- recs:
|
|
|
|
// Updates are delivered. Go on.
|
|
|
|
case <-ch:
|
|
|
|
ch <- recs
|
|
|
|
log.Debug("%s: replaced the last update", hostsContainerPrefix)
|
|
|
|
case ch <- recs:
|
|
|
|
// The previous update was just read and the next one pushed. Go on.
|
|
|
|
default:
|
|
|
|
log.Error("%s: the updates channel is broken", hostsContainerPrefix)
|
2021-11-16 16:16:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// hostsIndex is a [hostsfile.Set] to enumerate all the records.
|
|
|
|
type hostsIndex struct {
|
|
|
|
// addrs maps IP addresses to the records.
|
|
|
|
addrs Hosts
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// names maps hostnames to the records.
|
|
|
|
names map[string][]*hostsfile.Record
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// walk is a file walking function for hostsIndex.
|
|
|
|
func (idx *hostsIndex) walk(r io.Reader) (patterns []string, cont bool, err error) {
|
|
|
|
return nil, true, hostsfile.Parse(idx, r, nil)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// type check
|
|
|
|
var _ hostsfile.Set = (*hostsIndex)(nil)
|
|
|
|
|
2023-08-23 18:57:24 +03:00
|
|
|
// Add implements the [hostsfile.Set] interface for *hostsIndex.
|
2023-08-22 16:45:11 +03:00
|
|
|
func (idx *hostsIndex) Add(rec *hostsfile.Record) {
|
|
|
|
idx.addrs[rec.Addr] = append(idx.addrs[rec.Addr], rec)
|
|
|
|
for _, name := range rec.Names {
|
|
|
|
idx.names[name] = append(idx.names[name], rec)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
2022-07-07 19:33:32 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 18:57:24 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// 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)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// equalRecSlices is an equality function for slices of [*hostsfile.Record].
|
|
|
|
func equalRecSlices(a, b []*hostsfile.Record) (ok bool) { return slices.EqualFunc(a, b, equalRecs) }
|
2021-11-16 16:16:38 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// Equal returns true if indexes are equal.
|
|
|
|
func (idx *hostsIndex) Equal(other *hostsIndex) (ok bool) {
|
|
|
|
if idx == nil {
|
|
|
|
return other == nil
|
|
|
|
} else if other == nil {
|
|
|
|
return false
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
return maps.EqualFunc(idx.addrs, other.addrs, equalRecSlices)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
// refresh gets the data from specified files and propagates the updates if
|
|
|
|
// needed.
|
2021-11-17 17:21:10 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Accept a parameter to specify the files to refresh.
|
2021-10-14 19:39:21 +03:00
|
|
|
func (hc *HostsContainer) refresh() (err error) {
|
2023-03-28 13:27:46 +03:00
|
|
|
log.Debug("%s: refreshing", hostsContainerPrefix)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
var addrLen, nameLen int
|
|
|
|
last := hc.current.Load()
|
|
|
|
if last != nil {
|
|
|
|
addrLen, nameLen = len(last.addrs), len(last.names)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
2023-08-22 16:45:11 +03:00
|
|
|
idx := &hostsIndex{
|
|
|
|
addrs: make(Hosts, addrLen),
|
|
|
|
names: make(map[string][]*hostsfile.Record, nameLen),
|
2021-11-16 16:16:38 +03:00
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
_, err = aghos.FileWalker(idx.walk).Walk(hc.fsys, hc.patterns...)
|
|
|
|
if err != nil {
|
2023-08-23 18:57:24 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
// TODO(e.burkov): Serialize updates using time.
|
|
|
|
if !last.Equal(idx) {
|
|
|
|
hc.current.Store(idx)
|
|
|
|
hc.sendUpd(idx.addrs)
|
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-16 14:05:10 +03:00
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ upstream.Resolver = (*HostsContainer)(nil)
|
|
|
|
|
|
|
|
// LookupNetIP implements the [upstream.Resolver] interface for *HostsContainer.
|
|
|
|
func (hc *HostsContainer) LookupNetIP(
|
|
|
|
ctx context.Context,
|
|
|
|
network string,
|
|
|
|
hostname string,
|
|
|
|
) (addrs []netip.Addr, err error) {
|
|
|
|
// TODO(e.burkov): Think of extracting this logic to a golibs function if
|
|
|
|
// needed anywhere else.
|
|
|
|
var isDesiredProto func(ip netip.Addr) (ok bool)
|
|
|
|
switch network {
|
|
|
|
case "ip4":
|
|
|
|
isDesiredProto = (netip.Addr).Is4
|
|
|
|
case "ip6":
|
|
|
|
isDesiredProto = (netip.Addr).Is6
|
|
|
|
case "ip":
|
|
|
|
isDesiredProto = func(ip netip.Addr) (ok bool) { return true }
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported network: %q", network)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := hc.current.Load()
|
|
|
|
recs := idx.names[strings.ToLower(hostname)]
|
|
|
|
|
|
|
|
addrs = make([]netip.Addr, 0, len(recs))
|
|
|
|
for _, rec := range recs {
|
|
|
|
if isDesiredProto(rec.Addr) {
|
|
|
|
addrs = append(addrs, rec.Addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return slices.Clip(addrs), nil
|
|
|
|
}
|