Skip to content

Commit

Permalink
all: merge hosts container more
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 14, 2021
1 parent 884a985 commit 187251c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 67 deletions.
17 changes: 13 additions & 4 deletions internal/aghnet/hostscontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestHostsContainer_MatchRequest(t *testing.T) {
var (
ip4 = net.IP{127, 0, 0, 1}
ip6 = net.IP{
0, 0, 0, 0,
128, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1,
Expand All @@ -236,9 +236,9 @@ func TestHostsContainer_MatchRequest(t *testing.T) {

gsfs := fstest.MapFS{
filename: &fstest.MapFile{Data: []byte(
strings.Join([]string{ip4.String(), hostname4, hostname4a}, sp) + nl +
strings.Join([]string{ip6.String(), hostname6}, sp) + nl +
strings.Join([]string{"256.256.256.256", "fakebroadcast"}, sp) + nl,
ip4.String() + " " + hostname4 + " " + hostname4a + nl +
ip6.String() + " " + hostname6 + nl +
`256.256.256.256 fakebroadcast` + nl,
)},
}

Expand All @@ -265,6 +265,15 @@ func TestHostsContainer_MatchRequest(t *testing.T) {
Hostname: hostname4,
DNSType: dns.TypeA,
},
}, {
name: "a_for_aaaa",
want: []interface{}{
ip4.To16(),
},
req: urlfilter.DNSRequest{
Hostname: hostname4,
DNSType: dns.TypeAAAA,
},
}, {
name: "aaaa",
want: []interface{}{ip6},
Expand Down
22 changes: 2 additions & 20 deletions internal/dnsforward/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,7 @@ func (s *Server) filterDNSRequest(ctx *dnsContext) (*filtering.Result, error) {
// original question is readded in processFilteringAfterResponse.
ctx.origQuestion = q
req.Question[0].Name = dns.Fqdn(res.CanonName)
case res.Reason == filtering.RewrittenAutoHosts && len(res.ReverseHosts) != 0:
resp := s.makeResponse(req)
hdr := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypePTR,
Ttl: s.conf.BlockedResponseTTL,
Class: dns.ClassINET,
}
for _, h := range res.ReverseHosts {
ptr := &dns.PTR{
Hdr: hdr,
Ptr: h,
}

resp.Answer = append(resp.Answer, ptr)
}

d.Res = resp
case res.Reason.In(filtering.Rewritten, filtering.RewrittenAutoHosts):
case res.Reason == filtering.Rewritten:
resp := s.makeResponse(req)

name := host
Expand All @@ -123,7 +105,7 @@ func (s *Server) filterDNSRequest(ctx *dnsContext) (*filtering.Result, error) {
}

d.Res = resp
case res.Reason == filtering.RewrittenRule:
case res.Reason.In(filtering.RewrittenRule, filtering.RewrittenAutoHosts):
if err = s.filterDNSRewrite(req, res, d); err != nil {
return nil, err
}
Expand Down
13 changes: 4 additions & 9 deletions internal/filtering/dnsrewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ type DNSRewriteResultResponse map[rules.RRType][]rules.RRValue

// processDNSRewrites processes DNS rewrite rules in dnsr. It returns an empty
// result if dnsr is empty. Otherwise, the result will have either CanonName or
// DNSRewriteResult set.
// DNSRewriteResult set. dnsr is expected to be non-empty.
func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
if len(dnsr) == 0 {
return Result{}
}

var rules []*ResultRule
dnsrr := &DNSRewriteResult{
Response: DNSRewriteResultResponse{},
Expand All @@ -31,8 +27,7 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
for _, nr := range dnsr {
dr := nr.DNSRewrite
if dr.NewCNAME != "" {
// NewCNAME rules have a higher priority than
// the other rules.
// NewCNAME rules have a higher priority than the other rules.
rules = []*ResultRule{{
FilterListID: int64(nr.GetFilterListID()),
Text: nr.RuleText,
Expand All @@ -54,8 +49,8 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
Text: nr.RuleText,
})
default:
// RcodeRefused and other such codes have higher
// priority. Return immediately.
// RcodeRefused and other such codes have higher priority. Return
// immediately.
rules = []*ResultRule{{
FilterListID: int64(nr.GetFilterListID()),
Text: nr.RuleText,
Expand Down
45 changes: 11 additions & 34 deletions internal/filtering/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,53 +450,30 @@ func (d *DNSFilter) CheckHost(
}

// matchSysHosts tries to match the host against the operating system's hosts
// database.
// database. err is always nil.
func (d *DNSFilter) matchSysHosts(
host string,
qtype uint16,
setts *Settings,
) (res Result, err error) {
if !setts.FilteringEnabled || d.EtcHosts == nil {
return Result{}, nil
}

dnsres, _ := d.EtcHosts.MatchRequest(urlfilter.DNSRequest{
// Go on.
} else if dnsres, _ := d.EtcHosts.MatchRequest(urlfilter.DNSRequest{
Hostname: host,
SortedClientTags: setts.ClientTags,
// TODO(e.burkov): Wait for urlfilter update to pass net.IP.
// TODO(e.burkov): Wait for urlfilter update to pass net.IP.
ClientIP: setts.ClientIP.String(),
ClientName: setts.ClientName,
DNSType: qtype,
})
if dnsres == nil {
return Result{}, nil
}

dnsr := dnsres.DNSRewrites()
if len(dnsr) == 0 {
return Result{}, nil
}

var ips []net.IP
var revHosts []string
for _, nr := range dnsr {
if nr.DNSRewrite == nil {
continue
}

switch val := nr.DNSRewrite.Value.(type) {
case net.IP:
ips = append(ips, val)
case string:
revHosts = append(revHosts, val)
}
}); dnsres == nil {
// Go on.
} else if dnsr := dnsres.DNSRewrites(); len(dnsr) > 0 {
// Check DNS rewrites first, because the API there is a bit awkward.
res = d.processDNSRewrites(dnsr)
res.Reason = RewrittenAutoHosts
}

return Result{
Reason: RewrittenAutoHosts,
IPList: ips,
ReverseHosts: revHosts,
}, nil
return res, nil
}

// Process rewrites table
Expand Down

0 comments on commit 187251c

Please sign in to comment.