2022-03-26 18:34:15 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2022-06-10 13:36:09 +08:00
|
|
|
"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"
|
2025-07-07 16:16:16 +08:00
|
|
|
"github.com/metacubex/mihomo/rules/common"
|
2022-03-26 18:34:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type classicalStrategy struct {
|
2025-06-10 20:11:50 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-06-10 20:11:50 +08:00
|
|
|
func (c *classicalStrategy) Match(metadata *C.Metadata, helper C.RuleMatchHelper) bool {
|
2022-03-26 18:34:15 +08:00
|
|
|
for _, rule := range c.rules {
|
2025-06-10 20:11:50 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (c *classicalStrategy) Reset() {
|
|
|
|
c.rules = nil
|
|
|
|
c.count = 0
|
|
|
|
}
|
2023-01-20 16:29:08 +08:00
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (c *classicalStrategy) Insert(rule string) {
|
2025-07-10 11:27:54 +08:00
|
|
|
r, err := c.payloadToRule(rule)
|
2023-04-01 14:11:09 +08:00
|
|
|
if err != nil {
|
2025-07-10 11:27:54 +08:00
|
|
|
log.Warnln("parse classical rule [%s] error: %s", rule, err.Error())
|
2023-04-01 14:11:09 +08:00
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
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
|
|
|
}
|