plugin_dns64: don't send queries to self

Fixes #1477
This commit is contained in:
Frank Denis 2020-09-17 00:44:37 +02:00
parent b460ca9fa8
commit 4eab88c017
2 changed files with 20 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"net" "net"
"sync" "sync"
"time"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -21,6 +22,7 @@ type PluginDNS64 struct {
pref64 []*net.IPNet pref64 []*net.IPNet
dns64Resolvers []string dns64Resolvers []string
ipv4Resolver string ipv4Resolver string
proxy *Proxy
} }
func (plugin *PluginDNS64) Name() string { func (plugin *PluginDNS64) Name() string {
@ -34,6 +36,7 @@ func (plugin *PluginDNS64) Description() string {
func (plugin *PluginDNS64) Init(proxy *Proxy) error { func (plugin *PluginDNS64) Init(proxy *Proxy) error {
plugin.ipv4Resolver = proxy.listenAddresses[0] //recursively to ourselves plugin.ipv4Resolver = proxy.listenAddresses[0] //recursively to ourselves
plugin.pref64Mutex = new(sync.RWMutex) plugin.pref64Mutex = new(sync.RWMutex)
plugin.proxy = proxy
if len(proxy.dns64Prefixes) != 0 { if len(proxy.dns64Prefixes) != 0 {
plugin.pref64Mutex.RLock() plugin.pref64Mutex.RLock()
@ -65,26 +68,29 @@ func (plugin *PluginDNS64) Reload() error {
} }
func (plugin *PluginDNS64) Eval(pluginsState *PluginsState, msg *dns.Msg) error { func (plugin *PluginDNS64) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
if !hasAAAAQuestion(pluginsState.questionMsg) || hasAAAAAnswer(msg) { if hasAAAAAnswer(msg) {
return nil return nil
} }
questions := msg.Question question := pluginsState.questionMsg.Question[0]
if len(questions) != 1 { if question.Qclass != dns.ClassINET || question.Qtype != dns.TypeAAAA {
return nil
}
question := questions[0]
if question.Qclass != dns.ClassINET {
return nil return nil
} }
msgA := new(dns.Msg) msgA := pluginsState.questionMsg.Copy()
msgA.SetQuestion(question.Name, dns.TypeA) msgA.SetQuestion(question.Name, dns.TypeA)
msgAPacket, err := msgA.Pack()
if err != nil {
return err
}
client := new(dns.Client) respPacket := plugin.proxy.processIncomingQuery("trampoline", plugin.proxy.mainProto, msgAPacket, nil, nil, time.Now())
resp, _, err := client.Exchange(msgA, plugin.ipv4Resolver) resp := dns.Msg{}
if err := resp.Unpack(respPacket); err != nil {
return err
}
if err != nil || resp == nil || resp.Rcode != dns.RcodeSuccess { if err != nil || resp.Rcode != dns.RcodeSuccess {
return nil return nil
} }
@ -134,10 +140,6 @@ func (plugin *PluginDNS64) Eval(pluginsState *PluginsState, msg *dns.Msg) error
return nil return nil
} }
func hasAAAAQuestion(msg *dns.Msg) bool {
return msg.Question[0].Qtype == dns.TypeAAAA
}
func hasAAAAAnswer(msg *dns.Msg) bool { func hasAAAAAnswer(msg *dns.Msg) bool {
for _, answer := range msg.Answer { for _, answer := range msg.Answer {
if answer.Header().Rrtype == dns.TypeAAAA { if answer.Header().Rrtype == dns.TypeAAAA {

View File

@ -58,8 +58,10 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err
var clientIPStr string var clientIPStr string
if pluginsState.clientProto == "udp" { if pluginsState.clientProto == "udp" {
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String() clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
} else { } else if pluginsState.clientProto == "tcp" {
clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String() clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String()
} else {
clientIPStr = "-"
} }
qName := pluginsState.qName qName := pluginsState.qName