mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 06:55:48 +03:00
3b2f5d7842
Merge in DNS/adguard-home from 2704-local-addresses-vol.1 to master Updates #2704. Updates #2829. Updates #2846. Squashed commit of the following: commit 9a49b3d27edcb30da7f16a065226907833b1dc81 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Mar 22 15:39:17 2021 +0300 aghnet: imp docs and logging commit 74f95a29c55b9e732276601b0ecc63fb7c3a9f9e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 20:56:51 2021 +0300 all: fix friday evening mistakes commit 0e2066bc5c16ed807fa601780b99e154502361a9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 20:51:15 2021 +0300 all: upd testify, imp code quality commit 8237c50b670c58361ccf7adec3ff2452b1196677 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 20:19:29 2021 +0300 aghnet: imp test naming commit 14eb1e189339554c0a6d38e2ba7a93917774ebab Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 19:41:43 2021 +0300 aghnet: isolate windows-specific functionality commit d461ac8b18c187999da3e3aba116571b7ebe6785 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 14:50:05 2021 +0300 aghnet: imp code quality commit d0ee01cb1f8613de2085c0f2f2f396e46beb52a5 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 19 11:59:10 2021 +0300 all: mv funcs to agherr, mk system resolvers getter
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package aghnet
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// DefaultRefreshIvl is the default period of time between refreshing cached
|
|
// addresses.
|
|
// const DefaultRefreshIvl = 5 * time.Minute
|
|
|
|
// HostGenFunc is the signature for functions generating fake hostnames. The
|
|
// implementation must be safe for concurrent use.
|
|
type HostGenFunc func() (host string)
|
|
|
|
// unit is an alias for an existing map value.
|
|
type unit = struct{}
|
|
|
|
// SystemResolvers helps to work with local resolvers' addresses provided by OS.
|
|
type SystemResolvers interface {
|
|
// Get returns the slice of local resolvers' addresses.
|
|
// It should be safe for concurrent use.
|
|
Get() (rs []string)
|
|
// Refresh refreshes the local resolvers' addresses cache. It should be
|
|
// safe for concurrent use.
|
|
Refresh() (err error)
|
|
}
|
|
|
|
const (
|
|
// fakeDialErr is an error which dialFunc is expected to return.
|
|
fakeDialErr agherr.Error = "this error signals the successful dialFunc work"
|
|
|
|
// badAddrPassedErr is returned when dialFunc can't parse an IP address.
|
|
badAddrPassedErr agherr.Error = "the passed string is not a valid IP address"
|
|
)
|
|
|
|
// refreshWithTicker refreshes the cache of sr after each tick form tickCh.
|
|
func refreshWithTicker(sr SystemResolvers, tickCh <-chan time.Time) {
|
|
defer agherr.LogPanic("systemResolvers")
|
|
|
|
// TODO(e.burkov): Implement a functionality to stop ticker.
|
|
for range tickCh {
|
|
err := sr.Refresh()
|
|
if err != nil {
|
|
log.Error("systemResolvers: error in refreshing goroutine: %s", err)
|
|
|
|
continue
|
|
}
|
|
|
|
log.Debug("systemResolvers: local addresses cache is refreshed")
|
|
}
|
|
}
|
|
|
|
// NewSystemResolvers returns a SystemResolvers with the cache refresh rate
|
|
// defined by refreshIvl. It disables auto-resfreshing if refreshIvl is 0. If
|
|
// nil is passed for hostGenFunc, the default generator will be used.
|
|
func NewSystemResolvers(
|
|
refreshIvl time.Duration,
|
|
hostGenFunc HostGenFunc,
|
|
) (sr SystemResolvers, err error) {
|
|
sr = newSystemResolvers(refreshIvl, hostGenFunc)
|
|
|
|
// Fill cache.
|
|
err = sr.Refresh()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if refreshIvl > 0 {
|
|
ticker := time.NewTicker(refreshIvl)
|
|
|
|
go refreshWithTicker(sr, ticker.C)
|
|
}
|
|
|
|
return sr, nil
|
|
}
|