| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-04-26 20:45:29 +01:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/response"
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	gcache "github.com/patrickmn/go-cache"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Cache is middleware that looks up responses in a cache and caches replies.
 | 
					
						
							|  |  |  | type Cache struct {
 | 
					
						
							|  |  |  | 	Next  middleware.Handler
 | 
					
						
							|  |  |  | 	Zones []string
 | 
					
						
							|  |  |  | 	cache *gcache.Cache
 | 
					
						
							|  |  |  | 	cap   time.Duration
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewCache(ttl int, zones []string, next middleware.Handler) Cache {
 | 
					
						
							|  |  |  | 	return Cache{Next: next, Zones: zones, cache: gcache.New(defaultDuration, purgeDuration), cap: time.Duration(ttl) * time.Second}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func cacheKey(m *dns.Msg, t response.Type, do bool) string {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	if m.Truncated {
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	qtype := m.Question[0].Qtype
 | 
					
						
							| 
									
										
										
										
											2016-04-26 20:45:29 +01:00
										 |  |  | 	qname := strings.ToLower(m.Question[0].Name)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	switch t {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.Success:
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		fallthrough
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.Delegation:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return successKey(qname, qtype, do)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.NameError:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return nameErrorKey(qname, do)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.NoData:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return noDataKey(qname, qtype, do)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.OtherError:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return ""
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type CachingResponseWriter struct {
 | 
					
						
							|  |  |  | 	dns.ResponseWriter
 | 
					
						
							|  |  |  | 	cache *gcache.Cache
 | 
					
						
							|  |  |  | 	cap   time.Duration
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewCachingResponseWriter(w dns.ResponseWriter, cache *gcache.Cache, cap time.Duration) *CachingResponseWriter {
 | 
					
						
							|  |  |  | 	return &CachingResponseWriter{w, cache, cap}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (c *CachingResponseWriter) WriteMsg(res *dns.Msg) error {
 | 
					
						
							|  |  |  | 	do := false
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	mt, opt := response.Classify(res)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	if opt != nil {
 | 
					
						
							|  |  |  | 		do = opt.Do()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	key := cacheKey(res, mt, do)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	c.set(res, key, mt)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if c.cap != 0 {
 | 
					
						
							|  |  |  | 		setCap(res, uint32(c.cap.Seconds()))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return c.ResponseWriter.WriteMsg(res)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (c *CachingResponseWriter) set(m *dns.Msg, key string, mt response.Type) {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	if key == "" {
 | 
					
						
							| 
									
										
										
										
											2016-05-22 19:43:58 +01:00
										 |  |  | 		log.Printf("[ERROR] Caching called with empty cache key")
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	duration := c.cap
 | 
					
						
							|  |  |  | 	switch mt {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.Success, response.Delegation:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		if c.cap == 0 {
 | 
					
						
							|  |  |  | 			duration = minTtl(m.Answer, mt)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		i := newItem(m, duration)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c.cache.Set(key, i, duration)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.NameError, response.NoData:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		if c.cap == 0 {
 | 
					
						
							|  |  |  | 			duration = minTtl(m.Ns, mt)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		i := newItem(m, duration)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c.cache.Set(key, i, duration)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.OtherError:
 | 
					
						
							| 
									
										
										
										
											2016-05-22 19:43:58 +01:00
										 |  |  | 		// don't cache these
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		log.Printf("[WARNING] Caching called with unknown middleware MsgType: %d", mt)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (c *CachingResponseWriter) Write(buf []byte) (int, error) {
 | 
					
						
							|  |  |  | 	log.Printf("[WARNING] Caching called with Write: not caching reply")
 | 
					
						
							|  |  |  | 	n, err := c.ResponseWriter.Write(buf)
 | 
					
						
							|  |  |  | 	return n, err
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (c *CachingResponseWriter) Hijack() {
 | 
					
						
							|  |  |  | 	c.ResponseWriter.Hijack()
 | 
					
						
							|  |  |  | 	return
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func minTtl(rrs []dns.RR, mt response.Type) time.Duration {
 | 
					
						
							|  |  |  | 	if mt != response.Success && mt != response.NameError && mt != response.NoData {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return 0
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	minTtl := maxTtl
 | 
					
						
							|  |  |  | 	for _, r := range rrs {
 | 
					
						
							|  |  |  | 		switch mt {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		case response.NameError, response.NoData:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 			if r.Header().Rrtype == dns.TypeSOA {
 | 
					
						
							|  |  |  | 				return time.Duration(r.(*dns.SOA).Minttl) * time.Second
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		case response.Success, response.Delegation:
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 			if r.Header().Ttl < minTtl {
 | 
					
						
							|  |  |  | 				minTtl = r.Header().Ttl
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return time.Duration(minTtl) * time.Second
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	purgeDuration          = 1 * time.Minute
 | 
					
						
							|  |  |  | 	defaultDuration        = 20 * time.Minute
 | 
					
						
							|  |  |  | 	baseTtl                = 5 // minimum ttl that we will allow
 | 
					
						
							|  |  |  | 	maxTtl          uint32 = 2 * 3600
 | 
					
						
							|  |  |  | )
 |