1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-09-19 20:15:59 +08:00
Files
mihomo/rules/provider/classical_strategy.go

65 lines
1.4 KiB
Go
Raw Normal View History

2022-03-26 18:34:15 +08:00
package provider
import (
"fmt"
2024-03-29 13:43:11 +08:00
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
2024-07-27 10:36:11 +08:00
P "github.com/metacubex/mihomo/constant/provider"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/rules/common"
2022-03-26 18:34:15 +08:00
)
type classicalStrategy struct {
rules []C.Rule
count int
2025-07-10 11:27:54 +08:00
parse common.ParseRuleFunc
2022-03-26 18:34:15 +08:00
}
2024-07-27 10:36:11 +08:00
func (c *classicalStrategy) Behavior() P.RuleBehavior {
return P.Classical
}
func (c *classicalStrategy) Match(metadata *C.Metadata, helper C.RuleMatchHelper) bool {
2022-03-26 18:34:15 +08:00
for _, rule := range c.rules {
if m, _ := rule.Match(metadata, helper); m {
2022-03-26 18:34:15 +08:00
return true
}
}
return false
}
func (c *classicalStrategy) Count() int {
return c.count
}
func (c *classicalStrategy) Reset() {
c.rules = nil
c.count = 0
}
func (c *classicalStrategy) Insert(rule string) {
2025-07-10 11:27:54 +08:00
r, err := c.payloadToRule(rule)
if err != nil {
2025-07-10 11:27:54 +08:00
log.Warnln("parse classical rule [%s] error: %s", rule, err.Error())
} else {
c.rules = append(c.rules, r)
c.count++
}
2022-03-26 18:34:15 +08:00
}
2025-07-10 11:27:54 +08:00
func (c *classicalStrategy) payloadToRule(rule string) (C.Rule, error) {
tp, payload, target, params := common.ParseRulePayload(rule, false)
switch tp {
case "MATCH", "RULE-SET", "SUB-RULE":
return nil, fmt.Errorf("unsupported rule type on classical rule-set: %s", tp)
}
return c.parse(tp, payload, target, params, nil)
}
func (c *classicalStrategy) FinishInsert() {}
2025-07-10 11:27:54 +08:00
func NewClassicalStrategy(parse common.ParseRuleFunc) *classicalStrategy {
return &classicalStrategy{rules: []C.Rule{}, parse: parse}
2022-03-26 18:34:15 +08:00
}