2021-10-14 19:39:21 +03:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
2022-10-25 15:08:12 +03:00
|
|
|
"net/netip"
|
2021-10-14 19:39:21 +03:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
|
|
|
"github.com/AdguardTeam/urlfilter"
|
|
|
|
"github.com/AdguardTeam/urlfilter/filterlist"
|
2021-11-16 16:16:38 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2021-10-14 19:39:21 +03:00
|
|
|
"github.com/miekg/dns"
|
2022-10-25 15:08:12 +03:00
|
|
|
"golang.org/x/exp/maps"
|
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()
|
|
|
|
}
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
// requestMatcher combines the logic for matching requests and translating the
|
|
|
|
// appropriate rules.
|
|
|
|
type requestMatcher struct {
|
|
|
|
// stateLock protects all the fields of requestMatcher.
|
|
|
|
stateLock *sync.RWMutex
|
|
|
|
|
|
|
|
// rulesStrg stores the rules obtained from the hosts' file.
|
|
|
|
rulesStrg *filterlist.RuleStorage
|
|
|
|
// engine serves rulesStrg.
|
|
|
|
engine *urlfilter.DNSEngine
|
|
|
|
|
|
|
|
// translator maps generated $dnsrewrite rules into hosts-syntax rules.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Store the filename from which the rule was parsed.
|
|
|
|
translator map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchRequest processes the request rewriting hostnames and addresses read
|
2022-01-27 20:18:52 +03:00
|
|
|
// from the operating system's hosts files. res is nil for any request having
|
|
|
|
// not an A/AAAA or PTR type, see man 5 hosts.
|
2021-11-23 18:01:48 +03:00
|
|
|
//
|
|
|
|
// It's safe for concurrent use.
|
|
|
|
func (rm *requestMatcher) MatchRequest(
|
2022-04-13 18:16:33 +03:00
|
|
|
req *urlfilter.DNSRequest,
|
2021-11-23 18:01:48 +03:00
|
|
|
) (res *urlfilter.DNSResult, ok bool) {
|
|
|
|
switch req.DNSType {
|
|
|
|
case dns.TypeA, dns.TypeAAAA, dns.TypePTR:
|
2023-01-24 19:50:19 +03:00
|
|
|
log.Debug("%s: handling the request for %s", hostsContainerPref, req.Hostname)
|
2021-11-23 18:01:48 +03:00
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
rm.stateLock.RLock()
|
|
|
|
defer rm.stateLock.RUnlock()
|
|
|
|
|
|
|
|
return rm.engine.MatchRequest(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate returns the source hosts-syntax rule for the generated dnsrewrite
|
2022-01-12 12:14:59 +03:00
|
|
|
// rule or an empty string if the last doesn't exist. The returned rules are in
|
|
|
|
// a processed format like:
|
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
// ip host1 host2 ...
|
2021-11-23 18:01:48 +03:00
|
|
|
func (rm *requestMatcher) Translate(rule string) (hostRule string) {
|
|
|
|
rm.stateLock.RLock()
|
|
|
|
defer rm.stateLock.RUnlock()
|
|
|
|
|
|
|
|
return rm.translator[rule]
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetEng updates container's engine and the translation map.
|
|
|
|
func (rm *requestMatcher) resetEng(rulesStrg *filterlist.RuleStorage, tr map[string]string) {
|
|
|
|
rm.stateLock.Lock()
|
|
|
|
defer rm.stateLock.Unlock()
|
|
|
|
|
|
|
|
rm.rulesStrg = rulesStrg
|
|
|
|
rm.engine = urlfilter.NewDNSEngine(rm.rulesStrg)
|
|
|
|
|
|
|
|
rm.translator = tr
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
// hostsContainerPref is a prefix for logging and wrapping errors in
|
|
|
|
// HostsContainer's methods.
|
|
|
|
const hostsContainerPref = "hosts container"
|
|
|
|
|
|
|
|
// 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-23 18:01:48 +03:00
|
|
|
// requestMatcher matches the requests and translates the rules. It's
|
|
|
|
// embedded to implement MatchRequest and Translate for *HostsContainer.
|
2021-12-16 15:58:54 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov, e.burkov): Consider fully merging into HostsContainer.
|
2021-11-23 18:01:48 +03:00
|
|
|
requestMatcher
|
2021-10-14 19:39:21 +03:00
|
|
|
|
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.
|
2022-10-25 15:08:12 +03:00
|
|
|
updates chan HostsRecords
|
2021-12-16 15:58:54 +03:00
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
// last is the set of hosts that was cached within last detected change.
|
2022-10-25 15:08:12 +03:00
|
|
|
last HostsRecords
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
// fsys is the working file system to read hosts files from.
|
|
|
|
fsys fs.FS
|
|
|
|
|
|
|
|
// w tracks the changes in specified files and directories.
|
|
|
|
w 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
|
2021-12-16 15:58:54 +03:00
|
|
|
|
|
|
|
// listID is the identifier for the list of generated rules.
|
|
|
|
listID int
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
// HostsRecords is a mapping of an IP address to its hosts data.
|
|
|
|
type HostsRecords map[netip.Addr]*HostsRecord
|
|
|
|
|
|
|
|
// HostsRecord represents a single hosts file record.
|
|
|
|
type HostsRecord struct {
|
|
|
|
Aliases *stringutil.Set
|
|
|
|
Canonical string
|
|
|
|
}
|
|
|
|
|
|
|
|
// equal returns true if all fields of rec are equal to field in other or they
|
|
|
|
// both are nil.
|
|
|
|
func (rec *HostsRecord) equal(other *HostsRecord) (ok bool) {
|
|
|
|
if rec == nil {
|
|
|
|
return other == nil
|
2022-11-07 16:51:07 +03:00
|
|
|
} else if other == nil {
|
|
|
|
return false
|
2022-10-25 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rec.Canonical == other.Canonical && rec.Aliases.Equal(other.Aliases)
|
|
|
|
}
|
|
|
|
|
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(
|
2021-11-26 18:25:43 +03:00
|
|
|
listID int,
|
2021-10-14 19:39:21 +03:00
|
|
|
fsys fs.FS,
|
|
|
|
w aghos.FSWatcher,
|
|
|
|
paths ...string,
|
|
|
|
) (hc *HostsContainer, err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "%s: %w", hostsContainerPref) }()
|
|
|
|
|
|
|
|
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-23 18:01:48 +03:00
|
|
|
requestMatcher: requestMatcher{
|
|
|
|
stateLock: &sync.RWMutex{},
|
|
|
|
},
|
2021-11-26 18:25:43 +03:00
|
|
|
listID: listID,
|
2021-11-17 17:21:10 +03:00
|
|
|
done: make(chan struct{}, 1),
|
2022-10-25 15:08:12 +03:00
|
|
|
updates: make(chan HostsRecords, 1),
|
2021-10-14 19:39:21 +03:00
|
|
|
fsys: fsys,
|
|
|
|
w: w,
|
|
|
|
patterns: patterns,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("%s: starting", hostsContainerPref)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-01-12 12:14:59 +03:00
|
|
|
log.Debug("%s: %s is expected to exist but doesn't", hostsContainerPref, p)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go hc.handleEvents()
|
|
|
|
|
|
|
|
return hc, nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 17:21:10 +03:00
|
|
|
// Close implements the io.Closer interface for *HostsContainer. Close must
|
|
|
|
// only be called once. The returned err is always nil.
|
2021-10-14 19:39:21 +03:00
|
|
|
func (hc *HostsContainer) Close() (err error) {
|
2021-11-16 16:16:38 +03:00
|
|
|
log.Debug("%s: closing", hostsContainerPref)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2021-11-17 17:21:10 +03:00
|
|
|
close(hc.done)
|
|
|
|
|
|
|
|
return nil
|
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.
|
|
|
|
func (hc *HostsContainer) Upd() (updates <-chan HostsRecords) {
|
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() {
|
|
|
|
defer log.OnPanic(fmt.Sprintf("%s: handling events", hostsContainerPref))
|
|
|
|
|
|
|
|
defer close(hc.updates)
|
|
|
|
|
2021-11-17 17:21:10 +03:00
|
|
|
ok, eventsCh := true, hc.w.Events()
|
|
|
|
for ok {
|
|
|
|
select {
|
|
|
|
case _, ok = <-eventsCh:
|
|
|
|
if !ok {
|
|
|
|
log.Debug("%s: watcher closed the events channel", hostsContainerPref)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hc.refresh(); err != nil {
|
|
|
|
log.Error("%s: %s", hostsContainerPref, err)
|
|
|
|
}
|
|
|
|
case _, ok = <-hc.done:
|
|
|
|
// Go on.
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hostsParser is a helper type to parse rules from the operating system's hosts
|
2021-11-23 18:01:48 +03:00
|
|
|
// file. It exists for only a single refreshing session.
|
2021-10-14 19:39:21 +03:00
|
|
|
type hostsParser struct {
|
2022-01-12 12:14:59 +03:00
|
|
|
// rulesBuilder builds the resulting rules list content.
|
2021-11-23 18:01:48 +03:00
|
|
|
rulesBuilder *strings.Builder
|
|
|
|
|
2022-01-27 20:18:52 +03:00
|
|
|
// translations maps generated rules into actual hosts file lines.
|
|
|
|
translations map[string]string
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
// table stores only the unique IP-hostname pairs. It's also sent to the
|
|
|
|
// updates channel afterwards.
|
2022-10-25 15:08:12 +03:00
|
|
|
table HostsRecords
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 12:14:59 +03:00
|
|
|
// newHostsParser creates a new *hostsParser with buffers of size taken from the
|
|
|
|
// previous parse.
|
2021-11-16 16:16:38 +03:00
|
|
|
func (hc *HostsContainer) newHostsParser() (hp *hostsParser) {
|
|
|
|
return &hostsParser{
|
2021-11-23 18:01:48 +03:00
|
|
|
rulesBuilder: &strings.Builder{},
|
2022-01-27 20:18:52 +03:00
|
|
|
translations: map[string]string{},
|
2022-10-25 15:08:12 +03:00
|
|
|
table: make(HostsRecords, len(hc.last)),
|
2021-11-16 16:16:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseFile is a aghos.FileWalker for parsing the files with hosts syntax. It
|
|
|
|
// never signs to stop walking and never returns any additional patterns.
|
2021-10-14 19:39:21 +03:00
|
|
|
//
|
|
|
|
// See man hosts(5).
|
2022-01-12 12:14:59 +03:00
|
|
|
func (hp *hostsParser) parseFile(r io.Reader) (patterns []string, cont bool, err error) {
|
2021-10-14 19:39:21 +03:00
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
for s.Scan() {
|
|
|
|
ip, hosts := hp.parseLine(s.Text())
|
2022-10-25 15:08:12 +03:00
|
|
|
if ip == (netip.Addr{}) || len(hosts) == 0 {
|
2021-10-14 19:39:21 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
hp.addRecord(ip, hosts)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, true, s.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseLine parses the line having the hosts syntax ignoring invalid ones.
|
2022-10-25 15:08:12 +03:00
|
|
|
func (hp *hostsParser) parseLine(line string) (ip netip.Addr, hosts []string) {
|
2021-10-14 19:39:21 +03:00
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 2 {
|
2022-10-25 15:08:12 +03:00
|
|
|
return netip.Addr{}, nil
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
ip, err := netip.ParseAddr(fields[0])
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, nil
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fields[1:] {
|
2021-12-16 15:58:54 +03:00
|
|
|
hashIdx := strings.IndexByte(f, '#')
|
|
|
|
if hashIdx == 0 {
|
|
|
|
// The rest of the fields are a part of the comment so return.
|
|
|
|
break
|
|
|
|
} else if hashIdx > 0 {
|
|
|
|
// Only a part of the field is a comment.
|
|
|
|
f = f[:hashIdx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that invalid hosts aren't turned into rules.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3946.
|
2022-01-27 20:18:52 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Investigate if hosts may contain DNS-SD domains.
|
2023-02-21 16:52:33 +03:00
|
|
|
err = netutil.ValidateHostname(f)
|
2021-12-16 15:58:54 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("%s: host %q is invalid, ignoring", hostsContainerPref, f)
|
2021-12-09 18:26:56 +03:00
|
|
|
|
|
|
|
continue
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
2021-12-09 18:26:56 +03:00
|
|
|
|
2021-12-16 15:58:54 +03:00
|
|
|
hosts = append(hosts, f)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ip, hosts
|
|
|
|
}
|
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
// addRecord 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.
|
2022-10-25 15:08:12 +03:00
|
|
|
func (hp *hostsParser) addRecord(ip netip.Addr, hosts []string) {
|
2022-07-07 19:33:32 +03:00
|
|
|
line := strings.Join(append([]string{ip.String()}, hosts...), " ")
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
rec, ok := hp.table[ip]
|
2022-01-27 20:18:52 +03:00
|
|
|
if !ok {
|
2022-07-07 19:33:32 +03:00
|
|
|
rec = &HostsRecord{
|
|
|
|
Aliases: stringutil.NewSet(),
|
|
|
|
}
|
2021-11-23 18:01:48 +03:00
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
rec.Canonical, hosts = hosts[0], hosts[1:]
|
|
|
|
hp.addRules(ip, rec.Canonical, line)
|
2022-10-25 15:08:12 +03:00
|
|
|
hp.table[ip] = rec
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
2022-01-12 12:14:59 +03:00
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
for _, host := range hosts {
|
|
|
|
if rec.Canonical == host || rec.Aliases.Has(host) {
|
2021-11-23 18:01:48 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
rec.Aliases.Add(host)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
hp.addRules(ip, host, line)
|
2022-01-27 20:18:52 +03:00
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
// addRules adds rules and rule translations for the line.
|
2022-10-25 15:08:12 +03:00
|
|
|
func (hp *hostsParser) addRules(ip netip.Addr, host, line string) {
|
2022-07-07 19:33:32 +03:00
|
|
|
rule, rulePtr := hp.writeRules(host, ip)
|
|
|
|
hp.translations[rule], hp.translations[rulePtr] = line, line
|
|
|
|
|
|
|
|
log.Debug("%s: added ip-host pair %q-%q", hostsContainerPref, ip, host)
|
|
|
|
}
|
|
|
|
|
2022-03-23 20:47:45 +03:00
|
|
|
// writeRules writes the actual rule for the qtype and the PTR for the host-ip
|
|
|
|
// pair into internal builders.
|
2022-10-25 15:08:12 +03:00
|
|
|
func (hp *hostsParser) writeRules(host string, ip netip.Addr) (rule, rulePtr string) {
|
|
|
|
// TODO(a.garipov): Add a netip.Addr version to netutil.
|
|
|
|
arpa, err := netutil.IPToReversedAddr(ip.AsSlice())
|
2021-10-14 19:39:21 +03:00
|
|
|
if err != nil {
|
2022-01-27 20:18:52 +03:00
|
|
|
return "", ""
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
const (
|
|
|
|
nl = "\n"
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
rwSuccess = "^$dnsrewrite=NOERROR;"
|
|
|
|
rwSuccessPTR = "^$dnsrewrite=NOERROR;PTR;"
|
|
|
|
|
2021-12-23 16:35:10 +03:00
|
|
|
modLen = len(rules.MaskPipe) + len(rwSuccess) + len(";")
|
|
|
|
modLenPTR = len(rules.MaskPipe) + len(rwSuccessPTR)
|
2021-11-16 16:16:38 +03:00
|
|
|
)
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
var qtype string
|
|
|
|
// The validation of the IP address has been performed earlier so it is
|
|
|
|
// guaranteed to be either an IPv4 or an IPv6.
|
2022-10-25 15:08:12 +03:00
|
|
|
if ip.Is4() {
|
2021-11-23 18:01:48 +03:00
|
|
|
qtype = "A"
|
|
|
|
} else {
|
|
|
|
qtype = "AAAA"
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
ipStr := ip.String()
|
|
|
|
fqdn := dns.Fqdn(host)
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
ruleBuilder := &strings.Builder{}
|
|
|
|
ruleBuilder.Grow(modLen + len(host) + len(qtype) + len(ipStr))
|
2022-01-27 20:18:52 +03:00
|
|
|
stringutil.WriteToBuilder(ruleBuilder, rules.MaskPipe, host, rwSuccess, qtype, ";", ipStr)
|
|
|
|
rule = ruleBuilder.String()
|
2021-11-23 18:01:48 +03:00
|
|
|
|
|
|
|
ruleBuilder.Reset()
|
2021-12-23 16:35:10 +03:00
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
ruleBuilder.Grow(modLenPTR + len(arpa) + len(fqdn))
|
2021-12-23 16:35:10 +03:00
|
|
|
stringutil.WriteToBuilder(ruleBuilder, rules.MaskPipe, arpa, rwSuccessPTR, fqdn)
|
|
|
|
|
2022-01-27 20:18:52 +03:00
|
|
|
rulePtr = ruleBuilder.String()
|
2021-11-23 18:01:48 +03:00
|
|
|
|
2022-01-27 20:18:52 +03:00
|
|
|
hp.rulesBuilder.Grow(len(rule) + len(rulePtr) + 2*len(nl))
|
|
|
|
stringutil.WriteToBuilder(hp.rulesBuilder, rule, nl, rulePtr, nl)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2022-01-27 20:18:52 +03:00
|
|
|
return rule, rulePtr
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendUpd tries to send the parsed data to the ch.
|
2022-10-25 15:08:12 +03:00
|
|
|
func (hp *hostsParser) sendUpd(ch chan HostsRecords) {
|
2021-10-14 19:39:21 +03:00
|
|
|
log.Debug("%s: sending upd", hostsContainerPref)
|
2021-11-16 16:16:38 +03:00
|
|
|
|
|
|
|
upd := hp.table
|
2021-10-14 19:39:21 +03:00
|
|
|
select {
|
2021-11-16 16:16:38 +03:00
|
|
|
case ch <- upd:
|
2021-10-14 19:39:21 +03:00
|
|
|
// Updates are delivered. Go on.
|
2021-11-16 16:16:38 +03:00
|
|
|
case <-ch:
|
|
|
|
ch <- upd
|
|
|
|
log.Debug("%s: replaced the last update", hostsContainerPref)
|
|
|
|
case ch <- upd:
|
|
|
|
// The previous update was just read and the next one pushed. Go on.
|
2021-10-14 19:39:21 +03:00
|
|
|
default:
|
2021-11-23 18:01:48 +03:00
|
|
|
log.Error("%s: the updates channel is broken", hostsContainerPref)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStrg creates a new rules storage from parsed data.
|
2021-11-26 18:25:43 +03:00
|
|
|
func (hp *hostsParser) newStrg(id int) (s *filterlist.RuleStorage, err error) {
|
2021-10-14 19:39:21 +03:00
|
|
|
return filterlist.NewRuleStorage([]filterlist.RuleList{&filterlist.StringRuleList{
|
2021-11-26 18:25:43 +03:00
|
|
|
ID: id,
|
2021-11-23 18:01:48 +03:00
|
|
|
RulesText: hp.rulesBuilder.String(),
|
2021-10-14 19:39:21 +03:00
|
|
|
IgnoreCosmetic: true,
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
log.Debug("%s: refreshing", hostsContainerPref)
|
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
hp := hc.newHostsParser()
|
|
|
|
if _, err = aghos.FileWalker(hp.parseFile).Walk(hc.fsys, hc.patterns...); err != nil {
|
|
|
|
return fmt.Errorf("refreshing : %w", err)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-11-07 16:51:07 +03:00
|
|
|
// hc.last is nil on the first refresh, so let that one through.
|
|
|
|
if hc.last != nil && maps.EqualFunc(hp.table, hc.last, (*HostsRecord).equal) {
|
2022-01-12 12:14:59 +03:00
|
|
|
log.Debug("%s: no changes detected", hostsContainerPref)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
defer hp.sendUpd(hc.updates)
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
hc.last = maps.Clone(hp.table)
|
2021-11-16 16:16:38 +03:00
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
var rulesStrg *filterlist.RuleStorage
|
2021-11-26 18:25:43 +03:00
|
|
|
if rulesStrg, err = hp.newStrg(hc.listID); err != nil {
|
2021-10-14 19:39:21 +03:00
|
|
|
return fmt.Errorf("initializing rules storage: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-01-27 20:18:52 +03:00
|
|
|
hc.resetEng(rulesStrg, hp.translations)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|