domainset

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2024 License: AGPL-3.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const MaxLinearDomains = 16

MaxLinearDomains is the maximum number of domain rules under which a linear matcher can outperform a map matcher.

View Source
const MaxLinearSuffixes = 4

MaxLinearSuffixes is the maximum number of suffix rules under which a linear matcher can outperform a trie matcher.

Variables

This section is empty.

Functions

func ParseCapacityHint added in v1.3.0

func ParseCapacityHint(line string) (dskr [4]int, found bool, err error)

ParseCapacityHint parses the capacity hint from the line.

Types

type Builder

type Builder [4]MatcherBuilder

Builder stores the content of a domain set and provides methods for writing in different formats.

func BuilderFromGob

func BuilderFromGob(r io.Reader) (Builder, error)

BuilderFromGob reads a gob-encoded builder from the reader.

func BuilderFromGobString added in v1.11.1

func BuilderFromGobString(s string) (Builder, error)

BuilderFromGobString reads a gob-encoded builder from the string.

func BuilderFromText

func BuilderFromText(text string) (Builder, error)

BuilderFromText parses the text for domain set rules, inserts them into appropriate matcher builders, and returns the resulting domain set builder.

The rule strings are not cloned. They reference the same memory as the input text.

func (Builder) DomainMatcherBuilder added in v1.11.1

func (dsb Builder) DomainMatcherBuilder() MatcherBuilder

DomainMatcherBuilder returns the domain matcher builder.

func (Builder) DomainSet

func (dsb Builder) DomainSet() (DomainSet, error)

DomainSet builds the matchers and returns them as a DomainSet.

func (Builder) KeywordMatcherBuilder added in v1.11.1

func (dsb Builder) KeywordMatcherBuilder() MatcherBuilder

KeywordMatcherBuilder returns the keyword matcher builder.

func (Builder) RegexpMatcherBuilder added in v1.11.1

func (dsb Builder) RegexpMatcherBuilder() MatcherBuilder

RegexpMatcherBuilder returns the regexp matcher builder.

func (Builder) SuffixMatcherBuilder added in v1.11.1

func (dsb Builder) SuffixMatcherBuilder() MatcherBuilder

SuffixMatcherBuilder returns the suffix matcher builder.

func (Builder) WriteGob

func (dsb Builder) WriteGob(w io.Writer) error

WriteGob writes the builder to the writer in gob format.

func (Builder) WriteText

func (dsb Builder) WriteText(w io.Writer) error

WriteText writes the builder to the writer in text format.

type BuilderGob added in v1.3.0

type BuilderGob struct {
	Domains  DomainMapMatcher
	Suffixes DomainSuffixTrie
	Keywords KeywordLinearMatcher
	Regexps  RegexpMatcherBuilder
}

BuilderGob is a gob-encoded representation of a Builder.

func BuilderGobFromBuilder added in v1.3.0

func BuilderGobFromBuilder(dsb Builder) (bg BuilderGob)

BuilderGobFromBuilder converts a Builder to its gob representation.

func BuilderGobFromReader added in v1.3.0

func BuilderGobFromReader(r io.Reader) (bg BuilderGob, err error)

BuilderGobFromReader reads a gob representation from the reader.

func (BuilderGob) Builder added in v1.3.0

func (bg BuilderGob) Builder() Builder

Builder returns a Builder from the gob representation.

func (BuilderGob) WriteGob added in v1.3.0

func (bg BuilderGob) WriteGob(w io.Writer) error

WriteGob writes the gob representation to the writer.

type Config

type Config struct {
	// Name is the name of the domain set.
	Name string `json:"name"`

	// Type is the type of the domain set.
	//
	//	- "text": text format (default)
	//	- "gob": gob format
	Type string `json:"type"`

	// Path is the path to the domain set file.
	Path string `json:"path"`
}

Config is the configuration for a DomainSet.

func (Config) DomainSet

func (dsc Config) DomainSet() (DomainSet, error)

DomainSet creates a DomainSet from the configuration.

type DomainBinarySearchMatcher added in v1.7.0

type DomainBinarySearchMatcher struct {
	// contains filtered or unexported fields
}

DomainBinarySearchMatcher matches domain rules using binary search.

func DomainBinarySearchMatcherFromSeq added in v1.11.1

func DomainBinarySearchMatcherFromSeq(domainCount int, domainSeq iter.Seq[string]) DomainBinarySearchMatcher

DomainBinarySearchMatcherFromSeq creates a DomainBinarySearchMatcher from a sequence of domain rules.

func DomainBinarySearchMatcherFromSlice added in v1.7.0

func DomainBinarySearchMatcherFromSlice(domains []string) DomainBinarySearchMatcher

DomainBinarySearchMatcherFromSlice creates a DomainBinarySearchMatcher from a slice of domain rules.

func (*DomainBinarySearchMatcher) AppendTo added in v1.7.0

func (dbsm *DomainBinarySearchMatcher) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements [MatcherBuilder.AppendTo].

func (*DomainBinarySearchMatcher) Insert added in v1.7.0

func (dbsm *DomainBinarySearchMatcher) Insert(rule string)

Insert implements [MatcherBuilder.Insert].

func (DomainBinarySearchMatcher) Match added in v1.7.0

func (dbsm DomainBinarySearchMatcher) Match(domain string) bool

Match implements [Matcher.Match].

func (DomainBinarySearchMatcher) MatcherCount added in v1.7.0

func (dbsm DomainBinarySearchMatcher) MatcherCount() int

MatcherCount implements [MatcherBuilder.MatcherCount].

func (DomainBinarySearchMatcher) Rules added in v1.7.0

func (dbsm DomainBinarySearchMatcher) Rules() (int, iter.Seq[string])

Rules implements [MatcherBuilder.Rules].

type DomainLinearMatcher added in v1.3.0

type DomainLinearMatcher []string

DomainLinearMatcher matches domain rules using linear search. It is faster than DomainMapMatcher when the number of rules is no greater than MaxLinearDomains.

func DomainLinearMatcherFromSeq added in v1.11.1

func DomainLinearMatcherFromSeq(domainCount int, domainSeq iter.Seq[string]) DomainLinearMatcher

DomainLinearMatcherFromSeq creates a DomainLinearMatcher from a sequence of domain rules.

func (*DomainLinearMatcher) AppendTo added in v1.3.0

func (dlmp *DomainLinearMatcher) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements [MatcherBuilder.AppendTo].

func (*DomainLinearMatcher) Insert added in v1.3.0

func (dlmp *DomainLinearMatcher) Insert(rule string)

Insert implements [MatcherBuilder.Insert].

func (DomainLinearMatcher) Match added in v1.3.0

func (dlm DomainLinearMatcher) Match(domain string) bool

Match implements [Matcher.Match].

func (DomainLinearMatcher) MatcherCount added in v1.3.0

func (dlm DomainLinearMatcher) MatcherCount() int

MatcherCount implements [MatcherBuilder.MatcherCount].

func (DomainLinearMatcher) Rules added in v1.3.0

func (dlm DomainLinearMatcher) Rules() (int, iter.Seq[string])

Rules implements [MatcherBuilder.Rules].

type DomainMapMatcher added in v1.3.0

type DomainMapMatcher map[string]struct{}

DomainMapMatcher matches domain rules using a map. It is faster than DomainLinearMatcher when the number of rules is greater than MaxLinearDomains.

func DomainMapMatcherFromSeq added in v1.11.1

func DomainMapMatcherFromSeq(domainCount int, domainSeq iter.Seq[string]) DomainMapMatcher

DomainMapMatcherFromSeq creates a DomainMapMatcher from a sequence of domain rules.

func DomainMapMatcherFromSlice added in v1.3.0

func DomainMapMatcherFromSlice(domains []string) DomainMapMatcher

DomainMapMatcherFromSlice creates a DomainMapMatcher from a slice of domain rules.

func (*DomainMapMatcher) AppendTo added in v1.3.0

func (dmmp *DomainMapMatcher) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements [MatcherBuilder.AppendTo].

func (DomainMapMatcher) Insert added in v1.3.0

func (dmm DomainMapMatcher) Insert(rule string)

Insert implements [MatcherBuilder.Insert].

func (DomainMapMatcher) Match added in v1.3.0

func (dmm DomainMapMatcher) Match(domain string) bool

Match implements [Matcher.Match].

func (DomainMapMatcher) MatcherCount added in v1.3.0

func (dmm DomainMapMatcher) MatcherCount() int

MatcherCount implements [MatcherBuilder.MatcherCount].

func (DomainMapMatcher) Rules added in v1.3.0

func (dmm DomainMapMatcher) Rules() (int, iter.Seq[string])

Rules implements [MatcherBuilder.Rules].

type DomainSet

type DomainSet []Matcher

DomainSet is a set of domain matchers built from matching rules.

func (DomainSet) Match

func (ds DomainSet) Match(domain string) bool

Match returns whether the domain set contains the domain.

type DomainSuffixTrie

type DomainSuffixTrie struct {
	// Children maps the next domain part to its child node.
	//
	// If Children is nil, the node is a leaf node.
	Children map[string]DomainSuffixTrie
}

DomainSuffixTrie is a trie of domain parts segmented by '.'.

func DomainSuffixTrieFromSeq added in v1.11.1

func DomainSuffixTrieFromSeq(_ int, suffixSeq iter.Seq[string]) DomainSuffixTrie

DomainSuffixTrieFromSeq creates a DomainSuffixTrie from a sequence of suffix rules.

func DomainSuffixTrieFromSlice added in v1.3.0

func DomainSuffixTrieFromSlice(suffixes []string) DomainSuffixTrie

DomainSuffixTrieFromSlice creates a DomainSuffixTrie from a slice of suffix rules.

func NewDomainSuffixTrie added in v1.3.0

func NewDomainSuffixTrie() DomainSuffixTrie

NewDomainSuffixTrie returns a new DomainSuffixTrie.

func (*DomainSuffixTrie) AppendTo added in v1.3.0

func (dst *DomainSuffixTrie) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements [MatcherBuilder.AppendTo].

func (DomainSuffixTrie) Insert

func (dst DomainSuffixTrie) Insert(domain string)

Insert inserts a domain suffix to the trie. Insertion purges the leaf node's children. If say, we insert "www.google.com" and then "google.com", The children of node "google" will be purged.

Insert implements [MatcherBuilder.Insert].

func (DomainSuffixTrie) KeyCount added in v1.11.1

func (dst DomainSuffixTrie) KeyCount() int

KeyCount returns the number of keys in the trie.

func (DomainSuffixTrie) KeySlice added in v1.11.1

func (dst DomainSuffixTrie) KeySlice() (keys []string)

KeySlice returns the keys in the trie as a slice.

func (DomainSuffixTrie) Keys

func (dst DomainSuffixTrie) Keys() iter.Seq[string]

Keys returns an iterator over the keys in the trie.

func (DomainSuffixTrie) Match

func (dst DomainSuffixTrie) Match(domain string) bool

Match returns true if the domain matches any suffix in the trie.

Match implements [Matcher.Match].

func (DomainSuffixTrie) MatcherCount added in v1.3.0

func (dst DomainSuffixTrie) MatcherCount() int

MatcherCount implements [MatcherBuilder.MatcherCount].

func (DomainSuffixTrie) Rules added in v1.3.0

func (dst DomainSuffixTrie) Rules() (int, iter.Seq[string])

Rules implements [MatcherBuilder.Rules].

type KeywordLinearMatcher added in v1.3.0

type KeywordLinearMatcher []string

KeywordLinearMatcher matches keyword rules by iterating over the keywords.

func KeywordLinearMatcherFromSeq added in v1.11.1

func KeywordLinearMatcherFromSeq(keywordCount int, keywordSeq iter.Seq[string]) KeywordLinearMatcher

KeywordLinearMatcherFromSeq creates a KeywordLinearMatcher from a sequence of keyword rules.

func (KeywordLinearMatcher) AppendTo added in v1.3.0

func (klm KeywordLinearMatcher) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements the MatcherBuilder AppendTo method.

func (*KeywordLinearMatcher) Insert added in v1.3.0

func (klmp *KeywordLinearMatcher) Insert(rule string)

Insert implements the MatcherBuilder Insert method.

func (KeywordLinearMatcher) Match added in v1.3.0

func (klm KeywordLinearMatcher) Match(domain string) bool

Match implements the Matcher Match method.

func (KeywordLinearMatcher) MatcherCount added in v1.3.0

func (klm KeywordLinearMatcher) MatcherCount() int

MatcherCount implements the MatcherBuilder MatcherCount method.

func (KeywordLinearMatcher) Rules added in v1.3.0

func (klm KeywordLinearMatcher) Rules() (int, iter.Seq[string])

Rules implements the MatcherBuilder Rules method.

type Matcher added in v1.3.0

type Matcher interface {
	// Match returns whether the domain is matched by the matcher.
	Match(domain string) bool
}

Matcher provides functionality for matching domain names against a set of rules.

type MatcherBuilder added in v1.3.0

type MatcherBuilder interface {
	// Insert inserts the rule string to the matcher.
	//
	// The rule string must not include the rule identifier.
	// For example, if the rule line is "suffix:google.com",
	// the rule string should be "google.com".
	Insert(rule string)

	// Rules returns the number of rules and an iterator over them.
	Rules() (int, iter.Seq[string])

	// MatcherCount returns the number of matchers that would be appended
	// to the matcher slice by calling [AppendTo].
	MatcherCount() int

	// AppendTo builds the matcher, appends the matcher to the matcher slice,
	// and returns the updated slice or an error.
	AppendTo(matchers []Matcher) ([]Matcher, error)
}

MatcherBuilder provides methods for building a Matcher.

func NewDomainBinarySearchMatcher added in v1.7.0

func NewDomainBinarySearchMatcher(capacity int) MatcherBuilder

NewDomainBinarySearchMatcher creates a DomainBinarySearchMatcher with the specified initial capacity.

func NewDomainLinearMatcher added in v1.3.0

func NewDomainLinearMatcher(capacity int) MatcherBuilder

NewDomainLinearMatcher creates a DomainLinearMatcher with the specified initial capacity.

func NewDomainMapMatcher added in v1.3.0

func NewDomainMapMatcher(capacity int) MatcherBuilder

NewDomainMapMatcher creates a DomainMapMatcher with the specified initial capacity.

func NewDomainSuffixTrieMatcherBuilder added in v1.11.1

func NewDomainSuffixTrieMatcherBuilder(_ int) MatcherBuilder

NewDomainSuffixTrieMatcherBuilder returns a new *DomainSuffixTrie as a MatcherBuilder.

func NewKeywordLinearMatcher added in v1.3.0

func NewKeywordLinearMatcher(capacity int) MatcherBuilder

NewKeywordLinearMatcher creates a KeywordLinearMatcher with the specified initial capacity.

func NewRegexpMatcherBuilder added in v1.3.0

func NewRegexpMatcherBuilder(capacity int) MatcherBuilder

NewRegexpMatcherBuilder creates a new RegexpMatcherBuilder with the specified initial capacity.

func NewSuffixLinearMatcher added in v1.3.0

func NewSuffixLinearMatcher(capacity int) MatcherBuilder

NewSuffixLinearMatcher creates a SuffixLinearMatcher with the specified initial capacity.

func NewSuffixMapMatcher added in v1.3.0

func NewSuffixMapMatcher(capacity int) MatcherBuilder

NewSuffixMapMatcher creates a SuffixMapMatcher with the specified initial capacity.

type RegexpMatcher added in v1.3.0

type RegexpMatcher regexp.Regexp

RegexpMatcher adapts regexp.Regexp to the Matcher interface.

func (*RegexpMatcher) Match added in v1.3.0

func (rlmp *RegexpMatcher) Match(domain string) bool

Match implements the Matcher Match method.

type RegexpMatcherBuilder added in v1.3.0

type RegexpMatcherBuilder []string

RegexpMatcherBuilder stores regular expressions for building RegexpMatcher instances.

func RegexpMatcherBuilderFromSeq added in v1.11.1

func RegexpMatcherBuilderFromSeq(regexCount int, regexSeq iter.Seq[string]) RegexpMatcherBuilder

RegexpMatcherBuilderFromSeq creates a RegexpMatcherBuilder from a sequence of regular expressions.

func (RegexpMatcherBuilder) AppendTo added in v1.3.0

func (rmb RegexpMatcherBuilder) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements the MatcherBuilder AppendTo method.

func (*RegexpMatcherBuilder) Insert added in v1.3.0

func (rmbp *RegexpMatcherBuilder) Insert(rule string)

Insert implements the MatcherBuilder Insert method.

func (RegexpMatcherBuilder) MatcherCount added in v1.3.0

func (rmb RegexpMatcherBuilder) MatcherCount() int

MatcherCount implements the MatcherBuilder MatcherCount method.

func (RegexpMatcherBuilder) Rules added in v1.3.0

func (rmb RegexpMatcherBuilder) Rules() (int, iter.Seq[string])

Rules implements the MatcherBuilder Rules method.

type SuffixLinearMatcher added in v1.3.0

type SuffixLinearMatcher []string

SuffixLinearMatcher matches suffix rules by iterating over the suffixes. It is faster than [SuffixTrieMatcher] when the number of rules is no greater than MaxLinearSuffixes.

func (*SuffixLinearMatcher) AppendTo added in v1.3.0

func (slmp *SuffixLinearMatcher) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements the MatcherBuilder AppendTo method.

func (*SuffixLinearMatcher) Insert added in v1.3.0

func (slmp *SuffixLinearMatcher) Insert(rule string)

Insert implements the MatcherBuilder Insert method.

func (SuffixLinearMatcher) Match added in v1.3.0

func (slm SuffixLinearMatcher) Match(domain string) bool

Match implements the Matcher Match method.

func (SuffixLinearMatcher) MatcherCount added in v1.3.0

func (slm SuffixLinearMatcher) MatcherCount() int

MatcherCount implements the MatcherBuilder MatcherCount method.

func (SuffixLinearMatcher) Rules added in v1.3.0

func (slm SuffixLinearMatcher) Rules() (int, iter.Seq[string])

Rules implements the MatcherBuilder Rules method.

type SuffixMapMatcher added in v1.3.0

type SuffixMapMatcher map[string]struct{}

SuffixMapMatcher matches suffix rules using a single map.

func SuffixMapMatcherFromSeq added in v1.11.1

func SuffixMapMatcherFromSeq(suffixCount int, suffixSeq iter.Seq[string]) SuffixMapMatcher

SuffixMapMatcherFromSeq creates a SuffixMapMatcher from a sequence of suffix rules.

func SuffixMapMatcherFromSlice added in v1.3.0

func SuffixMapMatcherFromSlice(suffixes []string) SuffixMapMatcher

SuffixMapMatcherFromSlice creates a SuffixMapMatcher from a slice of suffix rules.

func (*SuffixMapMatcher) AppendTo added in v1.3.0

func (smmp *SuffixMapMatcher) AppendTo(matchers []Matcher) ([]Matcher, error)

AppendTo implements the MatcherBuilder AppendTo method.

func (SuffixMapMatcher) Insert added in v1.3.0

func (smm SuffixMapMatcher) Insert(rule string)

Insert implements the MatcherBuilder Insert method.

func (SuffixMapMatcher) Match added in v1.3.0

func (smm SuffixMapMatcher) Match(domain string) bool

Match implements the Matcher Match method.

func (SuffixMapMatcher) MatcherCount added in v1.3.0

func (smm SuffixMapMatcher) MatcherCount() int

MatcherCount implements the MatcherBuilder MatcherCount method.

func (SuffixMapMatcher) Rules added in v1.3.0

func (smm SuffixMapMatcher) Rules() (int, iter.Seq[string])

Rules implements the MatcherBuilder Rules method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL