forked from XTLS/libXray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geo_cut.go
146 lines (129 loc) · 3.32 KB
/
geo_cut.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
package libXray
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"github.com/xtls/libxray/nodep"
"github.com/xtls/xray-core/app/router"
"google.golang.org/protobuf/proto"
)
type geoCutCode struct {
Dat []geoCutCodeDat `json:"dat,omitempty"`
}
// Type must be the value of geoType
type geoCutCodeDat struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
UrlMd5 string `json:"urlMd5,omitempty"`
Codes []string `json:"codes,omitempty"`
}
// Read geo data and cut the codes we need.
// datDir means the dir which geo dat are in.
// dstDir means the dir which new geo dat are in.
// cutCodePath means geoCutCode json file path
//
// This function is used to reduce memory when init instance.
// You can cut the country codes which rules and nameservers contain.
func CutGeoData(datDir string, dstDir string, cutCodePath string) string {
codeBytes, err := os.ReadFile(cutCodePath)
if err != nil {
return err.Error()
}
var code geoCutCode
if err := json.Unmarshal(codeBytes, &code); err != nil {
return err.Error()
}
for _, dat := range code.Dat {
switch dat.Type {
case geoTypeDomain:
if err := cutGeoSite(datDir, dstDir, dat); err != nil {
return err.Error()
}
case geoTypeIP:
if err := cutGeoIP(datDir, dstDir, dat); err != nil {
return err.Error()
}
default:
return fmt.Errorf("wrong geoType: %s", dat.Type).Error()
}
}
return ""
}
func cutGeoSite(datDir string, dstDir string, dat geoCutCodeDat) error {
srcName := dat.UrlMd5 + ".dat"
dstName := dat.Name + ".dat"
srcPath := path.Join(datDir, srcName)
dstPath := path.Join(dstDir, dstName)
geositeBytes, err := os.ReadFile(srcPath)
if err != nil {
return err
}
var geositeList router.GeoSiteList
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
return err
}
var newEntry []*router.GeoSite
for _, site := range geositeList.Entry {
if containsCountryCode(dat.Codes, site.CountryCode) {
newEntry = append(newEntry, site)
}
}
var newGeositeList router.GeoSiteList
newGeositeList.Entry = newEntry
newDatBytes, err := proto.Marshal(&newGeositeList)
if err != nil {
return err
}
if err := nodep.WriteBytes(newDatBytes, dstPath); err != nil {
return err
}
return nil
}
func cutGeoIP(datDir string, dstDir string, dat geoCutCodeDat) error {
srcName := dat.UrlMd5 + ".dat"
dstName := dat.Name + ".dat"
srcPath := path.Join(datDir, srcName)
dstPath := path.Join(dstDir, dstName)
geoipBytes, err := os.ReadFile(srcPath)
if err != nil {
return err
}
var geoipList router.GeoIPList
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
return err
}
var newEntry []*router.GeoIP
for _, ip := range geoipList.Entry {
if containsCountryCode(dat.Codes, ip.CountryCode) {
newEntry = append(newEntry, ip)
}
}
var newGeoipList router.GeoIPList
newGeoipList.Entry = newEntry
newDatBytes, err := proto.Marshal(&newGeoipList)
if err != nil {
return err
}
if err := nodep.WriteBytes(newDatBytes, dstPath); err != nil {
return err
}
return nil
}
func containsCountryCode(slice []string, element string) bool {
for _, code := range slice {
e := strings.ToUpper(code)
if strings.Contains(e, "@") {
codes := strings.Split(e, "@")
if codes[0] == element {
return true
}
} else {
if e == element {
return true
}
}
}
return false
}