-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (95 loc) · 3.16 KB
/
Copy pathmain.cpp
File metadata and controls
114 lines (95 loc) · 3.16 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
// Xray-core C++ implementation
// Main entry point
#include <xray/core/xray_core.hpp>
#include <xray/core/config.hpp>
#include <async_net/io/io_context.hpp>
#include <cstdio>
#include <cstring>
#include <csignal>
#include <atomic>
static std::atomic<bool> g_running{true};
static async_net::io_context* g_ctx = nullptr;
static xray::xray_core* g_core = nullptr;
static void signal_handler(int sig) {
printf("\nReceived signal %d, shutting down...\n", sig);
g_running.store(false);
// Don't close acceptors - just stop the event loop
// OS will clean up resources on exit
if (g_ctx) g_ctx->stop();
}
static void print_usage() {
printf("Xray - Advanced proxy platform (C++ implementation)\n\n");
printf("Usage: xray [options]\n\n");
printf("Options:\n");
printf(" -config <file> Configuration file path (required)\n");
printf(" -test Test configuration and exit\n");
printf(" -version Print version and exit\n");
printf(" -help Print this help message\n");
}
int main(int argc, char* argv[]) {
std::string config_path;
bool test_only = false;
// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-config") == 0 && i + 1 < argc) {
config_path = argv[++i];
} else if (strcmp(argv[i], "-test") == 0) {
test_only = true;
} else if (strcmp(argv[i], "-version") == 0) {
printf("Xray C++ 1.0.0\n");
return 0;
} else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
print_usage();
return 0;
}
}
if (config_path.empty()) {
// Default config path
config_path = "config.json";
}
// Load configuration
printf("Loading configuration from: %s\n", config_path.c_str());
xray::xray_config config;
try {
config = xray::xray_config::from_file(config_path);
} catch (const std::exception& e) {
fprintf(stderr, "Error loading config: %s\n", e.what());
return 1;
}
// Validate configuration
std::string error;
if (!config.validate(error)) {
fprintf(stderr, "Configuration error: %s\n", error.c_str());
return 1;
}
printf("Configuration loaded successfully\n");
printf(" Inbounds: %zu\n", config.inbounds.size());
printf(" Outbounds: %zu\n", config.outbounds.size());
printf(" Routing rules: %zu\n", config.routing.rules.size());
if (test_only) {
printf("Configuration test passed\n");
return 0;
}
// Setup signal handlers
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
// Create io_context and xray core
async_net::io_context ctx;
g_ctx = &ctx;
xray::xray_core core(ctx);
g_core = &core;
if (!core.init(config)) {
fprintf(stderr, "Failed to initialize Xray core\n");
return 1;
}
// Start core (non-blocking, starts inbound handlers)
core.start().resume();
printf("Xray started successfully\n");
// Run event loop
ctx.run();
// Cleanup
g_core = nullptr;
g_ctx = nullptr;
printf("Xray stopped\n");
return 0;
}