| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Package whoami implements a plugin that returns details about the resolving
 | 
					
						
							| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // querying it.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | package whoami
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Whoami is a plugin that returns your IP address, port and the protocol used for connecting
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // to CoreDNS.
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | type Whoami struct{}
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 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.Authoritative = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ip := state.IP()
 | 
					
						
							|  |  |  | 	var rr dns.RR
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch state.Family() {
 | 
					
						
							|  |  |  | 	case 1:
 | 
					
						
							|  |  |  | 		rr = new(dns.A)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:50:16 +01:00
										 |  |  | 		rr.(*dns.A).Hdr = dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: state.QClass()}
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 		rr.(*dns.A).A = net.ParseIP(ip).To4()
 | 
					
						
							|  |  |  | 	case 2:
 | 
					
						
							|  |  |  | 		rr = new(dns.AAAA)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:50:16 +01:00
										 |  |  | 		rr.(*dns.AAAA).Hdr = dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeAAAA, Class: state.QClass()}
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 		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()}
 | 
					
						
							| 
									
										
										
										
											2017-10-05 06:14:02 -07:00
										 |  |  | 	if state.QName() == "." {
 | 
					
						
							|  |  |  | 		srv.Hdr.Name = "_" + state.Proto() + state.QName()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	port, _ := strconv.Atoi(state.Port())
 | 
					
						
							|  |  |  | 	srv.Port = uint16(port)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:50:16 +01:00
										 |  |  | 	srv.Target = "."
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:50:16 +01:00
										 |  |  | 	a.Extra = []dns.RR{rr, srv}
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	w.WriteMsg(a)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:50:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	return 0, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (wh Whoami) Name() string { return "whoami" }
 |