2023-10-08 21:11:49 +03:30
|
|
|
use regex::Regex;
|
|
|
|
|
|
2023-10-09 18:05:39 +03:30
|
|
|
pub enum Protocols {
|
2023-10-08 20:46:52 +03:30
|
|
|
Vmess,
|
|
|
|
|
Vless,
|
|
|
|
|
Shadowsocks,
|
|
|
|
|
Trojan,
|
|
|
|
|
Socks,
|
|
|
|
|
Http,
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 18:05:39 +03:30
|
|
|
pub fn get_uri_protocol(uri: &str) -> Option<Protocols> {
|
2023-10-08 21:11:49 +03:30
|
|
|
let uri_regex = Regex::new(r"^[a-z]+:\/\/.+$").unwrap();
|
|
|
|
|
if !uri_regex.is_match(uri) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2023-10-08 20:46:52 +03:30
|
|
|
if uri.starts_with("vmess://") {
|
2023-10-09 18:05:39 +03:30
|
|
|
return Some(Protocols::Vmess);
|
2023-10-08 20:46:52 +03:30
|
|
|
}
|
|
|
|
|
if uri.starts_with("vless://") {
|
2023-10-09 18:05:39 +03:30
|
|
|
return Some(Protocols::Vless);
|
2023-10-08 20:46:52 +03:30
|
|
|
}
|
2023-10-09 17:59:25 +03:30
|
|
|
if uri.starts_with("ss://") {
|
2023-10-09 18:05:39 +03:30
|
|
|
return Some(Protocols::Shadowsocks);
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
|
|
|
|
if uri.starts_with("socks://") {
|
2023-10-09 18:05:39 +03:30
|
|
|
return Some(Protocols::Socks);
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
|
|
|
|
if uri.starts_with("http://") {
|
2023-10-09 18:05:39 +03:30
|
|
|
return Some(Protocols::Http);
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
|
|
|
|
if uri.starts_with("trojan://") {
|
2023-10-09 18:05:39 +03:30
|
|
|
return Some(Protocols::Trojan);
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
2023-10-08 20:46:52 +03:30
|
|
|
return None;
|
|
|
|
|
}
|
2023-10-08 21:11:49 +03:30
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
#[test]
|
|
|
|
|
fn return_none_for_invalid_uri() {
|
2023-10-09 17:59:25 +03:30
|
|
|
let protocol = get_uri_protocol("123-vless://3d1c3f04-729d-59d3-bdb6-3f3f4352e173@root.ii.one:2083?security=reality&sni=www.spamhaus.org&fp=safari&pbk=7xhH4b_VkliBxGulljcyPOH-bYUA2dl-XAdZAsfhk04&sid=6ba85179e30d4fc2&type=tcp&flow=xtls-rprx-vision#Ha-ac");
|
2023-10-08 21:11:49 +03:30
|
|
|
assert!(matches!(protocol, None));
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
2023-10-09 17:59:25 +03:30
|
|
|
fn recognize_vless_protocol() {
|
|
|
|
|
let protocol = get_uri_protocol("vless://3d1c3f04-729d-59d3-bdb6-3f3f4352e173@root.ii.one:2083?security=reality&sni=www.spamhaus.org&fp=safari&pbk=7xhH4b_VkliBxGulljcyPOH-bYUA2dl-XAdZAsfhk04&sid=6ba85179e30d4fc2&type=tcp&flow=xtls-rprx-vision#Ha-ac").unwrap();
|
2023-10-09 18:05:39 +03:30
|
|
|
assert!(matches!(protocol, Protocols::Vless));
|
2023-10-08 21:11:49 +03:30
|
|
|
}
|
2023-10-09 17:59:25 +03:30
|
|
|
#[test]
|
|
|
|
|
fn recognize_vmess_protocol() {
|
|
|
|
|
let protocol = get_uri_protocol("vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaG9zdCI6IiIsImlkIjoiOHM2OTdlMmMtZXMxNy00MDNkLTI0ZjMtZHMyYzYwc2I4ZjUiLCJuZXQiOiJ0Y3AiLCJwYXRoIjoiIiwicG9ydCI6IjgwODAiLCJwcyI6InRlc3QiLCJzY3kiOiJhdXRvIiwic25pIjoiIiwidGxzIjoiIiwidHlwZSI6Im5vbmUiLCJ2IjoiMiJ9").unwrap();
|
2023-10-09 18:05:39 +03:30
|
|
|
assert!(matches!(protocol, Protocols::Vmess));
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn recognize_shadowsocks_protocol() {
|
|
|
|
|
let protocol = get_uri_protocol("ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpXNzRYRkFMS0t1dzZtNUlB@www.outline.aasf.cyou:443#test").unwrap();
|
2023-10-09 18:05:39 +03:30
|
|
|
assert!(matches!(protocol, Protocols::Shadowsocks));
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn recognize_trojan_protocol() {
|
|
|
|
|
let protocol = get_uri_protocol("trojan://test-pw@13.50.100.84:22222?security=tls&sni=trj.rollingnext.co.uk&type=tcp#test").unwrap();
|
2023-10-09 18:05:39 +03:30
|
|
|
assert!(matches!(protocol, Protocols::Trojan));
|
2023-10-09 17:59:25 +03:30
|
|
|
}
|
2023-10-08 21:11:49 +03:30
|
|
|
}
|