2022-03-24 14:43:41 +00:00
|
|
|
// Package split is a CoreDNS plugin that prints "example" to stdout on every packet received.
|
2022-03-24 14:26:15 +00:00
|
|
|
//
|
|
|
|
// It serves as an example CoreDNS plugin with numerous code comments.
|
2022-03-24 14:43:41 +00:00
|
|
|
package split
|
2022-03-24 14:26:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 17:33:18 +00:00
|
|
|
"net"
|
2022-03-24 14:26:15 +00:00
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2022-12-17 22:09:46 +00:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/upstream"
|
2022-03-24 17:33:18 +00:00
|
|
|
"github.com/coredns/coredns/request"
|
2022-03-24 14:26:15 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Define log to be a logger with the plugin name in it. This way we can just use log.Info and
|
|
|
|
// friends to log.
|
2022-03-24 14:43:41 +00:00
|
|
|
var log = clog.NewWithPlugin("split")
|
2022-03-24 14:26:15 +00:00
|
|
|
|
2022-12-17 22:09:46 +00:00
|
|
|
const noFallback = "split-no-fallback"
|
|
|
|
|
|
|
|
func isNoFallback(ctx context.Context) bool {
|
|
|
|
if ctx == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v, ok := ctx.Value(noFallback).(bool); ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-24 14:43:41 +00:00
|
|
|
// Split is an example plugin to show how to write a plugin.
|
|
|
|
type Split struct {
|
2022-03-24 14:26:15 +00:00
|
|
|
Next plugin.Handler
|
2022-03-24 17:33:18 +00:00
|
|
|
|
2022-10-27 11:18:20 +00:00
|
|
|
Rules []Rule
|
2022-03-24 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 11:18:20 +00:00
|
|
|
type Rule struct {
|
|
|
|
Zones []string
|
|
|
|
Networks []Network
|
|
|
|
Fallback net.IP
|
2022-03-24 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 11:18:20 +00:00
|
|
|
type Network struct {
|
|
|
|
RecordNetwork *net.IPNet
|
|
|
|
Allowed []*net.IPNet
|
2022-03-24 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeDNS implements the plugin.Handler interface. This method gets called when example is used
|
|
|
|
// in a Server.
|
2022-03-24 17:33:18 +00:00
|
|
|
func (s Split) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2022-03-24 14:26:15 +00:00
|
|
|
// This function could be simpler. I.e. just fmt.Println("example") here, but we want to show
|
|
|
|
// a slightly more complex example as to make this more interesting.
|
|
|
|
// Here we wrap the dns.ResponseWriter in a new ResponseWriter and call the next plugin, when the
|
|
|
|
// answer comes back, it will print "example".
|
|
|
|
|
2022-03-24 17:33:18 +00:00
|
|
|
// Debug log that we've seen the query. This will only be shown when the debug plugin is loaded.
|
2022-03-24 14:26:15 +00:00
|
|
|
log.Debug("Received response")
|
|
|
|
|
|
|
|
// Wrap.
|
2022-12-17 22:09:46 +00:00
|
|
|
pw := s.NewResponsePrinter(ctx, w, r)
|
2022-03-24 14:26:15 +00:00
|
|
|
|
|
|
|
// Export metric with the server label set to the current server handling the request.
|
|
|
|
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
|
|
|
|
|
|
|
|
// Call next plugin (if any).
|
2022-03-24 17:33:18 +00:00
|
|
|
return plugin.NextOrFailure(s.Name(), s.Next, ctx, pw, r)
|
2022-03-24 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements the Handler interface.
|
2022-03-24 17:33:18 +00:00
|
|
|
func (s Split) Name() string { return "split" }
|
2022-03-24 14:26:15 +00:00
|
|
|
|
|
|
|
// ResponsePrinter wrap a dns.ResponseWriter and will write example to standard output when WriteMsg is called.
|
|
|
|
type ResponsePrinter struct {
|
|
|
|
dns.ResponseWriter
|
2022-12-17 22:09:46 +00:00
|
|
|
ctx context.Context
|
2022-03-24 17:33:18 +00:00
|
|
|
state request.Request
|
|
|
|
r *dns.Msg
|
|
|
|
src net.IP
|
2022-10-27 11:18:20 +00:00
|
|
|
rules []Rule
|
2022-03-24 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponsePrinter returns ResponseWriter.
|
2022-12-17 22:09:46 +00:00
|
|
|
func (s Split) NewResponsePrinter(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) *ResponsePrinter {
|
2022-03-24 17:33:18 +00:00
|
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
ip := net.ParseIP(state.IP())
|
2022-12-17 22:09:46 +00:00
|
|
|
return &ResponsePrinter{ctx: ctx, ResponseWriter: w, r: r, src: ip, rules: s.Rules, state: state}
|
2022-03-24 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "example" to standard output.
|
|
|
|
func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
|
2022-12-17 22:09:46 +00:00
|
|
|
filter := func(rec *dns.A) (rule Rule, allowed, match bool) {
|
|
|
|
for _, v := range r.rules {
|
|
|
|
zone := plugin.Zones(v.Zones).Matches(r.state.Name())
|
|
|
|
if zone == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rule = v
|
|
|
|
break
|
2022-03-24 17:33:18 +00:00
|
|
|
}
|
2022-10-27 11:18:20 +00:00
|
|
|
var net *Network
|
|
|
|
for _, vv := range rule.Networks {
|
|
|
|
if vv.RecordNetwork.Contains(rec.A) {
|
2022-03-24 17:33:18 +00:00
|
|
|
net = &vv
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if net == nil {
|
2022-12-17 22:09:46 +00:00
|
|
|
return rule, true, false
|
2022-03-24 17:33:18 +00:00
|
|
|
}
|
2022-12-17 22:09:46 +00:00
|
|
|
|
2022-10-27 11:18:20 +00:00
|
|
|
for _, vv := range net.Allowed {
|
2022-03-24 17:33:18 +00:00
|
|
|
if vv.Contains(r.src) {
|
2022-12-17 22:09:46 +00:00
|
|
|
return rule, true, true
|
2022-03-24 17:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 22:09:46 +00:00
|
|
|
return rule, false, true
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
rule Rule
|
|
|
|
answers []dns.RR
|
|
|
|
netAnswers []dns.RR
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, v := range res.Answer {
|
|
|
|
switch rec := v.(type) {
|
|
|
|
case *dns.A:
|
|
|
|
var allowed, match bool
|
|
|
|
rule, allowed, match = filter(rec)
|
|
|
|
if !match {
|
|
|
|
answers = append(answers, v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if allowed {
|
|
|
|
answers = append(answers, v)
|
|
|
|
netAnswers = append(netAnswers, v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Infof("request source %s: %s: filtering %s", r.src.String(), rec.Hdr.Name, rec.A)
|
|
|
|
case *dns.CNAME:
|
|
|
|
res, err := r.query(rec.Target)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error querying %s: %s", rec.Target, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if res == nil || len(res.Answer) == 0 {
|
|
|
|
log.Debugf("no answers for %s", rec.Target)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
answers = append(answers, v)
|
|
|
|
case *dns.SRV:
|
|
|
|
res, err := r.query(rec.Target)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error querying %s: %s", rec.Target, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if res == nil || len(res.Answer) == 0 {
|
|
|
|
log.Debugf("no answers for %s", rec.Target)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
answers = append(answers, v)
|
|
|
|
case *dns.PTR:
|
|
|
|
a, err := r.query(rec.Ptr)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error querying %s: %s", rec.Ptr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if res == nil || len(a.Answer) == 0 {
|
|
|
|
log.Debugf("no answer for %s", rec.Ptr)
|
|
|
|
continue
|
|
|
|
}
|
2022-03-24 17:33:18 +00:00
|
|
|
answers = append(answers, v)
|
2022-12-17 22:09:46 +00:00
|
|
|
default:
|
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
2022-03-24 17:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(netAnswers) != 0 {
|
|
|
|
res.Answer = netAnswers
|
|
|
|
} else {
|
|
|
|
res.Answer = answers
|
|
|
|
}
|
2022-12-17 22:09:46 +00:00
|
|
|
if len(res.Answer) != 0 || len(rule.Zones) == 0 {
|
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
|
|
}
|
|
|
|
if isNoFallback(r.ctx) {
|
|
|
|
log.Debugf("no fallback requested for %s", r.state.Name())
|
2022-10-27 11:18:20 +00:00
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
|
|
}
|
|
|
|
if rule.Fallback == nil {
|
2022-12-17 22:09:46 +00:00
|
|
|
log.Debugf("no fallback configured for zones %v", rule.Zones)
|
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
2022-10-27 11:18:20 +00:00
|
|
|
}
|
2022-12-17 22:09:46 +00:00
|
|
|
log.Debugf("request source %s: %s: using fallback %s", r.src.String(), r.state.Name(), rule.Fallback)
|
2022-10-27 11:18:20 +00:00
|
|
|
c := new(dns.Client)
|
|
|
|
req := r.state.Req.Copy()
|
|
|
|
req.Id = dns.Id()
|
|
|
|
in, _, err := c.Exchange(req, rule.Fallback.String()+":53")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.Answer = append(res.Answer, in.Answer...)
|
2022-03-24 14:26:15 +00:00
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
|
|
}
|
2022-12-17 22:09:46 +00:00
|
|
|
|
|
|
|
func (r *ResponsePrinter) query(name string) (*dns.Msg, error) {
|
|
|
|
log.Debugf("internally querying %s", name)
|
|
|
|
ctx := context.WithValue(r.ctx, noFallback, true)
|
|
|
|
res, err := upstream.New().Lookup(ctx, r.state, name, dns.TypeA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|