(m *dns.Msg)
| 88 | } |
| 89 | |
| 90 | func (d *DNSServer) Lookup(m *dns.Msg) (*dns.Msg, error) { |
| 91 | key := makekey(m) |
| 92 | |
| 93 | // check the cache first |
| 94 | if item, found := d.cache.Get(key); found { |
| 95 | logrus.Debugf("dns cache hit %s", prettyPrintMsg(m)) |
| 96 | return item.(*dns.Msg), nil |
| 97 | } |
| 98 | |
| 99 | // fallback to upstream exchange |
| 100 | response, _, err := d.client.Exchange(m, net.JoinHostPort(d.upstream[0], "53")) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | if len(response.Answer) > 0 { |
| 106 | ttl := time.Duration(response.Answer[0].Header().Ttl) * time.Second |
| 107 | logrus.Debugf("caching dns response for %s for %v seconds", prettyPrintMsg(m), ttl) |
| 108 | d.cache.Set(key, response, ttl) |
| 109 | } |
| 110 | |
| 111 | return response, nil |
| 112 | } |
| 113 | |
| 114 | func makekey(m *dns.Msg) string { |
| 115 | q := m.Question[0] |
no test coverage detected