forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request 2108: 6541 hosts nodata
Updates AdguardTeam#6541. Squashed commit of the following: commit e79507f Merge: 1a09de9 d328327 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Dec 18 14:29:21 2023 +0300 Merge branch 'master' into 6541-hosts-nodata commit 1a09de9 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 14 18:24:56 2023 +0300 filtering: separate files commit e00d1d2 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 14 18:00:52 2023 +0300 filtering: fix hosts nodata
- Loading branch information
1 parent
d328327
commit 4fc6bf5
Showing
5 changed files
with
288 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package filtering | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
"github.com/AdguardTeam/golibs/hostsfile" | ||
"github.com/AdguardTeam/golibs/log" | ||
"github.com/AdguardTeam/golibs/netutil" | ||
"github.com/AdguardTeam/urlfilter/rules" | ||
"github.com/miekg/dns" | ||
) | ||
|
||
// matchSysHosts tries to match the host against the operating system's hosts | ||
// database. err is always nil. | ||
func (d *DNSFilter) matchSysHosts( | ||
host string, | ||
qtype uint16, | ||
setts *Settings, | ||
) (res Result, err error) { | ||
// TODO(e.burkov): Where else is this checked? | ||
if !setts.FilteringEnabled || d.conf.EtcHosts == nil { | ||
return Result{}, nil | ||
} | ||
|
||
vals, rs, matched := hostsRewrites(qtype, host, d.conf.EtcHosts) | ||
if !matched { | ||
return Result{}, nil | ||
} | ||
|
||
return Result{ | ||
DNSRewriteResult: &DNSRewriteResult{ | ||
Response: DNSRewriteResultResponse{ | ||
qtype: vals, | ||
}, | ||
RCode: dns.RcodeSuccess, | ||
}, | ||
Rules: rs, | ||
Reason: RewrittenAutoHosts, | ||
}, nil | ||
} | ||
|
||
// hostsRewrites returns values and rules matched by qt and host within hs. | ||
func hostsRewrites( | ||
qtype uint16, | ||
host string, | ||
hs hostsfile.Storage, | ||
) (vals []rules.RRValue, rls []*ResultRule, matched bool) { | ||
var isValidProto func(netip.Addr) (ok bool) | ||
switch qtype { | ||
case dns.TypeA: | ||
isValidProto = netip.Addr.Is4 | ||
case dns.TypeAAAA: | ||
isValidProto = netip.Addr.Is6 | ||
case dns.TypePTR: | ||
// TODO(e.burkov): Add some [netip]-aware alternative to [netutil]. | ||
ip, err := netutil.IPFromReversedAddr(host) | ||
if err != nil { | ||
log.Debug("filtering: failed to parse PTR record %q: %s", host, err) | ||
|
||
return nil, nil, false | ||
} | ||
|
||
addr, _ := netip.AddrFromSlice(ip) | ||
names := hs.ByAddr(addr) | ||
|
||
for _, name := range names { | ||
vals = append(vals, name) | ||
rls = append(rls, &ResultRule{ | ||
Text: fmt.Sprintf("%s %s", addr, name), | ||
FilterListID: SysHostsListID, | ||
}) | ||
} | ||
|
||
return vals, rls, len(names) > 0 | ||
default: | ||
log.Debug("filtering: unsupported qtype %d", qtype) | ||
|
||
return nil, nil, false | ||
} | ||
|
||
addrs := hs.ByName(host) | ||
for _, addr := range addrs { | ||
if isValidProto(addr) { | ||
vals = append(vals, addr) | ||
} | ||
rls = append(rls, &ResultRule{ | ||
Text: fmt.Sprintf("%s %s", addr, host), | ||
FilterListID: SysHostsListID, | ||
}) | ||
} | ||
|
||
return vals, rls, len(addrs) > 0 | ||
} |
Oops, something went wrong.