fix: parse logic rule error

This commit is contained in:
Skyxim 2022-04-09 22:25:39 +08:00
parent b2981f921c
commit 07906c0aa5
4 changed files with 30 additions and 13 deletions

View file

@ -1,6 +1,8 @@
package logic package logic
import ( import (
"fmt"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/rule/common" "github.com/Dreamacro/clash/rule/common"
) )
@ -19,12 +21,16 @@ func (A *AND) ShouldFindProcess() bool {
func NewAND(payload string, adapter string) (*AND, error) { func NewAND(payload string, adapter string) (*AND, error) {
and := &AND{Base: &common.Base{}, payload: payload, adapter: adapter} and := &AND{Base: &common.Base{}, payload: payload, adapter: adapter}
rules, err := parseRuleByPayload(payload) rules, err := parseRuleByPayload(payload, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
and.rules = rules and.rules = rules
if len(and.rules) == 0 {
return nil, fmt.Errorf("And rule is error, may be format error or not contain least one rule")
}
for _, rule := range rules { for _, rule := range rules {
if rule.ShouldResolveIP() { if rule.ShouldResolveIP() {
and.needIP = true and.needIP = true

View file

@ -2,19 +2,20 @@ package logic
import ( import (
"fmt" "fmt"
"github.com/Dreamacro/clash/common/collections"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
RC "github.com/Dreamacro/clash/rule/common"
"github.com/Dreamacro/clash/rule/provider"
"io" "io"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
"strings" "strings"
"github.com/Dreamacro/clash/common/collections"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
RC "github.com/Dreamacro/clash/rule/common"
"github.com/Dreamacro/clash/rule/provider"
) )
func parseRuleByPayload(payload string) ([]C.Rule, error) { func parseRuleByPayload(payload string, skip bool) ([]C.Rule, error) {
regex, err := regexp.Compile("\\(.*\\)") regex, err := regexp.Compile("\\(.*\\)")
if err != nil { if err != nil {
return nil, err return nil, err
@ -27,7 +28,7 @@ func parseRuleByPayload(payload string) ([]C.Rule, error) {
} }
rules := make([]C.Rule, 0, len(subAllRanges)) rules := make([]C.Rule, 0, len(subAllRanges))
subRanges := findSubRuleRange(payload, subAllRanges) subRanges := findSubRuleRange(payload, subAllRanges, skip)
for _, subRange := range subRanges { for _, subRange := range subRanges {
subPayload := payload[subRange.start+1 : subRange.end] subPayload := payload[subRange.start+1 : subRange.end]
@ -51,6 +52,10 @@ func containRange(r Range, preStart, preEnd int) bool {
func payloadToRule(subPayload string) (C.Rule, error) { func payloadToRule(subPayload string) (C.Rule, error) {
splitStr := strings.SplitN(subPayload, ",", 2) splitStr := strings.SplitN(subPayload, ",", 2)
if len(splitStr) < 2 {
return nil, fmt.Errorf("The logic rule contain a rule of error format")
}
tp := splitStr[0] tp := splitStr[0]
payload := splitStr[1] payload := splitStr[1]
if tp == "NOT" || tp == "OR" || tp == "AND" { if tp == "NOT" || tp == "OR" || tp == "AND" {
@ -164,11 +169,11 @@ func format(payload string) ([]Range, error) {
return sortResult, nil return sortResult, nil
} }
func findSubRuleRange(payload string, ruleRanges []Range) []Range { func findSubRuleRange(payload string, ruleRanges []Range, skip bool) []Range {
payloadLen := len(payload) payloadLen := len(payload)
subRuleRange := make([]Range, 0) subRuleRange := make([]Range, 0)
for _, rr := range ruleRanges { for _, rr := range ruleRanges {
if rr.start == 0 && rr.end == payloadLen-1 { if rr.start == 0 && rr.end == payloadLen-1 && skip {
// 最大范围跳过 // 最大范围跳过
continue continue
} }

View file

@ -19,13 +19,13 @@ func (not *NOT) ShouldFindProcess() bool {
func NewNOT(payload string, adapter string) (*NOT, error) { func NewNOT(payload string, adapter string) (*NOT, error) {
not := &NOT{Base: &common.Base{}, payload: payload, adapter: adapter} not := &NOT{Base: &common.Base{}, payload: payload, adapter: adapter}
rule, err := parseRuleByPayload(payload) rule, err := parseRuleByPayload(payload, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(rule) < 1 { if len(rule) < 1 {
return nil, fmt.Errorf("the parsed rule is empty") return nil, fmt.Errorf("NOT rule have not a rule")
} }
not.rule = rule[0] not.rule = rule[0]

View file

@ -1,6 +1,8 @@
package logic package logic
import ( import (
"fmt"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/rule/common" "github.com/Dreamacro/clash/rule/common"
) )
@ -45,12 +47,16 @@ func (or *OR) ShouldResolveIP() bool {
func NewOR(payload string, adapter string) (*OR, error) { func NewOR(payload string, adapter string) (*OR, error) {
or := &OR{Base: &common.Base{}, payload: payload, adapter: adapter} or := &OR{Base: &common.Base{}, payload: payload, adapter: adapter}
rules, err := parseRuleByPayload(payload) rules, err := parseRuleByPayload(payload, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
or.rules = rules or.rules = rules
if len(or.rules) == 0 {
return nil, fmt.Errorf("Or rule is error, may be format error or not contain least one rule")
}
for _, rule := range rules { for _, rule := range rules {
if rule.ShouldResolveIP() { if rule.ShouldResolveIP() {
or.needIP = true or.needIP = true