Skip to content

Commit

Permalink
plugin_dns64: don't send queries to self
Browse files Browse the repository at this point in the history
Fixes #1477
  • Loading branch information
jedisct1 committed Sep 16, 2020
1 parent b460ca9 commit 4eab88c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
32 changes: 17 additions & 15 deletions dnscrypt-proxy/plugin_dns64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net"
"sync"
"time"

"github.com/jedisct1/dlog"
"github.com/miekg/dns"
Expand All @@ -21,6 +22,7 @@ type PluginDNS64 struct {
pref64 []*net.IPNet
dns64Resolvers []string
ipv4Resolver string
proxy *Proxy
}

func (plugin *PluginDNS64) Name() string {
Expand All @@ -34,6 +36,7 @@ func (plugin *PluginDNS64) Description() string {
func (plugin *PluginDNS64) Init(proxy *Proxy) error {
plugin.ipv4Resolver = proxy.listenAddresses[0] //recursively to ourselves
plugin.pref64Mutex = new(sync.RWMutex)
plugin.proxy = proxy

if len(proxy.dns64Prefixes) != 0 {
plugin.pref64Mutex.RLock()
Expand Down Expand Up @@ -65,26 +68,29 @@ func (plugin *PluginDNS64) Reload() error {
}

func (plugin *PluginDNS64) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
if !hasAAAAQuestion(pluginsState.questionMsg) || hasAAAAAnswer(msg) {
if hasAAAAAnswer(msg) {
return nil
}

questions := msg.Question
if len(questions) != 1 {
return nil
}
question := questions[0]
if question.Qclass != dns.ClassINET {
question := pluginsState.questionMsg.Question[0]
if question.Qclass != dns.ClassINET || question.Qtype != dns.TypeAAAA {
return nil
}

msgA := new(dns.Msg)
msgA := pluginsState.questionMsg.Copy()
msgA.SetQuestion(question.Name, dns.TypeA)
msgAPacket, err := msgA.Pack()
if err != nil {
return err
}

client := new(dns.Client)
resp, _, err := client.Exchange(msgA, plugin.ipv4Resolver)
respPacket := plugin.proxy.processIncomingQuery("trampoline", plugin.proxy.mainProto, msgAPacket, nil, nil, time.Now())
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
}

Expand Down Expand Up @@ -134,10 +140,6 @@ func (plugin *PluginDNS64) Eval(pluginsState *PluginsState, msg *dns.Msg) error
return nil
}

func hasAAAAQuestion(msg *dns.Msg) bool {
return msg.Question[0].Qtype == dns.TypeAAAA
}

func hasAAAAAnswer(msg *dns.Msg) bool {
for _, answer := range msg.Answer {
if answer.Header().Rrtype == dns.TypeAAAA {
Expand Down
4 changes: 3 additions & 1 deletion dnscrypt-proxy/plugin_query_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err
var clientIPStr string
if pluginsState.clientProto == "udp" {
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
} else {
} else if pluginsState.clientProto == "tcp" {
clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String()
} else {
clientIPStr = "-"
}
qName := pluginsState.qName

Expand Down

0 comments on commit 4eab88c

Please sign in to comment.