Skip to content

Commit

Permalink
feat: ruleset support text format
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Apr 14, 2023
1 parent b3794ff commit e4926c8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 43 deletions.
44 changes: 31 additions & 13 deletions constant/provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,27 @@ type ProxyProvider interface {
Version() uint32
}

// Rule Type
// RuleProvider interface
type RuleProvider interface {
Provider
Behavior() RuleBehavior
Match(*constant.Metadata) bool
ShouldResolveIP() bool
ShouldFindProcess() bool
AsRule(adaptor string) constant.Rule
}

// Rule Behavior
const (
Domain RuleType = iota
Domain RuleBehavior = iota
IPCIDR
Classical
)

// RuleType defined
type RuleType int
// RuleBehavior defined
type RuleBehavior int

func (rt RuleType) String() string {
func (rt RuleBehavior) String() string {
switch rt {
case Domain:
return "Domain"
Expand All @@ -96,12 +106,20 @@ func (rt RuleType) String() string {
}
}

// RuleProvider interface
type RuleProvider interface {
Provider
Behavior() RuleType
Match(*constant.Metadata) bool
ShouldResolveIP() bool
ShouldFindProcess() bool
AsRule(adaptor string) constant.Rule
const (
YamlRule RuleFormat = iota
TextRule
)

type RuleFormat int

func (rf RuleFormat) String() string {
switch rf {
case YamlRule:
return "YamlRule"
case TextRule:
return "TextRule"
default:
return "Unknown"
}
}
16 changes: 14 additions & 2 deletions rules/provider/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ruleProviderSchema struct {
Behavior string `provider:"behavior"`
Path string `provider:"path"`
URL string `provider:"url,omitempty"`
Format string `provider:"format,omitempty"`
Interval int `provider:"interval,omitempty"`
}

Expand All @@ -23,7 +24,7 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
if err := decoder.Decode(mapping, schema); err != nil {
return nil, err
}
var behavior P.RuleType
var behavior P.RuleBehavior

switch schema.Behavior {
case "domain":
Expand All @@ -36,6 +37,17 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
return nil, fmt.Errorf("unsupported behavior type: %s", schema.Behavior)
}

var format P.RuleFormat

switch schema.Format {
case "", "yaml":
format = P.YamlRule
case "text":
format = P.TextRule
default:
return nil, fmt.Errorf("unsupported format type: %s", schema.Format)
}

path := C.Path.Resolve(schema.Path)
var vehicle P.Vehicle
switch schema.Type {
Expand All @@ -47,5 +59,5 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
}

return NewRuleSetProvider(name, behavior, time.Duration(uint(schema.Interval))*time.Second, vehicle, parse), nil
return NewRuleSetProvider(name, behavior, format, time.Duration(uint(schema.Interval))*time.Second, vehicle, parse), nil
}
78 changes: 50 additions & 28 deletions rules/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"gopkg.in/yaml.v3"
"runtime"
"strings"
"time"

"github.com/Dreamacro/clash/common/pool"
Expand All @@ -20,7 +21,8 @@ var (

type ruleSetProvider struct {
*resource.Fetcher[any]
behavior P.RuleType
behavior P.RuleBehavior
format P.RuleFormat
strategy ruleStrategy
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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(),
Expand All @@ -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{}) {
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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{}
Expand All @@ -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
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 && (len(schema.Rules) > 0 || len(schema.Payload) > 0) { // found
if err != nil {
continue
}

// not found or err!=nil
firstLineBuffer.Truncate(0)
firstLineLength = 0
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
}
Expand Down

0 comments on commit e4926c8

Please sign in to comment.