2020-01-12 18:23:35 +01:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blocky/config"
|
|
|
|
"blocky/util"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConditionalUpstreamResolver(cfg config.ConditionalUpstreamConfig) ChainedResolver {
|
|
|
|
m := make(map[string]Resolver)
|
2020-12-27 23:40:27 +01:00
|
|
|
for domain, upstream := range cfg.Mapping.Upstreams {
|
|
|
|
m[strings.ToLower(domain)] = NewParallelBestResolver(upstream)
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ConditionalUpstreamResolver{mapping: m}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConditionalUpstreamResolver) Configuration() (result []string) {
|
|
|
|
if len(r.mapping) > 0 {
|
|
|
|
for key, val := range r.mapping {
|
|
|
|
result = append(result, fmt.Sprintf("%s = \"%s\"", key, val))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = []string{"deactivated"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConditionalUpstreamResolver) Resolve(request *Request) (*Response, error) {
|
|
|
|
logger := withPrefix(request.Log, "conditional_resolver")
|
|
|
|
|
|
|
|
if len(r.mapping) > 0 {
|
|
|
|
for _, question := range request.Req.Question {
|
|
|
|
domain := util.ExtractDomain(question)
|
|
|
|
|
|
|
|
// try with domain with and without sub-domains
|
|
|
|
for len(domain) > 0 {
|
|
|
|
r, found := r.mapping[domain]
|
|
|
|
if found {
|
|
|
|
response, err := r.Resolve(request)
|
|
|
|
if err == nil {
|
|
|
|
response.Reason = "CONDITIONAL"
|
2020-04-10 17:08:59 +02:00
|
|
|
response.RType = CONDITIONAL
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:20:13 +02:00
|
|
|
var answer string
|
|
|
|
if response != nil {
|
|
|
|
answer = util.AnswerToString(response.Res.Answer)
|
|
|
|
}
|
|
|
|
|
2020-01-12 18:23:35 +01:00
|
|
|
logger.WithFields(logrus.Fields{
|
2020-05-04 22:20:13 +02:00
|
|
|
"answer": answer,
|
2020-01-12 18:23:35 +01:00
|
|
|
"domain": domain,
|
|
|
|
"upstream": r,
|
|
|
|
}).Debugf("received response from conditional upstream")
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := strings.Index(domain, "."); i >= 0 {
|
|
|
|
domain = domain[i+1:]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|