forked from Loyalsoldier/domain-list-custom
-
Notifications
You must be signed in to change notification settings - Fork 12
/
listinfo.go
362 lines (326 loc) · 11.9 KB
/
listinfo.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"bufio"
"errors"
"fmt"
"os"
"sort"
"strings"
"time"
router "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
)
// ListInfo is the information structure of a single file in data directory.
// It includes all types of rules of the file, as well as servel types of
// sturctures of same items for convenience in later process.
type ListInfo struct {
Name fileName
HasInclusion bool
InclusionAttributeMap map[fileName][]attribute
FullTypeList []*router.Domain
KeywordTypeList []*router.Domain
RegexpTypeList []*router.Domain
AttributeRuleUniqueList []*router.Domain
DomainTypeList []*router.Domain
DomainTypeUniqueList []*router.Domain
AttributeRuleListMap map[attribute][]*router.Domain
GeoSite *router.GeoSite
}
// NewListInfo return a ListInfo
func NewListInfo() *ListInfo {
return &ListInfo{
InclusionAttributeMap: make(map[fileName][]attribute),
FullTypeList: make([]*router.Domain, 0, 10),
KeywordTypeList: make([]*router.Domain, 0, 10),
RegexpTypeList: make([]*router.Domain, 0, 10),
AttributeRuleUniqueList: make([]*router.Domain, 0, 10),
DomainTypeList: make([]*router.Domain, 0, 10),
DomainTypeUniqueList: make([]*router.Domain, 0, 10),
AttributeRuleListMap: make(map[attribute][]*router.Domain),
}
}
// ProcessList processes each line of every single file in the data directory
// and generates a ListInfo of each file.
func (l *ListInfo) ProcessList(file *os.File) error {
scanner := bufio.NewScanner(file)
// Parse a file line by line to generate ListInfo
for scanner.Scan() {
line := scanner.Text()
if isEmpty(line) {
continue
}
line = removeComment(line)
if isEmpty(line) {
continue
}
parsedRule, err := l.parseRule(line)
if err != nil {
return err
}
if parsedRule == nil {
continue
}
l.classifyRule(parsedRule)
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
// parseRule parses a single rule
func (l *ListInfo) parseRule(line string) (*router.Domain, error) {
line = strings.TrimSpace(line)
if line == "" {
return nil, errors.New("empty line")
}
// Parse `include` rule first, eg: `include:google`, `include:google @cn @gfw`
if strings.HasPrefix(line, "include:") {
l.parseInclusion(line)
return nil, nil
}
parts := strings.Split(line, " ")
ruleWithType := strings.TrimSpace(parts[0])
if ruleWithType == "" {
return nil, errors.New("empty rule")
}
var rule router.Domain
if err := l.parseTypeRule(ruleWithType, &rule); err != nil {
return nil, err
}
for _, attrString := range parts[1:] {
if attrString = strings.TrimSpace(attrString); attrString != "" {
attr, err := l.parseAttribute(attrString)
if err != nil {
return nil, err
}
rule.Attribute = append(rule.Attribute, attr)
}
}
return &rule, nil
}
func (l *ListInfo) parseInclusion(inclusion string) {
inclusionVal := strings.TrimPrefix(strings.TrimSpace(inclusion), "include:")
l.HasInclusion = true
inclusionValSlice := strings.Split(inclusionVal, "@")
filename := fileName(strings.ToUpper(strings.TrimSpace(inclusionValSlice[0])))
switch len(inclusionValSlice) {
case 1: // Inclusion without attribute
// Use '@' as the placeholder attribute for 'include:filename'
l.InclusionAttributeMap[filename] = append(l.InclusionAttributeMap[filename], attribute("@"))
default: // Inclusion with attribute(s)
// support new inclusion syntax, eg: `include:google @cn @gfw`
for _, attr := range inclusionValSlice[1:] {
attr = strings.ToLower(strings.TrimSpace(attr))
if attr != "" {
// Added in this format: '@cn'
l.InclusionAttributeMap[filename] = append(l.InclusionAttributeMap[filename], attribute("@"+attr))
}
}
}
}
func (l *ListInfo) parseTypeRule(domain string, rule *router.Domain) error {
kv := strings.Split(domain, ":")
switch len(kv) {
case 1: // line without type prefix
rule.Type = router.Domain_RootDomain
rule.Value = strings.ToLower(strings.TrimSpace(kv[0]))
case 2: // line with type prefix
ruleType := strings.TrimSpace(kv[0])
ruleVal := strings.TrimSpace(kv[1])
rule.Value = strings.ToLower(ruleVal)
switch strings.ToLower(ruleType) {
case "full":
rule.Type = router.Domain_Full
case "domain":
rule.Type = router.Domain_RootDomain
case "keyword":
rule.Type = router.Domain_Plain
case "regexp":
rule.Type = router.Domain_Regex
rule.Value = ruleVal
default:
return errors.New("unknown domain type: " + ruleType)
}
}
return nil
}
func (l *ListInfo) parseAttribute(attr string) (*router.Domain_Attribute, error) {
if attr[0] != '@' {
return nil, errors.New("invalid attribute: " + attr)
}
attr = attr[1:] // Trim out attribute prefix `@` character
var attribute router.Domain_Attribute
attribute.Key = strings.ToLower(attr)
attribute.TypedValue = &router.Domain_Attribute_BoolValue{BoolValue: true}
return &attribute, nil
}
// classifyRule classifies a single rule and write into *ListInfo
func (l *ListInfo) classifyRule(rule *router.Domain) {
if len(rule.Attribute) > 0 {
l.AttributeRuleUniqueList = append(l.AttributeRuleUniqueList, rule)
var attrsString attribute
for _, attr := range rule.Attribute {
attrsString += attribute("@" + attr.GetKey()) // attrsString will be "@cn@ads" if there are more than one attributes
}
l.AttributeRuleListMap[attrsString] = append(l.AttributeRuleListMap[attrsString], rule)
} else {
switch rule.Type {
case router.Domain_Full:
l.FullTypeList = append(l.FullTypeList, rule)
case router.Domain_RootDomain:
l.DomainTypeList = append(l.DomainTypeList, rule)
case router.Domain_Plain:
l.KeywordTypeList = append(l.KeywordTypeList, rule)
case router.Domain_Regex:
l.RegexpTypeList = append(l.RegexpTypeList, rule)
}
}
}
// Flatten flattens the rules in a file that have "include" syntax
// in data directory, and adds those need-to-included rules into it.
// This feature supports the "include:filename@attribute" syntax.
// It also generates a domain trie of domain-typed rules for each file
// to remove duplications of them.
func (l *ListInfo) Flatten(lm *ListInfoMap) error {
if l.HasInclusion {
for filename, attrs := range l.InclusionAttributeMap {
for _, attrWanted := range attrs {
includedList := (*lm)[filename]
switch string(attrWanted) {
case "@":
l.FullTypeList = append(l.FullTypeList, includedList.FullTypeList...)
l.DomainTypeList = append(l.DomainTypeList, includedList.DomainTypeList...)
l.KeywordTypeList = append(l.KeywordTypeList, includedList.KeywordTypeList...)
l.RegexpTypeList = append(l.RegexpTypeList, includedList.RegexpTypeList...)
l.AttributeRuleUniqueList = append(l.AttributeRuleUniqueList, includedList.AttributeRuleUniqueList...)
for attr, domainList := range includedList.AttributeRuleListMap {
l.AttributeRuleListMap[attr] = append(l.AttributeRuleListMap[attr], domainList...)
}
default:
for attr, domainList := range includedList.AttributeRuleListMap {
// If there are more than one attribute attached to the rule,
// the attribute key of AttributeRuleListMap in ListInfo
// will be like: "@cn@ads".
// So if to extract rules with a specific attribute, it is necessary
// also to test the multi-attribute keys of AttributeRuleListMap.
// Notice: if "include:google @cn" and "include:google @ads" appear
// at the same time in the parent list. There are chances that the same
// rule with that two attributes(`@cn` and `@ads`) will be included twice in the parent list.
if strings.Contains(string(attr)+"@", string(attrWanted)+"@") {
l.AttributeRuleListMap[attr] = append(l.AttributeRuleListMap[attr], domainList...)
l.AttributeRuleUniqueList = append(l.AttributeRuleUniqueList, domainList...)
}
}
}
}
}
}
sort.Slice(l.DomainTypeList, func(i, j int) bool {
return len(strings.Split(l.DomainTypeList[i].GetValue(), ".")) < len(strings.Split(l.DomainTypeList[j].GetValue(), "."))
})
trie := NewDomainTrie()
for _, domain := range l.DomainTypeList {
success, err := trie.Insert(domain.GetValue())
if err != nil {
return err
}
if success {
l.DomainTypeUniqueList = append(l.DomainTypeUniqueList, domain)
}
}
return nil
}
// ToGeoSite converts every ListInfo into a router.GeoSite structure.
// It also excludes rules with certain attributes in certain files that
// user specified in command line when runing the program.
func (l *ListInfo) ToGeoSite(excludeAttrs map[fileName]map[attribute]bool) {
geosite := new(router.GeoSite)
geosite.CountryCode = string(l.Name)
geosite.Domain = append(geosite.Domain, l.FullTypeList...)
geosite.Domain = append(geosite.Domain, l.DomainTypeUniqueList...)
geosite.Domain = append(geosite.Domain, l.RegexpTypeList...)
for _, keywordRule := range l.KeywordTypeList {
if len(strings.TrimSpace(keywordRule.GetValue())) > 0 {
geosite.Domain = append(geosite.Domain, keywordRule)
}
}
if excludeAttrs != nil && excludeAttrs[l.Name] != nil {
excludeAttrsMap := excludeAttrs[l.Name]
for _, domain := range l.AttributeRuleUniqueList {
ifKeep := true
for _, attr := range domain.GetAttribute() {
if excludeAttrsMap[attribute(attr.GetKey())] {
ifKeep = false
break
}
}
if ifKeep {
geosite.Domain = append(geosite.Domain, domain)
}
}
} else {
geosite.Domain = append(geosite.Domain, l.AttributeRuleUniqueList...)
}
l.GeoSite = geosite
}
// ToPlainText convert router.GeoSite structure to plaintext format.
func (l *ListInfo) ToPlainText() []byte {
plaintextBytes := make([]byte, 0, 1024*512)
for _, rule := range l.GeoSite.Domain {
ruleVal := strings.TrimSpace(rule.GetValue())
if len(ruleVal) == 0 {
continue
}
var ruleString string
switch rule.Type {
case router.Domain_Full:
ruleString = "full:" + ruleVal
case router.Domain_RootDomain:
ruleString = "domain:" + ruleVal
case router.Domain_Plain:
ruleString = "keyword:" + ruleVal
case router.Domain_Regex:
ruleString = "regexp:" + ruleVal
}
if len(rule.Attribute) > 0 {
ruleString += ":"
for _, attr := range rule.Attribute {
ruleString += "@" + attr.GetKey() + ","
}
ruleString = strings.TrimRight(ruleString, ",")
}
// Output format is: type:domain.tld:@attr1,@attr2
plaintextBytes = append(plaintextBytes, []byte(ruleString+"\n")...)
}
return plaintextBytes
}
// ToGFWList converts router.GeoSite to GFWList format.
func (l *ListInfo) ToGFWList() []byte {
loc, _ := time.LoadLocation("Asia/Shanghai")
timeString := fmt.Sprintf("! Last Modified: %s\n", time.Now().In(loc).Format(time.RFC1123))
gfwlistBytes := make([]byte, 0, 1024*512)
gfwlistBytes = append(gfwlistBytes, []byte("[AutoProxy 0.2.9]\n")...)
gfwlistBytes = append(gfwlistBytes, []byte(timeString)...)
gfwlistBytes = append(gfwlistBytes, []byte("! Expires: 24h\n")...)
gfwlistBytes = append(gfwlistBytes, []byte("! HomePage: https://github.com/Loyalsoldier/domain-list-custom\n")...)
gfwlistBytes = append(gfwlistBytes, []byte("! GitHub URL: https://raw.githubusercontent.com/Loyalsoldier/domain-list-custom/release/gfwlist.txt\n")...)
gfwlistBytes = append(gfwlistBytes, []byte("! jsdelivr URL: https://cdn.jsdelivr.net/gh/Loyalsoldier/domain-list-custom@release/gfwlist.txt\n")...)
gfwlistBytes = append(gfwlistBytes, []byte("\n")...)
for _, rule := range l.GeoSite.Domain {
ruleVal := strings.TrimSpace(rule.GetValue())
if len(ruleVal) == 0 {
continue
}
switch rule.Type {
case router.Domain_Full:
gfwlistBytes = append(gfwlistBytes, []byte("|http://"+ruleVal+"\n")...)
gfwlistBytes = append(gfwlistBytes, []byte("|https://"+ruleVal+"\n")...)
case router.Domain_RootDomain:
gfwlistBytes = append(gfwlistBytes, []byte("||"+ruleVal+"\n")...)
case router.Domain_Plain:
gfwlistBytes = append(gfwlistBytes, []byte(ruleVal+"\n")...)
case router.Domain_Regex:
gfwlistBytes = append(gfwlistBytes, []byte("/"+ruleVal+"/\n")...)
}
}
return gfwlistBytes
}