feat: ruleset support text format
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"gopkg.in/yaml.v3"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
@ -20,7 +21,8 @@ var (
|
||||
|
||||
type ruleSetProvider struct {
|
||||
*resource.Fetcher[any]
|
||||
behavior P.RuleType
|
||||
behavior P.RuleBehavior
|
||||
format P.RuleFormat
|
||||
strategy ruleStrategy
|
||||
}
|
||||
|
||||
@ -81,7 +83,7 @@ func (rp *ruleSetProvider) Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) Behavior() P.RuleType {
|
||||
func (rp *ruleSetProvider) Behavior() P.RuleBehavior {
|
||||
return rp.behavior
|
||||
}
|
||||
|
||||
@ -105,6 +107,7 @@ func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(
|
||||
map[string]interface{}{
|
||||
"behavior": rp.behavior.String(),
|
||||
"format": rp.format.String(),
|
||||
"name": rp.Name(),
|
||||
"ruleCount": rp.strategy.Count(),
|
||||
"type": rp.Type().String(),
|
||||
@ -113,10 +116,11 @@ func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
|
||||
})
|
||||
}
|
||||
|
||||
func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration, vehicle P.Vehicle,
|
||||
func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle,
|
||||
parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) P.RuleProvider {
|
||||
rp := &ruleSetProvider{
|
||||
behavior: behavior,
|
||||
format: format,
|
||||
}
|
||||
|
||||
onUpdate := func(elm interface{}) {
|
||||
@ -125,7 +129,7 @@ func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration
|
||||
}
|
||||
|
||||
rp.strategy = newStrategy(behavior, parse)
|
||||
rp.Fetcher = resource.NewFetcher(name, interval, vehicle, func(bytes []byte) (any, error) { return rulesParse(bytes, newStrategy(behavior, parse)) }, onUpdate)
|
||||
rp.Fetcher = resource.NewFetcher(name, interval, vehicle, func(bytes []byte) (any, error) { return rulesParse(bytes, newStrategy(behavior, parse), format) }, onUpdate)
|
||||
|
||||
wrapper := &RuleSetProvider{
|
||||
rp,
|
||||
@ -136,7 +140,7 @@ func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration
|
||||
return wrapper
|
||||
}
|
||||
|
||||
func newStrategy(behavior P.RuleType, parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) ruleStrategy {
|
||||
func newStrategy(behavior P.RuleBehavior, parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) ruleStrategy {
|
||||
switch behavior {
|
||||
case P.Domain:
|
||||
strategy := NewDomainStrategy()
|
||||
@ -154,7 +158,7 @@ func newStrategy(behavior P.RuleType, parse func(tp, payload, target string, par
|
||||
|
||||
var ErrNoPayload = errors.New("file must have a `payload` field")
|
||||
|
||||
func rulesParse(buf []byte, strategy ruleStrategy) (any, error) {
|
||||
func rulesParse(buf []byte, strategy ruleStrategy, format P.RuleFormat) (any, error) {
|
||||
strategy.Reset()
|
||||
|
||||
schema := &RulePayload{}
|
||||
@ -177,36 +181,54 @@ func rulesParse(buf []byte, strategy ruleStrategy) (any, error) {
|
||||
return nil, ErrNoPayload
|
||||
}
|
||||
}
|
||||
firstLineBuffer.Write(line)
|
||||
if firstLineLength == 0 { // find payload head
|
||||
firstLineLength = firstLineBuffer.Len()
|
||||
firstLineBuffer.WriteString(" - ''") // a test line
|
||||
var str string
|
||||
switch format {
|
||||
case P.TextRule:
|
||||
firstLineLength = -1 // don't return ErrNoPayload when read last line
|
||||
str = string(line)
|
||||
str = strings.TrimSpace(str)
|
||||
if str[0] == '#' { // comment
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(str, "//") { // comment in Premium core
|
||||
continue
|
||||
}
|
||||
case P.YamlRule:
|
||||
if bytes.TrimSpace(line)[0] == '#' { // comment
|
||||
continue
|
||||
}
|
||||
firstLineBuffer.Write(line)
|
||||
if firstLineLength == 0 { // find payload head
|
||||
firstLineLength = firstLineBuffer.Len()
|
||||
firstLineBuffer.WriteString(" - ''") // a test line
|
||||
|
||||
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
|
||||
firstLineBuffer.Truncate(firstLineLength)
|
||||
if err == nil && (len(schema.Rules) > 0 || len(schema.Payload) > 0) { // found
|
||||
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
|
||||
firstLineBuffer.Truncate(firstLineLength)
|
||||
if err == nil && (len(schema.Rules) > 0 || len(schema.Payload) > 0) { // found
|
||||
continue
|
||||
}
|
||||
|
||||
// not found or err!=nil
|
||||
firstLineBuffer.Truncate(0)
|
||||
firstLineLength = 0
|
||||
continue
|
||||
}
|
||||
|
||||
// not found or err!=nil
|
||||
firstLineBuffer.Truncate(0)
|
||||
firstLineLength = 0
|
||||
continue
|
||||
// parse payload body
|
||||
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
|
||||
firstLineBuffer.Truncate(firstLineLength)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(schema.Rules) > 0 {
|
||||
str = schema.Rules[0]
|
||||
}
|
||||
if len(schema.Payload) > 0 {
|
||||
str = schema.Payload[0]
|
||||
}
|
||||
}
|
||||
|
||||
// parse payload body
|
||||
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
|
||||
firstLineBuffer.Truncate(firstLineLength)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var str string
|
||||
if len(schema.Rules) > 0 {
|
||||
str = schema.Rules[0]
|
||||
}
|
||||
if len(schema.Payload) > 0 {
|
||||
str = schema.Payload[0]
|
||||
}
|
||||
if str == "" {
|
||||
continue
|
||||
}
|
||||
|
Reference in New Issue
Block a user