mirror of
https://github.com/house-of-vanity/v2-uri-parser.git
synced 2025-12-15 14:47:51 +00:00
Merge pull request #15 from Keivan-sf/13-extract-config-name
13 extract config name
This commit is contained in:
15
README.md
15
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] <URI>
|
||||
Usage: v2parser [OPTIONS] <uri>
|
||||
|
||||
Arguments:
|
||||
<URI>
|
||||
<uri> V2ray URI to parse
|
||||
|
||||
Options:
|
||||
--socksport <socksport>
|
||||
--httpport <httpport>
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
--socksport <PORT> Optional SOCKS proxy port for inbound
|
||||
--httpport <PORT> Optional HTTP proxy port for inbound
|
||||
--get-name Only print the config name
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
```
|
||||
|
||||
@@ -186,6 +186,7 @@ pub struct Config {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct RawData {
|
||||
pub remarks: String,
|
||||
pub security: Option<String>,
|
||||
pub vnext_security: Option<String>,
|
||||
pub sni: Option<String>,
|
||||
|
||||
58
src/main.rs
58
src/main.rs
@@ -1,20 +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<u16>,
|
||||
#[arg(long, value_name = "httpport")]
|
||||
httpport: Option<u16>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let json_config = parser::create_json_config(&cli.uri, cli.socksport, cli.httpport);
|
||||
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::<String>("uri").unwrap();
|
||||
let socksport = matches.get_one::<u16>("socksport").copied();
|
||||
let httpport = matches.get_one::<u16>("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(uri, socksport, httpport);
|
||||
println!("{}", json_config);
|
||||
}
|
||||
|
||||
@@ -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<u16>, http_port: Option<u16>) -> 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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -19,6 +19,7 @@ fn get_raw_data_from_base64(decoded_base64: &Vec<u8>) -> RawData {
|
||||
let json = serde_json::from_str::<Value>(json_str).unwrap();
|
||||
|
||||
return RawData {
|
||||
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::<u16>().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),
|
||||
|
||||
Reference in New Issue
Block a user