Documentation ¶
Overview ¶
Package filterlist provides methods to work with filter lists.
Index ¶
- Variables
- type FileRuleList
- type RuleList
- type RuleScanner
- type RuleStorage
- func (s *RuleStorage) Close() (err error)
- func (s *RuleStorage) GetCacheSize() (sz int)
- func (s *RuleStorage) NewRuleStorageScanner() (sc *RuleStorageScanner)
- func (s *RuleStorage) RetrieveHostRule(idx int64) (hr *rules.HostRule)
- func (s *RuleStorage) RetrieveNetworkRule(idx int64) (nr *rules.NetworkRule)
- func (s *RuleStorage) RetrieveRule(storageIdx int64) (r rules.Rule, err error)
- type RuleStorageScanner
- type StringRuleList
Constants ¶
This section is empty.
Variables ¶
var ErrRuleRetrieval errors.Error = "cannot retrieve the rule"
ErrRuleRetrieval signals that the rule cannot be retrieved by RuleList by the the specified index
Functions ¶
This section is empty.
Types ¶
type FileRuleList ¶
type FileRuleList struct { // File with rules. File *os.File // Mutex protects all the fields. sync.Mutex // ID is the rule list ID. ID int // IgnoreCosmetic tells whether to ignore cosmetic rules or not. IgnoreCosmetic bool // contains filtered or unexported fields }
FileRuleList represents a file-based rule list
func NewFileRuleList ¶
func NewFileRuleList(id int, path string, ignoreCosmetic bool) (*FileRuleList, error)
NewFileRuleList initializes a new file-based rule list
func (*FileRuleList) GetID ¶
func (l *FileRuleList) GetID() int
GetID returns the rule list identifier
func (*FileRuleList) NewScanner ¶
func (l *FileRuleList) NewScanner() *RuleScanner
NewScanner creates a new rules scanner that reads the list contents
func (*FileRuleList) RetrieveRule ¶
func (l *FileRuleList) RetrieveRule(ruleIdx int) (rules.Rule, error)
RetrieveRule finds and deserializes rule by its index. If there's no rule by that index or rule is invalid, it will return an error.
type RuleList ¶
type RuleList interface { GetID() int // GetID returns the rule list identifier NewScanner() *RuleScanner // Creates a new scanner that reads the list contents RetrieveRule(ruleIdx int) (rules.Rule, error) // Retrieves a rule by its index io.Closer // Closes the rules list }
RuleList represents a set of filtering rules
type RuleScanner ¶
type RuleScanner struct {
// contains filtered or unexported fields
}
RuleScanner implements an interface for reading filtering rules.
func NewRuleScanner ¶
func NewRuleScanner(r io.Reader, listID int, ignoreCosmetic bool) *RuleScanner
NewRuleScanner returns a new RuleScanner to read from r. r -- source of the filtering rules listID -- filter list ID IgnoreCosmetic -- if true, cosmetic rules will be ignored
func (*RuleScanner) Rule ¶
func (s *RuleScanner) Rule() (rules.Rule, int)
Rule returns the most recent rule generated by a call to Scan, and the index of this rule's text.
func (*RuleScanner) Scan ¶
func (s *RuleScanner) Scan() bool
Scan advances the RuleScanner to the next rule, which will then be available through the Rule method. It returns false when the scan stops, either by reaching the end of the input or an error.
type RuleStorage ¶
type RuleStorage struct {
// contains filtered or unexported fields
}
RuleStorage is an abstraction that combines several rule lists It can be scanned using a RuleStorageScanner, and also it allows retrieving rules by its index
The idea is to keep rules in a serialized format (even original format in the case of FileRuleList) and create them in a lazy manner only when we really need them. When the filtering engine is being initialized, we need to scan the rule lists once in order to fill up the lookup tables. We use rule indexes as a unique rule identifier instead of the rule itself. The rule is created (see RuleStorage.RetrieveRule) only when there's a chance that it's needed.
Rule index is an int64 value that actually consists of two int32 values: one is the rule list identifier, and the second is the index of the rule inside of that list.
func NewRuleStorage ¶
func NewRuleStorage(lists []RuleList) (s *RuleStorage, err error)
NewRuleStorage creates a new instance of the RuleStorage and validates the list of rules specified.
func (*RuleStorage) Close ¶
func (s *RuleStorage) Close() (err error)
Close closes the storage instance.
func (*RuleStorage) GetCacheSize ¶
func (s *RuleStorage) GetCacheSize() (sz int)
GetCacheSize returns the size of the in-memory rules cache.
func (*RuleStorage) NewRuleStorageScanner ¶
func (s *RuleStorage) NewRuleStorageScanner() (sc *RuleStorageScanner)
NewRuleStorageScanner creates a new instance of RuleStorageScanner. It can be used to read and parse all the storage contents.
func (*RuleStorage) RetrieveHostRule ¶
func (s *RuleStorage) RetrieveHostRule(idx int64) (hr *rules.HostRule)
RetrieveHostRule is a helper method that retrieves a host rule from the storage. It returns a pointer to the rule or nil in any other case (not found or error).
func (*RuleStorage) RetrieveNetworkRule ¶
func (s *RuleStorage) RetrieveNetworkRule(idx int64) (nr *rules.NetworkRule)
RetrieveNetworkRule is a helper method that retrieves a network rule from the storage. It returns a pointer to the rule or nil in any other case (not found or error).
func (*RuleStorage) RetrieveRule ¶
func (s *RuleStorage) RetrieveRule(storageIdx int64) (r rules.Rule, err error)
RetrieveRule looks for the filtering rule in this storage. storageIdx is the lookup index that you can get from the rule storage scanner.
type RuleStorageScanner ¶
type RuleStorageScanner struct { // Scanners is the list of list scanners backing this combined scanner, Scanners []*RuleScanner // contains filtered or unexported fields }
RuleStorageScanner scans multiple RuleScanner instances The rule index is built from the rule index in the list + the list ID First 4 bytes is the rule index in the list Second 4 bytes is the list ID
func (*RuleStorageScanner) Rule ¶
func (s *RuleStorageScanner) Rule() (rules.Rule, int64)
Rule returns the most recent rule generated by a call to Scan, and the index of this rule. See ruleListIdxToStorageIdx for more information on what this index is.
func (*RuleStorageScanner) Scan ¶
func (s *RuleStorageScanner) Scan() bool
Scan advances the RuleStorageScanner to the next rule, which will then be available through the Rule method. It returns false when the scan stops, either by reaching the end of the input or an error.
type StringRuleList ¶
type StringRuleList struct { // RulesText is a string with filtering rules (one per line). RulesText string // ID is the rule list ID. ID int // IgnoreCosmetic tells whether to ignore cosmetic rules or not. IgnoreCosmetic bool }
StringRuleList represents a string-based rule list
func (*StringRuleList) Close ¶
func (l *StringRuleList) Close() error
Close does nothing as there's nothing to close in the StringRuleList
func (*StringRuleList) GetID ¶
func (l *StringRuleList) GetID() int
GetID returns the rule list identifier
func (*StringRuleList) NewScanner ¶
func (l *StringRuleList) NewScanner() *RuleScanner
NewScanner creates a new rules scanner that reads the list contents
func (*StringRuleList) RetrieveRule ¶
func (l *StringRuleList) RetrieveRule(ruleIdx int) (rules.Rule, error)
RetrieveRule finds and deserializes rule by its index. If there's no rule by that index or rule is invalid, it will return an error.