-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdomainlist.go
245 lines (230 loc) · 6.25 KB
/
domainlist.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
package main
import (
"bufio"
"github.com/pkg/errors"
router "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
"golang.org/x/text/encoding"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
type DomainList struct {
Name string
FullDomains []*router.Domain
RegexpDomains []*router.Domain
DomainDomains []*router.Domain
UniqueDomainDomains []*router.Domain
GeoSite *router.GeoSite
}
func (l *DomainList) push(rule *router.Domain) {
switch rule.Type {
case router.Domain_Full:
l.FullDomains = append(l.FullDomains, rule)
case router.Domain_RootDomain:
l.DomainDomains = append(l.DomainDomains, rule)
case router.Domain_Regex:
l.RegexpDomains = append(l.RegexpDomains, rule)
}
}
func (l *DomainList) parseDomainType(domain string) router.Domain_Type {
if strings.Contains(domain, "*") {
return router.Domain_Regex
}
scheme := regexp.MustCompile("^[a-zA-Z]+://")
if scheme.MatchString(domain) {
return router.Domain_Full
}
return router.Domain_RootDomain
}
func (l *DomainList) wildcardToRegex(domain string) (string, error) {
_, parts := splitAndTrim(strings.ReplaceAll(strings.ReplaceAll(domain, "https://", ""), "http://", ""), "/")
_, parts = splitAfterAndCount(parts[0], "*.")
var sb strings.Builder
for _, column := range parts {
if column == "*." {
sb.WriteString("(.*\\.)?")
continue
}
sb.WriteString(regexp.QuoteMeta(column))
}
return sb.String(), nil
}
func (l *DomainList) parseDomain(domain string) (*router.Domain, error) {
if isBlank(domain) {
return nil, nil
}
domainType := l.parseDomainType(domain)
var sb strings.Builder
switch domainType {
case router.Domain_Regex:
parsed, err := l.wildcardToRegex(domain)
if err != nil {
return nil, err
}
if isBlank(parsed) {
return nil, nil
}
sb.WriteString(parsed)
case router.Domain_Full:
sb.WriteString(domain)
case router.Domain_RootDomain:
sb.WriteString(domain)
default:
return nil, errors.New("unexpected domain type: " + domainType.String())
}
domain = sb.String()
var routerDomain router.Domain
routerDomain.Type = domainType
routerDomain.Value = domain
return &routerDomain, nil
}
func (l *DomainList) parseDomains(line string) ([]*router.Domain, error) {
if isBlank(line) {
return nil, nil
}
_, parts := splitAndTrim(line, "|")
var domains []*router.Domain
for _, domain := range parts {
routerDomain, err := l.parseDomain(domain)
if err != nil {
return nil, err
}
if routerDomain == nil {
continue
}
domains = append(domains, routerDomain)
}
return domains, nil
}
func (l *DomainList) parseRule(line string) ([]*router.Domain, error) {
if isBlank(line) {
return nil, errors.New("line is empty")
}
count, columns := splitAndTrim(line, ";")
if count <= 1 {
return nil, errors.New("line is missing delimiters")
}
var domains []*router.Domain
for index, column := range columns[:count-2] {
if index == 0 {
continue
}
parsed, err := l.parseDomains(column)
if err != nil {
return nil, err
}
domains = append(domains, parsed...)
}
return domains, nil
}
func (l *DomainList) Flatten() error {
sort.Slice(l.DomainDomains, func(i, j int) bool {
return len(strings.Split(l.DomainDomains[i].GetValue(), ".")) < len(strings.Split(l.DomainDomains[j].GetValue(), "."))
})
trie := NewDomainTrie()
for _, domain := range l.DomainDomains {
success, err := trie.Insert(domain.GetValue())
if err != nil {
return err
}
if success {
l.UniqueDomainDomains = append(l.UniqueDomainDomains, domain)
}
}
return nil
}
func (l *DomainList) ToGeoSites() *router.GeoSiteList {
domainList := new(router.GeoSiteList)
domain := new(router.GeoSite)
domain.CountryCode = l.Name
domain.Domain = append(domain.Domain, l.FullDomains...)
domain.Domain = append(domain.Domain, l.UniqueDomainDomains...)
domain.Domain = append(domain.Domain, l.RegexpDomains...)
domain.Domain = append(domain.Domain)
l.GeoSite = domain
domainList.Entry = append(domainList.Entry, l.GeoSite)
return domainList
}
func (l *DomainList) ToPlainText() []byte {
bytes := 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_Regex:
ruleString = "regexp:" + ruleVal
}
bytes = append(bytes, []byte(ruleString+"\n")...)
}
return bytes
}
func readAndParseLine(decoder *encoding.Decoder, scanner *bufio.Scanner, list *DomainList, lineNum int) (int, error) {
if err := scanner.Err(); err != nil {
return lineNum, err
}
if !scanner.Scan() {
return lineNum, nil
}
utf8, err := decoder.Bytes(scanner.Bytes())
if err != nil {
return lineNum, err
}
line := strings.TrimSpace(string(utf8))
if isEmpty(line) {
return readAndParseLine(decoder, scanner, list, lineNum+1)
}
parsedRules, err := list.parseRule(line)
if err != nil {
return lineNum, err
}
for _, rule := range parsedRules {
list.push(rule)
}
return readAndParseLine(decoder, scanner, list, lineNum+1)
}
func NewDomainList(name string) *DomainList {
return &DomainList{
Name: name,
FullDomains: make([]*router.Domain, 0, 10),
RegexpDomains: make([]*router.Domain, 0, 10),
DomainDomains: make([]*router.Domain, 0, 10),
UniqueDomainDomains: make([]*router.Domain, 0, 10),
}
}
func (l *DomainList) unmarshalCSV(decoder *encoding.Decoder, file *os.File) (int, error) {
scanner := bufio.NewScanner(file)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
if scanner.Scan() && scanner.Err() == nil {
if lineNum, err := readAndParseLine(decoder, scanner, l, 2); err != nil {
return lineNum, err
}
}
return 1, scanner.Err()
}
func Unmarshal(decoder *encoding.Decoder, listName string, path string) (*DomainList, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer func(file *os.File) {
if err := file.Close(); err != nil {
panic(err)
}
}(file)
list := NewDomainList(listName)
if lineNum, err := list.unmarshalCSV(decoder, file); err != nil {
ex := errors.Wrap(err, "could not parse rule at line number: "+strconv.Itoa(lineNum))
return nil, ex
}
return list, nil
}