aboutsummaryrefslogtreecommitdiff
path: root/middleware/whoami/whoami.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2016-09-17 17:09:05 +0100
committerGravatar GitHub <noreply@github.com> 2016-09-17 17:09:05 +0100
commit30fd224504d7e6fcfa7da027d62d2105ecb6f0c5 (patch)
treed63f32750f56265e13d5a8c98ef840f1c343395f /middleware/whoami/whoami.go
parented907d33278003072443029fef666232f16e1985 (diff)
downloadcoredns-30fd224504d7e6fcfa7da027d62d2105ecb6f0c5.tar.gz
coredns-30fd224504d7e6fcfa7da027d62d2105ecb6f0c5.tar.zst
coredns-30fd224504d7e6fcfa7da027d62d2105ecb6f0c5.zip
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.
Diffstat (limited to 'middleware/whoami/whoami.go')
-rw-r--r--middleware/whoami/whoami.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/middleware/whoami/whoami.go b/middleware/whoami/whoami.go
new file mode 100644
index 000000000..4117db35e
--- /dev/null
+++ b/middleware/whoami/whoami.go
@@ -0,0 +1,50 @@
+package whoami
+
+import (
+ "net"
+ "strconv"
+
+ "github.com/miekg/coredns/middleware"
+ "github.com/miekg/coredns/request"
+
+ "github.com/miekg/dns"
+ "golang.org/x/net/context"
+)
+
+type Whoami struct {
+ Next middleware.Handler
+}
+
+func (wh Whoami) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
+ state := request.Request{W: w, Req: r}
+
+ a := new(dns.Msg)
+ a.SetReply(r)
+ a.Compress = true
+ a.Authoritative = true
+
+ ip := state.IP()
+ var rr dns.RR
+
+ switch state.Family() {
+ case 1:
+ rr = new(dns.A)
+ rr.(*dns.A).Hdr = dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()}
+ rr.(*dns.A).A = net.ParseIP(ip).To4()
+ case 2:
+ rr = new(dns.AAAA)
+ rr.(*dns.AAAA).Hdr = dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()}
+ rr.(*dns.AAAA).AAAA = net.ParseIP(ip)
+ }
+
+ srv := new(dns.SRV)
+ srv.Hdr = dns.RR_Header{Name: "_" + state.Proto() + "." + state.QName(), Rrtype: dns.TypeSRV, Class: state.QClass()}
+ port, _ := strconv.Atoi(state.Port())
+ srv.Port = uint16(port)
+
+ a.Extra = append(a.Extra, rr)
+ a.Extra = append(a.Extra, srv)
+
+ w.WriteMsg(a)
+ return 0, nil
+}