mirror of
https://github.com/house-of-vanity/v2-uri-parser.git
synced 2025-12-17 07:27:51 +00:00
chore: return None for invalid uri
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use regex::Regex;
|
||||
|
||||
pub enum protocols {
|
||||
Vmess,
|
||||
Vless,
|
||||
@@ -8,6 +10,10 @@ pub enum protocols {
|
||||
}
|
||||
|
||||
pub fn get_uri_format(uri: &str) -> Option<protocols> {
|
||||
let uri_regex = Regex::new(r"^[a-z]+:\/\/.+$").unwrap();
|
||||
if !uri_regex.is_match(uri) {
|
||||
return None;
|
||||
}
|
||||
if uri.starts_with("vmess://") {
|
||||
return Some(protocols::Vmess);
|
||||
}
|
||||
@@ -16,3 +22,18 @@ pub fn get_uri_format(uri: &str) -> Option<protocols> {
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn return_none_for_invalid_uri() {
|
||||
let protocol = get_uri_format("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");
|
||||
assert!(matches!(protocol, None));
|
||||
}
|
||||
#[test]
|
||||
fn recognize_vless_format() {
|
||||
let protocol = get_uri_format("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();
|
||||
assert!(matches!(protocol, protocols::Vless));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user