forked from MetaCubeX/geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeosite.go
71 lines (65 loc) · 1.65 KB
/
geosite.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
// Package geosite implements functions for parsing and processing GeoSite resource files.
package geosite
import (
"regexp"
"strings"
"github.com/sagernet/sing/common/domain"
)
type Database struct {
matchers map[string]*domain.Matcher
domainKeyword map[string][]string
domainRegex map[string][]*regexp.Regexp
SourceType Type
CodeCount int
}
func (db Database) LookupCode(domainName string) string {
domainName = strings.ToLower(domainName)
for code, matcher := range db.matchers {
if matcher.Match(domainName) {
return strings.ToLower(code)
}
}
for code, keywords := range db.domainKeyword {
for _, keyword := range keywords {
if strings.Contains(domainName, keyword) {
return strings.ToLower(code)
}
}
}
for code, regexList := range db.domainRegex {
for _, re := range regexList {
if re.MatchString(domainName) {
return strings.ToLower(code)
}
}
}
return ""
}
func (db Database) LookupCodes(domainName string) []string {
results := map[string]struct{}{}
domainName = strings.ToLower(domainName)
for code, matcher := range db.matchers {
if matcher.Match(domainName) {
results[strings.ToLower(code)] = struct{}{}
}
}
for code, keywords := range db.domainKeyword {
for _, keyword := range keywords {
if strings.Contains(domainName, keyword) {
results[strings.ToLower(code)] = struct{}{}
}
}
}
for code, regexList := range db.domainRegex {
for _, re := range regexList {
if re.MatchString(domainName) {
results[strings.ToLower(code)] = struct{}{}
}
}
}
codes := make([]string, 0, len(results))
for code := range results {
codes = append(codes, code)
}
return codes
}