chore: refactor vless query parser

This commit is contained in:
Keivan-sf
2023-10-11 17:36:53 +03:30
parent 9f06874eb2
commit b2c2ea0a00

View File

@@ -69,121 +69,40 @@ pub fn parse_vless_address(raw_data: &str) -> VlessAddress {
} }
pub fn parse_vless_query(raw_query: &str) -> VlessQuery { pub fn parse_vless_query(raw_query: &str) -> VlessQuery {
let query = querystring::querify(raw_query); let query: Vec<(&str, &str)> = querystring::querify(raw_query);
let a = VlessQuery { let a = VlessQuery {
path: query path: get_parameter_value(&query, "path"),
.iter() pbk: get_parameter_value(&query, "pbk"),
.find(|q| String::from(q.0) == String::from("path")) security: get_parameter_value(&query, "security"),
.unwrap_or(&("", "")) sid: get_parameter_value(&query, "sid"),
.1 flow: get_parameter_value(&query, "flow"),
.to_string(), sni: get_parameter_value(&query, "sni"),
pbk: query fp: get_parameter_value(&query, "fp"),
.iter() r#type: get_parameter_value(&query, "type"),
.find(|q| String::from(q.0) == String::from("pbk")) encryption: get_parameter_value(&query, "encryption"),
.unwrap_or(&("", "")) header_type: get_parameter_value(&query, "headerType"),
.1 host: get_parameter_value(&query, "host"),
.to_string(), seed: get_parameter_value(&query, "seed"),
security: query quic_security: get_parameter_value(&query, "quicSecurity"),
.iter() key: get_parameter_value(&query, "key"),
.find(|q| String::from(q.0) == String::from("security")) mode: get_parameter_value(&query, "mode"),
.unwrap_or(&("", "")) service_name: get_parameter_value(&query, "serviceName"),
.1 slpn: get_parameter_value(&query, "slpn"),
.to_string(), spx: get_parameter_value(&query, "spx"),
sid: query
.iter()
.find(|q| String::from(q.0) == String::from("sid"))
.unwrap_or(&("", ""))
.1
.to_string(),
flow: query
.iter()
.find(|q| String::from(q.0) == String::from("flow"))
.unwrap_or(&("", ""))
.1
.to_string(),
sni: query
.iter()
.find(|q| String::from(q.0) == String::from("sni"))
.unwrap_or(&("", ""))
.1
.to_string(),
fp: query
.iter()
.find(|q| String::from(q.0) == String::from("fp"))
.unwrap_or(&("", ""))
.1
.to_string(),
r#type: query
.iter()
.find(|q| String::from(q.0) == String::from("type"))
.unwrap_or(&("", ""))
.1
.to_string(),
encryption: query
.iter()
.find(|q| String::from(q.0) == String::from("encryption"))
.unwrap_or(&("", ""))
.1
.to_string(),
header_type: query
.iter()
.find(|q| String::from(q.0) == String::from("headerType"))
.unwrap_or(&("", ""))
.1
.to_string(),
host: query
.iter()
.find(|q| String::from(q.0) == String::from("host"))
.unwrap_or(&("", ""))
.1
.to_string(),
seed: query
.iter()
.find(|q| String::from(q.0) == String::from("seed"))
.unwrap_or(&("", ""))
.1
.to_string(),
quic_security: query
.iter()
.find(|q| String::from(q.0) == String::from("quicSecurity"))
.unwrap_or(&("", ""))
.1
.to_string(),
key: query
.iter()
.find(|q| String::from(q.0) == String::from("key"))
.unwrap_or(&("", ""))
.1
.to_string(),
mode: query
.iter()
.find(|q| String::from(q.0) == String::from("mode"))
.unwrap_or(&("", ""))
.1
.to_string(),
service_name: query
.iter()
.find(|q| String::from(q.0) == String::from("serviceName"))
.unwrap_or(&("", ""))
.1
.to_string(),
slpn: query
.iter()
.find(|q| String::from(q.0) == String::from("slpn"))
.unwrap_or(&("", ""))
.1
.to_string(),
spx: query
.iter()
.find(|q| String::from(q.0) == String::from("spx"))
.unwrap_or(&("", ""))
.1
.to_string(),
}; };
return a; return a;
} }
fn get_parameter_value(query: &Vec<(&str, &str)>, param: &str) -> String {
return query
.iter()
.find(|q| String::from(q.0) == String::from(param))
.unwrap_or(&("", ""))
.1
.to_string();
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;