Pull request 1786: rm-context-fields
Merge in DNS/adguard-home from rm-context-fields to master Squashed commit of the following: commit 402bbba6a68f163cd6a3751a50ecd544eebc875f Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Mar 28 13:07:11 2023 +0300 aghnet: names commit 209a2bb7878ee9110595f1fcf09df7f83777b80f Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Mar 28 13:03:33 2023 +0300 home: typo commit 61684815c260d5d7a34be612e06714456399a833 Merge: 7adfcf15132ec556
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Mar 28 13:02:18 2023 +0300 Merge branch 'master' into rm-context-fields commit 7adfcf1576b52f80137aa9044355bfbec6243e28 Merge: 0d5b85f50df32601
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Sun Mar 26 14:25:27 2023 +0300 Merge branch 'master' into rm-context-fields commit 0d5b85f51b3eb72c3e905d5ad82c17cd932b246b Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Sun Mar 26 13:21:26 2023 +0300 all: rm unnecessary home context fields
This commit is contained in:
parent
132ec556dc
commit
487675b9ad
|
@ -56,7 +56,7 @@ func (rm *requestMatcher) MatchRequest(
|
||||||
) (res *urlfilter.DNSResult, ok bool) {
|
) (res *urlfilter.DNSResult, ok bool) {
|
||||||
switch req.DNSType {
|
switch req.DNSType {
|
||||||
case dns.TypeA, dns.TypeAAAA, dns.TypePTR:
|
case dns.TypeA, dns.TypeAAAA, dns.TypePTR:
|
||||||
log.Debug("%s: handling the request for %s", hostsContainerPref, req.Hostname)
|
log.Debug("%s: handling the request for %s", hostsContainerPrefix, req.Hostname)
|
||||||
default:
|
default:
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
@ -90,9 +90,9 @@ func (rm *requestMatcher) resetEng(rulesStrg *filterlist.RuleStorage, tr map[str
|
||||||
rm.translator = tr
|
rm.translator = tr
|
||||||
}
|
}
|
||||||
|
|
||||||
// hostsContainerPref is a prefix for logging and wrapping errors in
|
// hostsContainerPrefix is a prefix for logging and wrapping errors in
|
||||||
// HostsContainer's methods.
|
// HostsContainer's methods.
|
||||||
const hostsContainerPref = "hosts container"
|
const hostsContainerPrefix = "hosts container"
|
||||||
|
|
||||||
// HostsContainer stores the relevant hosts database provided by the OS and
|
// HostsContainer stores the relevant hosts database provided by the OS and
|
||||||
// processes both A/AAAA and PTR DNS requests for those.
|
// processes both A/AAAA and PTR DNS requests for those.
|
||||||
|
@ -115,8 +115,8 @@ type HostsContainer struct {
|
||||||
// fsys is the working file system to read hosts files from.
|
// fsys is the working file system to read hosts files from.
|
||||||
fsys fs.FS
|
fsys fs.FS
|
||||||
|
|
||||||
// w tracks the changes in specified files and directories.
|
// watcher tracks the changes in specified files and directories.
|
||||||
w aghos.FSWatcher
|
watcher aghos.FSWatcher
|
||||||
|
|
||||||
// patterns stores specified paths in the fs.Glob-compatible form.
|
// patterns stores specified paths in the fs.Glob-compatible form.
|
||||||
patterns []string
|
patterns []string
|
||||||
|
@ -160,7 +160,7 @@ func NewHostsContainer(
|
||||||
w aghos.FSWatcher,
|
w aghos.FSWatcher,
|
||||||
paths ...string,
|
paths ...string,
|
||||||
) (hc *HostsContainer, err error) {
|
) (hc *HostsContainer, err error) {
|
||||||
defer func() { err = errors.Annotate(err, "%s: %w", hostsContainerPref) }()
|
defer func() { err = errors.Annotate(err, "%s: %w", hostsContainerPrefix) }()
|
||||||
|
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return nil, ErrNoHostsPaths
|
return nil, ErrNoHostsPaths
|
||||||
|
@ -182,11 +182,11 @@ func NewHostsContainer(
|
||||||
done: make(chan struct{}, 1),
|
done: make(chan struct{}, 1),
|
||||||
updates: make(chan HostsRecords, 1),
|
updates: make(chan HostsRecords, 1),
|
||||||
fsys: fsys,
|
fsys: fsys,
|
||||||
w: w,
|
watcher: w,
|
||||||
patterns: patterns,
|
patterns: patterns,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("%s: starting", hostsContainerPref)
|
log.Debug("%s: starting", hostsContainerPrefix)
|
||||||
|
|
||||||
// Load initially.
|
// Load initially.
|
||||||
if err = hc.refresh(); err != nil {
|
if err = hc.refresh(); err != nil {
|
||||||
|
@ -199,7 +199,7 @@ func NewHostsContainer(
|
||||||
return nil, fmt.Errorf("adding path: %w", err)
|
return nil, fmt.Errorf("adding path: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("%s: %s is expected to exist but doesn't", hostsContainerPref, p)
|
log.Debug("%s: %s is expected to exist but doesn't", hostsContainerPrefix, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,14 +208,21 @@ func NewHostsContainer(
|
||||||
return hc, nil
|
return hc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the io.Closer interface for *HostsContainer. Close must
|
// Close implements the [io.Closer] interface for *HostsContainer. It closes
|
||||||
// only be called once. The returned err is always nil.
|
// both itself and its [aghos.FSWatcher]. Close must only be called once.
|
||||||
func (hc *HostsContainer) Close() (err error) {
|
func (hc *HostsContainer) Close() (err error) {
|
||||||
log.Debug("%s: closing", hostsContainerPref)
|
log.Debug("%s: closing", hostsContainerPrefix)
|
||||||
|
|
||||||
|
err = hc.watcher.Close()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("closing fs watcher: %w", err)
|
||||||
|
|
||||||
|
// Go on and close the container either way.
|
||||||
|
}
|
||||||
|
|
||||||
close(hc.done)
|
close(hc.done)
|
||||||
|
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upd returns the channel into which the updates are sent.
|
// Upd returns the channel into which the updates are sent.
|
||||||
|
@ -251,22 +258,22 @@ func pathsToPatterns(fsys fs.FS, paths []string) (patterns []string, err error)
|
||||||
// update channel of HostsContainer when finishes. It's used to be called
|
// update channel of HostsContainer when finishes. It's used to be called
|
||||||
// within a separate goroutine.
|
// within a separate goroutine.
|
||||||
func (hc *HostsContainer) handleEvents() {
|
func (hc *HostsContainer) handleEvents() {
|
||||||
defer log.OnPanic(fmt.Sprintf("%s: handling events", hostsContainerPref))
|
defer log.OnPanic(fmt.Sprintf("%s: handling events", hostsContainerPrefix))
|
||||||
|
|
||||||
defer close(hc.updates)
|
defer close(hc.updates)
|
||||||
|
|
||||||
ok, eventsCh := true, hc.w.Events()
|
ok, eventsCh := true, hc.watcher.Events()
|
||||||
for ok {
|
for ok {
|
||||||
select {
|
select {
|
||||||
case _, ok = <-eventsCh:
|
case _, ok = <-eventsCh:
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debug("%s: watcher closed the events channel", hostsContainerPref)
|
log.Debug("%s: watcher closed the events channel", hostsContainerPrefix)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := hc.refresh(); err != nil {
|
if err := hc.refresh(); err != nil {
|
||||||
log.Error("%s: %s", hostsContainerPref, err)
|
log.Error("%s: %s", hostsContainerPrefix, err)
|
||||||
}
|
}
|
||||||
case _, ok = <-hc.done:
|
case _, ok = <-hc.done:
|
||||||
// Go on.
|
// Go on.
|
||||||
|
@ -345,7 +352,7 @@ func (hp *hostsParser) parseLine(line string) (ip netip.Addr, hosts []string) {
|
||||||
// TODO(e.burkov): Investigate if hosts may contain DNS-SD domains.
|
// TODO(e.burkov): Investigate if hosts may contain DNS-SD domains.
|
||||||
err = netutil.ValidateHostname(f)
|
err = netutil.ValidateHostname(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("%s: host %q is invalid, ignoring", hostsContainerPref, f)
|
log.Error("%s: host %q is invalid, ignoring", hostsContainerPrefix, f)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -389,7 +396,7 @@ func (hp *hostsParser) addRules(ip netip.Addr, host, line string) {
|
||||||
rule, rulePtr := hp.writeRules(host, ip)
|
rule, rulePtr := hp.writeRules(host, ip)
|
||||||
hp.translations[rule], hp.translations[rulePtr] = line, line
|
hp.translations[rule], hp.translations[rulePtr] = line, line
|
||||||
|
|
||||||
log.Debug("%s: added ip-host pair %q-%q", hostsContainerPref, ip, host)
|
log.Debug("%s: added ip-host pair %q-%q", hostsContainerPrefix, ip, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeRules writes the actual rule for the qtype and the PTR for the host-ip
|
// writeRules writes the actual rule for the qtype and the PTR for the host-ip
|
||||||
|
@ -443,7 +450,7 @@ func (hp *hostsParser) writeRules(host string, ip netip.Addr) (rule, rulePtr str
|
||||||
|
|
||||||
// sendUpd tries to send the parsed data to the ch.
|
// sendUpd tries to send the parsed data to the ch.
|
||||||
func (hp *hostsParser) sendUpd(ch chan HostsRecords) {
|
func (hp *hostsParser) sendUpd(ch chan HostsRecords) {
|
||||||
log.Debug("%s: sending upd", hostsContainerPref)
|
log.Debug("%s: sending upd", hostsContainerPrefix)
|
||||||
|
|
||||||
upd := hp.table
|
upd := hp.table
|
||||||
select {
|
select {
|
||||||
|
@ -451,11 +458,11 @@ func (hp *hostsParser) sendUpd(ch chan HostsRecords) {
|
||||||
// Updates are delivered. Go on.
|
// Updates are delivered. Go on.
|
||||||
case <-ch:
|
case <-ch:
|
||||||
ch <- upd
|
ch <- upd
|
||||||
log.Debug("%s: replaced the last update", hostsContainerPref)
|
log.Debug("%s: replaced the last update", hostsContainerPrefix)
|
||||||
case ch <- upd:
|
case ch <- upd:
|
||||||
// The previous update was just read and the next one pushed. Go on.
|
// The previous update was just read and the next one pushed. Go on.
|
||||||
default:
|
default:
|
||||||
log.Error("%s: the updates channel is broken", hostsContainerPref)
|
log.Error("%s: the updates channel is broken", hostsContainerPrefix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,7 +480,7 @@ func (hp *hostsParser) newStrg(id int) (s *filterlist.RuleStorage, err error) {
|
||||||
//
|
//
|
||||||
// TODO(e.burkov): Accept a parameter to specify the files to refresh.
|
// TODO(e.burkov): Accept a parameter to specify the files to refresh.
|
||||||
func (hc *HostsContainer) refresh() (err error) {
|
func (hc *HostsContainer) refresh() (err error) {
|
||||||
log.Debug("%s: refreshing", hostsContainerPref)
|
log.Debug("%s: refreshing", hostsContainerPrefix)
|
||||||
|
|
||||||
hp := hc.newHostsParser()
|
hp := hc.newHostsParser()
|
||||||
if _, err = aghos.FileWalker(hp.parseFile).Walk(hc.fsys, hc.patterns...); err != nil {
|
if _, err = aghos.FileWalker(hp.parseFile).Walk(hc.fsys, hc.patterns...); err != nil {
|
||||||
|
@ -482,7 +489,7 @@ func (hc *HostsContainer) refresh() (err error) {
|
||||||
|
|
||||||
// hc.last is nil on the first refresh, so let that one through.
|
// 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) {
|
if hc.last != nil && maps.EqualFunc(hp.table, hc.last, (*HostsRecord).equal) {
|
||||||
log.Debug("%s: no changes detected", hostsContainerPref)
|
log.Debug("%s: no changes detected", hostsContainerPrefix)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ func TestNewHostsContainer(t *testing.T) {
|
||||||
hc, err := NewHostsContainer(0, testFS, &aghtest.FSWatcher{
|
hc, err := NewHostsContainer(0, testFS, &aghtest.FSWatcher{
|
||||||
OnEvents: onEvents,
|
OnEvents: onEvents,
|
||||||
OnAdd: onAdd,
|
OnAdd: onAdd,
|
||||||
OnClose: func() (err error) { panic("not implemented") },
|
OnClose: func() (err error) { return nil },
|
||||||
}, tc.paths...)
|
}, tc.paths...)
|
||||||
if tc.wantErr != nil {
|
if tc.wantErr != nil {
|
||||||
require.ErrorIs(t, err, tc.wantErr)
|
require.ErrorIs(t, err, tc.wantErr)
|
||||||
|
@ -124,7 +124,7 @@ func TestNewHostsContainer(t *testing.T) {
|
||||||
errWatcher := &aghtest.FSWatcher{
|
errWatcher := &aghtest.FSWatcher{
|
||||||
OnEvents: func() (e <-chan struct{}) { panic("not implemented") },
|
OnEvents: func() (e <-chan struct{}) { panic("not implemented") },
|
||||||
OnAdd: func(name string) (err error) { return errOnAdd },
|
OnAdd: func(name string) (err error) { return errOnAdd },
|
||||||
OnClose: func() (err error) { panic("not implemented") },
|
OnClose: func() (err error) { return nil },
|
||||||
}
|
}
|
||||||
|
|
||||||
hc, err := NewHostsContainer(0, testFS, errWatcher, p)
|
hc, err := NewHostsContainer(0, testFS, errWatcher, p)
|
||||||
|
@ -155,7 +155,7 @@ func TestHostsContainer_refresh(t *testing.T) {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
OnClose: func() (err error) { panic("not implemented") },
|
OnClose: func() (err error) { return nil },
|
||||||
}
|
}
|
||||||
|
|
||||||
hc, err := NewHostsContainer(0, testFS, w, "dir")
|
hc, err := NewHostsContainer(0, testFS, w, "dir")
|
||||||
|
@ -292,7 +292,7 @@ func TestHostsContainer_Translate(t *testing.T) {
|
||||||
stubWatcher := aghtest.FSWatcher{
|
stubWatcher := aghtest.FSWatcher{
|
||||||
OnEvents: func() (e <-chan struct{}) { return nil },
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
||||||
OnAdd: func(name string) (err error) { return nil },
|
OnAdd: func(name string) (err error) { return nil },
|
||||||
OnClose: func() (err error) { panic("not implemented") },
|
OnClose: func() (err error) { return nil },
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, fstest.TestFS(testdata, "etc_hosts"))
|
require.NoError(t, fstest.TestFS(testdata, "etc_hosts"))
|
||||||
|
@ -524,7 +524,7 @@ func TestHostsContainer(t *testing.T) {
|
||||||
stubWatcher := aghtest.FSWatcher{
|
stubWatcher := aghtest.FSWatcher{
|
||||||
OnEvents: func() (e <-chan struct{}) { return nil },
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
||||||
OnAdd: func(name string) (err error) { return nil },
|
OnAdd: func(name string) (err error) { return nil },
|
||||||
OnClose: func() (err error) { panic("not implemented") },
|
OnClose: func() (err error) { return nil },
|
||||||
}
|
}
|
||||||
|
|
||||||
hc, err := NewHostsContainer(listID, testdata, &stubWatcher, "etc_hosts")
|
hc, err := NewHostsContainer(listID, testdata, &stubWatcher, "etc_hosts")
|
||||||
|
|
|
@ -61,11 +61,10 @@ type homeContext struct {
|
||||||
filters *filtering.DNSFilter // DNS filtering module
|
filters *filtering.DNSFilter // DNS filtering module
|
||||||
web *Web // Web (HTTP, HTTPS) module
|
web *Web // Web (HTTP, HTTPS) module
|
||||||
tls *tlsManager // TLS module
|
tls *tlsManager // TLS module
|
||||||
// etcHosts is an IP-hostname pairs set taken from system configuration
|
|
||||||
// (e.g. /etc/hosts) files.
|
// etcHosts contains IP-hostname mappings taken from the OS-specific hosts
|
||||||
|
// configuration files, for example /etc/hosts.
|
||||||
etcHosts *aghnet.HostsContainer
|
etcHosts *aghnet.HostsContainer
|
||||||
// hostsWatcher is the watcher to detect changes in the hosts files.
|
|
||||||
hostsWatcher aghos.FSWatcher
|
|
||||||
|
|
||||||
updater *updater.Updater
|
updater *updater.Updater
|
||||||
|
|
||||||
|
@ -80,7 +79,6 @@ type homeContext struct {
|
||||||
pidFileName string // PID file name. Empty if no PID file was created.
|
pidFileName string // PID file name. Empty if no PID file was created.
|
||||||
controlLock sync.Mutex
|
controlLock sync.Mutex
|
||||||
tlsRoots *x509.CertPool // list of root CAs for TLSv1.2
|
tlsRoots *x509.CertPool // list of root CAs for TLSv1.2
|
||||||
transport *http.Transport
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
appSignalChannel chan os.Signal // Channel for receiving OS signals by the console app
|
appSignalChannel chan os.Signal // Channel for receiving OS signals by the console app
|
||||||
|
|
||||||
|
@ -150,7 +148,9 @@ func setupContext(opts options) {
|
||||||
setupContextFlags(opts)
|
setupContextFlags(opts)
|
||||||
|
|
||||||
Context.tlsRoots = aghtls.SystemRootCAs()
|
Context.tlsRoots = aghtls.SystemRootCAs()
|
||||||
Context.transport = &http.Transport{
|
Context.client = &http.Client{
|
||||||
|
Timeout: time.Minute * 5,
|
||||||
|
Transport: &http.Transport{
|
||||||
DialContext: customDialContext,
|
DialContext: customDialContext,
|
||||||
Proxy: getHTTPProxy,
|
Proxy: getHTTPProxy,
|
||||||
TLSClientConfig: &tls.Config{
|
TLSClientConfig: &tls.Config{
|
||||||
|
@ -158,10 +158,7 @@ func setupContext(opts options) {
|
||||||
CipherSuites: Context.tlsCipherIDs,
|
CipherSuites: Context.tlsCipherIDs,
|
||||||
MinVersion: tls.VersionTLS12,
|
MinVersion: tls.VersionTLS12,
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
Context.client = &http.Client{
|
|
||||||
Timeout: time.Minute * 5,
|
|
||||||
Transport: Context.transport,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !Context.firstRun {
|
if !Context.firstRun {
|
||||||
|
@ -264,7 +261,7 @@ func configureOS(conf *configuration) (err error) {
|
||||||
// setupHostsContainer initializes the structures to keep up-to-date the hosts
|
// setupHostsContainer initializes the structures to keep up-to-date the hosts
|
||||||
// provided by the OS.
|
// provided by the OS.
|
||||||
func setupHostsContainer() (err error) {
|
func setupHostsContainer() (err error) {
|
||||||
Context.hostsWatcher, err = aghos.NewOSWritesWatcher()
|
hostsWatcher, err := aghos.NewOSWritesWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("initing hosts watcher: %w", err)
|
return fmt.Errorf("initing hosts watcher: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -272,18 +269,18 @@ func setupHostsContainer() (err error) {
|
||||||
Context.etcHosts, err = aghnet.NewHostsContainer(
|
Context.etcHosts, err = aghnet.NewHostsContainer(
|
||||||
filtering.SysHostsListID,
|
filtering.SysHostsListID,
|
||||||
aghos.RootDirFS(),
|
aghos.RootDirFS(),
|
||||||
Context.hostsWatcher,
|
hostsWatcher,
|
||||||
aghnet.DefaultHostsPaths()...,
|
aghnet.DefaultHostsPaths()...,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cerr := Context.hostsWatcher.Close()
|
closeErr := hostsWatcher.Close()
|
||||||
if errors.Is(err, aghnet.ErrNoHostsPaths) && cerr == nil {
|
if errors.Is(err, aghnet.ErrNoHostsPaths) && closeErr == nil {
|
||||||
log.Info("warning: initing hosts container: %s", err)
|
log.Info("warning: initing hosts container: %s", err)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.WithDeferred(fmt.Errorf("initing hosts container: %w", err), cerr)
|
return errors.WithDeferred(fmt.Errorf("initing hosts container: %w", err), closeErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -755,13 +752,6 @@ func cleanup(ctx context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if Context.etcHosts != nil {
|
if Context.etcHosts != nil {
|
||||||
// Currently Context.hostsWatcher is only used in Context.etcHosts and
|
|
||||||
// needs closing only in case of the successful initialization of
|
|
||||||
// Context.etcHosts.
|
|
||||||
if err = Context.hostsWatcher.Close(); err != nil {
|
|
||||||
log.Error("closing hosts watcher: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = Context.etcHosts.Close(); err != nil {
|
if err = Context.etcHosts.Close(); err != nil {
|
||||||
log.Error("closing hosts container: %s", err)
|
log.Error("closing hosts container: %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue