2022-03-26 18:34:15 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type classicalStrategy struct {
|
|
|
|
rules []C.Rule
|
|
|
|
count int
|
|
|
|
shouldResolveIP bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *classicalStrategy) Match(metadata *C.Metadata) bool {
|
|
|
|
for _, rule := range c.rules {
|
|
|
|
if rule.Match(metadata) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *classicalStrategy) Count() int {
|
|
|
|
return c.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *classicalStrategy) ShouldResolveIP() bool {
|
|
|
|
return c.shouldResolveIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *classicalStrategy) OnUpdate(rules []string) {
|
2022-06-04 19:12:50 +08:00
|
|
|
var classicalRules []C.Rule
|
|
|
|
shouldResolveIP := false
|
2022-03-26 18:34:15 +08:00
|
|
|
for _, rawRule := range rules {
|
|
|
|
ruleType, rule, params := ruleParse(rawRule)
|
|
|
|
r, err := parseRule(ruleType, rule, "", params)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("parse rule error:[%s]", err.Error())
|
2022-03-28 21:04:50 +08:00
|
|
|
} else {
|
2022-06-04 19:12:50 +08:00
|
|
|
if !shouldResolveIP {
|
|
|
|
shouldResolveIP = r.ShouldResolveIP()
|
2022-03-28 21:04:50 +08:00
|
|
|
}
|
2022-03-26 18:34:15 +08:00
|
|
|
|
2022-06-04 19:12:50 +08:00
|
|
|
classicalRules = append(classicalRules, r)
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 19:12:50 +08:00
|
|
|
|
|
|
|
c.rules = classicalRules
|
|
|
|
c.count = len(classicalRules)
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClassicalStrategy() *classicalStrategy {
|
|
|
|
return &classicalStrategy{rules: []C.Rule{}}
|
|
|
|
}
|