2018-01-26 18:43:28 +00:00
|
|
|
// The example plugin prints example to stdout on every packet received.
|
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"
|
2017-07-29 08:17:24 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
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-01-26 18:43:28 +00:00
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
2017-07-29 08:17:24 +00:00
|
|
|
func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2017-07-29 09:48:27 +00:00
|
|
|
// Somewhat convoluted, as we could have printed "example" here and just call
|
2018-01-26 18:43:28 +00:00
|
|
|
// the next plugin - but as an example, show how to wrap a ResponseWriter might be
|
2017-07-29 09:48:27 +00:00
|
|
|
// educational.
|
2017-07-29 08:17:24 +00:00
|
|
|
pw := NewResponsePrinter(w)
|
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" }
|
|
|
|
|
|
|
|
type ResponsePrinter struct {
|
|
|
|
dns.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponsePrinter returns a dns.Msg modifier that print example when a query is received.
|
|
|
|
func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter {
|
|
|
|
return &ResponsePrinter{ResponseWriter: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method.
|
|
|
|
func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
|
2018-01-26 18:43:28 +00:00
|
|
|
fmt.Fprintf(out, "example")
|
2017-07-29 08:17:24 +00:00
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
|
|
}
|
2018-01-26 18:43:28 +00:00
|
|
|
|
|
|
|
var out io.Writer = os.Stdout
|