Added --run mode to run xray with a parsed config.

This commit is contained in:
Ultradesu
2025-11-27 17:33:52 +02:00
parent 9d525dfad8
commit 596369d732
4 changed files with 654 additions and 19 deletions

View File

@@ -2,8 +2,10 @@ use clap::{value_parser, Arg, Command};
pub mod config_models;
mod parser;
pub mod utils;
mod xray_runner;
fn main() {
#[tokio::main]
async fn main() {
let matches = Command::new("v2parser")
.version("0.3.1")
.about("Parses V2ray URI and generates JSON config for xray")
@@ -33,12 +35,26 @@ fn main() {
.help("Only print config meta data")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("run")
.long("run")
.help("Run xray-core with the generated config")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("xray_binary")
.long("xray-binary")
.help("Path to xray-core binary (default: xray from PATH)")
.value_name("PATH"),
)
.get_matches();
let uri = matches.get_one::<String>("uri").unwrap();
let socksport = matches.get_one::<u16>("socksport").copied();
let httpport = matches.get_one::<u16>("httpport").copied();
let get_metadata = matches.get_flag("get_metadata");
let run_mode = matches.get_flag("run");
let xray_binary = matches.get_one::<String>("xray_binary").map(|s| s.as_str()).unwrap_or("xray-core");
if get_metadata {
print!("{}", parser::get_metadata(uri));
@@ -46,5 +62,32 @@ fn main() {
}
let json_config = parser::create_json_config(uri, socksport, httpport);
println!("{}", json_config);
if run_mode {
// Run mode: start xray-core with the config
let mut runner = xray_runner::XrayRunner::new();
match runner.start(&json_config, xray_binary).await {
Ok(()) => {
println!("xray-core started successfully. Press Ctrl+C to stop.");
// Wait for shutdown signal
xray_runner::wait_for_shutdown_signal().await;
// Stop xray-core
if let Err(e) = runner.stop().await {
eprintln!("Error stopping xray-core: {}", e);
} else {
println!("xray-core stopped successfully.");
}
}
Err(e) => {
eprintln!("Failed to start xray-core: {}", e);
std::process::exit(1);
}
}
} else {
// Normal mode: just print the config
println!("{}", json_config);
}
}