mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 11:26:47 +00:00
DomainSet emits at most one exact entry and one suffix-only entry for each domain. Sorting without the leading dot makes matching pairs adjacent, so they can be combined in place. Replace per-entry binary searches and the separate rule slice with an adjacent pairing pass. Merge pairs into +.domain, remove consumed entries from the original slice, and sort the resulting rules by their full text. Add regression coverage for multiple pairs and wildcard ordering. Output semantics and early termination remain unchanged.
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/metacubex/mihomo/component/trie"
|
|
C "github.com/metacubex/mihomo/constant"
|
|
P "github.com/metacubex/mihomo/constant/provider"
|
|
"github.com/metacubex/mihomo/log"
|
|
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
type domainStrategy struct {
|
|
count int
|
|
domainSetBuilder trie.DomainSetBuilder
|
|
domainSet *trie.DomainSet
|
|
}
|
|
|
|
func (d *domainStrategy) Behavior() P.RuleBehavior {
|
|
return P.Domain
|
|
}
|
|
|
|
func (d *domainStrategy) Match(metadata *C.Metadata, helper C.RuleMatchHelper) bool {
|
|
return d.domainSet != nil && d.domainSet.Has(metadata.RuleHost())
|
|
}
|
|
|
|
func (d *domainStrategy) Count() int {
|
|
return d.count
|
|
}
|
|
|
|
func (d *domainStrategy) Reset() {
|
|
d.domainSetBuilder.Reset()
|
|
d.domainSet = nil
|
|
d.count = 0
|
|
}
|
|
|
|
func (d *domainStrategy) Insert(rule string) {
|
|
if strings.ContainsRune(rule, '/') {
|
|
log.Warnln("skip invalid domain from rule provider: invalid domain %q: slash is not allowed", rule)
|
|
return
|
|
}
|
|
err := d.domainSetBuilder.Insert(rule)
|
|
if err != nil {
|
|
log.Warnln("skip invalid domain from rule provider: %s", err)
|
|
} else {
|
|
d.count++
|
|
}
|
|
}
|
|
|
|
func (d *domainStrategy) FinishInsert() {
|
|
d.domainSet = d.domainSetBuilder.Build()
|
|
}
|
|
|
|
func (d *domainStrategy) FromMrs(r io.Reader, count int) error {
|
|
domainSet, err := trie.ReadDomainSetBin(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.count = count
|
|
d.domainSet = domainSet
|
|
return nil
|
|
}
|
|
|
|
func (d *domainStrategy) WriteMrs(w io.Writer) error {
|
|
if d.domainSet == nil {
|
|
return errors.New("nil domainSet")
|
|
}
|
|
return d.domainSet.WriteBin(w)
|
|
}
|
|
|
|
// DumpMrs emits a compact domain rule list in lexicographical order through f.
|
|
// A "domain" entry and a ".domain" entry are represented by one "+.domain" rule;
|
|
// either entry alone is emitted unchanged. Returning false from f stops iteration.
|
|
func (d *domainStrategy) DumpMrs(f func(key string) bool) {
|
|
if d.domainSet != nil {
|
|
var keys []string
|
|
d.domainSet.Foreach(func(key string) bool {
|
|
keys = append(keys, key)
|
|
return true
|
|
})
|
|
|
|
// Group exact and suffix-only entries for the same domain.
|
|
// Set entries are unique, so each group contains at most two entries.
|
|
slices.SortFunc(keys, func(left, right string) int {
|
|
left = strings.TrimPrefix(left, ".")
|
|
right = strings.TrimPrefix(right, ".")
|
|
return strings.Compare(left, right)
|
|
})
|
|
for index := 0; index+1 < len(keys); index++ {
|
|
domain := strings.TrimPrefix(keys[index], ".")
|
|
if domain == strings.TrimPrefix(keys[index+1], ".") {
|
|
// Both the domain itself and its subdomains are covered by one "+." rule.
|
|
keys[index] = "+." + domain
|
|
keys[index+1] = ""
|
|
index++
|
|
}
|
|
}
|
|
keys = slices.DeleteFunc(keys, func(key string) bool { return key == "" })
|
|
// Sort the compact rules by their full text, including wildcard prefixes.
|
|
slices.Sort(keys)
|
|
|
|
for _, key := range keys {
|
|
if !f(key) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var _ mrsRuleStrategy = (*domainStrategy)(nil)
|
|
|
|
func NewDomainStrategy() *domainStrategy {
|
|
return &domainStrategy{}
|
|
}
|