2020-01-12 18:23:35 +01:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-25 22:06:34 +02:00
|
|
|
"github.com/0xERR0R/blocky/config"
|
2021-08-29 21:51:24 +02:00
|
|
|
"github.com/0xERR0R/blocky/model"
|
2021-08-25 22:06:34 +02:00
|
|
|
"github.com/0xERR0R/blocky/util"
|
|
|
|
|
2021-03-10 22:59:04 +01:00
|
|
|
"github.com/miekg/dns"
|
2020-01-12 18:23:35 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConditionalUpstreamResolver delegates DNS question to other DNS resolver dependent on domain name in question
|
|
|
|
type ConditionalUpstreamResolver struct {
|
|
|
|
NextResolver
|
|
|
|
mapping map[string]Resolver
|
|
|
|
}
|
|
|
|
|
2021-02-26 13:45:57 +01:00
|
|
|
// NewConditionalUpstreamResolver returns new resolver instance
|
2022-11-29 18:55:12 +01:00
|
|
|
func NewConditionalUpstreamResolver(
|
|
|
|
cfg config.ConditionalUpstreamConfig, bootstrap *Bootstrap, shouldVerifyUpstreams bool,
|
|
|
|
) (ChainedResolver, error) {
|
2022-03-17 22:30:21 +01:00
|
|
|
m := make(map[string]Resolver, len(cfg.Mapping.Upstreams))
|
2021-03-10 22:59:04 +01:00
|
|
|
|
2020-12-27 23:40:27 +01:00
|
|
|
for domain, upstream := range cfg.Mapping.Upstreams {
|
2021-04-22 22:37:59 +02:00
|
|
|
upstreams := make(map[string][]config.Upstream)
|
|
|
|
upstreams[upstreamDefaultCfgName] = upstream
|
2022-04-22 22:12:35 +02:00
|
|
|
|
2022-11-29 18:55:12 +01:00
|
|
|
r, err := NewParallelBestResolver(upstreams, bootstrap, shouldVerifyUpstreams)
|
2022-04-22 22:12:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m[strings.ToLower(domain)] = r
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 22:12:35 +02:00
|
|
|
return &ConditionalUpstreamResolver{mapping: m}, nil
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 13:45:57 +01:00
|
|
|
// Configuration returns current configuration
|
2020-01-12 18:23:35 +01:00
|
|
|
func (r *ConditionalUpstreamResolver) Configuration() (result []string) {
|
2022-12-02 02:42:17 +01:00
|
|
|
if len(r.mapping) == 0 {
|
|
|
|
return configDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range r.mapping {
|
|
|
|
result = append(result, fmt.Sprintf("%s = \"%s\"", key, val))
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-10 09:09:50 +02:00
|
|
|
func (r *ConditionalUpstreamResolver) processRequest(request *model.Request) (bool, *model.Response, error) {
|
|
|
|
domainFromQuestion := util.ExtractDomain(request.Req.Question[0])
|
|
|
|
domain := domainFromQuestion
|
|
|
|
|
|
|
|
if strings.Contains(domainFromQuestion, ".") {
|
|
|
|
// try with domain with and without sub-domains
|
|
|
|
for len(domain) > 0 {
|
|
|
|
if resolver, found := r.mapping[domain]; found {
|
|
|
|
resp, err := r.internalResolve(resolver, domainFromQuestion, domain, request)
|
|
|
|
|
|
|
|
return true, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := strings.Index(domain, "."); i >= 0 {
|
|
|
|
domain = domain[i+1:]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if resolver, found := r.mapping["."]; found {
|
|
|
|
resp, err := r.internalResolve(resolver, domainFromQuestion, domain, request)
|
|
|
|
|
|
|
|
return true, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 13:45:57 +01:00
|
|
|
// Resolve uses the conditional resolver to resolve the query
|
2021-08-29 21:51:24 +02:00
|
|
|
func (r *ConditionalUpstreamResolver) Resolve(request *model.Request) (*model.Response, error) {
|
2020-01-12 18:23:35 +01:00
|
|
|
logger := withPrefix(request.Log, "conditional_resolver")
|
|
|
|
|
|
|
|
if len(r.mapping) > 0 {
|
2022-05-10 09:09:50 +02:00
|
|
|
resolved, resp, err := r.processRequest(request)
|
|
|
|
if resolved {
|
|
|
|
return resp, err
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:32:24 +01:00
|
|
|
logger.WithField("next_resolver", Name(r.next)).Trace("go to next resolver")
|
2020-01-12 18:23:35 +01:00
|
|
|
|
|
|
|
return r.next.Resolve(request)
|
|
|
|
}
|
2022-01-19 22:26:39 +01:00
|
|
|
|
|
|
|
func (r *ConditionalUpstreamResolver) internalResolve(reso Resolver, doFQ, do string,
|
2022-12-26 22:11:45 +01:00
|
|
|
req *model.Request,
|
|
|
|
) (*model.Response, error) {
|
2022-01-19 22:26:39 +01:00
|
|
|
// internal request resolution
|
|
|
|
logger := withPrefix(req.Log, "conditional_resolver")
|
|
|
|
|
|
|
|
req.Req.Question[0].Name = dns.Fqdn(doFQ)
|
|
|
|
response, err := reso.Resolve(req)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
response.Reason = "CONDITIONAL"
|
|
|
|
response.RType = model.ResponseTypeCONDITIONAL
|
|
|
|
response.Res.Question[0].Name = req.Req.Question[0].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
var answer string
|
|
|
|
if response != nil {
|
|
|
|
answer = util.AnswerToString(response.Res.Answer)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.WithFields(logrus.Fields{
|
|
|
|
"answer": answer,
|
|
|
|
"domain": do,
|
|
|
|
"upstream": reso,
|
|
|
|
}).Debugf("received response from conditional upstream")
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|