aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/upstream/upstream.go
blob: 239d3dd96ae9f6d66976ec8de04441e58e6f37a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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/nonwriter"
	"github.com/coredns/coredns/plugin/pkg/parse"
	"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
}

// New creates a new Upstream for given destination(s). If dests is empty it default to upstreaming to
// the coredns process.
func New(dests []string) (Upstream, error) {
	u := Upstream{}
	if len(dests) == 0 {
		u.self = true
		return u, nil
	}
	u.self = false
	ups, err := parse.HostPortOrFile(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
}