From 20554587e583a960456026163d8929603da94201 Mon Sep 17 00:00:00 2001 From: Keivan-sf Date: Sun, 27 Jul 2025 15:56:47 +0330 Subject: [PATCH 1/4] Add remarks to raw data --- src/config_models/mod.rs | 1 + src/parser/vless/data.rs | 6 +++--- src/parser/vmess/data.rs | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/config_models/mod.rs b/src/config_models/mod.rs index d277953..bea63b6 100644 --- a/src/config_models/mod.rs +++ b/src/config_models/mod.rs @@ -186,6 +186,7 @@ pub struct Config { #[derive(Serialize, Deserialize)] #[allow(non_snake_case)] pub struct RawData { + pub remarks: String, pub security: Option, pub vnext_security: Option, pub sni: Option, diff --git a/src/parser/vless/data.rs b/src/parser/vless/data.rs index 42b01ee..a1c3fa3 100644 --- a/src/parser/vless/data.rs +++ b/src/parser/vless/data.rs @@ -6,14 +6,14 @@ use http::Uri; pub fn get_data(uri: &str) -> RawData { let data = uri.split_once("vless://").unwrap().1; let query_and_name = uri.split_once("?").unwrap().1; - let raw_query = query_and_name + let (raw_query, name) = query_and_name .split_once("#") - .unwrap_or((query_and_name, "")) - .0; + .unwrap_or((query_and_name, "")); let parsed_address = parse_vless_address(data.split_once("?").unwrap().0); let query: Vec<(&str, &str)> = querystring::querify(raw_query); return RawData { + remarks: String::from(name), uuid: Some(parsed_address.uuid), port: Some(parsed_address.port), address: Some(parsed_address.address), diff --git a/src/parser/vmess/data.rs b/src/parser/vmess/data.rs index 1877f14..ef68feb 100644 --- a/src/parser/vmess/data.rs +++ b/src/parser/vmess/data.rs @@ -19,6 +19,7 @@ fn get_raw_data_from_base64(decoded_base64: &Vec) -> RawData { let json = serde_json::from_str::(json_str).unwrap(); return RawData { + remarks: get_str_field(&json, "id").unwrap_or(String::from("")), uuid: get_str_field(&json, "id"), port: get_str_field(&json, "port") .and_then(|s| Some(s.parse::().expect("port is not a number"))), @@ -65,14 +66,14 @@ fn get_raw_data_from_uri(uri: &str) -> RawData { let data = uri.split_once("vmess://").unwrap().1; let query_and_name = uri.split_once("?").unwrap().1; - let raw_query = query_and_name + let (raw_query, name) = query_and_name .split_once("#") - .unwrap_or((query_and_name, "")) - .0; + .unwrap_or((query_and_name, "")); let parsed_address = parse_vmess_address(data.split_once("?").unwrap().0); let query: Vec<(&str, &str)> = querystring::querify(raw_query); return RawData { + remarks: String::from(name), uuid: Some(parsed_address.uuid), port: Some(parsed_address.port), address: Some(parsed_address.address), From bc5306ca87014f9055d39f3e121fa7b0e0c7f3c2 Mon Sep 17 00:00:00 2001 From: Keivan-sf Date: Sun, 27 Jul 2025 16:14:45 +0330 Subject: [PATCH 2/4] Fix vmess wrong name --- src/parser/vmess/data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/vmess/data.rs b/src/parser/vmess/data.rs index ef68feb..4456297 100644 --- a/src/parser/vmess/data.rs +++ b/src/parser/vmess/data.rs @@ -19,7 +19,7 @@ fn get_raw_data_from_base64(decoded_base64: &Vec) -> RawData { let json = serde_json::from_str::(json_str).unwrap(); return RawData { - remarks: get_str_field(&json, "id").unwrap_or(String::from("")), + remarks: get_str_field(&json, "ps").unwrap_or(String::from("")), uuid: get_str_field(&json, "id"), port: get_str_field(&json, "port") .and_then(|s| Some(s.parse::().expect("port is not a number"))), From f282a1ae4b428cd42339dbb977d98860775fa8dd Mon Sep 17 00:00:00 2001 From: Keivan-sf Date: Sun, 27 Jul 2025 16:15:04 +0330 Subject: [PATCH 3/4] Add `--get-name` flag --- src/main.rs | 6 ++++++ src/parser/mod.rs | 47 ++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index e4ae9c7..f545a5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,16 @@ struct Cli { socksport: Option, #[arg(long, value_name = "httpport")] httpport: Option, + #[arg(long, action = clap::ArgAction::SetTrue, value_name = "get-name")] + get_name: Option, } fn main() { let cli = Cli::parse(); + if cli.get_name == Some(true) { + print!("{}", parser::get_name(&cli.uri)); + return; + } let json_config = parser::create_json_config(&cli.uri, cli.socksport, cli.httpport); println!("{}", json_config); } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 6675b37..057206f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9,6 +9,11 @@ mod uri_identifier; mod vless; mod vmess; +pub fn get_name(uri: &str) -> String { + let (_, data, _) = get_uri_data(uri); + return data.remarks; +} + pub fn create_json_config(uri: &str, socks_port: Option, http_port: Option) -> String { let config = create_config(uri, socks_port, http_port); let serialized = serde_json::to_string(&config).unwrap(); @@ -34,25 +39,7 @@ pub fn create_config( } pub fn create_outbound_object(uri: &str) -> config_models::Outbound { - let protocol = uri_identifier::get_uri_protocol(uri); - let (name, data, outbound_settings): (String, RawData, OutboundSettings) = match protocol { - Some(uri_identifier::Protocols::Vless) => { - let d = vless::data::get_data(uri); - let s = vless::create_outbound_settings(&d); - (String::from("vless"), d, s) - } - Some(uri_identifier::Protocols::Vmess) => { - let d = vmess::data::get_data(uri); - let s = vmess::create_outbound_settings(&d); - (String::from("vmess"), d, s) - } - Some(_) => { - panic!("The protocol was recognized but is not supported yet"); - } - None => { - panic!("The protocol is not supported"); - } - }; + let (name, data, outbound_settings) = get_uri_data(uri); let network_type = data.r#type.clone().unwrap_or(String::from("")); let allow_insecure = data.allowInsecure == Some(String::from("true")) @@ -161,3 +148,25 @@ pub fn create_outbound_object(uri: &str) -> config_models::Outbound { return outbound; } + +fn get_uri_data(uri: &str) -> (String, RawData, OutboundSettings) { + let protocol = uri_identifier::get_uri_protocol(uri); + return match protocol { + Some(uri_identifier::Protocols::Vless) => { + let d = vless::data::get_data(uri); + let s = vless::create_outbound_settings(&d); + (String::from("vless"), d, s) + } + Some(uri_identifier::Protocols::Vmess) => { + let d = vmess::data::get_data(uri); + let s = vmess::create_outbound_settings(&d); + (String::from("vmess"), d, s) + } + Some(_) => { + panic!("The protocol was recognized but is not supported yet"); + } + None => { + panic!("The protocol is not supported"); + } + }; +} From 657ec8af9901c6af4486721a4877874972fa9fd9 Mon Sep 17 00:00:00 2001 From: Keivan-sf Date: Sun, 27 Jul 2025 16:27:11 +0330 Subject: [PATCH 4/4] Improve command line interface --- README.md | 15 +++++++------- src/main.rs | 60 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 809eab1..c5afe1b 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,17 @@ V2ray URI parser for xray core Currently supports: `vless` ``` -V2ray URI parser +Parses V2ray URI and generates JSON config for xray -Usage: v2parser [OPTIONS] +Usage: v2parser [OPTIONS] Arguments: - + V2ray URI to parse Options: - --socksport - --httpport - -h, --help Print help - -V, --version Print version + --socksport Optional SOCKS proxy port for inbound + --httpport Optional HTTP proxy port for inbound + --get-name Only print the config name + -h, --help Print help + -V, --version Print version ``` diff --git a/src/main.rs b/src/main.rs index f545a5e..4028e3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,50 @@ -mod parser; -use clap::Parser; +use clap::{value_parser, Arg, Command}; pub mod config_models; +mod parser; pub mod utils; -#[derive(Parser)] -#[command(author ,version = "0.1.1", about = "V2ray URI parser", long_about = None)] -struct Cli { - uri: String, - #[arg(long, value_name = "socksport")] - socksport: Option, - #[arg(long, value_name = "httpport")] - httpport: Option, - #[arg(long, action = clap::ArgAction::SetTrue, value_name = "get-name")] - get_name: Option, -} - fn main() { - let cli = Cli::parse(); - if cli.get_name == Some(true) { - print!("{}", parser::get_name(&cli.uri)); + let matches = Command::new("v2ray-uri-parser") + .version("0.1.1") + .about("Parses V2ray URI and generates JSON config for xray") + .arg( + Arg::new("uri") + .help("V2ray URI to parse") + .required(true) + .index(1), + ) + .arg( + Arg::new("socksport") + .long("socksport") + .help("Optional SOCKS proxy port for inbound") + .value_name("PORT") + .value_parser(value_parser!(u16)), + ) + .arg( + Arg::new("httpport") + .long("httpport") + .help("Optional HTTP proxy port for inbound") + .value_name("PORT") + .value_parser(value_parser!(u16)), + ) + .arg( + Arg::new("get_name") + .long("get-name") + .help("Only print the config name") + .action(clap::ArgAction::SetTrue), + ) + .get_matches(); + + let uri = matches.get_one::("uri").unwrap(); + let socksport = matches.get_one::("socksport").copied(); + let httpport = matches.get_one::("httpport").copied(); + let get_name = matches.get_flag("get_name"); + + if get_name { + print!("{}", parser::get_name(uri)); return; } - let json_config = parser::create_json_config(&cli.uri, cli.socksport, cli.httpport); + + let json_config = parser::create_json_config(uri, socksport, httpport); println!("{}", json_config); }