-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
rule.go
103 lines (97 loc) · 2.7 KB
/
rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package geosite
import "github.com/sagernet/sing-box/option"
type ItemType = uint8
const (
RuleTypeDomain ItemType = iota
RuleTypeDomainSuffix
RuleTypeDomainKeyword
RuleTypeDomainRegex
)
type Item struct {
Type ItemType
Value string
}
func Compile(code []Item) option.DefaultRule {
var domainLength int
var domainSuffixLength int
var domainKeywordLength int
var domainRegexLength int
for _, item := range code {
switch item.Type {
case RuleTypeDomain:
domainLength++
case RuleTypeDomainSuffix:
domainSuffixLength++
case RuleTypeDomainKeyword:
domainKeywordLength++
case RuleTypeDomainRegex:
domainRegexLength++
}
}
var codeRule option.DefaultRule
if domainLength > 0 {
codeRule.Domain = make([]string, 0, domainLength)
}
if domainSuffixLength > 0 {
codeRule.DomainSuffix = make([]string, 0, domainSuffixLength)
}
if domainKeywordLength > 0 {
codeRule.DomainKeyword = make([]string, 0, domainKeywordLength)
}
if domainRegexLength > 0 {
codeRule.DomainRegex = make([]string, 0, domainRegexLength)
}
for _, item := range code {
switch item.Type {
case RuleTypeDomain:
codeRule.Domain = append(codeRule.Domain, item.Value)
case RuleTypeDomainSuffix:
codeRule.DomainSuffix = append(codeRule.DomainSuffix, item.Value)
case RuleTypeDomainKeyword:
codeRule.DomainKeyword = append(codeRule.DomainKeyword, item.Value)
case RuleTypeDomainRegex:
codeRule.DomainRegex = append(codeRule.DomainRegex, item.Value)
}
}
return codeRule
}
func Merge(rules []option.DefaultRule) option.DefaultRule {
var domainLength int
var domainSuffixLength int
var domainKeywordLength int
var domainRegexLength int
for _, subRule := range rules {
domainLength += len(subRule.Domain)
domainSuffixLength += len(subRule.DomainSuffix)
domainKeywordLength += len(subRule.DomainKeyword)
domainRegexLength += len(subRule.DomainRegex)
}
var rule option.DefaultRule
if domainLength > 0 {
rule.Domain = make([]string, 0, domainLength)
}
if domainSuffixLength > 0 {
rule.DomainSuffix = make([]string, 0, domainSuffixLength)
}
if domainKeywordLength > 0 {
rule.DomainKeyword = make([]string, 0, domainKeywordLength)
}
if domainRegexLength > 0 {
rule.DomainRegex = make([]string, 0, domainRegexLength)
}
for _, subRule := range rules {
if len(subRule.Domain) > 0 {
rule.Domain = append(rule.Domain, subRule.Domain...)
}
if len(subRule.DomainSuffix) > 0 {
rule.DomainSuffix = append(rule.DomainSuffix, subRule.DomainSuffix...)
}
if len(subRule.DomainKeyword) > 0 {
rule.DomainKeyword = append(rule.DomainKeyword, subRule.DomainKeyword...)
}
if len(subRule.DomainRegex) > 0 {
rule.DomainRegex = append(rule.DomainRegex, subRule.DomainRegex...)
}
}
return rule
}