diff options
Diffstat (limited to 'middleware/httpproxy')
-rw-r--r-- | middleware/httpproxy/README.md | 21 | ||||
-rw-r--r-- | middleware/httpproxy/google.go | 40 | ||||
-rw-r--r-- | middleware/httpproxy/proxy.go | 4 | ||||
-rw-r--r-- | middleware/httpproxy/setup_test.go | 6 | ||||
-rw-r--r-- | middleware/httpproxy/tls.go | 3 |
5 files changed, 59 insertions, 15 deletions
diff --git a/middleware/httpproxy/README.md b/middleware/httpproxy/README.md index 026fbdc71..f0bf58903 100644 --- a/middleware/httpproxy/README.md +++ b/middleware/httpproxy/README.md @@ -48,3 +48,24 @@ proxy . dns.google.com { upstream /etc/resolv.conf } ~~~ + +## Debug queries + +Debug queries are enabled by default and currently there is no way to turn them off. When CoreDNS +receives a debug queries (i.e. the name is prefixed with `o-o.debug.` a TXT record with Comment from +`dns.google.com` is added. Note this is not always set, but sometimes you'll see: + +`dig @localhost -p 1053 mx o-o.debug.example.org`: + +~~~ txt +;; OPT PSEUDOSECTION: +; EDNS: version: 0, flags:; udp: 4096 +;; QUESTION SECTION: +;o-o.debug.example.org. IN MX + +;; AUTHORITY SECTION: +example.org. 1799 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2016110711 7200 3600 1209600 3600 + +;; ADDITIONAL SECTION: +. 0 CH TXT "Response from 199.43.133.53" +~~~ diff --git a/middleware/httpproxy/google.go b/middleware/httpproxy/google.go index 78b5ea864..dafd0d6a6 100644 --- a/middleware/httpproxy/google.go +++ b/middleware/httpproxy/google.go @@ -12,7 +12,9 @@ import ( "sync/atomic" "time" + "github.com/miekg/coredns/middleware/pkg/debug" "github.com/miekg/coredns/middleware/proxy" + "github.com/miekg/coredns/request" "github.com/miekg/dns" ) @@ -30,11 +32,17 @@ type google struct { func newGoogle() *google { return &google{client: newClient(ghost), quit: make(chan bool)} } -func (g *google) Exchange(req *dns.Msg) (*dns.Msg, error) { +func (g *google) Exchange(state request.Request) (*dns.Msg, error) { v := url.Values{} - v.Set("name", req.Question[0].Name) - v.Set("type", fmt.Sprintf("%d", req.Question[0].Qtype)) + v.Set("name", state.Name()) + v.Set("type", fmt.Sprintf("%d", state.QType())) + + optDebug := false + if bug := debug.IsDebug(state.Name()); bug != "" { + optDebug = true + v.Set("name", bug) + } start := time.Now() @@ -60,12 +68,20 @@ func (g *google) Exchange(req *dns.Msg) (*dns.Msg, error) { return nil, err } - m, err := toMsg(gm) + m, debug, err := toMsg(gm) if err != nil { return nil, err } - m.Id = req.Id + if optDebug { + // reset question + m.Question[0].Name = state.QName() + // prepend debug RR to the additional section + m.Extra = append([]dns.RR{debug}, m.Extra...) + + } + + m.Id = state.Req.Id return m, nil } @@ -223,8 +239,11 @@ func (g *google) do(addr, json string) ([]byte, error) { return buf, nil } -func toMsg(g *googleMsg) (*dns.Msg, error) { +// toMsg converts a googleMsg into the dns message. The returned RR is the comment disquised as a TXT +// record. +func toMsg(g *googleMsg) (*dns.Msg, dns.RR, error) { m := new(dns.Msg) + m.Response = true m.Rcode = g.Status m.Truncated = g.TC m.RecursionDesired = g.RD @@ -243,23 +262,24 @@ func toMsg(g *googleMsg) (*dns.Msg, error) { for i := 0; i < len(m.Answer); i++ { m.Answer[i], err = toRR(g.Answer[i]) if err != nil { - return nil, err + return nil, nil, err } } for i := 0; i < len(m.Ns); i++ { m.Ns[i], err = toRR(g.Authority[i]) if err != nil { - return nil, err + return nil, nil, err } } for i := 0; i < len(m.Extra); i++ { m.Extra[i], err = toRR(g.Additional[i]) if err != nil { - return nil, err + return nil, nil, err } } - return m, nil + txt, _ := dns.NewRR(". 0 CH TXT " + g.Comment) + return m, txt, nil } func toRR(g googleRR) (dns.RR, error) { diff --git a/middleware/httpproxy/proxy.go b/middleware/httpproxy/proxy.go index 6b1243dff..3ef638a8f 100644 --- a/middleware/httpproxy/proxy.go +++ b/middleware/httpproxy/proxy.go @@ -27,9 +27,9 @@ func (p *Proxy) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) start := time.Now() state := request.Request{W: w, Req: r} - reply, backendErr := p.e.Exchange(r) + reply, backendErr := p.e.Exchange(state) - if backendErr == nil { + if backendErr == nil && reply != nil { state.SizeAndDo(reply) w.WriteMsg(reply) diff --git a/middleware/httpproxy/setup_test.go b/middleware/httpproxy/setup_test.go index 71d631220..82db40aff 100644 --- a/middleware/httpproxy/setup_test.go +++ b/middleware/httpproxy/setup_test.go @@ -2,6 +2,7 @@ package httpproxy import ( "io/ioutil" + "log" "os" "strings" "testing" @@ -9,7 +10,9 @@ import ( "github.com/mholt/caddy" ) -func TestSetupChaos(t *testing.T) { +func TestSetupHttpproxy(t *testing.T) { + log.SetOutput(ioutil.Discard) + tests := []struct { input string shouldErr bool @@ -55,7 +58,6 @@ func TestSetupChaos(t *testing.T) { } if err != nil { - t.Logf("%q", err) if !test.shouldErr { t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err) } diff --git a/middleware/httpproxy/tls.go b/middleware/httpproxy/tls.go index 9651ac1c6..2c05a0331 100644 --- a/middleware/httpproxy/tls.go +++ b/middleware/httpproxy/tls.go @@ -5,13 +5,14 @@ import ( "net/http" "time" + "github.com/miekg/coredns/request" "github.com/miekg/dns" ) // Exchanger is an interface that specifies a type implementing a DNS resolver that // uses a HTTPS server. type Exchanger interface { - Exchange(*dns.Msg) (*dns.Msg, error) + Exchange(request.Request) (*dns.Msg, error) SetUpstream(*simpleUpstream) error OnStartup() error |