| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Package chaos implements a plugin that answer to 'CH version.bind TXT' type queries.
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | package chaos
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 09:10:07 +00:00
										 |  |  | // Chaos allows CoreDNS to reply to CH TXT queries and return author or
 | 
					
						
							|  |  |  | // version information.
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | type Chaos struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next    plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 	Version string
 | 
					
						
							| 
									
										
										
										
											2016-04-04 09:10:07 +00:00
										 |  |  | 	Authors map[string]bool
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | func (c Chaos) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	state := request.Request{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:30:38 +00:00
										 |  |  | 	if state.QClass() != dns.ClassCHAOS || state.QType() != dns.TypeTXT {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.NextOrFailure(c.Name(), c.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:17 +01:00
										 |  |  | 	m.SetReply(r)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 	hdr := dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeTXT, Class: dns.ClassCHAOS, Ttl: 0}
 | 
					
						
							|  |  |  | 	switch state.Name() {
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		return c.Next.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 	case "authors.bind.":
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 		for a := range c.Authors {
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 			m.Answer = append(m.Answer, &dns.TXT{Hdr: hdr, Txt: []string{trim(a)}})
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	case "version.bind.", "version.server.":
 | 
					
						
							|  |  |  | 		m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{trim(c.Version)}}}
 | 
					
						
							|  |  |  | 	case "hostname.bind.", "id.server.":
 | 
					
						
							|  |  |  | 		hostname, err := os.Hostname()
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			hostname = "localhost"
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{trim(hostname)}}}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	state.SizeAndDo(m)
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | 	w.WriteMsg(m)
 | 
					
						
							|  |  |  | 	return 0, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (c Chaos) Name() string { return "chaos" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:58:31 +00:00
										 |  |  | func trim(s string) string {
 | 
					
						
							|  |  |  | 	if len(s) < 256 {
 | 
					
						
							|  |  |  | 		return s
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return s[:255]
 | 
					
						
							|  |  |  | }
 |