mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 02:03:13 -05: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:
50
middleware/whoami/whoami.go
Normal file
50
middleware/whoami/whoami.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user