-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathlibv2ray_main.go
More file actions
347 lines (299 loc) · 9.58 KB
/
libv2ray_main.go
File metadata and controls
347 lines (299 loc) · 9.58 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package libv2ray
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
coreapplog "github.com/xtls/xray-core/app/log"
corecommlog "github.com/xtls/xray-core/common/log"
corenet "github.com/xtls/xray-core/common/net"
corefilesystem "github.com/xtls/xray-core/common/platform/filesystem"
"github.com/xtls/xray-core/common/serial"
core "github.com/xtls/xray-core/core"
corestats "github.com/xtls/xray-core/features/stats"
coreserial "github.com/xtls/xray-core/infra/conf/serial"
_ "github.com/xtls/xray-core/main/distro/all"
mobasset "golang.org/x/mobile/asset"
)
// Constants for environment variables
const (
coreAsset = "xray.location.asset"
coreCert = "xray.location.cert"
xudpBaseKey = "xray.xudp.basekey"
tunFdKey = "xray.tun.fd"
)
// CoreController represents a controller for managing Xray core instance lifecycle
type CoreController struct {
CallbackHandler CoreCallbackHandler
statsManager corestats.Manager
coreMutex sync.Mutex
coreInstance *core.Instance
IsRunning bool
}
// CoreCallbackHandler defines interface for receiving callbacks and notifications from the core service
type CoreCallbackHandler interface {
Startup() int
Shutdown() int
OnEmitStatus(int, string) int
}
// consoleLogWriter implements a log writer without datetime stamps
// as Android system already adds timestamps to each log line
type consoleLogWriter struct {
logger *log.Logger // Standard logger
}
// setEnvVariable safely sets an environment variable and logs any errors encountered.
func setEnvVariable(key, value string) {
if err := os.Setenv(key, value); err != nil {
log.Printf("Failed to set environment variable %s: %v. Please check your configuration.", key, err)
}
}
// InitCoreEnv initializes environment variables and file system handlers for the core
// It sets up asset path, certificate path, XUDP base key and customizes the file reader
// to support Android asset system
func InitCoreEnv(envPath string, key string) {
// Set asset/cert paths
if len(envPath) > 0 {
setEnvVariable(coreAsset, envPath)
setEnvVariable(coreCert, envPath)
}
// Set XUDP encryption key
if len(key) > 0 {
setEnvVariable(xudpBaseKey, key)
}
// Custom file reader with path validation
corefilesystem.NewFileReader = func(path string) (io.ReadCloser, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
_, file := filepath.Split(path)
return mobasset.Open(file)
}
return os.Open(path)
}
}
// NewCoreController initializes and returns a new CoreController instance
// Sets up the console log handler and associates it with the provided callback handler
func NewCoreController(s CoreCallbackHandler) *CoreController {
// Register custom logger
if err := coreapplog.RegisterHandlerCreator(
coreapplog.LogType_Console,
func(lt coreapplog.LogType, options coreapplog.HandlerCreatorOptions) (corecommlog.Handler, error) {
return corecommlog.NewLogger(createStdoutLogWriter()), nil
},
); err != nil {
log.Printf("Failed to register log handler: %v", err)
}
return &CoreController{
CallbackHandler: s,
}
}
// StartLoop initializes and starts the core processing loop
// Thread-safe method that configures and runs the Xray core with the provided configuration
// Returns immediately if the core is already running
func (x *CoreController) StartLoop(configContent string, tunFd int32) (err error) {
// Set TUN fd key, 0 means do not use TUN
setEnvVariable(tunFdKey, strconv.Itoa(int(tunFd)))
x.coreMutex.Lock()
defer x.coreMutex.Unlock()
if x.IsRunning {
log.Println("Core is already running")
return nil
}
return x.doStartLoop(configContent)
}
// StopLoop safely stops the core processing loop and releases resources
// Thread-safe method that shuts down the core instance and triggers necessary callbacks
func (x *CoreController) StopLoop() error {
x.coreMutex.Lock()
defer x.coreMutex.Unlock()
if x.IsRunning {
x.doShutdown()
x.CallbackHandler.OnEmitStatus(0, "Core stopped")
}
return nil
}
// QueryStats retrieves and resets traffic statistics for a specific outbound tag and direction
// Returns the accumulated traffic value and resets the counter to zero
// Returns 0 if the stats manager is not initialized or the counter doesn't exist
func (x *CoreController) QueryStats(tag string, direct string) int64 {
if x.statsManager == nil {
return 0
}
counter := x.statsManager.GetCounter(fmt.Sprintf("outbound>>>%s>>>traffic>>>%s", tag, direct))
if counter == nil {
return 0
}
return counter.Set(0)
}
// MeasureDelay measures network latency to a specified URL through the current core instance
// Uses a 12-second timeout context and returns the round-trip time in milliseconds
// An error is returned if the connection fails or returns an unexpected status
func (x *CoreController) MeasureDelay(url string) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
defer cancel()
return measureInstDelay(ctx, x.coreInstance, url)
}
// MeasureOutboundDelay measures the outbound delay for a given configuration and URL
func MeasureOutboundDelay(ConfigureFileContent string, url string) (int64, error) {
config, err := coreserial.LoadJSONConfig(strings.NewReader(ConfigureFileContent))
if err != nil {
return -1, fmt.Errorf("config load error: %w", err)
}
// Simplify config for testing
config.Inbound = nil
var essentialApp []*serial.TypedMessage
for _, app := range config.App {
if app.Type == "xray.app.proxyman.OutboundConfig" ||
app.Type == "xray.app.dispatcher.Config" ||
app.Type == "xray.app.log.Config" {
essentialApp = append(essentialApp, app)
}
}
config.App = essentialApp
inst, err := core.New(config)
if err != nil {
return -1, fmt.Errorf("instance creation failed: %w", err)
}
if err := inst.Start(); err != nil {
return -1, fmt.Errorf("startup failed: %w", err)
}
defer inst.Close()
return measureInstDelay(context.Background(), inst, url)
}
// CheckVersionX returns the library and Xray versions
func CheckVersionX() string {
var version = 35
return fmt.Sprintf("Lib v%d, Xray-core v%s", version, core.Version())
}
// doShutdown shuts down the Xray instance and cleans up resources
func (x *CoreController) doShutdown() {
if x.coreInstance != nil {
if err := x.coreInstance.Close(); err != nil {
log.Printf("core shutdown error: %v", err)
}
x.coreInstance = nil
}
x.IsRunning = false
x.statsManager = nil
}
// doStartLoop sets up and starts the Xray core
func (x *CoreController) doStartLoop(configContent string) error {
log.Println("initializing core...")
config, err := coreserial.LoadJSONConfig(strings.NewReader(configContent))
if err != nil {
return fmt.Errorf("config error: %w", err)
}
x.coreInstance, err = core.New(config)
if err != nil {
return fmt.Errorf("core init failed: %w", err)
}
x.statsManager = x.coreInstance.GetFeature(corestats.ManagerType()).(corestats.Manager)
log.Println("starting core...")
x.IsRunning = true
if err := x.coreInstance.Start(); err != nil {
x.IsRunning = false
return fmt.Errorf("startup failed: %w", err)
}
x.CallbackHandler.Startup()
x.CallbackHandler.OnEmitStatus(0, "Started successfully, running")
log.Println("Starting core successfully")
return nil
}
// measureInstDelay measures the delay for an instance to a given URL
func measureInstDelay(ctx context.Context, inst *core.Instance, url string) (int64, error) {
if inst == nil {
return -1, errors.New("core instance is nil")
}
tr := &http.Transport{
TLSHandshakeTimeout: 6 * time.Second,
DisableKeepAlives: false,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dest, err := corenet.ParseDestination(fmt.Sprintf("%s:%s", network, addr))
if err != nil {
return nil, err
}
return core.Dial(ctx, inst, dest)
},
}
client := &http.Client{
Transport: tr,
Timeout: 12 * time.Second,
}
if url == "" {
url = "https://www.google.com/generate_204"
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return -1, fmt.Errorf("failed to create HTTP request: %w", err)
}
var minDuration int64 = -1
success := false
var lastErr error
// Add exception handling and increase retry attempts
const attempts = 2
for i := 0; i < attempts; i++ {
select {
case <-ctx.Done():
// Return immediately when context is canceled
if !success {
return -1, ctx.Err()
}
return minDuration, nil
default:
// Continue execution
}
start := time.Now()
resp, err := client.Do(req)
if err != nil {
lastErr = err
continue
}
// Ensure response body is closed
defer func(resp *http.Response) {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}(resp)
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
lastErr = fmt.Errorf("invalid status: %s", resp.Status)
continue
}
// Handle possible errors when reading response body
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
lastErr = fmt.Errorf("failed to read response body: %w", err)
continue
}
duration := time.Since(start).Milliseconds()
if !success || duration < minDuration {
minDuration = duration
}
success = true
}
if !success {
return -1, lastErr
}
return minDuration, nil
}
// Log writer implementation
func (w *consoleLogWriter) Write(s string) error {
w.logger.Print(s)
return nil
}
func (w *consoleLogWriter) Close() error {
return nil
}
// createStdoutLogWriter creates a logger that won't print date/time stamps
func createStdoutLogWriter() corecommlog.WriterCreator {
return func() corecommlog.Writer {
return &consoleLogWriter{
logger: log.New(os.Stdout, "", 0),
}
}
}