-
Notifications
You must be signed in to change notification settings - Fork 60
/
xray_wrapper.go
138 lines (123 loc) · 3.86 KB
/
xray_wrapper.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
// 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/nodep"
"github.com/xtls/libxray/xray"
)
type loadGeoDataRequest 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 LoadGeoData(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request loadGeoDataRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = xray.LoadGeoData(request.DatDir, request.Name, request.GeoType)
return response.EncodeToBase64("", err)
}
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)
}
// convert text to uuid
func CustomUUID(base64Text string) string {
var response nodep.CallResponse[string]
text, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
uuid := xray.CustomUUID(string(text))
return response.EncodeToBase64(uuid, nil)
}
type testXrayRequest struct {
DatDir string `json:"datDir,omitempty"`
ConfigPath string `json:"configPath,omitempty"`
}
// 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 testXrayRequest
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"`
ConfigPath string `json:"configPath,omitempty"`
MaxMemory int64 `json:"maxMemory,omitempty"`
}
// 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.ConfigPath, request.MaxMemory)
return response.EncodeToBase64("", err)
}
// 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)
}