Pull request: 5972-ip-dupl-ans

Updates #5972.

Squashed commit of the following:

commit 0e089f9ff8fd7e6d7cb53aa7c3b92435d1d41a81
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Tue Jul 11 15:33:16 2023 +0300

    dnsforward: imp code

commit 39527c078fd9ad6ea4906659e185d54e74ef6465
Merge: 03641b0b5 61ed74374
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Tue Jul 11 11:29:19 2023 +0300

    Merge remote-tracking branch 'origin/master' into 5972-ip-dupl-ans

    # Conflicts:
    #	CHANGELOG.md

commit 03641b0b511f8e48d386be76d0a4776296cf047d
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Jul 10 14:03:28 2023 +0300

    all: dupl ips in answer
This commit is contained in:
Dimitry Kolyshev 2023-07-11 15:46:01 +03:00
parent 61ed743748
commit 65b526b969
3 changed files with 32 additions and 3 deletions

View File

@ -66,6 +66,7 @@ In this release, the schema version has changed from 23 to 24.
### Fixed ### Fixed
- Two unspecified IPs when a host is blocked in two filter lists ([#5972]).
- Incorrect setting of Parental Control cache size. - Incorrect setting of Parental Control cache size.
- Excessive RAM and CPU consumption by Safe Browsing and Parental Control - Excessive RAM and CPU consumption by Safe Browsing and Parental Control
filters ([#5896]). filters ([#5896]).
@ -81,6 +82,7 @@ In this release, the schema version has changed from 23 to 24.
image, and reload it from scratch. image, and reload it from scratch.
[#5896]: https://github.com/AdguardTeam/AdGuardHome/issues/5896 [#5896]: https://github.com/AdguardTeam/AdGuardHome/issues/5896
[#5972]: https://github.com/AdguardTeam/AdGuardHome/issues/5972
<!-- <!--
NOTE: Add new changes ABOVE THIS COMMENT. NOTE: Add new changes ABOVE THIS COMMENT.

View File

@ -21,6 +21,8 @@ func TestHandleDNSRequest_filterDNSResponse(t *testing.T) {
||cname.specific^$dnstype=~CNAME ||cname.specific^$dnstype=~CNAME
||0.0.0.1^$dnstype=~A ||0.0.0.1^$dnstype=~A
||::1^$dnstype=~AAAA ||::1^$dnstype=~AAAA
0.0.0.0 duplicate.domain
0.0.0.0 duplicate.domain
` `
forwardConf := ServerConfig{ forwardConf := ServerConfig{
@ -137,6 +139,17 @@ func TestHandleDNSRequest_filterDNSResponse(t *testing.T) {
}, },
A: netutil.IPv4Zero(), A: netutil.IPv4Zero(),
}}, }},
}, {
req: createTestMessage("duplicate.domain."),
name: "duplicate_domain",
wantAns: []dns.RR{&dns.A{
Hdr: dns.RR_Header{
Name: "duplicate.domain.",
Rrtype: dns.TypeA,
Class: dns.ClassINET,
},
A: netutil.IPv4Zero(),
}},
}} }}
for _, tc := range testCases { for _, tc := range testCases {

View File

@ -26,11 +26,25 @@ func (s *Server) makeResponse(req *dns.Msg) (resp *dns.Msg) {
return resp return resp
} }
// ipsFromRules extracts non-IP addresses from the filtering result rules. // containsIP returns true if the IP is already in the list.
func containsIP(ips []net.IP, ip net.IP) bool {
for _, a := range ips {
if a.Equal(ip) {
return true
}
}
return false
}
// ipsFromRules extracts unique non-IP addresses from the filtering result
// rules.
func ipsFromRules(resRules []*filtering.ResultRule) (ips []net.IP) { func ipsFromRules(resRules []*filtering.ResultRule) (ips []net.IP) {
for _, r := range resRules { for _, r := range resRules {
if r.IP != nil { // len(resRules) and len(ips) are actually small enough for O(n^2) to do
ips = append(ips, r.IP) // not raise performance questions.
if ip := r.IP; ip != nil && !containsIP(ips, ip) {
ips = append(ips, ip)
} }
} }