chore: return None for invalid uri

This commit is contained in:
Keivan-sf
2023-10-08 21:11:49 +03:30
parent a77da502ed
commit 081e036257
3 changed files with 69 additions and 0 deletions

47
Cargo.lock generated
View File

@@ -2,6 +2,53 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab"
dependencies = [
"memchr",
]
[[package]]
name = "memchr"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "regex"
version = "1.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]] [[package]]
name = "v2parser" name = "v2parser"
version = "0.1.0" version = "0.1.0"
dependencies = [
"regex",
]

View File

@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
regex = "1.9.6"

View File

@@ -1,3 +1,5 @@
use regex::Regex;
pub enum protocols { pub enum protocols {
Vmess, Vmess,
Vless, Vless,
@@ -8,6 +10,10 @@ pub enum protocols {
} }
pub fn get_uri_format(uri: &str) -> Option<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://") { if uri.starts_with("vmess://") {
return Some(protocols::Vmess); return Some(protocols::Vmess);
} }
@@ -16,3 +22,18 @@ pub fn get_uri_format(uri: &str) -> Option<protocols> {
} }
return None; 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));
}
}