forked from SagerNet/sing-geosite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
v2ray_rules_dat.go
275 lines (268 loc) · 7.36 KB
/
v2ray_rules_dat.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
package main
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/google/go-github/v45/github"
"github.com/sagernet/sing-box/common/geosite"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)
func parse(vGeositeData []byte) (map[string][]geosite.Item, error) {
vGeositeList := routercommon.GeoSiteList{}
err := proto.Unmarshal(vGeositeData, &vGeositeList)
if err != nil {
return nil, err
}
domainMap := make(map[string][]geosite.Item)
for _, vGeositeEntry := range vGeositeList.Entry {
code := strings.ToLower(vGeositeEntry.CountryCode)
domains := make([]geosite.Item, 0, len(vGeositeEntry.Domain)*2)
attributes := make(map[string][]*routercommon.Domain)
for _, domain := range vGeositeEntry.Domain {
if len(domain.Attribute) > 0 {
for _, attribute := range domain.Attribute {
attributes[attribute.Key] = append(attributes[attribute.Key], domain)
}
}
switch domain.Type {
case routercommon.Domain_Plain:
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomainKeyword,
Value: domain.Value,
})
case routercommon.Domain_Regex:
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomainRegex,
Value: domain.Value,
})
case routercommon.Domain_RootDomain:
if strings.Contains(domain.Value, ".") {
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomainSuffix,
Value: "." + domain.Value,
})
case routercommon.Domain_Full:
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
}
domainMap[code] = common.Uniq(domains)
for attribute, attributeEntries := range attributes {
attributeDomains := make([]geosite.Item, 0, len(attributeEntries)*2)
for _, domain := range attributeEntries {
switch domain.Type {
case routercommon.Domain_Plain:
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomainKeyword,
Value: domain.Value,
})
case routercommon.Domain_Regex:
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomainRegex,
Value: domain.Value,
})
case routercommon.Domain_RootDomain:
if strings.Contains(domain.Value, ".") {
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomainSuffix,
Value: "." + domain.Value,
})
case routercommon.Domain_Full:
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
}
domainMap[code+"@"+attribute] = common.Uniq(attributeDomains)
}
}
return domainMap, nil
}
type filteredCodePair struct {
code string
badCode string
}
func filterTags(data map[string][]geosite.Item) {
var codeList []string
for code := range data {
codeList = append(codeList, code)
}
var badCodeList []filteredCodePair
var filteredCodeMap []string
var mergedCodeMap []string
for _, code := range codeList {
codeParts := strings.Split(code, "@")
if len(codeParts) != 2 {
continue
}
leftParts := strings.Split(codeParts[0], "-")
var lastName string
if len(leftParts) > 1 {
lastName = leftParts[len(leftParts)-1]
}
if lastName == "" {
lastName = codeParts[0]
}
if lastName == codeParts[1] {
delete(data, code)
filteredCodeMap = append(filteredCodeMap, code)
continue
}
if "!"+lastName == codeParts[1] {
badCodeList = append(badCodeList, filteredCodePair{
code: codeParts[0],
badCode: code,
})
} else if lastName == "!"+codeParts[1] {
badCodeList = append(badCodeList, filteredCodePair{
code: codeParts[0],
badCode: code,
})
}
}
for _, it := range badCodeList {
badList := data[it.badCode]
if badList == nil {
panic("bad list not found: " + it.badCode)
}
delete(data, it.badCode)
newMap := make(map[geosite.Item]bool)
for _, item := range data[it.code] {
newMap[item] = true
}
for _, item := range badList {
delete(newMap, item)
}
newList := make([]geosite.Item, 0, len(newMap))
for item := range newMap {
newList = append(newList, item)
}
data[it.code] = newList
mergedCodeMap = append(mergedCodeMap, it.badCode)
}
sort.Strings(filteredCodeMap)
sort.Strings(mergedCodeMap)
log.Info("filtered " + strings.Join(filteredCodeMap, ","))
log.Info("merged " + strings.Join(mergedCodeMap, ","))
}
func mergeTags(data map[string][]geosite.Item) {
var codeList []string
for code := range data {
codeList = append(codeList, code)
}
var cnCodeList []string
for _, code := range codeList {
codeParts := strings.Split(code, "@")
if len(codeParts) != 2 {
continue
}
if codeParts[1] != "cn" {
continue
}
if !strings.HasPrefix(codeParts[0], "category-") {
continue
}
if strings.HasSuffix(codeParts[0], "-cn") || strings.HasSuffix(codeParts[0], "-!cn") {
continue
}
cnCodeList = append(cnCodeList, code)
}
newMap := make(map[geosite.Item]bool)
for _, item := range data["geolocation-cn"] {
newMap[item] = true
}
for _, code := range cnCodeList {
for _, item := range data[code] {
newMap[item] = true
}
}
newList := make([]geosite.Item, 0, len(newMap))
for item := range newMap {
newList = append(newList, item)
}
data["geolocation-cn"] = newList
println("merged cn categories: " + strings.Join(cnCodeList, ","))
}
func generateV2rayRulesDat(release *github.RepositoryRelease, output string, cnOutput string) error {
vData, err := download(release, "geosite.dat")
if err != nil {
return err
}
domainMap, err := parse(vData)
if err != nil {
return err
}
filterTags(domainMap)
mergeTags(domainMap)
outputPath, _ := filepath.Abs(output)
log.Info("write " + outputPath)
outputFile, err := os.Create(output)
if err != nil {
return err
}
defer outputFile.Close()
err = geosite.Write(outputFile, domainMap)
if err != nil {
return err
}
cnCodes := []string{
"geolocation-cn",
}
cnDomainMap := make(map[string][]geosite.Item)
for _, cnCode := range cnCodes {
cnDomainMap[cnCode] = domainMap[cnCode]
}
cnOutputFile, err := os.Create(cnOutput)
if err != nil {
return err
}
defer cnOutputFile.Close()
err = geosite.Write(cnOutputFile, cnDomainMap)
if err != nil {
return err
}
var eg errgroup.Group
for code, domains := range domainMap {
name := "geosite-" + code
var headlessRule option.DefaultHeadlessRule
defaultRule := geosite.Compile(domains)
headlessRule.Domain = defaultRule.Domain
headlessRule.DomainSuffix = defaultRule.DomainSuffix
headlessRule.DomainKeyword = defaultRule.DomainKeyword
headlessRule.DomainRegex = defaultRule.DomainRegex
var plainRuleSet option.PlainRuleSet
plainRuleSet.Rules = []option.HeadlessRule{
{
Type: C.RuleTypeDefault,
DefaultOptions: headlessRule,
},
}
eg.Go(func() error {
return generateSource(plainRuleSet, name)
})
eg.Go(func() error {
return generateBinary(plainRuleSet, name)
})
}
return eg.Wait()
}