Skip to content

Commit c64a36c

Browse files
committed
Pull request 2176: AG-20945-rule-list-engine
Squashed commit of the following: commit 56756b2 Author: Ainar Garipov <[email protected]> Date: Fri Mar 15 15:40:39 2024 +0300 all: imp code, docs, tests commit 45849e6 Author: Ainar Garipov <[email protected]> Date: Thu Mar 14 20:18:07 2024 +0300 rulelist: add engine, textengine
1 parent ee01441 commit c64a36c

15 files changed

+584
-93
lines changed

internal/filtering/blocked.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
11+
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
1112
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
1213
"github.com/AdguardTeam/golibs/log"
1314
"github.com/AdguardTeam/urlfilter/rules"
@@ -28,7 +29,7 @@ func initBlockedServices() {
2829
for i, s := range blockedServices {
2930
netRules := make([]*rules.NetworkRule, 0, len(s.Rules))
3031
for _, text := range s.Rules {
31-
rule, err := rules.NewNetworkRule(text, BlockedSvcsListID)
32+
rule, err := rules.NewNetworkRule(text, rulelist.URLFilterIDBlockedService)
3233
if err != nil {
3334
log.Error("parsing blocked service %q rule %q: %s", s.ID, text, err)
3435

internal/filtering/filter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func (d *DNSFilter) EnableFilters(async bool) {
608608
func (d *DNSFilter) enableFiltersLocked(async bool) {
609609
filters := make([]Filter, 1, len(d.conf.Filters)+len(d.conf.WhitelistFilters)+1)
610610
filters[0] = Filter{
611-
ID: CustomListID,
611+
ID: rulelist.URLFilterIDCustom,
612612
Data: []byte(strings.Join(d.conf.UserRules, "\n")),
613613
}
614614

internal/filtering/filtering.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ import (
3232
"github.com/miekg/dns"
3333
)
3434

35-
// The IDs of built-in filter lists.
36-
//
37-
// Keep in sync with client/src/helpers/constants.js.
38-
// TODO(d.kolyshev): Add RewritesListID and don't forget to keep in sync.
39-
const (
40-
CustomListID = -iota
41-
SysHostsListID
42-
BlockedSvcsListID
43-
ParentalListID
44-
SafeBrowsingListID
45-
SafeSearchListID
46-
)
47-
4835
// ServiceEntry - blocked service array element
4936
type ServiceEntry struct {
5037
Name string
@@ -1139,7 +1126,7 @@ func (d *DNSFilter) checkSafeBrowsing(
11391126
res = Result{
11401127
Rules: []*ResultRule{{
11411128
Text: "adguard-malware-shavar",
1142-
FilterListID: SafeBrowsingListID,
1129+
FilterListID: rulelist.URLFilterIDSafeBrowsing,
11431130
}},
11441131
Reason: FilteredSafeBrowsing,
11451132
IsFiltered: true,
@@ -1171,7 +1158,7 @@ func (d *DNSFilter) checkParental(
11711158
res = Result{
11721159
Rules: []*ResultRule{{
11731160
Text: "parental CATEGORY_BLACKLISTED",
1174-
FilterListID: ParentalListID,
1161+
FilterListID: rulelist.URLFilterIDParentalControl,
11751162
}},
11761163
Reason: FilteredParental,
11771164
IsFiltered: true,

internal/filtering/hosts.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/netip"
66

7+
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
78
"github.com/AdguardTeam/golibs/hostsfile"
89
"github.com/AdguardTeam/golibs/log"
910
"github.com/AdguardTeam/golibs/netutil"
@@ -66,7 +67,7 @@ func hostsRewrites(
6667
vals = append(vals, name)
6768
rls = append(rls, &ResultRule{
6869
Text: fmt.Sprintf("%s %s", addr, name),
69-
FilterListID: SysHostsListID,
70+
FilterListID: rulelist.URLFilterIDEtcHosts,
7071
})
7172
}
7273

@@ -84,7 +85,7 @@ func hostsRewrites(
8485
}
8586
rls = append(rls, &ResultRule{
8687
Text: fmt.Sprintf("%s %s", addr, host),
87-
FilterListID: SysHostsListID,
88+
FilterListID: rulelist.URLFilterIDEtcHosts,
8889
})
8990
}
9091

internal/filtering/hosts_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
1010
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
11+
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
1112
"github.com/AdguardTeam/golibs/testutil"
1213
"github.com/AdguardTeam/urlfilter/rules"
1314
"github.com/miekg/dns"
@@ -71,7 +72,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
7172
dtyp: dns.TypeA,
7273
wantRules: []*ResultRule{{
7374
Text: "1.2.3.4 v4.host.example",
74-
FilterListID: SysHostsListID,
75+
FilterListID: rulelist.URLFilterIDEtcHosts,
7576
}},
7677
wantResps: []rules.RRValue{addrv4},
7778
}, {
@@ -80,7 +81,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
8081
dtyp: dns.TypeAAAA,
8182
wantRules: []*ResultRule{{
8283
Text: "::1 v6.host.example",
83-
FilterListID: SysHostsListID,
84+
FilterListID: rulelist.URLFilterIDEtcHosts,
8485
}},
8586
wantResps: []rules.RRValue{addrv6},
8687
}, {
@@ -89,7 +90,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
8990
dtyp: dns.TypeAAAA,
9091
wantRules: []*ResultRule{{
9192
Text: "::ffff:1.2.3.4 mapped.host.example",
92-
FilterListID: SysHostsListID,
93+
FilterListID: rulelist.URLFilterIDEtcHosts,
9394
}},
9495
wantResps: []rules.RRValue{addrMapped},
9596
}, {
@@ -98,7 +99,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
9899
dtyp: dns.TypePTR,
99100
wantRules: []*ResultRule{{
100101
Text: "1.2.3.4 v4.host.example",
101-
FilterListID: SysHostsListID,
102+
FilterListID: rulelist.URLFilterIDEtcHosts,
102103
}},
103104
wantResps: []rules.RRValue{"v4.host.example"},
104105
}, {
@@ -107,7 +108,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
107108
dtyp: dns.TypePTR,
108109
wantRules: []*ResultRule{{
109110
Text: "::ffff:1.2.3.4 mapped.host.example",
110-
FilterListID: SysHostsListID,
111+
FilterListID: rulelist.URLFilterIDEtcHosts,
111112
}},
112113
wantResps: []rules.RRValue{"mapped.host.example"},
113114
}, {
@@ -134,7 +135,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
134135
dtyp: dns.TypeAAAA,
135136
wantRules: []*ResultRule{{
136137
Text: fmt.Sprintf("%s v4.host.example", addrv4),
137-
FilterListID: SysHostsListID,
138+
FilterListID: rulelist.URLFilterIDEtcHosts,
138139
}},
139140
wantResps: nil,
140141
}, {
@@ -143,7 +144,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
143144
dtyp: dns.TypeA,
144145
wantRules: []*ResultRule{{
145146
Text: fmt.Sprintf("%s v6.host.example", addrv6),
146-
FilterListID: SysHostsListID,
147+
FilterListID: rulelist.URLFilterIDEtcHosts,
147148
}},
148149
wantResps: nil,
149150
}, {
@@ -164,7 +165,7 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
164165
dtyp: dns.TypeA,
165166
wantRules: []*ResultRule{{
166167
Text: "4.3.2.1 v4.host.with-dup",
167-
FilterListID: SysHostsListID,
168+
FilterListID: rulelist.URLFilterIDEtcHosts,
168169
}},
169170
wantResps: []rules.RRValue{addrv4Dup},
170171
}}

0 commit comments

Comments
 (0)