Skip to content

Commit

Permalink
all: safesearch rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Apr 16, 2024
1 parent 88add21 commit fb6e669
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions internal/dnsforward/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func (s *Server) filterDNSRequest(dctx *dnsContext) (res *filtering.Result, err
// TODO(a.garipov): Make CheckHost return a pointer.
res = &resVal
switch {
case res.IsFiltered && res.CanonName == "":
log.Debug("dnsforward: host %q is filtered, reason: %q", host, res.Reason)
pctx.Res = s.genDNSFilterMessage(pctx, res)
case res.Reason.In(
filtering.Rewritten,
filtering.RewrittenRule,
Expand All @@ -53,6 +50,9 @@ func (s *Server) filterDNSRequest(dctx *dnsContext) (res *filtering.Result, err
// original question is readded in processFilteringAfterResponse.
dctx.origQuestion = q
req.Question[0].Name = dns.Fqdn(res.CanonName)
case res.IsFiltered:
log.Debug("dnsforward: host %q is filtered, reason: %q", host, res.Reason)
pctx.Res = s.genDNSFilterMessage(pctx, res)
case res.Reason.In(filtering.Rewritten, filtering.FilteredSafeSearch):
pctx.Res = s.getCNAMEWithIPs(req, res.IPList, res.CanonName)
case res.Reason.In(filtering.RewrittenRule, filtering.RewrittenAutoHosts):
Expand Down
9 changes: 4 additions & 5 deletions internal/dnsforward/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,10 @@ func (s *Server) processFilteringAfterResponse(dctx *dnsContext) (rc resultCode)

pctx := dctx.proxyCtx
pctx.Req.Question[0], pctx.Res.Question[0] = dctx.origQuestion, dctx.origQuestion
if len(pctx.Res.Answer) > 0 {
rr := s.genAnswerCNAME(pctx.Req, res.CanonName)
answer := append([]dns.RR{rr}, pctx.Res.Answer...)
pctx.Res.Answer = answer
}

rr := s.genAnswerCNAME(pctx.Req, res.CanonName)
answer := append([]dns.RR{rr}, pctx.Res.Answer...)
pctx.Res.Answer = answer

return resultCodeSuccess
default:
Expand Down
2 changes: 2 additions & 0 deletions internal/filtering/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ type Result struct {
Reason Reason `json:",omitempty"`

// IsFiltered is true if the request is filtered.
//
// TODO(d.kolyshev): Get rid of this flag.
IsFiltered bool `json:",omitempty"`
}

Expand Down
5 changes: 3 additions & 2 deletions internal/filtering/safesearch/safesearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func TestDefault_CheckHost_yandex(t *testing.T) {
} else {
require.Len(t, res.Rules, 1)

assert.Equal(t, tc.want, res.Rules[0].IP)
assert.Equal(t, rulelist.URLFilterIDSafeSearch, res.Rules[0].FilterListID)
rule := res.Rules[0]
assert.Equal(t, tc.want, rule.IP)
assert.Equal(t, rulelist.URLFilterIDSafeSearch, rule.FilterListID)
}
}
})
Expand Down

0 comments on commit fb6e669

Please sign in to comment.