-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathxray_wrapper.go
More file actions
217 lines (194 loc) · 6.05 KB
/
xray_wrapper.go
File metadata and controls
217 lines (194 loc) · 6.05 KB
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
// libXray is an Xray wrapper focusing on improving the experience of Xray-core mobile development.
package libXray
import (
"encoding/base64"
"encoding/json"
"github.com/xtls/libxray/geo"
"github.com/xtls/libxray/nodep"
"github.com/xtls/libxray/xray"
)
type CountGeoDataRequest struct {
DatDir string `json:"datDir,omitempty"`
Name string `json:"name,omitempty"`
GeoType string `json:"geoType,omitempty"`
}
// Read geo data and write all codes to text file.
func CountGeoData(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request CountGeoDataRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = geo.CountGeoData(request.DatDir, request.Name, request.GeoType)
return response.EncodeToBase64("", err)
}
type readGeoFilesResponse struct {
Domain []string `json:"domain,omitempty"`
IP []string `json:"ip,omitempty"`
}
// thin geo data
func ReadGeoFiles(base64Text string) string {
var response nodep.CallResponse[*readGeoFilesResponse]
xray, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64(nil, err)
}
domain, ip := geo.ReadGeoFiles(xray)
var resp readGeoFilesResponse
resp.Domain = domain
resp.IP = ip
return response.EncodeToBase64(&resp, nil)
}
type pingRequest struct {
DatDir string `json:"datDir,omitempty"`
ConfigPath string `json:"configPath,omitempty"`
Timeout int `json:"timeout,omitempty"`
Url string `json:"url,omitempty"`
Proxy string `json:"proxy,omitempty"`
}
// Ping Xray config and get the delay of its outbound.
func Ping(base64Text string) string {
var response nodep.CallResponse[int64]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64(nodep.PingDelayError, err)
}
var request pingRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64(nodep.PingDelayError, err)
}
delay, err := xray.Ping(request.DatDir, request.ConfigPath, request.Timeout, request.Url, request.Proxy)
return response.EncodeToBase64(delay, err)
}
// query inbound and outbound stats.
func QueryStats(base64Text string) string {
var response nodep.CallResponse[string]
server, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
stats, err := xray.QueryStats(string(server))
if err != nil {
return response.EncodeToBase64("", err)
}
return response.EncodeToBase64(stats, nil)
}
// Test Xray Config.
func TestXray(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request RunXrayRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = xray.TestXray(request.DatDir, request.ConfigPath)
return response.EncodeToBase64("", err)
}
type RunXrayRequest struct {
DatDir string `json:"datDir,omitempty"`
MphCachePath string `json:"mphCachePath,omitempty"`
ConfigPath string `json:"configPath,omitempty"`
}
type RunXrayFromJSONRequest struct {
DatDir string `json:"datDir,omitempty"`
MphCachePath string `json:"mphCachePath,omitempty"`
ConfigJSON string `json:"configJSON,omitempty"`
}
// Create Xray Run Request
func NewXrayRunRequest(datDir, mphCachePath, configPath string) (string, error) {
request := RunXrayRequest{
DatDir: datDir,
MphCachePath: mphCachePath,
ConfigPath: configPath,
}
requestBytes, err := json.Marshal(&request)
if err != nil {
return "", err
}
// Encode the JSON bytes to a base64 string
return base64.StdEncoding.EncodeToString(requestBytes), nil
}
// Create Xray Run From JSON Request
func NewXrayRunFromJSONRequest(datDir, mphCachePath, configJSON string) (string, error) {
request := RunXrayFromJSONRequest{
DatDir: datDir,
MphCachePath: mphCachePath,
ConfigJSON: configJSON,
}
requestBytes, err := json.Marshal(&request)
if err != nil {
return "", err
}
// Encode the JSON bytes to a base64 string
return base64.StdEncoding.EncodeToString(requestBytes), nil
}
// Run Xray instance.
func RunXray(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request RunXrayRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = xray.RunXray(request.DatDir, request.MphCachePath, request.ConfigPath)
return response.EncodeToBase64("", err)
}
// Run Xray instance with JSON configuration.
func RunXrayFromJSON(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request RunXrayFromJSONRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = xray.RunXrayFromJSON(request.DatDir, request.MphCachePath, request.ConfigJSON)
return response.EncodeToBase64("", err)
}
// Get Xray State
func GetXrayState() bool {
return xray.GetXrayState()
}
// Stop Xray instance.
func StopXray() string {
var response nodep.CallResponse[string]
err := xray.StopXray()
return response.EncodeToBase64("", err)
}
// Xray's version
func XrayVersion() string {
var response nodep.CallResponse[string]
return response.EncodeToBase64(xray.XrayVersion(), nil)
}
// Build Mph Cache
func BuildMphCache(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request RunXrayRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = xray.BuildMphCache(request.DatDir, request.MphCachePath, request.ConfigPath)
return response.EncodeToBase64("", err)
}