From 081e036257f70aeb91702270dcdd2a9a2d2a40dd Mon Sep 17 00:00:00 2001 From: Keivan-sf Date: Sun, 8 Oct 2023 21:11:49 +0330 Subject: [PATCH] chore: return None for invalid uri --- Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/parser/mod.rs | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9aa845e..c9c23fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,53 @@ # It is not intended for manual editing. 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]] name = "v2parser" version = "0.1.0" +dependencies = [ + "regex", +] diff --git a/Cargo.toml b/Cargo.toml index e7ba8ad..44a8072 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex = "1.9.6" diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0b4f269..b8d2f5e 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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 { + 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 { } 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)); + } +}