1
1
package logic
2
2
3
3
import (
4
+ "fmt"
4
5
"github.com/Dreamacro/clash/constant"
6
+ RC "github.com/Dreamacro/clash/rules/common"
7
+ RP "github.com/Dreamacro/clash/rules/provider"
5
8
"github.com/stretchr/testify/assert"
6
9
"testing"
7
10
)
8
11
12
+ func ParseRule (tp , payload , target string , params []string ) (parsed constant.Rule , parseErr error ) {
13
+ switch tp {
14
+ case "DOMAIN" :
15
+ parsed = RC .NewDomain (payload , target )
16
+ case "DOMAIN-SUFFIX" :
17
+ parsed = RC .NewDomainSuffix (payload , target )
18
+ case "DOMAIN-KEYWORD" :
19
+ parsed = RC .NewDomainKeyword (payload , target )
20
+ case "GEOSITE" :
21
+ parsed , parseErr = RC .NewGEOSITE (payload , target )
22
+ case "GEOIP" :
23
+ noResolve := RC .HasNoResolve (params )
24
+ parsed , parseErr = RC .NewGEOIP (payload , target , noResolve )
25
+ case "IP-CIDR" , "IP-CIDR6" :
26
+ noResolve := RC .HasNoResolve (params )
27
+ parsed , parseErr = RC .NewIPCIDR (payload , target , RC .WithIPCIDRNoResolve (noResolve ))
28
+ case "SRC-IP-CIDR" :
29
+ parsed , parseErr = RC .NewIPCIDR (payload , target , RC .WithIPCIDRSourceIP (true ), RC .WithIPCIDRNoResolve (true ))
30
+ case "IP-SUFFIX" :
31
+ noResolve := RC .HasNoResolve (params )
32
+ parsed , parseErr = RC .NewIPSuffix (payload , target , false , noResolve )
33
+ case "SRC-IP-SUFFIX" :
34
+ parsed , parseErr = RC .NewIPSuffix (payload , target , true , true )
35
+ case "SRC-PORT" :
36
+ parsed , parseErr = RC .NewPort (payload , target , true )
37
+ case "DST-PORT" :
38
+ parsed , parseErr = RC .NewPort (payload , target , false )
39
+ case "PROCESS-NAME" :
40
+ parsed , parseErr = RC .NewProcess (payload , target , true )
41
+ case "PROCESS-PATH" :
42
+ parsed , parseErr = RC .NewProcess (payload , target , false )
43
+ case "NETWORK" :
44
+ parsed , parseErr = RC .NewNetworkType (payload , target )
45
+ case "UID" :
46
+ parsed , parseErr = RC .NewUid (payload , target )
47
+ case "IN-TYPE" :
48
+ parsed , parseErr = RC .NewInType (payload , target )
49
+ case "AND" :
50
+ parsed , parseErr = NewAND (payload , target , ParseRule )
51
+ case "OR" :
52
+ parsed , parseErr = NewOR (payload , target , ParseRule )
53
+ case "NOT" :
54
+ parsed , parseErr = NewNOT (payload , target , ParseRule )
55
+ case "RULE-SET" :
56
+ noResolve := RC .HasNoResolve (params )
57
+ parsed , parseErr = RP .NewRuleSet (payload , target , noResolve , ParseRule )
58
+ case "MATCH" :
59
+ parsed = RC .NewMatch (target )
60
+ parseErr = nil
61
+ default :
62
+ parseErr = fmt .Errorf ("unsupported rule type %s" , tp )
63
+ }
64
+
65
+ return
66
+ }
67
+
9
68
func TestAND (t * testing.T ) {
10
- and , err := NewAND ("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" )
69
+ and , err := NewAND ("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" , ParseRule )
11
70
assert .Equal (t , nil , err )
12
71
assert .Equal (t , "DIRECT" , and .adapter )
13
72
assert .Equal (t , false , and .ShouldResolveIP ())
@@ -18,29 +77,29 @@ func TestAND(t *testing.T) {
18
77
DstPort : "20000" ,
19
78
}))
20
79
21
- and , err = NewAND ("(DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" )
80
+ and , err = NewAND ("(DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" , ParseRule )
22
81
assert .NotEqual (t , nil , err )
23
82
24
- and , err = NewAND ("((AND,(DOMAIN,baidu.com),(NETWORK,TCP)),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" )
83
+ and , err = NewAND ("((AND,(DOMAIN,baidu.com),(NETWORK,TCP)),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" , ParseRule )
25
84
assert .Equal (t , nil , err )
26
85
}
27
86
28
87
func TestNOT (t * testing.T ) {
29
- not , err := NewNOT ("((DST-PORT,6000-6500))" , "REJECT" )
88
+ not , err := NewNOT ("((DST-PORT,6000-6500))" , "REJECT" , ParseRule )
30
89
assert .Equal (t , nil , err )
31
90
assert .Equal (t , false , not .Match (& constant.Metadata {
32
91
DstPort : "6100" ,
33
92
}))
34
93
35
- _ , err = NewNOT ("((DST-PORT,5600-6666),(DOMAIN,baidu.com))" , "DIRECT" )
94
+ _ , err = NewNOT ("((DST-PORT,5600-6666),(DOMAIN,baidu.com))" , "DIRECT" , ParseRule )
36
95
assert .NotEqual (t , nil , err )
37
96
38
- _ , err = NewNOT ("(())" , "DIRECT" )
97
+ _ , err = NewNOT ("(())" , "DIRECT" , ParseRule )
39
98
assert .NotEqual (t , nil , err )
40
99
}
41
100
42
101
func TestOR (t * testing.T ) {
43
- or , err := NewOR ("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" )
102
+ or , err := NewOR ("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))" , "DIRECT" , ParseRule )
44
103
assert .Equal (t , nil , err )
45
104
assert .Equal (t , true , or .Match (& constant.Metadata {
46
105
NetWork : constant .TCP ,
0 commit comments