Files
coredns/plugin/pkg/upstream/upstream.go
Miek Gieben 26c41a0c17 plugin/file: fix local CNAME lookup (#1866)
* plugin/file: fix local CNAME lookup

Issue #1864 explains it will, when we serve the child zone as well we
should just recursive into ourself (upstream self). Thus relax the
IsSubDomain check in file/lookup.go and just query (even if the query
will hit a remote server).

I've looped over all other plugins that do something similar (CNAME
resolving) and they didn't do the IsSubDomain check; therefor I've
removed it from *file* as well.

Added test in file_upstream_test that shows this failed before but now
results in a reply.

Fixes #1864

* self does not need to be exported

* Fix test

We don't know if we had a valid reply. Check this.
2018-06-12 14:54:37 +01:00

59 lines
1.3 KiB
Go

// Package upstream abstracts a upstream lookups so that plugins
// can handle them in an unified way.
package upstream
import (
"github.com/miekg/dns"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/plugin/pkg/nonwriter"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/request"
)
// Upstream is used to resolve CNAME targets
type Upstream struct {
self bool
Forward *proxy.Proxy
}
// NewUpstream creates a new Upstream for given destination(s). If dests is empty
// it default to upstreaming to Self.
func NewUpstream(dests []string) (Upstream, error) {
u := Upstream{}
if len(dests) == 0 {
u.self = true
return u, nil
}
u.self = false
ups, err := dnsutil.ParseHostPortOrFile(dests...)
if err != nil {
return u, err
}
p := proxy.NewLookup(ups)
u.Forward = &p
return u, nil
}
// Lookup routes lookups to our selves or forward to a remote.
func (u Upstream) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
if u.self {
req := new(dns.Msg)
req.SetQuestion(name, typ)
nw := nonwriter.New(state.W)
server := state.Context.Value(dnsserver.Key{}).(*dnsserver.Server)
server.ServeDNS(state.Context, nw, req)
return nw.Msg, nil
}
if u.Forward != nil {
return u.Forward.Lookup(state, name, typ)
}
return nil, nil
}