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-03-10 22:59:04 +01:00
|
|
|
rewrite map[string]string
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 13:45:57 +01:00
|
|
|
// NewConditionalUpstreamResolver returns new resolver instance
|
2020-01-12 18:23:35 +01:00
|
|
|
func NewConditionalUpstreamResolver(cfg config.ConditionalUpstreamConfig) ChainedResolver {
|
|
|
|
m := make(map[string]Resolver)
|
2021-03-10 22:59:04 +01:00
|
|
|
rewrite := make(map[string]string)
|
|
|
|
|
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
|
|
|
|
m[strings.ToLower(domain)] = NewParallelBestResolver(upstreams)
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:59:04 +01:00
|
|
|
for k, v := range cfg.Rewrite {
|
|
|
|
rewrite[strings.ToLower(k)] = strings.ToLower(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ConditionalUpstreamResolver{mapping: m, rewrite: rewrite}
|
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) {
|
|
|
|
if len(r.mapping) > 0 {
|
|
|
|
for key, val := range r.mapping {
|
|
|
|
result = append(result, fmt.Sprintf("%s = \"%s\"", key, val))
|
|
|
|
}
|
2021-03-10 22:59:04 +01:00
|
|
|
|
2021-11-07 21:35:16 +01:00
|
|
|
if len(r.rewrite) > 0 {
|
|
|
|
result = append(result, "rewrite:")
|
|
|
|
for key, val := range r.rewrite {
|
|
|
|
result = append(result, fmt.Sprintf("%s = \"%s\"", key, val))
|
|
|
|
}
|
2021-03-10 22:59:04 +01:00
|
|
|
}
|
2020-01-12 18:23:35 +01:00
|
|
|
} else {
|
|
|
|
result = []string{"deactivated"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:59:04 +01:00
|
|
|
func (r *ConditionalUpstreamResolver) applyRewrite(domain string) string {
|
|
|
|
for k, v := range r.rewrite {
|
|
|
|
if strings.HasSuffix(domain, "."+k) {
|
|
|
|
return strings.TrimSuffix(domain, "."+k) + "." + v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-03-10 22:59:04 +01:00
|
|
|
question := request.Req.Question[0]
|
|
|
|
domainFromQuestion := r.applyRewrite(util.ExtractDomain(question))
|
|
|
|
domain := domainFromQuestion
|
|
|
|
|
|
|
|
// try with domain with and without sub-domains
|
|
|
|
for len(domain) > 0 {
|
|
|
|
r, found := r.mapping[domain]
|
|
|
|
if found {
|
|
|
|
request.Req.Question[0].Name = dns.Fqdn(domainFromQuestion)
|
|
|
|
response, err := r.Resolve(request)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
response.Reason = "CONDITIONAL"
|
2021-09-09 22:57:05 +02:00
|
|
|
response.RType = model.ResponseTypeCONDITIONAL
|
2021-03-10 22:59:04 +01:00
|
|
|
response.Res.Question[0].Name = question.Name
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:59:04 +01:00
|
|
|
var answer string
|
|
|
|
if response != nil {
|
|
|
|
answer = util.AnswerToString(response.Res.Answer)
|
2020-01-12 18:23:35 +01:00
|
|
|
}
|
2021-03-10 22:59:04 +01:00
|
|
|
|
|
|
|
logger.WithFields(logrus.Fields{
|
|
|
|
"answer": answer,
|
|
|
|
"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-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)
|
|
|
|
}
|