-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathxray_wrapper_test.go
232 lines (195 loc) · 6.33 KB
/
xray_wrapper_test.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package libXray
import (
"encoding/base64"
"encoding/json"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
// writeConfigToFile writes the configuration to a file
func writeConfigToFile(config map[string]interface{}, path string) error {
// Ensure the directory exists
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
}
// Create the configuration file
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
// Write the configuration to the file
encoder := json.NewEncoder(file)
return encoder.Encode(config)
}
// decodeVmessConfig decodes and unmarshals the VMess configuration from a base64 string
func decodeVmessConfig(vmess string) (map[string]interface{}, error) {
// Decode the VMess configuration from base64
decodedVmess, err := base64.StdEncoding.DecodeString(vmess)
if err != nil {
return nil, err
}
// Unmarshal the decoded string into a map
var vmessConfig map[string]interface{}
err = json.Unmarshal(decodedVmess, &vmessConfig)
if err != nil {
return nil, err
}
return vmessConfig, nil
}
// createXrayConfig creates an Xray configuration map based on the VMess configuration
func createXrayConfig(vmessConfig map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
"log": map[string]interface{}{
"loglevel": "debug",
},
"inbounds": []map[string]interface{}{
{
"port": 1080,
"protocol": "socks",
"settings": map[string]interface{}{
"auth": "noauth",
},
},
},
"outbounds": []map[string]interface{}{
{
"protocol": "vmess",
"settings": map[string]interface{}{
"vnext": []map[string]interface{}{
{
"address": vmessConfig["add"],
"port": vmessConfig["port"],
"users": []map[string]interface{}{
{
"id": vmessConfig["id"],
"alterId": vmessConfig["aid"],
"security": vmessConfig["scy"],
},
},
},
},
},
},
},
}
}
// base64EncodeRequest encodes a request struct into a base64 string
func base64EncodeRequest(request interface{}) (string, error) {
// Marshal the request struct into JSON bytes
requestBytes, err := json.Marshal(request)
if err != nil {
return "", err
}
// Encode the JSON bytes to a base64 string
return base64.StdEncoding.EncodeToString(requestBytes), nil
}
// handleTestResponse decodes and checks the response from Xray
func handleTestResponse(response string, t *testing.T) {
// Decode the base64 response
decoded, err := base64.StdEncoding.DecodeString(response)
if err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
// Parse the decoded response into a map
var result map[string]interface{}
if err := json.Unmarshal(decoded, &result); err != nil {
t.Fatalf("Failed to parse response JSON: %v", err)
}
// Check if the "success" field is true
if success, ok := result["success"].(bool); !ok || !success {
t.Fatalf("TestXray failed: %v", response)
}
t.Log("TestXray passed successfully", string(decoded))
}
// TestRunXrayWithVmess tests running Xray with VMess configuration
func TestRunXrayWithVmess(t *testing.T) {
// Example VMess configuration (base64 encoded)
vmess := `xxx`
// Decode and parse the VMess configuration
vmessConfig, err := decodeVmessConfig(vmess)
if err != nil {
t.Fatalf("Failed to decode VMess: %v", err)
}
// Create an Xray configuration from the VMess configuration
xrayConfig := createXrayConfig(vmessConfig)
// Prepare the path for the configuration file
projectRoot, _ := filepath.Abs(".")
configPath := filepath.Join(projectRoot, "config", "xray_config_test.json")
// Write the Xray configuration to a file
err = writeConfigToFile(xrayConfig, configPath)
if err != nil {
t.Fatalf("Failed to write Xray config: %v", err)
}
// Create a request for testing Xray
datDir := filepath.Join(projectRoot, "dat")
request := TestXrayRequest{
DatDir: datDir,
ConfigPath: configPath,
}
// Encode the request to base64
base64Request, err := base64EncodeRequest(request)
if err != nil {
t.Fatalf("Failed to encode request: %v", err)
}
// Call TestXray with the base64-encoded request
response := TestXray(base64Request)
// Handle and check the response
handleTestResponse(response, t)
}
// TestRunXray tests running Xray with a VMess configuration for real-world usage
func TestRunXray(t *testing.T) {
// Example VMess configuration (same as in the previous test)
vmess := `xxx`
// Decode and parse the VMess configuration
vmessConfig, err := decodeVmessConfig(vmess)
if err != nil {
t.Fatalf("Failed to decode VMess: %v", err)
}
// Create an Xray configuration from the VMess configuration
xrayConfig := createXrayConfig(vmessConfig)
// Prepare the path for the configuration file
projectRoot, _ := filepath.Abs(".")
configPath := filepath.Join(projectRoot, "config", "xray_config_run.json")
// Write the Xray configuration to a file
err = writeConfigToFile(xrayConfig, configPath)
if err != nil {
t.Fatalf("Failed to write Xray config: %v", err)
}
// Create a request for running Xray
datDir := filepath.Join(projectRoot, "dat")
runRequest := RunXrayRequest{
DatDir: datDir,
ConfigPath: configPath,
MaxMemory: 1024, // Set max memory limit for Xray
}
// Encode the request to base64
base64Request, err := base64EncodeRequest(runRequest)
if err != nil {
t.Fatalf("Failed to encode request: %v", err)
}
// Call RunXray with the base64-encoded request
response := RunXray(base64Request)
// Handle and check the response
handleTestResponse(response, t)
}
// TestXrayVersion tests the XrayVersion function.
func TestXrayVersion(t *testing.T) {
// Call the XrayVersion function
version := XrayVersion()
// Decode the base64 response to get the version string
decodedVersion, err := base64.StdEncoding.DecodeString(version)
assert.NoError(t, err, "Failed to decode the version string")
// Check that the decoded version is not empty
assert.NotEmpty(t, decodedVersion, "Xray version should not be empty")
// Log the decoded version for visibility
t.Log("Xray Version: ", string(decodedVersion))
// Optionally, you could assert against the expected version if known
// expectedVersion := "some_expected_version_string"
// assert.Equal(t, expectedVersion, string(decodedVersion), "Xray version mismatch")
}