Skip to content

Commit d38a94b

Browse files
author
yiguo
committed
remove thinGeoData
1 parent 151ae70 commit d38a94b

File tree

4 files changed

+114
-259
lines changed

4 files changed

+114
-259
lines changed

build/template/main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ func CGoCountGeoData(base64Text *C.char) *C.char {
4444
return C.CString(CountGeoData(text))
4545
}
4646

47-
//export CGoThinGeoData
48-
func CGoThinGeoData(base64Text *C.char) *C.char {
49-
text := C.GoString(base64Text)
50-
return C.CString(ThinGeoData(text))
51-
}
52-
5347
//export CGoReadGeoFiles
5448
func CGoReadGeoFiles(base64Text *C.char) *C.char {
5549
text := C.GoString(base64Text)

geo/read.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package geo
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/xtls/xray-core/infra/conf"
9+
)
10+
311
// Read all geo files in config file.
412
// configPath means where xray config file is.
513
func ReadGeoFiles(xrayBytes []byte) ([]string, []string) {
@@ -18,3 +26,109 @@ func ReadGeoFiles(xrayBytes []byte) ([]string, []string) {
1826

1927
return domainFiles, ipFiles
2028
}
29+
30+
func loadXrayConfig(configBytes []byte) ([]string, []string) {
31+
domain := []string{}
32+
ip := []string{}
33+
34+
var xray *conf.Config
35+
err := json.Unmarshal(configBytes, &xray)
36+
if err != nil {
37+
return domain, ip
38+
}
39+
40+
routingDomain, routingIP := filterRouting(xray)
41+
domain = append(domain, routingDomain...)
42+
ip = append(ip, routingIP...)
43+
44+
dnsDomain, dnsIP := filterDns(xray)
45+
domain = append(domain, dnsDomain...)
46+
ip = append(ip, dnsIP...)
47+
48+
return domain, ip
49+
}
50+
51+
func filterRouting(xray *conf.Config) ([]string, []string) {
52+
domain := []string{}
53+
ip := []string{}
54+
55+
routing := xray.RouterConfig
56+
if routing == nil {
57+
return domain, ip
58+
}
59+
rules := routing.RuleList
60+
if len(rules) == 0 {
61+
return domain, ip
62+
}
63+
// parse rules
64+
// we only care about domain and ip
65+
type RawRule struct {
66+
Domain *conf.StringList `json:"domain"`
67+
IP *conf.StringList `json:"ip"`
68+
}
69+
70+
for _, rule := range rules {
71+
var rawRule RawRule
72+
err := json.Unmarshal(rule, &rawRule)
73+
if err != nil {
74+
continue
75+
}
76+
if rawRule.Domain != nil {
77+
domain = append(domain, *rawRule.Domain...)
78+
}
79+
if rawRule.IP != nil {
80+
ip = append(ip, *rawRule.IP...)
81+
}
82+
}
83+
return domain, ip
84+
}
85+
86+
func filterDns(xray *conf.Config) ([]string, []string) {
87+
domain := []string{}
88+
ip := []string{}
89+
90+
dns := xray.DNSConfig
91+
if dns == nil {
92+
return domain, ip
93+
}
94+
servers := dns.Servers
95+
if len(servers) == 0 {
96+
return domain, ip
97+
}
98+
99+
for _, server := range servers {
100+
if len(server.Domains) > 0 {
101+
domain = append(domain, server.Domains...)
102+
}
103+
if len(server.ExpectIPs) > 0 {
104+
ip = append(ip, server.ExpectIPs...)
105+
}
106+
}
107+
return domain, ip
108+
}
109+
110+
func filterAndStrip(rules []string, retain string) map[string][]string {
111+
m := make(map[string][]string)
112+
retainPrefix := fmt.Sprintf("%s:", retain)
113+
retainFile := fmt.Sprintf("%s.dat", retain)
114+
for _, rule := range rules {
115+
if strings.HasPrefix(rule, retainPrefix) {
116+
values := strings.SplitN(rule, ":", 2)
117+
appendMap(m, retainFile, values[1])
118+
} else if strings.HasPrefix(rule, "ext:") {
119+
values := strings.SplitN(rule, ":", 3)
120+
appendMap(m, values[1], values[2])
121+
}
122+
}
123+
return m
124+
}
125+
126+
func appendMap(m map[string][]string, key string, value string) {
127+
v, ok := m[key]
128+
if ok {
129+
v = append(v, value)
130+
} else {
131+
v = []string{value}
132+
}
133+
m[key] = v
134+
}

geo/thin.go

Lines changed: 0 additions & 231 deletions
This file was deleted.

xray_wrapper.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,6 @@ func CountGeoData(base64Text string) string {
3232
return response.EncodeToBase64("", err)
3333
}
3434

35-
type ThinGeoDataRequest struct {
36-
DatDir string `json:"datDir,omitempty"`
37-
ConfigPath string `json:"configPath,omitempty"`
38-
DstDir string `json:"dstDir,omitempty"`
39-
}
40-
41-
// thin geo data
42-
func ThinGeoData(base64Text string) string {
43-
var response nodep.CallResponse[string]
44-
req, err := base64.StdEncoding.DecodeString(base64Text)
45-
if err != nil {
46-
return response.EncodeToBase64("", err)
47-
}
48-
var request ThinGeoDataRequest
49-
err = json.Unmarshal(req, &request)
50-
if err != nil {
51-
return response.EncodeToBase64("", err)
52-
}
53-
err = geo.ThinGeoData(request.DatDir, request.ConfigPath, request.DstDir)
54-
return response.EncodeToBase64("", err)
55-
}
56-
5735
type readGeoFilesResponse struct {
5836
Domain []string `json:"domain,omitempty"`
5937
IP []string `json:"ip,omitempty"`

0 commit comments

Comments
 (0)