diff --git a/README.md b/README.md index d57ac4c..03cea7b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You find releases in [Github Releases](https://github.com/talon-one/talon-access Or you can use `go get`: ```bash -go get gopkg.in/talon-one/talon-access-proxy.v1 +go get gopkg.in/talon-one/talon-access-proxy.v2 ``` # Usage diff --git a/dnscache/dnscache.go b/dnscache/dnscache.go index 2fbb304..733eab4 100644 --- a/dnscache/dnscache.go +++ b/dnscache/dnscache.go @@ -89,12 +89,13 @@ func (cache *DNSCache) Server() error { return nil } -func (cache *DNSCache) getCacheEntries(name string) ([]dns.RR, error) { +func (cache *DNSCache) getCacheEntries(name string, Qclass uint16, Qtype uint16) ([]dns.RR, error) { now := time.Now() var entries []dns.RR again: for i := 0; i < len(cache.entries); i++ { - if cache.entries[i].RR.Header().Name == name { + hdr := cache.entries[i].RR.Header() + if hdr.Class == Qclass && hdr.Rrtype == Qtype && hdr.Name == name { if !cache.entries[i].ValidUntil.IsZero() && cache.entries[i].ValidUntil.Before(now) { // dns entry is old if err := cache.refreshCacheEntries(&cache.entries[i]); err != nil { @@ -162,9 +163,7 @@ func (cache *DNSCache) ResolveAndAdd(dnsServer, net string, host string, Qclass client.Net = net client.DialTimeout = cache.DialTimeout - host = strings.TrimRightFunc(host, func(r rune) bool { - return r == '.' - }) + "." + sanitizeHost(&host) msg.Question = []dns.Question{ dns.Question{ @@ -243,3 +242,15 @@ func (cache *DNSCache) Addr() string { } return cache.server.PacketConn.LocalAddr().String() } + +// Lookup looks up an entry in the cache +func (cache *DNSCache) Lookup(host string, Qclass uint16, Qtype uint16) ([]dns.RR, error) { + sanitizeHost(&host) + return cache.getCacheEntries(host, Qclass, Qtype) +} + +func sanitizeHost(host *string) { + *host = strings.TrimRightFunc(strings.ToLower(*host), func(r rune) bool { + return r == '.' + }) + "." +} diff --git a/dnscache/handler.go b/dnscache/handler.go index bacd4e8..67db120 100644 --- a/dnscache/handler.go +++ b/dnscache/handler.go @@ -1,8 +1,6 @@ package dnscache import ( - "strings" - "github.com/miekg/dns" "go.uber.org/zap" ) @@ -17,15 +15,14 @@ func (handler handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { msg.Rcode = dns.RcodeServerFailure } else { for i := 0; i < len(r.Question); i++ { - name := strings.ToLower(r.Question[i].Name) - entries, err := handler.cache.getCacheEntries(name) + entries, err := handler.cache.Lookup(r.Question[i].Name, r.Question[i].Qclass, r.Question[i].Qtype) if err != nil { handler.cache.Logger.Error("Get cache entries failed", zap.String("error", err.Error())) msg.Answer = []dns.RR{} msg.Rcode = dns.RcodeServerFailure break } - handler.cache.addCacheEntries(name, entries, &msg.Answer) + handler.cache.addCacheEntries(r.Question[i].Name, entries, &msg.Answer) } if len(msg.Answer) <= 0 { msg.Rcode = dns.RcodeServerFailure diff --git a/tap.go b/tap.go index f7aaed7..881eb64 100644 --- a/tap.go +++ b/tap.go @@ -119,6 +119,8 @@ func (t *Tap) doHTTPRequest(r *http.Request) (*http.Response, error) { Close: false, } + req.Header.Set("X-TAP", Version) + logger := t.logger if t.logger.Core().Enabled(zap.DebugLevel) {