rewrite: imp code
This commit is contained in:
parent
f10a45361c
commit
0a1ad86ea7
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/urlfilter"
|
"github.com/AdguardTeam/urlfilter"
|
||||||
"github.com/AdguardTeam/urlfilter/filterlist"
|
"github.com/AdguardTeam/urlfilter/filterlist"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Storage is a storage for rewrite rules.
|
// Storage is a storage for rewrite rules.
|
||||||
|
@ -58,12 +59,13 @@ type Item struct {
|
||||||
Answer string `yaml:"answer"`
|
Answer string `yaml:"answer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// equal returns true if the rw is equal to the other.
|
// equal returns true if rw is equal to other. Panics if rw or other is nil.
|
||||||
func (rw *Item) equal(other *Item) (ok bool) {
|
func (rw *Item) equal(other *Item) (ok bool) {
|
||||||
return rw.Domain == other.Domain && rw.Answer == other.Answer
|
return rw.Domain == other.Domain && rw.Answer == other.Answer
|
||||||
}
|
}
|
||||||
|
|
||||||
// toRule converts this item to a filter rule.
|
// toRule converts this item to a filter rule.
|
||||||
|
// TODO(d.kolyshev): This is very simple implementation.
|
||||||
func (rw *Item) toRule() (res string) {
|
func (rw *Item) toRule() (res string) {
|
||||||
return fmt.Sprintf("|%s^$dnsrewrite=%s", rw.Domain, rw.Answer)
|
return fmt.Sprintf("|%s^$dnsrewrite=%s", rw.Domain, rw.Answer)
|
||||||
}
|
}
|
||||||
|
@ -93,6 +95,9 @@ var _ Storage = (*DefaultStorage)(nil)
|
||||||
|
|
||||||
// Match implements the Storage interface for *DefaultStorage.
|
// Match implements the Storage interface for *DefaultStorage.
|
||||||
func (s *DefaultStorage) Match(hostname string) (res *urlfilter.DNSResult, matched bool) {
|
func (s *DefaultStorage) Match(hostname string) (res *urlfilter.DNSResult, matched bool) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
return s.engine.Match(hostname)
|
return s.engine.Match(hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +112,7 @@ func (s *DefaultStorage) Add(item *Item) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements the Storage interface for *DefaultStorage.
|
// Remove implements the Storage interface for *DefaultStorage.
|
||||||
|
// TODO(d.kolyshev): Delete only current item.
|
||||||
func (s *DefaultStorage) Remove(item *Item) (err error) {
|
func (s *DefaultStorage) Remove(item *Item) (err error) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
@ -129,10 +135,10 @@ func (s *DefaultStorage) Remove(item *Item) (err error) {
|
||||||
|
|
||||||
// List implements the Storage interface for *DefaultStorage.
|
// List implements the Storage interface for *DefaultStorage.
|
||||||
func (s *DefaultStorage) List() (items []*Item) {
|
func (s *DefaultStorage) List() (items []*Item) {
|
||||||
s.mu.Lock()
|
s.mu.RLock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
return s.rewrites
|
return slices.Clone(s.rewrites)
|
||||||
}
|
}
|
||||||
|
|
||||||
// resetRules resets the filtering rules.
|
// resetRules resets the filtering rules.
|
||||||
|
|
Loading…
Reference in New Issue