use crate::{models::HYSTERIA_DEFAULT_GROUP, utils::url_decode, Proxy};
use std::collections::HashMap;
use url::Url;
pub fn explode_hysteria(hysteria: &str, node: &mut Proxy) -> bool {
if !hysteria.starts_with("hysteria://") {
return false;
}
let url = match Url::parse(hysteria) {
Ok(url) => url,
Err(_) => return false,
};
let host = match url.host_str() {
Some(host) => host,
None => return false,
};
let port = url.port().unwrap_or(443);
let mut params = HashMap::new();
for (key, value) in url.query_pairs() {
params.insert(key.to_string(), url_decode(&value));
}
let auth = params.get("auth").map(|s| s.as_str()).unwrap_or("");
let protocol = params.get("protocol").map(|s| s.as_str()).unwrap_or("udp");
let up = params.get("upmbps").map(|s| s.as_str()).unwrap_or("10");
let down = params.get("downmbps").map(|s| s.as_str()).unwrap_or("50");
let up_speed = up.parse::<u32>().ok();
let down_speed = down.parse::<u32>().ok();
let alpn_str = params.get("alpn").map(|s| s.as_str()).unwrap_or("");
let mut alpn = Vec::new();
if !alpn_str.is_empty() {
for a in alpn_str.split(',') {
alpn.push(a.trim().to_string());
}
}
let obfs = params.get("obfs").map(|s| s.as_str()).unwrap_or("");
let obfs_param = params.get("obfsParam").map(|s| s.as_str()).unwrap_or("");
let sni = params.get("peer").map(|s| s.as_str()).unwrap_or(host);
let insecure = params
.get("insecure")
.map(|s| s == "1" || s.to_lowercase() == "true")
.unwrap_or(false);
let remark = url.fragment().unwrap_or("");
let formatted_remark = if remark.is_empty() {
format!("{} ({})", host, port)
} else {
remark.to_string()
};
*node = Proxy::hysteria_construct(
HYSTERIA_DEFAULT_GROUP.to_string(),
formatted_remark,
host.to_string(),
port,
"".to_string(), protocol.to_string(),
obfs_param.to_string(),
up_speed,
down_speed,
auth.to_string(),
obfs.to_string(),
sni.to_string(),
"".to_string(), "".to_string(), "".to_string(), None, None, None, None, alpn,
None, Some(insecure),
None, );
true
}