mirror of
https://github.com/house-of-vanity/v2-uri-parser.git
synced 2025-12-16 06:57:52 +00:00
Improve config type and add authority
This makes the parser take a more passive approach, no unnecessary defaults or fileds
This commit is contained in:
@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||
pub struct VlessUser {
|
||||
pub id: String,
|
||||
pub encryption: String,
|
||||
pub flow: String,
|
||||
pub level: u8,
|
||||
pub flow: Option<String>,
|
||||
pub level: Option<u8>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -28,37 +28,38 @@ pub enum OutboundSettings {
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct NonHeaderObject {
|
||||
pub r#type: String,
|
||||
pub r#type: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct QuicSettings {
|
||||
pub header: Option<NonHeaderObject>,
|
||||
pub security: String,
|
||||
pub key: String,
|
||||
pub security: Option<String>,
|
||||
pub key: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GRPCSettings {
|
||||
pub multiMode: bool,
|
||||
pub serviceName: String,
|
||||
pub authority: Option<String>,
|
||||
pub multiMode: Option<bool>,
|
||||
pub serviceName: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RealitySettings {
|
||||
pub fingerprint: String,
|
||||
pub serverName: String,
|
||||
pub publicKey: String,
|
||||
pub shortId: String,
|
||||
pub spiderX: String,
|
||||
pub fingerprint: Option<String>,
|
||||
pub serverName: Option<String>,
|
||||
pub publicKey: Option<String>,
|
||||
pub shortId: Option<String>,
|
||||
pub spiderX: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct TCPHeader {
|
||||
pub r#type: String,
|
||||
pub r#type: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
@@ -101,8 +102,8 @@ pub struct TlsSettings {
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StreamSettings {
|
||||
pub network: String,
|
||||
pub security: String,
|
||||
pub network: Option<String>,
|
||||
pub security: Option<String>,
|
||||
pub tlsSettings: Option<TlsSettings>,
|
||||
pub wsSettings: Option<WsSettings>,
|
||||
pub tcpSettings: Option<TCPSettings>,
|
||||
|
||||
@@ -5,19 +5,16 @@ use http::Uri;
|
||||
use std::process::exit;
|
||||
|
||||
pub fn create_outbound_object(data: models::VlessData) -> Outbound {
|
||||
let network_type = data.query.r#type.clone().unwrap_or(String::from(""));
|
||||
return Outbound {
|
||||
protocol: String::from("vless"),
|
||||
tag: String::from("proxy"),
|
||||
streamSettings: StreamSettings {
|
||||
network: data.query.r#type.clone(),
|
||||
security: data.query.security.clone(),
|
||||
tlsSettings: if data.query.security == String::from("tls") {
|
||||
tlsSettings: if network_type == String::from("tls") {
|
||||
Some(TlsSettings {
|
||||
alpn: if data.query.alpn.len() > 1 {
|
||||
Some(vec![data.query.alpn])
|
||||
} else {
|
||||
None
|
||||
},
|
||||
alpn: data.query.alpn.map(|alpn| vec![alpn]),
|
||||
rejectUnknownSni: None,
|
||||
enableSessionResumption: None,
|
||||
minVersion: None,
|
||||
@@ -25,66 +22,59 @@ pub fn create_outbound_object(data: models::VlessData) -> Outbound {
|
||||
cipherSuites: None,
|
||||
disableSystemRoot: None,
|
||||
preferServerCipherSuites: None,
|
||||
fingerprint: Some(data.query.fp.clone()),
|
||||
serverName: Some(data.query.sni.clone()),
|
||||
fingerprint: data.query.fp.clone(),
|
||||
serverName: data.query.sni.clone(),
|
||||
allowInsecure: Some(false),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
wsSettings: if data.query.r#type == String::from("ws") {
|
||||
wsSettings: if network_type == String::from("ws") {
|
||||
Some(WsSettings {
|
||||
Host: Some(data.query.host),
|
||||
path: Some(
|
||||
urlencoding::decode(data.query.path.as_str())
|
||||
.unwrap()
|
||||
.into_owned(),
|
||||
),
|
||||
Host: data.query.host,
|
||||
path: data.query.path,
|
||||
acceptProxyProtocol: None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
tcpSettings: if data.query.r#type == String::from("tcp") {
|
||||
tcpSettings: if network_type == String::from("tcp") {
|
||||
Some(TCPSettings {
|
||||
header: Some(TCPHeader {
|
||||
r#type: if data.query.header_type.len() > 1 {
|
||||
data.query.header_type
|
||||
} else {
|
||||
String::from("none")
|
||||
},
|
||||
r#type: Some(data.query.header_type.unwrap_or(String::from("none"))),
|
||||
}),
|
||||
acceptProxyProtocol: None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
realitySettings: if data.query.security == String::from("reality") {
|
||||
realitySettings: if network_type == String::from("reality") {
|
||||
Some(RealitySettings {
|
||||
publicKey: data.query.pbk,
|
||||
serverName: data.query.sni.clone(),
|
||||
shortId: data.query.sid,
|
||||
spiderX: String::from(""),
|
||||
fingerprint: data.query.fp,
|
||||
spiderX: Some(String::from("")),
|
||||
fingerprint: data.query.fp.clone(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
grpcSettings: if data.query.r#type == String::from("grpc") {
|
||||
grpcSettings: if network_type == String::from("grpc") {
|
||||
Some(GRPCSettings {
|
||||
multiMode: false,
|
||||
authority: data.query.authority,
|
||||
multiMode: Some(false),
|
||||
serviceName: data.query.service_name,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
quicSettings: if data.query.r#type == String::from("quic") {
|
||||
quicSettings: if network_type == String::from("quic") {
|
||||
Some(QuicSettings {
|
||||
header: Some(NonHeaderObject {
|
||||
r#type: String::from("none"),
|
||||
r#type: Some(String::from("none")),
|
||||
}),
|
||||
security: String::from("none"),
|
||||
key: String::from(""),
|
||||
security: Some(String::from("none")),
|
||||
key: Some(String::from("")),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
@@ -97,12 +87,8 @@ pub fn create_outbound_object(data: models::VlessData) -> Outbound {
|
||||
users: vec![VlessUser {
|
||||
id: data.address_data.uuid,
|
||||
flow: data.query.flow,
|
||||
encryption: if data.query.encryption.len() > 0 {
|
||||
data.query.encryption
|
||||
} else {
|
||||
String::from("none")
|
||||
},
|
||||
level: 0,
|
||||
encryption: data.query.encryption.unwrap_or(String::from("none")),
|
||||
level: Some(0),
|
||||
}],
|
||||
}],
|
||||
}),
|
||||
@@ -147,12 +133,21 @@ fn parse_vless_query(raw_query: &str) -> models::VlessQuery {
|
||||
let query: Vec<(&str, &str)> = querystring::querify(raw_query);
|
||||
|
||||
let a = models::VlessQuery {
|
||||
alpn: urlencoding::decode(get_parameter_value(&query, "alpn").as_str())
|
||||
.unwrap()
|
||||
.into_owned(),
|
||||
path: urlencoding::decode(get_parameter_value(&query, "path").as_str())
|
||||
.unwrap()
|
||||
.into_owned(),
|
||||
alpn: get_parameter_value(&query, "alpn").and_then(|s| {
|
||||
urlencoding::decode(&s)
|
||||
.ok()
|
||||
.map(|decoded| decoded.into_owned())
|
||||
}),
|
||||
path: get_parameter_value(&query, "path").and_then(|s| {
|
||||
urlencoding::decode(&s)
|
||||
.ok()
|
||||
.map(|decoded| decoded.into_owned())
|
||||
}),
|
||||
authority: get_parameter_value(&query, "authority").and_then(|s| {
|
||||
urlencoding::decode(&s)
|
||||
.ok()
|
||||
.map(|decoded| decoded.into_owned())
|
||||
}),
|
||||
pbk: get_parameter_value(&query, "pbk"),
|
||||
security: get_parameter_value(&query, "security"),
|
||||
sid: get_parameter_value(&query, "sid"),
|
||||
@@ -174,13 +169,11 @@ fn parse_vless_query(raw_query: &str) -> models::VlessQuery {
|
||||
return a;
|
||||
}
|
||||
|
||||
fn get_parameter_value(query: &Vec<(&str, &str)>, param: &str) -> String {
|
||||
fn get_parameter_value(query: &Vec<(&str, &str)>, param: &str) -> Option<String> {
|
||||
return query
|
||||
.iter()
|
||||
.find(|q| String::from(q.0) == String::from(param))
|
||||
.unwrap_or(&("", ""))
|
||||
.1
|
||||
.to_string();
|
||||
.map(|q| q.1.to_string());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -198,7 +191,7 @@ mod tests {
|
||||
.header
|
||||
.unwrap()
|
||||
.r#type,
|
||||
"http"
|
||||
Some(String::from("http"))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -209,8 +202,8 @@ mod tests {
|
||||
assert_eq!(data.address_data.address, "nwarne.fast-ip.com");
|
||||
assert_eq!(data.address_data.uuid, "2dc56709-sdfs-sdfs-2234-128904");
|
||||
assert_eq!(data.address_data.port, 80);
|
||||
assert_eq!(data.query.encryption, "none");
|
||||
assert_eq!(data.query.r#type, "ws");
|
||||
assert_eq!(data.query.encryption.unwrap(), "none");
|
||||
assert_eq!(data.query.r#type.unwrap(), "ws");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -223,61 +216,61 @@ mod tests {
|
||||
"4d2c3e35-749d-52e3-bdb6-3f3f4950c183"
|
||||
);
|
||||
assert_eq!(data.address_data.port, 2053);
|
||||
assert_eq!(data.query.flow, "xtls-rprx-vision");
|
||||
assert_eq!(data.query.security, "reality");
|
||||
assert_eq!(data.query.r#type, "tcp");
|
||||
assert_eq!(data.query.flow.unwrap(), "xtls-rprx-vision");
|
||||
assert_eq!(data.query.security.unwrap(), "reality");
|
||||
assert_eq!(data.query.r#type.unwrap(), "tcp");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_vless_query_data() {
|
||||
let query = "security=reality&sni=bench.sh&fp=chrome&pbk=7xhH4b_VkliBxGulljcyPOH-bYUA2dl-XAdZAsfhk04&sid=6ba85179e30d4fc2&type=tcp&flow=xtls-rprx-vision&alpn=http%2F1.1&path=/";
|
||||
let parsed_query = parse_vless_query(query);
|
||||
assert_eq!(parsed_query.sni, "bench.sh");
|
||||
assert_eq!(parsed_query.security, "reality");
|
||||
assert_eq!(parsed_query.fp, "chrome");
|
||||
assert_eq!(parsed_query.sni.unwrap(), "bench.sh");
|
||||
assert_eq!(parsed_query.security.unwrap(), "reality");
|
||||
assert_eq!(parsed_query.fp.unwrap(), "chrome");
|
||||
assert_eq!(
|
||||
parsed_query.pbk,
|
||||
parsed_query.pbk.unwrap(),
|
||||
"7xhH4b_VkliBxGulljcyPOH-bYUA2dl-XAdZAsfhk04"
|
||||
);
|
||||
assert_eq!(parsed_query.sid, "6ba85179e30d4fc2");
|
||||
assert_eq!(parsed_query.r#type, "tcp");
|
||||
assert_eq!(parsed_query.flow, "xtls-rprx-vision");
|
||||
assert_eq!(parsed_query.alpn, "http/1.1");
|
||||
assert_eq!(parsed_query.path, "/");
|
||||
assert_eq!(parsed_query.encryption, "");
|
||||
assert_eq!(parsed_query.header_type, "");
|
||||
assert_eq!(parsed_query.host, "");
|
||||
assert_eq!(parsed_query.seed, "");
|
||||
assert_eq!(parsed_query.quic_security, "");
|
||||
assert_eq!(parsed_query.key, "");
|
||||
assert_eq!(parsed_query.mode, "");
|
||||
assert_eq!(parsed_query.service_name, "");
|
||||
assert_eq!(parsed_query.slpn, "");
|
||||
assert_eq!(parsed_query.spx, "");
|
||||
assert_eq!(parsed_query.sid.unwrap(), "6ba85179e30d4fc2");
|
||||
assert_eq!(parsed_query.r#type.unwrap(), "tcp");
|
||||
assert_eq!(parsed_query.flow.unwrap(), "xtls-rprx-vision");
|
||||
assert_eq!(parsed_query.alpn.unwrap(), "http/1.1");
|
||||
assert_eq!(parsed_query.path.unwrap(), "/");
|
||||
assert_eq!(parsed_query.encryption, None);
|
||||
assert_eq!(parsed_query.header_type, None);
|
||||
assert_eq!(parsed_query.host, None);
|
||||
assert_eq!(parsed_query.seed, None);
|
||||
assert_eq!(parsed_query.quic_security, None);
|
||||
assert_eq!(parsed_query.key, None);
|
||||
assert_eq!(parsed_query.mode, None);
|
||||
assert_eq!(parsed_query.service_name, None);
|
||||
assert_eq!(parsed_query.slpn, None);
|
||||
assert_eq!(parsed_query.spx, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_vless_query_with_defaults() {
|
||||
let query = "";
|
||||
let parsed_query = parse_vless_query(query);
|
||||
assert_eq!(parsed_query.sni, "");
|
||||
assert_eq!(parsed_query.security, "");
|
||||
assert_eq!(parsed_query.fp, "");
|
||||
assert_eq!(parsed_query.pbk, "");
|
||||
assert_eq!(parsed_query.sid, "");
|
||||
assert_eq!(parsed_query.r#type, "");
|
||||
assert_eq!(parsed_query.r#flow, "");
|
||||
assert_eq!(parsed_query.path, "");
|
||||
assert_eq!(parsed_query.encryption, "");
|
||||
assert_eq!(parsed_query.header_type, "");
|
||||
assert_eq!(parsed_query.host, "");
|
||||
assert_eq!(parsed_query.seed, "");
|
||||
assert_eq!(parsed_query.quic_security, "");
|
||||
assert_eq!(parsed_query.key, "");
|
||||
assert_eq!(parsed_query.mode, "");
|
||||
assert_eq!(parsed_query.service_name, "");
|
||||
assert_eq!(parsed_query.slpn, "");
|
||||
assert_eq!(parsed_query.spx, "");
|
||||
assert_eq!(parsed_query.sni, None);
|
||||
assert_eq!(parsed_query.security, None);
|
||||
assert_eq!(parsed_query.fp, None);
|
||||
assert_eq!(parsed_query.pbk, None);
|
||||
assert_eq!(parsed_query.sid, None);
|
||||
assert_eq!(parsed_query.r#type, None);
|
||||
assert_eq!(parsed_query.r#flow, None);
|
||||
assert_eq!(parsed_query.path, None);
|
||||
assert_eq!(parsed_query.encryption, None);
|
||||
assert_eq!(parsed_query.header_type, None);
|
||||
assert_eq!(parsed_query.host, None);
|
||||
assert_eq!(parsed_query.seed, None);
|
||||
assert_eq!(parsed_query.quic_security, None);
|
||||
assert_eq!(parsed_query.key, None);
|
||||
assert_eq!(parsed_query.mode, None);
|
||||
assert_eq!(parsed_query.service_name, None);
|
||||
assert_eq!(parsed_query.slpn, None);
|
||||
assert_eq!(parsed_query.spx, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -291,7 +284,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn parse_vless_ipv6_host() {
|
||||
let v = "vless://4d91916f-a7fd-419b-8b90-640bb8d1b9f4@[2a06:98c1:3120::1]:443?path=%2FPSZPkYG71g6bn84o%2FMTQxLjE0OC4yMDMuNg&security=tls&alpn=http%2F1.1&encryption=none&host=titantablomanahamrah.ir&fp=randomized&type=ws&sni=TITanTabLOmaNAHaMRaH.IR#";
|
||||
let raw_host = "uu0id@[2a06:98c1:3120::1]:443";
|
||||
let parsed = parse_vless_address(raw_host);
|
||||
assert_eq!(parsed.port, 443);
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
pub struct VlessQuery {
|
||||
pub security: String,
|
||||
pub sni: String,
|
||||
pub fp: String,
|
||||
pub pbk: String,
|
||||
pub sid: String,
|
||||
pub r#type: String,
|
||||
pub flow: String,
|
||||
pub path: String,
|
||||
pub encryption: String,
|
||||
pub header_type: String,
|
||||
pub host: String,
|
||||
pub seed: String,
|
||||
pub quic_security: String,
|
||||
pub r#key: String,
|
||||
pub mode: String,
|
||||
pub service_name: String,
|
||||
pub slpn: String,
|
||||
pub spx: String,
|
||||
pub alpn: String,
|
||||
pub security: Option<String>,
|
||||
pub sni: Option<String>,
|
||||
pub fp: Option<String>,
|
||||
pub pbk: Option<String>,
|
||||
pub sid: Option<String>,
|
||||
pub r#type: Option<String>,
|
||||
pub flow: Option<String>,
|
||||
pub path: Option<String>,
|
||||
pub encryption: Option<String>,
|
||||
pub header_type: Option<String>,
|
||||
pub host: Option<String>,
|
||||
pub seed: Option<String>,
|
||||
pub quic_security: Option<String>,
|
||||
pub r#key: Option<String>,
|
||||
pub mode: Option<String>,
|
||||
pub service_name: Option<String>,
|
||||
pub authority: Option<String>,
|
||||
pub slpn: Option<String>,
|
||||
pub spx: Option<String>,
|
||||
pub alpn: Option<String>,
|
||||
}
|
||||
|
||||
pub struct VlessAddress {
|
||||
|
||||
Reference in New Issue
Block a user