| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // Reflect provides middleware that reflects back some client properties.
 | 
					
						
							|  |  |  | // This is the default middleware when Caddy is run without configuration.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The left-most label must be `who`.
 | 
					
						
							|  |  |  | // When queried for type A (resp. AAAA), it sends back the IPv4 (resp. v6) address.
 | 
					
						
							|  |  |  | // In the additional section the port number and transport are shown.
 | 
					
						
							|  |  |  | // Basic use pattern:
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	dig @localhost -p 1053 who.miek.nl A
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	;; ANSWER SECTION:
 | 
					
						
							|  |  |  | //	who.miek.nl.		0	IN	A	127.0.0.1
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	;; ADDITIONAL SECTION:
 | 
					
						
							|  |  |  | //	who.miek.nl.		0	IN	TXT	"Port: 56195 (udp)"
 | 
					
						
							|  |  |  | package reflect
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"errors"
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Reflect struct {
 | 
					
						
							|  |  |  | 	Next middleware.Handler
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (rl Reflect) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	state := middleware.State{Req: r, W: w}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	class := r.Question[0].Qclass
 | 
					
						
							|  |  |  | 	qname := r.Question[0].Name
 | 
					
						
							|  |  |  | 	i, ok := dns.NextLabel(qname, 0)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if strings.ToLower(qname[:i]) != who || ok {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 		err := state.ErrorMessage(dns.RcodeFormatError)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		w.WriteMsg(err)
 | 
					
						
							|  |  |  | 		return dns.RcodeFormatError, errors.New(dns.RcodeToString[dns.RcodeFormatError])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	answer := new(dns.Msg)
 | 
					
						
							|  |  |  | 	answer.SetReply(r)
 | 
					
						
							|  |  |  | 	answer.Compress = true
 | 
					
						
							|  |  |  | 	answer.Authoritative = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	ip := state.IP()
 | 
					
						
							|  |  |  | 	proto := state.Proto()
 | 
					
						
							|  |  |  | 	port, _ := state.Port()
 | 
					
						
							|  |  |  | 	family := state.Family()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	var rr dns.RR
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch family {
 | 
					
						
							|  |  |  | 	case 1:
 | 
					
						
							|  |  |  | 		rr = new(dns.A)
 | 
					
						
							|  |  |  | 		rr.(*dns.A).Hdr = dns.RR_Header{Name: qname, Rrtype: dns.TypeA, Class: class, Ttl: 0}
 | 
					
						
							|  |  |  | 		rr.(*dns.A).A = net.ParseIP(ip).To4()
 | 
					
						
							|  |  |  | 	case 2:
 | 
					
						
							|  |  |  | 		rr = new(dns.AAAA)
 | 
					
						
							|  |  |  | 		rr.(*dns.AAAA).Hdr = dns.RR_Header{Name: qname, Rrtype: dns.TypeAAAA, Class: class, Ttl: 0}
 | 
					
						
							|  |  |  | 		rr.(*dns.AAAA).AAAA = net.ParseIP(ip)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t := new(dns.TXT)
 | 
					
						
							|  |  |  | 	t.Hdr = dns.RR_Header{Name: qname, Rrtype: dns.TypeTXT, Class: class, Ttl: 0}
 | 
					
						
							|  |  |  | 	t.Txt = []string{"Port: " + port + " (" + proto + ")"}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	switch state.Type() {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	case "TXT":
 | 
					
						
							|  |  |  | 		answer.Answer = append(answer.Answer, t)
 | 
					
						
							|  |  |  | 		answer.Extra = append(answer.Extra, rr)
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		fallthrough
 | 
					
						
							|  |  |  | 	case "AAAA", "A":
 | 
					
						
							|  |  |  | 		answer.Answer = append(answer.Answer, rr)
 | 
					
						
							|  |  |  | 		answer.Extra = append(answer.Extra, t)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	w.WriteMsg(answer)
 | 
					
						
							|  |  |  | 	return 0, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const who = "who."
 |