refactor: clear linkname,reduce cycle dependencies,transport init geosite function
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
@ -9,6 +10,7 @@ type classicalStrategy struct {
|
||||
rules []C.Rule
|
||||
count int
|
||||
shouldResolveIP bool
|
||||
parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)
|
||||
}
|
||||
|
||||
func (c *classicalStrategy) Match(metadata *C.Metadata) bool {
|
||||
@ -34,7 +36,7 @@ func (c *classicalStrategy) OnUpdate(rules []string) {
|
||||
shouldResolveIP := false
|
||||
for _, rawRule := range rules {
|
||||
ruleType, rule, params := ruleParse(rawRule)
|
||||
r, err := parseRule(ruleType, rule, "", params)
|
||||
r, err := c.parse(ruleType, rule, "", params)
|
||||
if err != nil {
|
||||
log.Warnln("parse rule error:[%s]", err.Error())
|
||||
} else {
|
||||
@ -50,6 +52,13 @@ func (c *classicalStrategy) OnUpdate(rules []string) {
|
||||
c.count = len(classicalRules)
|
||||
}
|
||||
|
||||
func NewClassicalStrategy() *classicalStrategy {
|
||||
return &classicalStrategy{rules: []C.Rule{}}
|
||||
func NewClassicalStrategy(parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) *classicalStrategy {
|
||||
return &classicalStrategy{rules: []C.Rule{}, parse: func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
||||
switch tp {
|
||||
case "MATCH":
|
||||
return nil, fmt.Errorf("unsupported rule type on rule-set")
|
||||
default:
|
||||
return parse(tp, payload, target, params)
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
@ -6,8 +6,6 @@ import (
|
||||
"github.com/Dreamacro/clash/common/structure"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
P "github.com/Dreamacro/clash/constant/provider"
|
||||
RC "github.com/Dreamacro/clash/rules/common"
|
||||
"github.com/Dreamacro/clash/rules/ruleparser"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -19,7 +17,7 @@ type ruleProviderSchema struct {
|
||||
Interval int `provider:"interval,omitempty"`
|
||||
}
|
||||
|
||||
func ParseRuleProvider(name string, mapping map[string]interface{}) (P.RuleProvider, error) {
|
||||
func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (P.RuleProvider, error) {
|
||||
schema := &ruleProviderSchema{}
|
||||
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
||||
if err := decoder.Decode(mapping, schema); err != nil {
|
||||
@ -49,19 +47,5 @@ func ParseRuleProvider(name string, mapping map[string]interface{}) (P.RuleProvi
|
||||
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
|
||||
}
|
||||
|
||||
return NewRuleSetProvider(name, behavior, time.Duration(uint(schema.Interval))*time.Second, vehicle), nil
|
||||
}
|
||||
|
||||
func parseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
||||
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, target, params)
|
||||
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
ruleExtra := &C.RuleExtra{
|
||||
Network: RC.FindNetwork(params),
|
||||
SourceIPs: RC.FindSourceIPs(params),
|
||||
}
|
||||
parsed.SetRuleExtra(ruleExtra)
|
||||
return parsed, parseErr
|
||||
return NewRuleSetProvider(name, behavior, time.Duration(uint(schema.Interval))*time.Second, vehicle, parse), nil
|
||||
}
|
||||
|
@ -99,7 +99,8 @@ func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
|
||||
})
|
||||
}
|
||||
|
||||
func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration, vehicle P.Vehicle) P.RuleProvider {
|
||||
func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration, vehicle P.Vehicle,
|
||||
parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) P.RuleProvider {
|
||||
rp := &ruleSetProvider{
|
||||
behavior: behavior,
|
||||
}
|
||||
@ -112,7 +113,7 @@ func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration
|
||||
|
||||
fetcher := newFetcher(name, interval, vehicle, rulesParse, onUpdate)
|
||||
rp.fetcher = fetcher
|
||||
rp.strategy = newStrategy(behavior)
|
||||
rp.strategy = newStrategy(behavior, parse)
|
||||
|
||||
wrapper := &RuleSetProvider{
|
||||
rp,
|
||||
@ -123,7 +124,7 @@ func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration
|
||||
return wrapper
|
||||
}
|
||||
|
||||
func newStrategy(behavior P.RuleType) ruleStrategy {
|
||||
func newStrategy(behavior P.RuleType, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) ruleStrategy {
|
||||
switch behavior {
|
||||
case P.Domain:
|
||||
strategy := NewDomainStrategy()
|
||||
@ -132,7 +133,7 @@ func newStrategy(behavior P.RuleType) ruleStrategy {
|
||||
strategy := NewIPCidrStrategy()
|
||||
return strategy
|
||||
case P.Classical:
|
||||
strategy := NewClassicalStrategy()
|
||||
strategy := NewClassicalStrategy(parse)
|
||||
return strategy
|
||||
default:
|
||||
return nil
|
||||
|
@ -47,7 +47,7 @@ func (rs *RuleSet) getProviders() P.RuleProvider {
|
||||
return rs.ruleProvider
|
||||
}
|
||||
|
||||
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
|
||||
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (*RuleSet, error) {
|
||||
rp, ok := RuleProviders()[ruleProviderName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
|
||||
|
Reference in New Issue
Block a user