fix: adjust sub_rule to logic package, and fix not rule failed
This commit is contained in:
parent
ef2f8317c7
commit
4597ed49cf
4 changed files with 13 additions and 16 deletions
|
@ -2,16 +2,14 @@ package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/constant"
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
RC "github.com/Dreamacro/clash/rules/common"
|
RC "github.com/Dreamacro/clash/rules/common"
|
||||||
RP "github.com/Dreamacro/clash/rules/provider"
|
RP "github.com/Dreamacro/clash/rules/provider"
|
||||||
"github.com/Dreamacro/clash/rules/sub_rule"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed constant.Rule, parseErr error) {
|
func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error) {
|
||||||
switch tp {
|
switch tp {
|
||||||
case "DOMAIN":
|
case "DOMAIN":
|
||||||
parsed = RC.NewDomain(payload, target)
|
parsed = RC.NewDomain(payload, target)
|
||||||
|
@ -49,7 +47,7 @@ func ParseRule(tp, payload, target string, params []string, subRules *map[string
|
||||||
case "IN-TYPE":
|
case "IN-TYPE":
|
||||||
parsed, parseErr = RC.NewInType(payload, target)
|
parsed, parseErr = RC.NewInType(payload, target)
|
||||||
case "SUB-RULE":
|
case "SUB-RULE":
|
||||||
parsed, parseErr = sub_rule.NewSubRule(payload, target, subRules, ParseRule)
|
parsed, parseErr = NewSubRule(payload, target, subRules, ParseRule)
|
||||||
case "AND":
|
case "AND":
|
||||||
parsed, parseErr = NewAND(payload, target, ParseRule)
|
parsed, parseErr = NewAND(payload, target, ParseRule)
|
||||||
case "OR":
|
case "OR":
|
||||||
|
@ -74,10 +72,10 @@ func TestAND(t *testing.T) {
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "DIRECT", and.adapter)
|
assert.Equal(t, "DIRECT", and.adapter)
|
||||||
assert.Equal(t, false, and.ShouldResolveIP())
|
assert.Equal(t, false, and.ShouldResolveIP())
|
||||||
m, _ := and.Match(&constant.Metadata{
|
m, _ := and.Match(&C.Metadata{
|
||||||
Host: "baidu.com",
|
Host: "baidu.com",
|
||||||
AddrType: constant.AtypDomainName,
|
AddrType: C.AtypDomainName,
|
||||||
NetWork: constant.TCP,
|
NetWork: C.TCP,
|
||||||
DstPort: "20000",
|
DstPort: "20000",
|
||||||
})
|
})
|
||||||
assert.Equal(t, true, m)
|
assert.Equal(t, true, m)
|
||||||
|
@ -92,7 +90,7 @@ func TestAND(t *testing.T) {
|
||||||
func TestNOT(t *testing.T) {
|
func TestNOT(t *testing.T) {
|
||||||
not, err := NewNOT("((DST-PORT,6000-6500))", "REJECT", ParseRule)
|
not, err := NewNOT("((DST-PORT,6000-6500))", "REJECT", ParseRule)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
m, _ := not.Match(&constant.Metadata{
|
m, _ := not.Match(&C.Metadata{
|
||||||
DstPort: "6100",
|
DstPort: "6100",
|
||||||
})
|
})
|
||||||
assert.Equal(t, false, m)
|
assert.Equal(t, false, m)
|
||||||
|
@ -107,8 +105,8 @@ func TestNOT(t *testing.T) {
|
||||||
func TestOR(t *testing.T) {
|
func TestOR(t *testing.T) {
|
||||||
or, err := NewOR("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule)
|
or, err := NewOR("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
m, _ := or.Match(&constant.Metadata{
|
m, _ := or.Match(&C.Metadata{
|
||||||
NetWork: constant.TCP,
|
NetWork: C.TCP,
|
||||||
})
|
})
|
||||||
assert.Equal(t, true, m)
|
assert.Equal(t, true, m)
|
||||||
assert.Equal(t, false, or.ShouldResolveIP())
|
assert.Equal(t, false, or.ShouldResolveIP())
|
||||||
|
|
|
@ -43,7 +43,7 @@ func (not *NOT) Match(metadata *C.Metadata) (bool, string) {
|
||||||
return true, not.adapter
|
return true, not.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
if m, _ := not.rule.Match(metadata); m {
|
if m, _ := not.rule.Match(metadata); !m {
|
||||||
return true, not.adapter
|
return true, not.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package sub_rule
|
package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/rules/common"
|
"github.com/Dreamacro/clash/rules/common"
|
||||||
"github.com/Dreamacro/clash/rules/logic"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubRule struct {
|
type SubRule struct {
|
||||||
|
@ -19,7 +19,7 @@ type SubRule struct {
|
||||||
|
|
||||||
func NewSubRule(payload, subName string, sub *map[string][]C.Rule,
|
func NewSubRule(payload, subName string, sub *map[string][]C.Rule,
|
||||||
parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*SubRule, error) {
|
parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*SubRule, error) {
|
||||||
payloadRule, err := logic.ParseRuleByPayload(fmt.Sprintf("(%s)", payload), parse)
|
payloadRule, err := ParseRuleByPayload(fmt.Sprintf("(%s)", payload), parse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
|
@ -6,7 +6,6 @@ import (
|
||||||
RC "github.com/Dreamacro/clash/rules/common"
|
RC "github.com/Dreamacro/clash/rules/common"
|
||||||
"github.com/Dreamacro/clash/rules/logic"
|
"github.com/Dreamacro/clash/rules/logic"
|
||||||
RP "github.com/Dreamacro/clash/rules/provider"
|
RP "github.com/Dreamacro/clash/rules/provider"
|
||||||
"github.com/Dreamacro/clash/rules/sub_rule"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error) {
|
func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error) {
|
||||||
|
@ -47,7 +46,7 @@ func ParseRule(tp, payload, target string, params []string, subRules *map[string
|
||||||
case "IN-TYPE":
|
case "IN-TYPE":
|
||||||
parsed, parseErr = RC.NewInType(payload, target)
|
parsed, parseErr = RC.NewInType(payload, target)
|
||||||
case "SUB-RULE":
|
case "SUB-RULE":
|
||||||
parsed, parseErr = sub_rule.NewSubRule(payload, target, subRules, ParseRule)
|
parsed, parseErr = logic.NewSubRule(payload, target, subRules, ParseRule)
|
||||||
case "AND":
|
case "AND":
|
||||||
parsed, parseErr = logic.NewAND(payload, target, ParseRule)
|
parsed, parseErr = logic.NewAND(payload, target, ParseRule)
|
||||||
case "OR":
|
case "OR":
|
||||||
|
|
Loading…
Reference in a new issue