Skip to content

Commit

Permalink
refacto handler to use new cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cottand committed Dec 18, 2024
1 parent c88d607 commit d7c0bf2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
12 changes: 5 additions & 7 deletions grimd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/cottand/leng/internal/metric"
"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"slices"
Expand Down Expand Up @@ -389,14 +390,11 @@ func TestConfigReloadForCustomRecords(t *testing.T) {

m1 = new(dns.Msg)
m1.SetQuestion(dns.Fqdn("old.com_custom"), dns.TypeA)
// no caching available, so this request requires internet!
reply, _, err = c.Exchange(m1, testDnsHost)
if err != nil {
fmt.Printf("Err was %v - expected this", err)
t.FailNow()
}
if len(reply.Answer) != 0 {
t.Fatalf("expected old.com_custom DNS to fail, but got %v", reply)
}
// no response but no error
assert.NoError(t, err)
assert.Len(t, reply.Answer, 0)

m1 = new(dns.Msg)
m1.SetQuestion(dns.Fqdn("boo.org"), dns.TypeA)
Expand Down
16 changes: 13 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,23 @@ func (h *EventLoop) responseFor(Net string, req *dns.Msg, _local net.Addr, _remo
return custom, true, false, true
}

// does not include custom DNS
defer metric.ReportDNSRespond(remote, resp, blocked, cached)
if len(req.Question) < 1 {
return nil, false, false, false
}

q := req.Question[0]
Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
logger.Infof("%s lookup %s\n", remote, Q.String())

defer func() {
if resp != nil {
resp.SetReply(req)
}
if resp != nil && remote != nil {
metric.ReportDNSRespond(remote, resp, blocked, cached)
}
}()

IPQuery := h.isIPQuery(q)

blocked = IPQuery > 0 && lengActive && h.blockCache.Exists(Q.Qname)
Expand Down Expand Up @@ -207,6 +217,7 @@ func (h *EventLoop) doRequest(Net string, w dns.ResponseWriter, req *dns.Msg) {

if !ok {
m := new(dns.Msg)
m.SetReply(req)
m.SetRcode(req, dns.RcodeServerFailure)
WriteReplyMsg(w, m)
metric.ReportDNSResponse(w, m, false)
Expand Down Expand Up @@ -325,7 +336,6 @@ func (h *EventLoop) isIPQuery(q dns.Question) int {
}
func (h *EventLoop) blockedResponseFor(req *dns.Msg, IPQuery int) *dns.Msg {
m := new(dns.Msg)
m.SetReply(req)
q := req.Question[0]

if h.config.Blocking.NXDomain {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ func (s *Server) Stop() {
func (s *Server) ReloadConfig(config *Config) {
newRecords := NewCustomDNSRecordsFromText(config.CustomDNSRecords)
s.eventLoop.customDns = NewCustomRecordsResolver(newRecords)
defer metric.CustomDNSConfigReload.Inc()
metric.CustomDNSConfigReload.Inc()
}

0 comments on commit d7c0bf2

Please sign in to comment.