Skip to content

Commit

Permalink
Send TAP version to Talon API
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Salzmann committed Apr 17, 2018
1 parent ea83963 commit b50bea0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions dnscache/dnscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 == '.'
}) + "."
}
7 changes: 2 additions & 5 deletions dnscache/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dnscache

import (
"strings"

"github.com/miekg/dns"
"go.uber.org/zap"
)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b50bea0

Please sign in to comment.