2021-10-14 17:39:21 +01:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
2022-10-25 13:08:12 +01:00
|
|
|
"net/netip"
|
2021-10-14 17:39:21 +01:00
|
|
|
"path"
|
2023-08-22 14:45:11 +01:00
|
|
|
"sync/atomic"
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2023-08-22 14:45:11 +01:00
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
2021-10-14 17:39:21 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2023-03-28 11:27:46 +01:00
|
|
|
// hostsContainerPrefix is a prefix for logging and wrapping errors in
|
2021-10-14 17:39:21 +01:00
|
|
|
// HostsContainer's methods.
|
2023-03-28 11:27:46 +01:00
|
|
|
const hostsContainerPrefix = "hosts container"
|
2021-10-14 17:39:21 +01: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 14:21:10 +00:00
|
|
|
// done is the channel to sign closing the container.
|
|
|
|
done chan struct{}
|
|
|
|
|
2021-11-16 13:16:38 +00:00
|
|
|
// updates is the channel for receiving updated hosts.
|
2023-12-07 13:48:55 +00:00
|
|
|
updates chan *hostsfile.DefaultStorage
|
2021-12-16 12:58:54 +00:00
|
|
|
|
2023-08-22 14:45:11 +01:00
|
|
|
// current is the last set of hosts parsed.
|
2023-12-07 13:48:55 +00:00
|
|
|
current atomic.Pointer[hostsfile.DefaultStorage]
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
// fsys is the working file system to read hosts files from.
|
|
|
|
fsys fs.FS
|
|
|
|
|
2023-03-28 11:27:46 +01:00
|
|
|
// watcher tracks the changes in specified files and directories.
|
|
|
|
watcher aghos.FSWatcher
|
2021-12-16 12:58:54 +00:00
|
|
|
|
2021-10-14 17:39:21 +01:00
|
|
|
// patterns stores specified paths in the fs.Glob-compatible form.
|
|
|
|
patterns []string
|
2022-10-25 13:08:12 +01:00
|
|
|
}
|
|
|
|
|
2021-11-17 14:21:10 +00: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 17:39:21 +01:00
|
|
|
|
|
|
|
// NewHostsContainer creates a container of hosts, that watches the paths with
|
2021-11-26 15:25:43 +00: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 17:39:21 +01:00
|
|
|
func NewHostsContainer(
|
|
|
|
fsys fs.FS,
|
|
|
|
w aghos.FSWatcher,
|
|
|
|
paths ...string,
|
|
|
|
) (hc *HostsContainer, err error) {
|
2023-03-28 11:27:46 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "%s: %w", hostsContainerPrefix) }()
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
if len(paths) == 0 {
|
2021-11-17 14:21:10 +00:00
|
|
|
return nil, ErrNoHostsPaths
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
2021-11-17 14:21:10 +00:00
|
|
|
var patterns []string
|
|
|
|
patterns, err = pathsToPatterns(fsys, paths)
|
2021-10-14 17:39:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-17 14:21:10 +00:00
|
|
|
} else if len(patterns) == 0 {
|
|
|
|
return nil, ErrNoHostsPaths
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hc = &HostsContainer{
|
2021-11-17 14:21:10 +00:00
|
|
|
done: make(chan struct{}, 1),
|
2023-12-07 13:48:55 +00:00
|
|
|
updates: make(chan *hostsfile.DefaultStorage, 1),
|
2021-10-14 17:39:21 +01:00
|
|
|
fsys: fsys,
|
2023-03-28 11:27:46 +01:00
|
|
|
watcher: w,
|
2021-10-14 17:39:21 +01:00
|
|
|
patterns: patterns,
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:27:46 +01:00
|
|
|
log.Debug("%s: starting", hostsContainerPrefix)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
// Load initially.
|
|
|
|
if err = hc.refresh(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
2021-11-17 14:21:10 +00: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 17:39:21 +01:00
|
|
|
|
2023-03-28 11:27:46 +01:00
|
|
|
log.Debug("%s: %s is expected to exist but doesn't", hostsContainerPrefix, p)
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go hc.handleEvents()
|
|
|
|
|
|
|
|
return hc, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:27:46 +01: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 17:39:21 +01:00
|
|
|
func (hc *HostsContainer) Close() (err error) {
|
2023-03-28 11:27:46 +01:00
|
|
|
log.Debug("%s: closing", hostsContainerPrefix)
|
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
err = errors.Annotate(hc.watcher.Close(), "closing fs watcher: %w")
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
// Go on and close the container either way.
|
2021-11-17 14:21:10 +00:00
|
|
|
close(hc.done)
|
|
|
|
|
2023-03-28 11:27:46 +01:00
|
|
|
return err
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:48:55 +00:00
|
|
|
// Upd returns the channel into which the updates are sent. The updates
|
|
|
|
// themselves must not be modified.
|
|
|
|
func (hc *HostsContainer) Upd() (updates <-chan *hostsfile.DefaultStorage) {
|
2021-10-14 17:39:21 +01:00
|
|
|
return hc.updates
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:48:55 +00:00
|
|
|
// type check
|
|
|
|
var _ hostsfile.Storage = (*HostsContainer)(nil)
|
|
|
|
|
|
|
|
// ByAddr implements the [hostsfile.Storage] interface for *HostsContainer.
|
|
|
|
func (hc *HostsContainer) ByAddr(addr netip.Addr) (names []string) {
|
|
|
|
return hc.current.Load().ByAddr(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByName implements the [hostsfile.Storage] interface for *HostsContainer.
|
|
|
|
func (hc *HostsContainer) ByName(name string) (addrs []netip.Addr) {
|
|
|
|
return hc.current.Load().ByName(name)
|
|
|
|
}
|
|
|
|
|
2021-10-14 17:39:21 +01:00
|
|
|
// 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 14:21:10 +00:00
|
|
|
fi, err = fs.Stat(fsys, p)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:48:55 +00:00
|
|
|
// Don't put a filename here since it's already added by [fs.Stat].
|
2021-11-17 14:21:10 +00:00
|
|
|
return nil, fmt.Errorf("path at index %d: %w", i, err)
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
p = path.Join(p, "*")
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns = append(patterns, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
// handleEvents concurrently handles the file system events. It closes the
|
2024-02-12 15:45:51 +00:00
|
|
|
// update channel of HostsContainer when finishes. It is intended to be used as
|
|
|
|
// a goroutine.
|
2021-10-14 17:39:21 +01:00
|
|
|
func (hc *HostsContainer) handleEvents() {
|
2023-03-28 11:27:46 +01:00
|
|
|
defer log.OnPanic(fmt.Sprintf("%s: handling events", hostsContainerPrefix))
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
defer close(hc.updates)
|
|
|
|
|
2023-03-28 11:27:46 +01:00
|
|
|
ok, eventsCh := true, hc.watcher.Events()
|
2021-11-17 14:21:10 +00:00
|
|
|
for ok {
|
|
|
|
select {
|
|
|
|
case _, ok = <-eventsCh:
|
|
|
|
if !ok {
|
2023-03-28 11:27:46 +01:00
|
|
|
log.Debug("%s: watcher closed the events channel", hostsContainerPrefix)
|
2021-11-17 14:21:10 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hc.refresh(); err != nil {
|
2023-08-23 16:57:24 +01:00
|
|
|
log.Error("%s: warning: refreshing: %s", hostsContainerPrefix, err)
|
2021-11-17 14:21:10 +00:00
|
|
|
}
|
|
|
|
case _, ok = <-hc.done:
|
|
|
|
// Go on.
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:45:11 +01:00
|
|
|
// sendUpd tries to send the parsed data to the ch.
|
2023-12-07 13:48:55 +00:00
|
|
|
func (hc *HostsContainer) sendUpd(recs *hostsfile.DefaultStorage) {
|
2023-08-22 14:45:11 +01:00
|
|
|
log.Debug("%s: sending upd", hostsContainerPrefix)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2023-08-22 14:45:11 +01: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 13:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// refresh gets the data from specified files and propagates the updates if
|
|
|
|
// needed.
|
2021-11-17 14:21:10 +00:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Accept a parameter to specify the files to refresh.
|
2021-10-14 17:39:21 +01:00
|
|
|
func (hc *HostsContainer) refresh() (err error) {
|
2023-03-28 11:27:46 +01:00
|
|
|
log.Debug("%s: refreshing", hostsContainerPrefix)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2023-12-07 13:48:55 +00:00
|
|
|
// The error is always nil here since no readers passed.
|
|
|
|
strg, _ := hostsfile.NewDefaultStorage()
|
|
|
|
_, err = aghos.FileWalker(func(r io.Reader) (patterns []string, cont bool, err error) {
|
|
|
|
// Don't wrap the error since it's already informative enough as is.
|
|
|
|
return nil, true, hostsfile.Parse(strg, r, nil)
|
|
|
|
}).Walk(hc.fsys, hc.patterns...)
|
2023-08-22 14:45:11 +01:00
|
|
|
if err != nil {
|
2023-08-23 16:57:24 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 13:48:55 +00:00
|
|
|
// TODO(e.burkov): Serialize updates using [time.Time].
|
|
|
|
if !hc.current.Load().Equal(strg) {
|
|
|
|
hc.current.Store(strg)
|
|
|
|
hc.sendUpd(strg)
|
2023-08-22 14:45:11 +01:00
|
|
|
}
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|