2018-04-01 11:56:41 +00:00
|
|
|
// Package example is a CoreDNS plugin that prints "example" to stdout on every packet received.
|
|
|
|
//
|
|
|
|
// It serves as an example CoreDNS plugin with numerous code comments.
|
2017-07-29 08:17:24 +00:00
|
|
|
package example
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-26 18:43:28 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-07-29 08:17:24 +00:00
|
|
|
|
2017-09-15 20:29:47 +00:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2018-04-01 11:56:41 +00:00
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
2018-04-25 18:32:42 +00:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2018-02-27 11:39:01 +00:00
|
|
|
|
2017-07-29 08:17:24 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2018-04-26 20:56:11 +00:00
|
|
|
// Define log to be a logger with the plugin name in it. This way we can just use log.Info and
|
|
|
|
// friends to log.
|
2018-04-25 18:32:42 +00:00
|
|
|
var log = clog.NewWithPlugin("example")
|
|
|
|
|
2018-01-26 18:43:28 +00:00
|
|
|
// Example is an example plugin to show how to write a plugin.
|
2017-07-29 08:17:24 +00:00
|
|
|
type Example struct {
|
2017-09-15 20:29:47 +00:00
|
|
|
Next plugin.Handler
|
2017-07-29 08:17:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// ServeDNS implements the plugin.Handler interface. This method gets called when example is used
|
|
|
|
// in a Server.
|
2017-07-29 08:17:24 +00:00
|
|
|
func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2018-02-25 08:52:52 +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".
|
|
|
|
|
2018-04-19 06:25:30 +00:00
|
|
|
// Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded.
|
|
|
|
log.Debug("Received response")
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Wrap.
|
2017-07-29 08:17:24 +00:00
|
|
|
pw := NewResponsePrinter(w)
|
2018-02-25 08:52:52 +00:00
|
|
|
|
2018-04-01 11:56:41 +00:00
|
|
|
// Export metric with the server label set to the current server handling the request.
|
2018-04-26 20:56:11 +00:00
|
|
|
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
|
2018-04-01 11:56:41 +00:00
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Call next plugin (if any).
|
2017-09-15 20:29:47 +00:00
|
|
|
return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
|
2017-07-29 08:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements the Handler interface.
|
|
|
|
func (e Example) Name() string { return "example" }
|
|
|
|
|
2018-04-26 20:56:11 +00:00
|
|
|
// ResponsePrinter wrap a dns.ResponseWriter and will write example to standard output when WriteMsg is called.
|
2017-07-29 08:17:24 +00:00
|
|
|
type ResponsePrinter struct {
|
|
|
|
dns.ResponseWriter
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// NewResponsePrinter returns ResponseWriter.
|
2017-07-29 08:17:24 +00:00
|
|
|
func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter {
|
|
|
|
return &ResponsePrinter{ResponseWriter: w}
|
|
|
|
}
|
|
|
|
|
2018-04-26 20:56:11 +00:00
|
|
|
// WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "example" to standard output.
|
2017-07-29 08:17:24 +00:00
|
|
|
func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
|
2018-02-25 08:52:52 +00:00
|
|
|
fmt.Fprintln(out, ex)
|
2017-07-29 08:17:24 +00:00
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
|
|
}
|
2018-01-26 18:43:28 +00:00
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Make out a reference to os.Stdout so we can easily overwrite it for testing.
|
2018-01-26 18:43:28 +00:00
|
|
|
var out io.Writer = os.Stdout
|
2018-02-25 08:52:52 +00:00
|
|
|
|
|
|
|
// What we will print.
|
|
|
|
const ex = "example"
|