2023-11-28 13:40:52 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2023-11-28 13:40:52 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// upstreamConfigValidator parses each section of an upstream configuration into
|
|
|
|
// a corresponding [*proxy.UpstreamConfig] and checks the actual DNS
|
|
|
|
// availability of each upstream.
|
2023-11-28 13:40:52 +00:00
|
|
|
type upstreamConfigValidator struct {
|
2024-02-05 13:12:27 +00:00
|
|
|
// generalUpstreamResults contains upstream results of a general section.
|
|
|
|
generalUpstreamResults map[string]*upstreamResult
|
2023-11-28 13:40:52 +00:00
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// fallbackUpstreamResults contains upstream results of a fallback section.
|
|
|
|
fallbackUpstreamResults map[string]*upstreamResult
|
2023-11-28 13:40:52 +00:00
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// privateUpstreamResults contains upstream results of a private section.
|
|
|
|
privateUpstreamResults map[string]*upstreamResult
|
|
|
|
|
|
|
|
// generalParseResults contains parsing results of a general section.
|
|
|
|
generalParseResults []*parseResult
|
|
|
|
|
|
|
|
// fallbackParseResults contains parsing results of a fallback section.
|
|
|
|
fallbackParseResults []*parseResult
|
|
|
|
|
|
|
|
// privateParseResults contains parsing results of a private section.
|
|
|
|
privateParseResults []*parseResult
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// upstreamResult is a result of parsing of an [upstream.Upstream] within an
|
2023-11-28 13:40:52 +00:00
|
|
|
// [proxy.UpstreamConfig].
|
|
|
|
type upstreamResult struct {
|
2024-02-05 13:12:27 +00:00
|
|
|
// server is the parsed upstream.
|
2023-11-28 13:40:52 +00:00
|
|
|
server upstream.Upstream
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// err is the upstream check error.
|
2023-11-28 13:40:52 +00:00
|
|
|
err error
|
|
|
|
|
|
|
|
// isSpecific is true if the upstream is domain-specific.
|
|
|
|
isSpecific bool
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// parseResult contains a original piece of upstream configuration and a
|
|
|
|
// corresponding error.
|
|
|
|
type parseResult struct {
|
|
|
|
err *proxy.ParseError
|
|
|
|
original string
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newUpstreamConfigValidator parses the upstream configuration and returns a
|
|
|
|
// validator for it. cv already contains the parsed upstreams along with errors
|
|
|
|
// related.
|
|
|
|
func newUpstreamConfigValidator(
|
|
|
|
general []string,
|
|
|
|
fallback []string,
|
|
|
|
private []string,
|
|
|
|
opts *upstream.Options,
|
|
|
|
) (cv *upstreamConfigValidator) {
|
2024-02-05 13:12:27 +00:00
|
|
|
cv = &upstreamConfigValidator{
|
|
|
|
generalUpstreamResults: map[string]*upstreamResult{},
|
|
|
|
fallbackUpstreamResults: map[string]*upstreamResult{},
|
|
|
|
privateUpstreamResults: map[string]*upstreamResult{},
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
conf, err := proxy.ParseUpstreamsConfig(general, opts)
|
|
|
|
cv.generalParseResults = collectErrResults(general, err)
|
|
|
|
insertConfResults(conf, cv.generalUpstreamResults)
|
|
|
|
|
|
|
|
conf, err = proxy.ParseUpstreamsConfig(fallback, opts)
|
|
|
|
cv.fallbackParseResults = collectErrResults(fallback, err)
|
|
|
|
insertConfResults(conf, cv.fallbackUpstreamResults)
|
|
|
|
|
|
|
|
conf, err = proxy.ParseUpstreamsConfig(private, opts)
|
|
|
|
cv.privateParseResults = collectErrResults(private, err)
|
|
|
|
insertConfResults(conf, cv.privateUpstreamResults)
|
|
|
|
|
2023-11-28 13:40:52 +00:00
|
|
|
return cv
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// collectErrResults parses err and returns parsing results containing the
|
|
|
|
// original upstream configuration line and the corresponding error. err can be
|
|
|
|
// nil.
|
|
|
|
func collectErrResults(lines []string, err error) (results []*parseResult) {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// limit is a maximum length for upstream configuration lines.
|
|
|
|
const limit = 80
|
|
|
|
|
|
|
|
wrapper, ok := err.(errors.WrapperSlice)
|
|
|
|
if !ok {
|
|
|
|
log.Debug("dnsforward: configvalidator: unwrapping: %s", err)
|
|
|
|
|
|
|
|
return nil
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
errs := wrapper.Unwrap()
|
|
|
|
results = make([]*parseResult, 0, len(errs))
|
|
|
|
for i, e := range errs {
|
|
|
|
var parseErr *proxy.ParseError
|
|
|
|
if !errors.As(e, &parseErr) {
|
|
|
|
log.Debug("dnsforward: configvalidator: inserting unexpected error %d: %s", i, err)
|
|
|
|
|
2023-11-28 13:40:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
idx := parseErr.Idx
|
|
|
|
line := []rune(lines[idx])
|
|
|
|
if len(line) > limit {
|
|
|
|
line = line[:limit]
|
|
|
|
line[limit-1] = '…'
|
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, &parseResult{
|
|
|
|
original: string(line),
|
|
|
|
err: parseErr,
|
|
|
|
})
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
return results
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// insertConfResults parses conf and inserts the upstream result into results.
|
|
|
|
// It can insert multiple results as well as none.
|
|
|
|
func insertConfResults(conf *proxy.UpstreamConfig, results map[string]*upstreamResult) {
|
|
|
|
insertListResults(conf.Upstreams, results, false)
|
|
|
|
|
|
|
|
for _, ups := range conf.DomainReservedUpstreams {
|
|
|
|
insertListResults(ups, results, true)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
for _, ups := range conf.SpecifiedDomainUpstreams {
|
|
|
|
insertListResults(ups, results, true)
|
|
|
|
}
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// insertListResults constructs upstream results from the upstream list and
|
|
|
|
// inserts them into results. It can insert multiple results as well as none.
|
|
|
|
func insertListResults(ups []upstream.Upstream, results map[string]*upstreamResult, specific bool) {
|
|
|
|
for _, u := range ups {
|
|
|
|
addr := u.Address()
|
|
|
|
_, ok := results[addr]
|
|
|
|
if ok {
|
|
|
|
continue
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
results[addr] = &upstreamResult{
|
|
|
|
server: u,
|
|
|
|
isSpecific: specific,
|
|
|
|
}
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check tries to exchange with each successfully parsed upstream and enriches
|
|
|
|
// the results with the healthcheck errors. It should not be called after the
|
|
|
|
// [upsConfValidator.close] method, since it makes no sense to check the closed
|
|
|
|
// upstreams.
|
|
|
|
func (cv *upstreamConfigValidator) check() {
|
|
|
|
const (
|
|
|
|
// testTLD is the special-use fully-qualified domain name for testing
|
|
|
|
// the DNS server reachability.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc6761#section-6.2.
|
|
|
|
testTLD = "test."
|
|
|
|
|
|
|
|
// inAddrARPATLD is the special-use fully-qualified domain name for PTR
|
|
|
|
// IP address resolution.
|
|
|
|
//
|
|
|
|
// See https://datatracker.ietf.org/doc/html/rfc1035#section-3.5.
|
|
|
|
inAddrARPATLD = "in-addr.arpa."
|
|
|
|
)
|
|
|
|
|
|
|
|
commonChecker := &healthchecker{
|
|
|
|
hostname: testTLD,
|
|
|
|
qtype: dns.TypeA,
|
|
|
|
ansEmpty: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
arpaChecker := &healthchecker{
|
|
|
|
hostname: inAddrARPATLD,
|
|
|
|
qtype: dns.TypePTR,
|
|
|
|
ansEmpty: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
2024-02-05 13:12:27 +00:00
|
|
|
wg.Add(len(cv.generalUpstreamResults) +
|
|
|
|
len(cv.fallbackUpstreamResults) +
|
|
|
|
len(cv.privateUpstreamResults))
|
2023-11-28 13:40:52 +00:00
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
for _, res := range cv.generalUpstreamResults {
|
|
|
|
go checkSrv(res, wg, commonChecker)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
2024-02-05 13:12:27 +00:00
|
|
|
for _, res := range cv.fallbackUpstreamResults {
|
|
|
|
go checkSrv(res, wg, commonChecker)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
2024-02-05 13:12:27 +00:00
|
|
|
for _, res := range cv.privateUpstreamResults {
|
|
|
|
go checkSrv(res, wg, arpaChecker)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkSrv runs hc on the server from res, if any, and stores any occurred
|
2024-02-05 13:12:27 +00:00
|
|
|
// error in res. wg is always marked done in the end. It is intended to be
|
|
|
|
// used as a goroutine.
|
|
|
|
func checkSrv(res *upstreamResult, wg *sync.WaitGroup, hc *healthchecker) {
|
|
|
|
defer log.OnPanic(fmt.Sprintf("dnsforward: checking upstream %s", res.server.Address()))
|
2023-11-28 13:40:52 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
res.err = hc.check(res.server)
|
|
|
|
if res.err != nil && res.isSpecific {
|
|
|
|
res.err = domainSpecificTestError{Err: res.err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close closes all the upstreams that were successfully parsed. It enriches
|
|
|
|
// the results with deferred closing errors.
|
|
|
|
func (cv *upstreamConfigValidator) close() {
|
2024-02-05 13:12:27 +00:00
|
|
|
all := []map[string]*upstreamResult{
|
|
|
|
cv.generalUpstreamResults,
|
|
|
|
cv.fallbackUpstreamResults,
|
|
|
|
cv.privateUpstreamResults,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range all {
|
|
|
|
for _, r := range m {
|
|
|
|
r.err = errors.WithDeferred(r.err, r.server.Close())
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// sections of the upstream configuration according to the text label of the
|
|
|
|
// localization.
|
|
|
|
//
|
|
|
|
// Keep in sync with client/src/__locales/en.json.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Refactor.
|
|
|
|
const (
|
|
|
|
generalTextLabel = "upstream_dns"
|
|
|
|
fallbackTextLabel = "fallback_dns_title"
|
|
|
|
privateTextLabel = "local_ptr_title"
|
|
|
|
)
|
|
|
|
|
2023-11-28 13:40:52 +00:00
|
|
|
// status returns all the data collected during parsing, healthcheck, and
|
|
|
|
// closing of the upstreams. The returned map is keyed by the original upstream
|
|
|
|
// configuration piece and contains the corresponding error or "OK" if there was
|
|
|
|
// no error.
|
|
|
|
func (cv *upstreamConfigValidator) status() (results map[string]string) {
|
2024-02-05 13:12:27 +00:00
|
|
|
// Names of the upstream configuration sections for logging.
|
|
|
|
const (
|
|
|
|
generalSection = "general"
|
|
|
|
fallbackSection = "fallback"
|
|
|
|
privateSection = "private"
|
|
|
|
)
|
|
|
|
|
|
|
|
results = map[string]string{}
|
2023-11-28 13:40:52 +00:00
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
for original, res := range cv.generalUpstreamResults {
|
|
|
|
upstreamResultToStatus(generalSection, string(original), res, results)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
2024-02-05 13:12:27 +00:00
|
|
|
for original, res := range cv.fallbackUpstreamResults {
|
|
|
|
upstreamResultToStatus(fallbackSection, string(original), res, results)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
2024-02-05 13:12:27 +00:00
|
|
|
for original, res := range cv.privateUpstreamResults {
|
|
|
|
upstreamResultToStatus(privateSection, string(original), res, results)
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
parseResultToStatus(generalTextLabel, generalSection, cv.generalParseResults, results)
|
|
|
|
parseResultToStatus(fallbackTextLabel, fallbackSection, cv.fallbackParseResults, results)
|
|
|
|
parseResultToStatus(privateTextLabel, privateSection, cv.privateParseResults, results)
|
|
|
|
|
|
|
|
return results
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// upstreamResultToStatus puts "OK" or an error message from res into resMap.
|
|
|
|
// section is the name of the upstream configuration section, i.e. "general",
|
2023-11-28 13:40:52 +00:00
|
|
|
// "fallback", or "private", and only used for logging.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Currently, the HTTP handler expects that all the results are
|
|
|
|
// put together in a single map, which may lead to collisions, see AG-27539.
|
|
|
|
// Improve the results compilation.
|
2024-02-05 13:12:27 +00:00
|
|
|
func upstreamResultToStatus(
|
|
|
|
section string,
|
|
|
|
original string,
|
|
|
|
res *upstreamResult,
|
|
|
|
resMap map[string]string,
|
|
|
|
) {
|
2023-11-28 13:40:52 +00:00
|
|
|
val := "OK"
|
|
|
|
if res.err != nil {
|
|
|
|
val = res.err.Error()
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
prevVal := resMap[original]
|
2023-11-28 13:40:52 +00:00
|
|
|
switch prevVal {
|
|
|
|
case "":
|
2024-02-05 13:12:27 +00:00
|
|
|
resMap[original] = val
|
2023-11-28 13:40:52 +00:00
|
|
|
case val:
|
2024-02-05 13:12:27 +00:00
|
|
|
log.Debug("dnsforward: duplicating %s config line %q", section, original)
|
2023-11-28 13:40:52 +00:00
|
|
|
default:
|
|
|
|
log.Debug(
|
|
|
|
"dnsforward: warning: %s config line %q (%v) had different result %v",
|
|
|
|
section,
|
|
|
|
val,
|
2024-02-05 13:12:27 +00:00
|
|
|
original,
|
2023-11-28 13:40:52 +00:00
|
|
|
prevVal,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:12:27 +00:00
|
|
|
// parseResultToStatus puts parsing error messages from results into resMap.
|
|
|
|
// section is the name of the upstream configuration section, i.e. "general",
|
|
|
|
// "fallback", or "private", and only used for logging.
|
|
|
|
//
|
|
|
|
// Parsing error message has the following format:
|
|
|
|
//
|
|
|
|
// sectionTextLabel line: parsing error
|
|
|
|
//
|
|
|
|
// Where sectionTextLabel is a section text label of a localization and line is
|
|
|
|
// a line number.
|
|
|
|
func parseResultToStatus(
|
|
|
|
textLabel string,
|
|
|
|
section string,
|
|
|
|
results []*parseResult,
|
|
|
|
resMap map[string]string,
|
|
|
|
) {
|
|
|
|
for _, res := range results {
|
|
|
|
original := res.original
|
|
|
|
_, ok := resMap[original]
|
|
|
|
if ok {
|
|
|
|
log.Debug("dnsforward: duplicating %s parsing error %q", section, original)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resMap[original] = fmt.Sprintf("%s %d: parsing error", textLabel, res.err.Idx+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:40:52 +00:00
|
|
|
// domainSpecificTestError is a wrapper for errors returned by checkDNS to mark
|
|
|
|
// the tested upstream domain-specific and therefore consider its errors
|
|
|
|
// non-critical.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Some common mechanism of distinguishing between errors and
|
|
|
|
// warnings (non-critical errors) is desired.
|
|
|
|
type domainSpecificTestError struct {
|
|
|
|
// Err is the actual error occurred during healthcheck test.
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ error = domainSpecificTestError{}
|
|
|
|
|
|
|
|
// Error implements the [error] interface for domainSpecificTestError.
|
|
|
|
func (err domainSpecificTestError) Error() (msg string) {
|
|
|
|
return fmt.Sprintf("WARNING: %s", err.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ errors.Wrapper = domainSpecificTestError{}
|
|
|
|
|
|
|
|
// Unwrap implements the [errors.Wrapper] interface for domainSpecificTestError.
|
|
|
|
func (err domainSpecificTestError) Unwrap() (wrapped error) {
|
|
|
|
return err.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// healthchecker checks the upstream's status by exchanging with it.
|
|
|
|
type healthchecker struct {
|
|
|
|
// hostname is the name of the host to put into healthcheck DNS request.
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
// qtype is the type of DNS request to use for healthcheck.
|
|
|
|
qtype uint16
|
|
|
|
|
|
|
|
// ansEmpty defines if the answer section within the response is expected to
|
|
|
|
// be empty.
|
|
|
|
ansEmpty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// check exchanges with u and validates the response.
|
|
|
|
func (h *healthchecker) check(u upstream.Upstream) (err error) {
|
|
|
|
req := &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: h.hostname,
|
|
|
|
Qtype: h.qtype,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, err := u.Exchange(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't communicate with upstream: %w", err)
|
|
|
|
} else if h.ansEmpty && len(reply.Answer) > 0 {
|
2024-02-05 13:12:27 +00:00
|
|
|
return errors.Error("wrong response")
|
2023-11-28 13:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|