-
Notifications
You must be signed in to change notification settings - Fork 1
/
upstream.go
35 lines (27 loc) · 1.22 KB
/
upstream.go
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
// Package upstream abstracts a upstream lookups so that plugins can handle them in an unified way.
package upstream
import (
"context"
"fmt"
"github.com/khulnasoft-lab/dnserver/core/dnsserver"
"github.com/khulnasoft-lab/dnserver/plugin/pkg/nonwriter"
"github.com/khulnasoft-lab/dnserver/request"
"github.com/miekg/dns"
)
// Upstream is used to resolve CNAME or other external targets via DNServer itself.
type Upstream struct{}
// New creates a new Upstream to resolve names using the dnsserver process.
func New() *Upstream { return &Upstream{} }
// Lookup routes lookups to our selves to make it follow the plugin chain *again*, but with a (possibly) new query. As
// we are doing the query against ourselves again, there is no actual new hop, as such RFC 6891 does not apply and we
// need the EDNS0 option present in the *original* query to be present here too.
func (u *Upstream) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
server, ok := ctx.Value(dnsserver.Key{}).(*dnsserver.Server)
if !ok {
return nil, fmt.Errorf("no full server is running")
}
req := state.NewWithQuestion(name, typ)
nw := nonwriter.New(state.W)
server.ServeDNS(ctx, nw, req.Req)
return nw.Msg, nil
}