This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
geosite_parse.go
239 lines (214 loc) · 5.22 KB
/
geosite_parse.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
package netLayer
import (
"bufio"
"errors"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
//来自 v2fly, 有一定改动.
// GeositeRawList 用于序列化
type GeositeRawList struct {
Name string
Domains []GeositeDomain
}
func LoadGeositeFile(path string) (*GeositeRawList, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
list := &GeositeRawList{
Name: strings.ToUpper(filepath.Base(path)),
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
line = removeGeositeComment(line)
if len(line) == 0 {
continue
}
entry, err := parseGeositeEntry(line)
if err != nil {
return nil, err
}
list.Domains = append(list.Domains, entry)
}
return list, nil
}
func parseGeositeEntry(line string) (GeositeDomain, error) {
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
var entry GeositeDomain
if len(parts) == 0 {
return entry, errors.New("empty entry")
}
if err := parseGeositeDomain(parts[0], &entry); err != nil {
return entry, err
}
for i := 1; i < len(parts); i++ {
attr, err := parseGeositeAttribute(parts[i])
if err != nil {
return entry, err
}
entry.Attrs = append(entry.Attrs, attr)
}
return entry, nil
}
func parseGeositeAttribute(attr string) (attribute GeositeAttr, err error) {
if len(attr) == 0 || attr[0] != '@' {
err = errors.New("invalid attribute: " + attr)
return
}
attr = attr[1:]
parts := strings.Split(attr, "=")
if len(parts) == 1 {
attribute.Key = strings.ToLower(parts[0])
attribute.Value = true
} else {
attribute.Key = strings.ToLower(parts[0])
var intv int
intv, err = strconv.Atoi(parts[1])
if err != nil {
err = errors.New("invalid attribute: " + attr + ": " + err.Error())
return
}
attribute.Value = intv
}
return
}
func parseGeositeDomain(domain string, entry *GeositeDomain) error {
kv := strings.Split(domain, ":")
if len(kv) == 1 {
entry.Type = "domain"
entry.Value = strings.ToLower(kv[0])
return nil
}
if len(kv) == 2 {
entry.Type = strings.ToLower(kv[0])
entry.Value = strings.ToLower(kv[1])
return nil
}
return errors.New("Invalid format: " + domain)
}
func removeGeositeComment(line string) string {
idx := strings.Index(line, "#")
if idx == -1 {
return line
}
return strings.TrimSpace(line[:idx])
}
func isMatchGeositeAttr(Attrs []GeositeAttr, includeKey string) bool {
isMatch := false
mustMatch := true
matchName := includeKey
if strings.HasPrefix(includeKey, "!") {
isMatch = true
mustMatch = false
matchName = strings.TrimLeft(includeKey, "!")
}
for _, Attr := range Attrs {
attrName := Attr.Key
if mustMatch {
if matchName == attrName {
isMatch = true
break
}
} else {
if matchName == attrName {
isMatch = false
break
}
}
}
return isMatch
}
func createGeositeIncludeAttrEntrys(list *GeositeRawList, matchAttr GeositeAttr) []GeositeDomain {
newEntryList := make([]GeositeDomain, 0, len(list.Domains))
matchName := matchAttr.Key
for _, entry := range list.Domains {
matched := isMatchGeositeAttr(entry.Attrs, matchName)
if matched {
newEntryList = append(newEntryList, entry)
}
}
return newEntryList
}
//这里从v2fly改动了一些. 因为我发现 Inclusion最终不会被用到
func ParseGeositeList(list *GeositeRawList, ref map[string]*GeositeRawList) (*GeositeRawList, error) {
inclu := make(map[string]bool)
pl := &GeositeRawList{
Name: list.Name,
//Inclusion: make(map[string]bool),
}
entryList := list.Domains
for {
newEntryList := make([]GeositeDomain, 0, len(entryList))
hasInclude := false
for _, entry := range entryList {
if entry.Type == "include" {
refName := strings.ToUpper(entry.Value)
if entry.Attrs != nil {
for _, attr := range entry.Attrs {
InclusionName := strings.ToUpper(refName + "@" + attr.Key)
if inclu[InclusionName] {
continue
}
inclu[InclusionName] = true
refList := ref[refName]
if refList == nil {
return nil, errors.New(entry.Value + " not found.")
}
attrEntrys := createGeositeIncludeAttrEntrys(refList, attr)
if len(attrEntrys) != 0 {
newEntryList = append(newEntryList, attrEntrys...)
}
}
} else {
InclusionName := refName
if inclu[InclusionName] {
continue
}
inclu[InclusionName] = true
refList := ref[refName]
if refList == nil {
return nil, errors.New(entry.Value + " not found.")
}
newEntryList = append(newEntryList, refList.Domains...)
}
hasInclude = true
} else {
newEntryList = append(newEntryList, entry)
}
}
entryList = newEntryList
if !hasInclude {
break
}
}
pl.Domains = entryList
return pl, nil
}
func (grl *GeositeRawList) ToGeositeList() (gl *GeositeList) {
gl = new(GeositeList)
gl.Name = grl.Name
gl.Domains = make(map[string]GeositeDomain)
gl.FullDomains = make(map[string]GeositeDomain)
gl.RegexDomains = make([]*regexp.Regexp, 0)
for _, v := range grl.Domains {
switch v.Type {
case "domain":
gl.Domains[v.Value] = v
case "regexp":
reg, err := regexp.Compile(v.Value)
if err == nil {
gl.RegexDomains = append(gl.RegexDomains, reg)
}
case "full":
gl.FullDomains[v.Value] = v
}
}
return
}