mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 10:43:17 -04:00
middleware/whoami: add (#264)
Add a new middleware that tells you who you are; IP, port and transport is echoed back. Also some various cleanup and documentation improvements while at it: * ResponseWriter: improve the documentation of these helper functions. * And add an NextHandler for use in tests. Make chaos_test.go and * whoam_test.go use it.
This commit is contained in:
@@ -224,6 +224,7 @@ func Section(t *testing.T, tc Case, sect Sect, rr []dns.RR) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ErrorHanlder returns a Handler that returns ServerFailure error when called.
|
||||
func ErrorHandler() Handler {
|
||||
return HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
m := new(dns.Msg)
|
||||
@@ -233,7 +234,14 @@ func ErrorHandler() Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// Copied here to prevent an import cycle.
|
||||
// NextHandler returns a Handler that returns rcode and err.
|
||||
func NextHandler(rcode int, err error) Handler {
|
||||
return HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
return rcode, err
|
||||
})
|
||||
}
|
||||
|
||||
// Copied here to prevent an import cycle, so that we can define to above handlers.
|
||||
type (
|
||||
// HandlerFunc is a convenience type like dns.HandlerFunc, except
|
||||
// ServeDNS returns an rcode and an error.
|
||||
|
||||
@@ -6,14 +6,19 @@ import (
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// ResponseWriter is useful for writing tests. It uses some fixed values for the client. The
|
||||
// remote will always be 10.240.0.1 and port 40212. The local address is always 127.0.0.1 and
|
||||
// port 53.
|
||||
type ResponseWriter struct{}
|
||||
|
||||
// LocalAddr returns the local address, always 127.0.0.1:53 (UDP).
|
||||
func (t *ResponseWriter) LocalAddr() net.Addr {
|
||||
ip := net.ParseIP("127.0.0.1")
|
||||
port := 53
|
||||
return &net.UDPAddr{IP: ip, Port: port, Zone: ""}
|
||||
}
|
||||
|
||||
// RemoteAddr returns the remote address, always 10.240.0.1:40212 (UDP).
|
||||
func (t *ResponseWriter) RemoteAddr() net.Addr {
|
||||
ip := net.ParseIP("10.240.0.1")
|
||||
port := 40212
|
||||
|
||||
Reference in New Issue
Block a user