-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
638 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package safesearch | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/filtering" | ||
"github.com/AdguardTeam/golibs/log" | ||
"github.com/AdguardTeam/urlfilter" | ||
"github.com/AdguardTeam/urlfilter/filterlist" | ||
"github.com/AdguardTeam/urlfilter/rules" | ||
) | ||
|
||
// Matcher interface. | ||
type Matcher interface { | ||
// MatchRequest returns matching safesearch rewrite rules for request. | ||
MatchRequest(dReq *urlfilter.DNSRequest) (rules []*rules.NetworkRule) | ||
} | ||
|
||
// DefaultMatcher is the default safesearch matcher. | ||
type DefaultMatcher struct { | ||
// engine is the DNS filtering engine. | ||
engine *urlfilter.DNSEngine | ||
} | ||
|
||
// NewDefaultMatcher returns new safesearch matcher. listID is used as an | ||
// identifier of the underlying rules list. | ||
func NewDefaultMatcher(listID int, settings filtering.SafeSearchConf) (m *DefaultMatcher, err error) { | ||
m = &DefaultMatcher{} | ||
|
||
err = m.resetRules(listID, settings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
// type check | ||
var _ Matcher = (*DefaultMatcher)(nil) | ||
|
||
// MatchRequest implements the [Matcher] interface for *DefaultMatcher. | ||
func (m *DefaultMatcher) MatchRequest(dReq *urlfilter.DNSRequest) (rules []*rules.NetworkRule) { | ||
res, _ := m.engine.MatchRequest(dReq) | ||
|
||
return res.DNSRewrites() | ||
} | ||
|
||
// resetRules resets the filtering rules. | ||
func (m *DefaultMatcher) resetRules(listID int, settings filtering.SafeSearchConf) (err error) { | ||
var sb strings.Builder | ||
for service, serviceRules := range safeSearchRules { | ||
if isServiceProtected(settings, service) { | ||
sb.WriteString(serviceRules) | ||
} | ||
} | ||
|
||
strList := &filterlist.StringRuleList{ | ||
ID: listID, | ||
RulesText: sb.String(), | ||
IgnoreCosmetic: true, | ||
} | ||
|
||
rs, err := filterlist.NewRuleStorage([]filterlist.RuleList{strList}) | ||
if err != nil { | ||
return fmt.Errorf("creating rule storage: %w", err) | ||
} | ||
|
||
m.engine = urlfilter.NewDNSEngine(rs) | ||
|
||
log.Info("safesearch: filter %d: reset %d rules", listID, m.engine.RulesCount) | ||
|
||
return nil | ||
} |
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,75 @@ | ||
package safesearch | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/filtering" | ||
"github.com/AdguardTeam/urlfilter" | ||
"github.com/miekg/dns" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var defaultSafeSearchConf = filtering.SafeSearchConf{ | ||
Enabled: true, | ||
Bing: true, | ||
DuckDuckGo: true, | ||
Google: true, | ||
Pixabay: true, | ||
Yandex: true, | ||
YouTube: true, | ||
} | ||
|
||
func TestNewDefaultMatcher(t *testing.T) { | ||
m, err := NewDefaultMatcher(-1, defaultSafeSearchConf) | ||
require.NoError(t, err) | ||
require.NotNil(t, m) | ||
} | ||
|
||
func TestDefaultMatcher_MatchRequest(t *testing.T) { | ||
m, err := NewDefaultMatcher(-1, defaultSafeSearchConf) | ||
require.NoError(t, err) | ||
|
||
testCases := []struct { | ||
name string | ||
host string | ||
want string | ||
dtyp uint16 | ||
}{{ | ||
name: "not_filtered", | ||
host: "test-not-filtered.com", | ||
want: "", | ||
dtyp: dns.TypeA, | ||
}, { | ||
name: "yandex", | ||
host: "yandex.by", | ||
want: "|yandex.by^$dnsrewrite=NOERROR;A;213.180.193.56", | ||
dtyp: dns.TypeA, | ||
}, { | ||
name: "yandex_ru", | ||
host: "yandex.ru", | ||
want: "|yandex.ru^$dnsrewrite=NOERROR;A;213.180.193.56", | ||
dtyp: dns.TypeA, | ||
}, { | ||
name: "google", | ||
host: "www.google.com", | ||
want: "|www.google.com^$dnsrewrite=NOERROR;CNAME;forcesafesearch.google.com", | ||
dtyp: dns.TypeA, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
rws := m.MatchRequest(&urlfilter.DNSRequest{ | ||
Hostname: tc.host, | ||
DNSType: tc.dtyp, | ||
}) | ||
|
||
if tc.want != "" { | ||
require.NotEmpty(t, rws) | ||
assert.Equal(t, tc.want, rws[0].RuleText) | ||
} else { | ||
assert.Empty(t, rws) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.