| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/pkg/singleflight"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | type dnsEx struct {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	Timeout time.Duration
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	Address string // address/name of this upstream
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	group *singleflight.Group
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | func newDNSEx(address string) *dnsEx {
 | 
					
						
							|  |  |  | 	return &dnsEx{Address: address, group: new(singleflight.Group), Timeout: defaultTimeout * time.Second}
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | func (d *dnsEx) OnStartup() error             { return nil }
 | 
					
						
							|  |  |  | func (d *dnsEx) OnShutdown() error            { return nil }
 | 
					
						
							|  |  |  | func (d *dnsEx) SetUpstream(u Upstream) error { return nil }
 | 
					
						
							|  |  |  | func (d *dnsEx) Protocol() protocol           { return dnsProto }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Exchange implements the Exchanger interface.
 | 
					
						
							|  |  |  | func (d *dnsEx) Exchange(state request.Request) (*dns.Msg, error) {
 | 
					
						
							|  |  |  | 	co, err := net.DialTimeout(state.Proto(), d.Address, d.Timeout)
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	reply, _, err := d.ExchangeConn(state.Req, co)
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	co.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if reply != nil && reply.Truncated {
 | 
					
						
							|  |  |  | 		// Suppress proxy error for truncated responses
 | 
					
						
							|  |  |  | 		err = nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reply.Compress = true
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	reply.Id = state.Req.Id
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return reply, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | func (d *dnsEx) ExchangeConn(m *dns.Msg, co net.Conn) (*dns.Msg, time.Duration, error) {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	t := "nop"
 | 
					
						
							|  |  |  | 	if t1, ok := dns.TypeToString[m.Question[0].Qtype]; ok {
 | 
					
						
							|  |  |  | 		t = t1
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cl := "nop"
 | 
					
						
							|  |  |  | 	if cl1, ok := dns.ClassToString[m.Question[0].Qclass]; ok {
 | 
					
						
							|  |  |  | 		cl = cl1
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	start := time.Now()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Name needs to be normalized! Bug in go dns.
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	r, err := d.group.Do(m.Question[0].Name+t+cl, func() (interface{}, error) {
 | 
					
						
							|  |  |  | 		return exchange(m, co)
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r1 := r.(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-11-23 19:04:37 +00:00
										 |  |  | 	rtt := time.Since(start)
 | 
					
						
							|  |  |  | 	return &r1, rtt, err
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // exchange does *not* return a pointer to dns.Msg because that leads to buffer reuse when
 | 
					
						
							|  |  |  | // group.Do is used in Exchange.
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | func exchange(m *dns.Msg, co net.Conn) (dns.Msg, error) {
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	opt := m.IsEdns0()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	udpsize := uint16(dns.MinMsgSize)
 | 
					
						
							|  |  |  | 	// If EDNS0 is used use that for size.
 | 
					
						
							|  |  |  | 	if opt != nil && opt.UDPSize() >= dns.MinMsgSize {
 | 
					
						
							|  |  |  | 		udpsize = opt.UDPSize()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnsco := &dns.Conn{Conn: co, UDPSize: udpsize}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 21:23:57 +00:00
										 |  |  | 	writeDeadline := time.Now().Add(defaultTimeout)
 | 
					
						
							|  |  |  | 	dnsco.SetWriteDeadline(writeDeadline)
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	dnsco.WriteMsg(m)
 | 
					
						
							| 
									
										
										
										
											2017-01-11 21:23:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	readDeadline := time.Now().Add(defaultTimeout)
 | 
					
						
							|  |  |  | 	co.SetReadDeadline(readDeadline)
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	r, err := dnsco.ReadMsg()
 | 
					
						
							| 
									
										
										
										
											2017-01-11 21:23:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	dnsco.Close()
 | 
					
						
							|  |  |  | 	if r == nil {
 | 
					
						
							|  |  |  | 		return dns.Msg{}, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return *r, err
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | const dnsProto protocol = "dns"
 |